General Motors posted its biggest quarterly profit in six years on Thursday, a day ahead of an expected IPO filing that will clear the way for the U.S. government to relinquish its majority stake in the top U.S. automaker.

GM reported second-quarter net earnings of $1.3 billion, compared with $865 million in the first quarter.

The second-quarter profit was the largest since 2004, when the U.S. auto market was still booming with annual sales of near 17 million vehicles and GM's brands accounted for more than one in four purchases of new cars and trucks.

The results reflected a 47 percent snap back in global production from the depressed levels of a year earlier when GM began operating under bankruptcy protection in a restructuring that included $50 billion in U.S. government funding.

GM was also aided by cost-cutting during bankruptcy and sales growth overseas led by its joint-ventures in China.

Revenue rose to $33.2 billion from $31.5 billion in the first quarter, boosted by higher sales of more profitable new models such as the Chevrolet Equinox.

GM Chief Executive Ed Whitacre had said last week that the quarterly results would be impressive.

Analysts expect the company to use the results to build the case for a stock offering expected to be the largest ever for the U.S. market and allow the U.S. government to reduce its 61 percent ownership stake.

A successful GM IPO would provide the Obama administration with evidence that the unprecedented and unpopular intervention in the U.S. auto industry has been a financial success.

Analysts said GM's results showed it can be profitable even in a still-weak U.S. auto market where sales are running at just over 11 million vehicles.

Over time, they are going to be printing money. They are set up to break even at the bottom, said Morningstar analyst David Whiston.

Europe, where GM is still struggling to restructure its Opel unit, remained a notable weak link for the automaker with an operating loss of $160 million.

North America had an operating profit of $1.6 billion. International operations, including GM's China joint ventures with SAIC and Wuling, had an operating profit of $672 million.

They are on the right track, IHS Automotive analyst Rebecca Lindland said. It's stunning to see how efficiently they can really run now that they've been through the bankruptcy.

Despite GM's recovery over the past year, analysts say it still faces a challenge in winning back consumers because of the lingering stigma from its bailout and the Government Motors label from critics.

GM's sales are up 13 percent in the United States through July, trailing a 15-percent gain in the overall market. GM cars and trucks also sell for an average discount of almost 16 percent off the U.S. sticker price -- a deeper discount than the rest of the industry -- according to tracking firm Edmunds.

Such incentives are tracked as an indicator of popularity and pricing power of auto brands with consumers.

GM also relied on lower-margin fleet sales -- including sales to car rental agencies -- for more than a third of its U.S. sales in the second-quarter.

SNAPPING A LONG LOSING STREAK

GM lost about $88 billion between 2005 and 2009 when it was driven into bankruptcy by plunging sales and tight credit.

The last time the automaker had consecutive quarters of profits was in 2004, when it had a 26-percent share in a U.S. auto market that was near record-high levels.

GM's U.S. market share was just over 19 percent in the quarter that ended in June, down from almost 21 percent a year earlier when it was still selling the now-scrapped Saturn, Saab, Pontiac and Hummer brands.

Since GM remains privately held Wall Street equity analysts had not released earnings forecasts for it.

GM's results show it is trailing its more successful and smaller rival Ford Motor Co, which posted a second-quarter profit of $2.6 billion, but ahead of Chrysler, which lost $172 million.

The turnaround in GM's North American operations was most striking from a year ago. In the most recent quarter, GM ran its North American factories at 93 percent of capacity, compared with 39 percent a year earlier.

Sources told Reuters on Wednesday that the largest U.S. automaker had secured a $5 billion credit facility, marking its return to the capital markets a year after it emerged from a government-funded landmark bankruptcy.

The credit facility was a last hurdle toward filing for an initial public offering of stock that allows the U.S. Treasury to reduce its nearly 61 percent stake in GM -- and the automaker to begin to shed its Government Motors label.

The filing for GM's IPO is expected on Friday.

The $27 billion in bonds issued by the pre-bankruptcy GM represent a 10 percent equity stake in the new company under the terms of the restructuring negotiated by the White House.

GM's 8.375 percent bonds due in 2033 rose less than a penny on the dollar to 35.75 cents in early trading on Thursday, yielding almost 24 percent.

The bond price has climbed from trading at 31 cents in early July, when the yield was more than 27 percent, according to MarketAxess data.

The higher bond price suggests that investors are placing a higher implied equity value on the automaker, which is expected to launch its stock offering by late November.

Second-quarter results were not directly comparable to the year-earlier period because GM was operating in Chapter 11 for part of the second quarter in 2009

GM's net income was $1.5 billion before accounting for just over $200 million in dividends on preferred stock held by the U.S. Treasury, a health-care trust affiliated with the United Auto Workers union and the governments of Canada and Ontario.

(Additional reporting by Bernie Woodall; Editing by Maureen Bavdek)