(Reuters) - Wall Street cut losses to end little changed on Tuesday while energy stocks rallied after U.S. President Donald Trump said the United States would quit the Iran nuclear deal, confirming what many investors had expected.

In a televised speech, Trump said the United States would withdraw from a 2015 international agreement designed to deny Tehran the ability to build nuclear weapons, and also reinstate sanctions on Iran.

The S&P energy sector .SPNY erased earlier losses to end the day up 0.78 percent as oil prices CLc1LCOc1 reduced earlier declines on bets that the sanctions would disrupt global crude supplies.

“You have seem some modest flight to quality, though it hasn’t been major,” said Brian Daingerfield, macro strategist at NatWest Markets in Stamford, Connecticut. “There’s still quite a bit of uncertainty about the future of the deal even now that the U.S. has made its intentions clear.”

The decision to leave the agreement with Iran is likely to raise the risk of conflict in the Middle East, upset America’s European allies and disrupt global oil supplies. Trump said he is willing to negotiate a new deal with Iran.

“He’s not closed the door and shunned them. He wants a new deal. The door is open to try again, which is probably less harsh than what he could have said,” said Brian Battle, director of trading at Performance Trust Capital Partners in Chicago.

Toward the end of the session, the stock market all but eliminated earlier losses.

The Dow Jones Industrial Average .DJI ended 0.01 percent higher at 24,360.21, while the S&P 500 .SPX ended down 0.03 percent to 2,671.92.

The Nasdaq Composite .IXIC added 0.02 percent to 7,266.90.

S&P industrials .SPLRCI, financials .SPSY and technology indexes .SPLRCT rose, while utilities .SPLRCU and telecom services .SPLRCL were the deepest decliners.

Comcast CMCSA.O fell 5.56 percent after Reuters reported the cable operator is preparing to make an all-cash offer for media assets that Twenty-First Century Fox FOXA.O has agreed to sell to Disney DIS.N for $52 billion.

After the bell, Disney edged up 0.5 percent after it reported its quarterly results that were powered by the hit superhero movie “Black Panther”.

Advancing issues outnumbered declining ones on the NYSE by a 1.04-to-1 ratio; on Nasdaq, a 1.19-to-1 ratio favored advancers.

The S&P 500 posted 17 new 52-week highs and 13 new lows; the Nasdaq Composite recorded 119 new highs and 42 new lows.

Volume on U.S. exchanges was 6.9 billion shares, compared to the 6.5 billion average over the last 20 trading days.