Citigroup Inc , one of the big U.S. banks hit hardest by the financial crisis, posted a quarterly profit as gains from the sale of its Smith Barney brokerage into a joint venture more than offset losses from its primary banking businesses.

Excluding the gains, the bank reported a second-quarter loss that was narrower than expected, and its shares rose 4 percent in early trading.

Citigroup, propped up with a $45 billion government bailout, recorded a $6.7 billion gain from merging its Smith Barney brokerage into a joint venture with Morgan Stanley .

The gain boosted net income to $4.28 billion, or 49 cents a share, compared with a year-earlier loss of $2.50 billion, or 55 cents. Without the gain, it lost 26 cents a share, better than the loss of 31 cents forecast by analysts polled by Reuters Estimates.

Revenue rose by $12.4 billion, or 71 percent, to $30.0 billion, thanks to the $11.1 billion pretax Smith Barney gain as well as net write-ups on mortgages and other assets.

It is hard to say whether they've completely turned the corner, but they are heading in that direction, said Walter Todd, portfolio manager at Greenwood Capital Associates. Part of that is the economy bottoming out, and part of that is actions they have taken internally. I think they are more dependent on the macro situation continuing to improve.

Credit costs increased to $12.4 billion, including $8.4 billion of net losses and the addition of $3.9 billion to loan loss reserves. That brings the total allowance for loan losses to 5.6 percent of total loans.

Citigroup shares are down 94 percent since peaking in May 2007 and have fallen by half this year, but they have tripled since financial services stocks began rallying in March.

GOVERNMENT RESCUE

The iconic New York bank has been among the biggest losers of the nearly three-year financial crisis, prompting the U.S. government to step in with multiple bailout plans.

Chief Executive Vikram Pandit in January decided to split the company in two: Citicorp includes the global commercial bank and other businesses it wants to keep; Citi Holdings is a collection of units and bad assets it will sell.

In the second quarter, Citicorp income from continuing operations fell 11 percent to $3.06 billion, driven mostly by plunging profit in regional consumer banking businesses.

The institutional client group, the company's investment banking and trading division, boosted operating income by 16 percent to $2.84 billion.

Citi Holdings posted operating income of $1.36 billion, compared with a loss of $5.23 billion a year earlier, as brokerage and asset management gains more than offset losses from consumer finance businesses and losses from winding down a portfolio of toxic debt.

Earnings attributable to common shareholders were reduced by $1.28 billion of preferred stock dividends to the U.S. government and other investors, compared with $361 million in the year-ago period.

The bank is expected to soon complete a swap that will convert the U.S. government's investment into a 34 percent equity stake in Citigroup.

(Reporting by Joseph Giannone; Additional reporting by Steve Eder; editing by John Wallace)