TEHRAN—Even here, nobody is happy about Iraq’s implosion. But Iranian officials are preparing to make the best of it. In a raft of interviews this week with government officials, a consensus has emerged: Iraq is a chance to make amends with the United States. It may even be the beginning of a beautiful friendship.

Already, the question both here and in Washington is not “if” Tehran and Washington should work together to combat the advance of the Islamist insurgency ISIS, but “how.” “We are open to any constructive process here that could minimize the violence, hold Iraq together — the integrity of the country — and eliminate the presence of outside terrorist forces that are ripping it apart,” Secretary of State John Kerry said in Washington Monday. President Obama added today that “Iran can play a constructive role.”

Iran is moving to cast itself as a U.S. ally in the fight against terrorism (despite its official sponsorship of Hezbollah). In meetings here over the last week, part of the Bergedorf Round Table — the session was called “Stability in the Middle East: Prospects for Cooperation between Iran and the West” — Iranian officials repeatedly said things like this: “Like the U.S. and Europe, we are fighting terrorism.”  They also stressed common interests with the West. (My trip, like the conference, was sponsored by the Körber Foundation, a nonpartisan group devoted to social development.)

It is, however, a fraught aspiration. For one thing, diplomats from both the West and Iran say they’re nowhere close on a deal to halt Tehran’s nuclear program; sanctions are not going away anytime soon. For another, Iran’s support of Hezbollah makes it a state sponsor of terrorism in the eyes of Western nations. Then there’s the government’s ambiguous treatment of terrorists inside its own borders: High-ranking al-Qaeda officials like Saif al-Adel had been here for years under “house arrest,” and officials have not worked to stop jihadists from Europe who cross Iran to join the fight against U.S. and NATO troops in Pakistan, according to U.S. and European intelligence sources.

Cooperating over Iraq isn’t just a way to temporarily paper over those differences. (This strategy is already working: One conference participant from Europe who cannot be named, because of the terms of conference participation, even suggested, “Europe should start direct relationships with Iran, with or without a deal in the nuclear question.”) It also allows Tehran to advance its interests in Baghdad: to reinforce Prime Minister Nuri al-Maliki’s shaky regime at the expense of Sunnis.

Of course that relationship has been part of the problem. Human rights organizations point to the Iraqi government’s discrimination against Sunnis. Shiite militants have also frequently taken the law in their own hands, torturing Sunni prisoners. No wonder ISIS had such an easy job recruiting among some parts of the Sunni population. “To avoid turning the conflict into a full-scale sectarian conflict, the immediate action must be a formation of a new national unity government under a national leader who believes in his Iraqism, thinks and acts as Iraqi not Shia or Sunni,” says Awadh Al-Badi, a scholar at King Faisal Center for Research and Islamic Studies in Riyadh.

Still, for all its happy talk about working with the United States, Iranian officials took every chance this week to hammer at their rivals, the monarchies across the Persian Gulf. They blamed Saudi Arabia for “financing ISIS.” (Maliki’s cabinet echoed this opinion Wednesday.) But while it’s true that Saudi Arabia, Qatar—whose new emir is expected to visit Iran soon—and other countries have backed opposition groups in Syria, U.S. and European intelligence officials say there is no evidence that they are funding ISIS, which now controls large swaths of northern Iraq.

In fact, Arab countries long ago told their citizens fighting in Syria to return home or be stripped of their citizenship. (It was, at least, a first step toward a badly needed Persian-Arab rapprochement in the region.) “Saudis and all others in the region have understood that ISIS or Nusra are not their friends either,” says one European intelligence official who spoke under the condition of anonymity. “A strong ISIS or a sectarian war could also endanger all the rulers in the region.”

In any case, too much cooperation between the United States and Iran could feed ISIS rhetoric that the West is supporting Shiites in an effort to destroy Sunnis. Washington must find a way to work with everyone in the region—especially the Arab League—without increasing the popularity of ISIS or sparking a Sunni-Shiite tsunami throughout the region, officials from around the world said here this week. That still leaves a seat for Iran at the table.