The examples you provide in the question are completely irrelevant. It doesn't matter where the brokerage is or where is the company you own stocks in. For a fairly standard case of an non-resident alien international student living full time in the US - your capital gains are US sourced.
Let me quote the following text a couple of paragraphs down the line you quoted on the same page:
Gain or loss from the sale or exchange of personal property generally
has its source in the United States if the alien has a tax home in the
United States. The key factor in determining if an individual is a
U.S. resident for purposes of the sourcing of capital gains is whether
the alien's "tax home" has shifted to the United States. If an alien
does not have a tax home in the United States, then the alien’s U.S.
source capital gains would be treated as foreign-source and thus
In general, under the "tax home" rules, a person who is away (or who
intends to be away) from his tax home for longer than 1 year has
shifted tax homes to his new location upon his arrival in that new
location. See Chapter 1 of Publication 463, Travel, Entertainment,
Gift, and Car Expenses
I'll assume you've read this and just want an explanation on what it means.
What it means is that if you move to the US for a significant period of time (expected length of 1 year or more), your tax home is assumed to have shifted to the US and the capital gains are sourced to the US from the start of your move.
For example: you are a foreign diplomat, and your 4-year assignment started in May. Year-end - you're not US tax resident (diplomats exempt), but you've stayed in the US for more than 183 days, and since your assignment is longer than 1 year - your tax home is now in the US. You'll pay the 30% flat tax.
Another example: You're a foreign airline pilot, coming to the US every other day flying the airline aircraft. You end up staying in the US 184 days, but your tax home hasn't shifted, nor you're a US tax resident - you don't pay the flat tax.
Keep in mind, that tax treaties may alter the situation since in many cases they also cover the capital gains situation for non-residents.