Mahmoud Ahmadinejad was both kissed and scolded on Tuesday when he began the first visit to Egypt by an Iranian president since Tehran's 1979 Islamic revolution.
The trip was meant to underline a thaw in relations since Egyptians elected an Islamist head of state, President Mohamed Morsi, last June. But it also highlighted deep theological and geopolitical differences.
Morsi, a member of the Sunni Muslim Brotherhood, kissed Ahmadinejad after he landed at Cairo airport and gave him a red carpet reception with military honours. Ahmadinejad beamed as he shook hands with waiting dignitaries.
But the Shi'ite Iranian leader received a stiff rebuke when he met Egypt's leading Sunni Muslim scholar later at Cairo's historic al-Azhar mosque and university.
Sheikh Ahmed El-Tayeb, head of the 1,000-year-old seat of religious learning, urged Iran to refrain from interfering in Gulf Arab states, to recognise Bahrain as a "sisterly Arab nation" and rejected the extension of Shi'ite Muslim influence in Sunni countries, a statement from al-Azhar said.
Visiting Cairo to attend an Islamic summit that begins on Wednesday, Ahmadinejad told a news conference he hoped his trip would be "a new starting point in relations between us".
However, a senior cleric from the Egyptian seminary, Hassan al-Shafai, who appeared alongside him, said the meeting had degenerated into an exchange of theological differences.
"There ensued some misunderstandings on certain issues that could have an effect on the cultural, political and social climate of both countries," Shafai said.
"The issues were such that the grand sheikh saw that the meeting ... did not serve the desired purpose."
The visit would have been unthinkable during the rule of Hosni Mubarak, the military-backed autocrat who preserved Egypt's peace treaty with Israel during his 30 years in power and deepened ties between Cairo and the West.
"The political geography of the region will change if Iran and Egypt take a unified position on the Palestinian question," Ahmadinejad said in an interview with Al Mayadeen, a Beirut-based TV station, on the eve of his trip.
He said he wanted to visit the Gaza Strip, the Palestinian territory which neighbours Egypt to the east and is run by the Islamist movement Hamas. "If they allow it, I would go to Gaza to visit the people," Ahmadinejad said.
Analysts doubt that the historic changes that brought Morsi to power will result in a full restoration of diplomatic ties between states whose relations were broken off after the conclusion of Egypt's peace treaty with Israel in 1979.
Obstacles to full ties
At the airport the two leaders discussed ways of improving relations and resolving the Syrian crisis "without resorting to military intervention", Egyptian state media reported.
Egypt is concerned by Iran's support for Syrian President Bashar al-Assad, who is trying to crush an uprising inspired by the revolt that swept Mubarak from power two years ago.
Egypt's overwhelmingly Sunni Muslim population is broadly supportive of the uprising against Assad's Alawite-led administration.
Egyptian Foreign Minister Mohamed Kamel Amr sought to reassure Gulf Arab allies - that are supporting Cairo's battered state finances and are deeply suspicious of Iran - that Egypt would not jeopardise their security.
"The security of the Gulf states is the security of Egypt," he said in remarks reported by the official MENA news agency.
Morsi wants to preserve ties with the United States, the source of $1.3 billion in aid each year to the influential Egyptian military.
"The restoration of full relations with Iran in this period is difficult, despite the warmth in ties ... because of many problems including the Syrian crisis and Cairo's links with the Gulf states, Israel and the United States," said one former Egyptian diplomat.
Iranian Foreign Minister Ali Akbar Salehi said he was optimistic that ties could grow closer.
"We are gradually improving. We have to be a little bit patient. I'm very hopeful about the expansion of the bilateral relationship," he told Reuters. Asked where he saw room for closer ties, he said: "Trade and economics."
Egypt and Iran have taken opposite courses since the late 1970s. Egypt, under Mubarak's predecessor Anwar Sadat, concluded a peace treaty with Israel in 1979 and became a close ally of the United States and Europe. Iran from 1979 turned into a centre of opposition to Western influence in the Middle East.
Symbolically, Iran named a street in Tehran after the Islamist who allegedly led the 1981 assassination of Sadat.
Egypt gave asylum and a state funeral to Iran's exiled Shah Reza Pahlavi, who was overthrown in the 1979 Iranian revolution. He is buried in a mosque beside Cairo's mediaeval Citadel alongside his ex-brother-in-law, Egypt's last king, Farouk.