What Buffett deal says about Goldman Sachs

Warren Buffett

FORTUNE — We knew Goldman Sachs was wounded in the financial crisis. It may have been in more trouble than is understood.

That’s the sense you get from Warren Buffett’s recently inked deal to turn the warrants Berkshire Hathaway received as part of its $5 billion investment in Goldman (GS) at the height of the financial crisis into stock. Do the math on the deal, and it becomes clear just how much Goldman’s executives were willing to pay for cash at the time. Or maybe this is just the magic of Buffett. Or maybe it’s both.

On Tuesday, as part of an amendment to its earlier deal, Goldman said it would hand $1.4 billion worth of shares to Berkshire (BRKA). Buffet’s company doesn’t have to pay a penny for the shares. And this is after Goldman has already paid Buffett back for his financial crisis era investment in full, plus interest. Nice work if you can get it.

MORE: Is Goldman riskier than it says it is?

Here’s how the Buffett-Goldman deal got to this point. Buffett agreed to give Goldman $5 billion in late September 2008. In return, Goldman handed over $5 billion in preferred shares and a warrant that would allow Buffett to purchase an additional $5 billion shares at a price of $115, even though the shares were trading at $125 at the time, so in the money from the beginning.

For the preferreds, Goldman agreed to pay Berkshire a yearly 10% dividend, with option of buying back the stock at any time for 10% more than what Berkshire had paid, which Goldman did in April 2011 for $5.5 billion. Linus Wilson, a finance professor at the University of Louisiana at Lafayette, who has looked at the Goldman deal, puts the dividends at $1.3 billion. So that gives Berkshire a total return of $1.8 billion on the preferreds.

Now come the warrants. In the deal struck on Tuesday, Buffett’s firm won’t have to put up the $5 billion to buy the 43.5 million shares it has a right to purchase, which would be worth roughly $6.4 billion today. Instead, Goldman is going to give Buffett the difference in stock at the time of the deal. Buffett’s return is the same, but he’s left with a much smaller stake in Goldman.

All told, that means Buffett is walking away with a $3.2 billion profit on his four-and-a-half-year-old investment in Goldman, for a return of 64%. A classic value investor move. Swoop in when others are selling, and pick up dollars for pennies. Buffett’s legend is secure.

MORE: Exclusive: Dell is paying Blackstone’s bills

Except that’s not really what happened. Almost all of the gain Buffett got on his Goldman investment comes from the special structure of the investment — the preferreds and the warrants. As a straight stock pick, Goldman hasn’t been all that remarkable since Buffett put his money in. The stock is up just 17.5%. In the same time period, the S&P 500 (SPX) is up 29%. Shares of drug company Pfizer (PFE) are up 53% in the same time. Apple (AAPL), despite falling recently, is still up 260%.

So how much trouble was Goldman in at the height of the financial crisis: $2.4 billion worth. That’s how much extra the bank paid Buffett above what he would have earned if he had just bought the shares on the open market. Goldman recently came out toward the bottom of the Federal Reserve stress test, so that’s probably capital it would like to have back now.

In the end, Goldman gave a sweetheart deal to one of its best clients, and his shareholders, at the expense of its own. And it’s not even clear why. Buffett’s investment in Goldman was seen as a vote of confidence in the firm and U.S. banks in general at a time when the market needed it. It didn’t work immediately. But I’m sure both Goldman and Buffett saw that as the real point of the deal. And since one of the biggest individual benefactors of a rising market is Buffett himself, it’s not exactly clear why he would have needed such sweeteners.

It could be that despite the Cherry Coke and down-home charm, Buffett drives a hard bargain. Or could be that in late September 2008, things were turning ugly at Goldman, and both the firm’s executives and Buffett knew it. You can make your own pick. But I would probably go with the latter.

Subscribe to Well Adjusted, our newsletter full of simple strategies to work smarter and live better, from the Fortune Well team. Sign up today.