U.S. stocks ended a three-day losing streak on Wednesday as recent underperformers led a thinly traded rally that wasn't seen as strong enough to overcome worries about waning global demand.

The S&P 500 hit its lowest intraday level since April 19 on Tuesday, and recent weak breadth suggested selling had gone too far for now. Energy and materials shares, which have lived and died by hopes for robust global recovery, returned to their winning ways.

Investors are tired of selling the market, especially with commodities finding their feet, said Andrew Wilkinson, senior market analyst at Interactive Brokers Group in Greenwich, Connecticut.

Energy shares gained on an unexpected drop in distillate stockpiles, which boosted heating oil futures. The S&P energy index <.GSPE> advanced 1.5 percent, by far the biggest percentage gainer among S&P 500 sectors. Dow component Exxon Mobil Corp rose 0.8 percent to $81.96 as the PHLX oil service sector index <.OSX> added 2.8 percent.

Bespoke Investment Group said breadth in the S&P 500 was very close to extremely oversold levels and that it was a buying opportunity in March, which was the last time those levels were reached.

Despite that, further upside was seen as limited, given headwinds from Europe and the prospect of an environment without supportive monetary policy.

There's a lot of listlessness, given the euro-zone issues and the volatility both ways in commodities, which is unsettling, said Rob McIver, co-portfolio manager of the Jensen Portfolio in Portland Oregon.

There are still opportunities, but also very divergent views about what will happen after ends.

The S&P material and industrial sectors' indexes each rose about 1 percent following a period of weakness for the groups.

The Dow Jones industrial average <.DJI> gained 38.45 points, or 0.31 percent, to end at 12,394.66. The Standard & Poor's 500 Index <.SPX> advanced 4.19 points, or 0.32 percent, to 1,320.47. The Nasdaq Composite Index <.IXIC> rose 15.22 points, or 0.55 percent, to 2,761.38.

Flextronics International Ltd contributed to the larger gains in the Nasdaq, rising 3.8 percent following an upgrade from Raymond James.

Homebuilders also advanced after luxury builder Toll Brothers Inc said orders rose in the latest quarter as low home prices induced its target market of the affluent to start buying again. The stock climbed 1.8 percent to $20.63. The Dow Jones U.S. home construction index <.DJUSHB> advanced 1.1 percent.

New orders for long-lasting durable goods posted their largest drop in six months in April as aircraft and motor vehicle orders tumbled, a government report showed.

Equities initially traded lower on the news, which, along with recent weak data on manufacturing figures from the Atlantic region as well as disappointing New York and Philadelphia Fed manufacturing surveys, pointed to a slowdown in the pace of economic growth.

American International Group Inc slumped 4 percent to $28.28, below the $29 offer price of the 300 million shares being sold by the U.S. Treasury and the bailed-out insurance company.

More than 60 percent of stocks traded on both the New York Stock Exchange and the Nasdaq ended in positive territory.

Volume was light, with about 6.69 billion shares traded on the New York Stock Exchange, the American Stock Exchange and Nasdaq, below last year's daily average of 8.47 billion.

Advancers outnumbered decliners by a ratio of about 2 to 1 on both the NYSE and the Nasdaq.

(Reporting by Ryan Vlastelica; Editing by Jan Paschal)