(CNN) — To buy Twitter, Elon Musk just sold a ton of stock and made a boatload of money on the sale. But because of a US tax law quirk, Musk’s stock sale could give him an enormous tax break.

Musk last month sold $8.5 billion worth of Tesla shares for more than 100 times what he paid for them (he exercised options and bought them for a massively reduced price). But the IRS doesn’t care what Musk initially paid for the stock; it cares only what the shares were worth when he bought and sold them.

Tesla’s stock was going for more than $1,000 a share when Musk bought 22.9 million shares late last year, but by the time he sold the 9.6 million shares last week, the price had fallen, and he received only $883 a share, on average. That difference comes to about $1.7 billion.

In the eyes of the IRS, the sale could count as a massive tax loss that would reduce Musk’s future taxes by between $400 million and $700 million.

It may sound like cheating, but it’s legal — and a common practice for wealthy investors.

Diagram of a massive tax break
Here’s how Musk could get his tax break.

Musk acquired Tesla stock through the exercise of options at the end of last year, part of his compensation for being CEO. Musk paid only $6.24 per share to exercise the options, even though each share was worth more than $1,000 each.

The basis is typically the cost of shares or other assets at the time they’re purchased. That amount is deductible from the revenue an investor receives when calculating capital gains tax.

But because those options where earned as compensation for being CEO, Musk had to pay income taxes on the value of the shares at the time he exercised the options. That gave him a massive $11 billion tax bill for last year, and prompted him to sell 10.3 million of the shares he acquired, mostly to cover withholding tax. But it also meant that the value of the shares, not his nominal out-of-pocket cost of $6.24 a share, was the basis of the remaining 12.6 million shares in the eyes of the IRS.

It’s not certain that that those shares he acquired last year are the ones he just sold. But it’s probable, tax experts say, because those would have the highest cost basis of any of the shares he owns.

“Of course, you’re going to sell the big basis shares first, and you’re allowed to do so in the law,” said Lee Sheppard, contributing editor of Tax Notes Federal.

Tax law allows investors to pick and chose which shares they sell when they are disposing of only a portion of a holdings. And the tax experts agree that selling the shares Musk acquired through exercising options in the final two months of 2021 would be the most beneficial for his future tax bills.

“It’s a common strategy,” said Steve Rosenthal, senior fellow, at the Tax Policy Center, a nonpartisan think tank. “Taxpayers like to select their losses as an investing tool.”

Musk’s paper tax loss means he can avoid paying taxes on that much capital gains in the future, depending upon how long he held onto whatever asset he is selling for a profit. He will be able to cash in that tax loss only against future capital gains, not shield what he might owe in regular income tax. But he can wait and use it whenever he does have a gain.

“He can carry it forward forever and ever,” said Sheppard.

It’s possible he might not have to wait long.

For example, if he sold some of his holdings in privately held SpaceX to raise additional cash for his Twitter purchase, he could shelter $1.7 billion in gains on those sales. If he is facing any capital gains tax for trading crypto currency, that also could be sheltered.

Longer term, if he succeeds in buying Twitter and then takes it public, as the Wall Street Journal reported Wednesday he is considering doing in a few years, up to $1.7 billion of any profit he makes on that offering could be sheltered from capital gains taxes this way.

The fact that Musk is able to use the value of the shares at the time he acquired them to calculate capital gains or losses, rather than the nominal amount the shares cost him, is a big advantage for him and others in his position.

“It’s a very good deal,” said Rosenthal. “If someone gave me the chance to buy Tesla shares for $6.24 a share when they were worth $1,000, I’d jump at it. Even if I had to share a big chunk of that with the government, I’d be way ahead.”

(Copyright (c) 2024 CNN. All Rights Reserved. This material may not be published, broadcast, rewritten, or redistributed.)

Join our Newsletter for the latest news right to your inbox