US stocks end mostly higher as S&P 500, Nasdaq close at records on Iran war optimism