And so they marched, a couple of hundred people in this capital of 12 million. American flags were burned, children waved portraits of President Obama with Dracula-like fangs, and security officers holding walkie-talkies tried to look inconspicuous.

While many, if not most, Iranians in the capital scoff at the “Death to America” crowd, anti-Americanism remains an important part of the Islamic republic’s ideology and legitimacy.

The state television program that criticized Mr. Rafsanjani cited a passage from the last will and testament of Ayatollah Khomeini, who died in 1989. “The U.S.A. is the foremost enemy of Islam,” it said. “It is a terrorist state by nature that has set fire to everything everywhere, and its ally, the international Zionism, does not stop short of any crime to achieve its base and greedy desires, crimes that the tongue and pen are ashamed to utter or write.”

The state news media give prominence to anti-American demonstrations, like a well-orchestrated outburst recently among Iranian pilgrims in Mecca, Saudi Arabia. This week, state television showed believers during Id al-Adha, the Islamic Feast of Sacrifice, who would almost not stop shouting “Death to America.”

Mr. Rouhani’s government has announced that it wants to conduct a public opinion survey on the advisability of its outreach to the United States. But it is unclear if this will happen, analysts say, because it would lay bare the ideological divisions in the Islamic republic.

A similar poll conducted in 2003 showed that 70 percent favored establishing ties with America. There was no follow-up, though, because the pollsters were jailed for several years.

In the Islamic republic, support of “the people” is often cited by all factions. But with the animosity toward the United States, things can get complicated. A majority of the Iranian electorate voted for Mr. Rouhani and his conciliatory international polices, while the revolutionary narrative prescribes that the fight against America is eternal and supported by all Iranians.