Mahmoud Ahmadinejad stands down after eight turbulent years as Iran president this summer with Iran isolated and shackled by sanctions while his own future is precarious.
Questions linger over Ahmadinejad's legacy
Supremely confident and boundlessly ambitious, Mahmoud Ahmadinejad, the pious son of a humble blacksmith, saw himself as a historic leader, determined to restore Iran to the ranks of world superpowers. And as a populist, he vowed to channel his country's immense oil wealth towards the poor.
But he stands down after eight turbulent years this summer with Iran isolated and shackled by sanctions while his own future is precarious.
He is in effect a political outcast after running afoul of the ruling establishment of conservative clerics and Revolutionary Guard commanders that once championed him.
A hardline vetting body has prevented a controversial ally he anointed to succeed him from contesting Friday's presidential election, a move the famously pugnacious Mr Ahmadinejad has surprisingly failed to challenge - at least so far.
He still has a significant political base, particularly among the rural poor who are grateful for the money he funnelled to long-neglected provincial areas. "And his brand of religious nationalism resonated with many Iranians," said Sir Richard Dalton, a British former ambassador to Tehran.
Some suspect Mr Ahmadinejad, 56, has been warned not to stir up trouble, others speculate he has been offered a cosy sinecure in return for his compliance.
"But he doesn't leave office until August so he could still go out swinging. If he does, that would be one of things most remembered about him," said Gary Sick, an Iran expert at Columbia University in New York. "So at the moment, his legacy is a work in progress."
At home, Mr Ahmadinejad will be remembered mostly for his disputed re-election four years ago when Iran's reformist camp claimed it had been robbed of victory by vote-rigging, which ignited the biggest street protests since the 1979 Islamic revolution. The peaceful demonstrations were ruthlessly suppressed.
Abroad, Mr Ahmadinejad is remembered for his repeated questioning of the Holocaust, mainly during his first term. Playing to the Arab gallery, he also gleefully and repeatedly predicted the demise of Israel.
That reckless rhetoric deepened western suspicions about Tehran's nuclear programme, which advanced significantly under his watch but at a very high cost. "His inflammatory outbursts made it far easier for the US to marshal increasingly punitive international sanctions against Iran," Sir Richard said.
In Iran and Israel, there are jokes that Mr Ahmadinejad was an agent of Mossad, the Israeli spy agency.
Educated Iranians winced whenever their quixotic president ventured abroad. Apart from his denials of the Holocaust, Mr Ahmadineajd was ridiculed during one trip to the United Nations General Assembly in New York - an annual jaunt he loved - when he claimed there were no gays in Iran.
"His presidency served to undercut Iran's deepest national interests," Mr Sick said. But Iran's supreme leader, Ayatollah Ali Khamenei "must also share responsibility" for that because he backed Mr Ahmadinejad's tainted re-election in 2009 as a "divine assessment".
Mr Ahmadinejad will leave office with Iran's power structure narrower than ever, dominated by hardliners shored up by the country's security apparatus. Reformists were mostly purged from the system four years ago.
Two years later, Iran's ruling hardliners were at each other's throats when the polarising president dared to challenge Ayatollah Khamenei's absolute authority and sought more power for himself and his allies. Inevitably, he lost and his fall from grace was swift and spectacular.
The six mostly hardline candidates vying to succeed him have ridiculed his legacy. They blame him for Iran's economic woes - the main concern of voters - and accuse him of recklessly antagonising the West with inflammatory rhetoric.
All tout their ability to mend ties with the international community without compromising Tehran's right to a peaceful nuclear programme or its staunch support for President Bashar Al Assad's regime in Syria.
For all his fiery tirades against the West, it is often overlooked that Mr Ahmadinejad had periodically pressed for better relations with the United States. As a populist, he knew that young Iranians yearn for their country to come in from the cold, and that sealing better relations with the "global arrogance" America, would bolster his legacy.
But his Holocaust denial had made him toxic to Washington and Ayatollah Khamenei blocked any outreach to the US, in part because he did not want his wayward president taking credit for a historic breakthrough.
At home, Mr Ahmadinejad overestimated his own popularity and failed to heed a reality: specifically, that the Iranian constitution subjugates the powers of the president to those of the unelected supreme leader.
Crossing the line carries a heavy price. The two reformist leaders who contested Mr Ahmadinejad's tainted re-election in 2009 had impeccable revolutionary credentials. But Mir Hossein Mousavi and Mehdi Karroubi have been under house arrest for two years.
"They weren't protected from being severely treated by being long-standing and faithful servants of the Islamic republic," Sir Richard said. "The same will go for Mr Ahmadinejad if he rocks the boat."
The more positive aspects of Mr Ahmadinejad's legacy, meanwhile, are mostly overlooked. "He had the guts to challenge the supreme leader, which made Khamenei look quite ordinary," said Muhmmad Sahimi, an Iran expert at the University of Southern California.
Mr Ahmadinejad also managed to slash a costly subsidy system on fuel and food without igniting unrest. He boasted it was the "biggest economic plan in the past 50 years".
In return, he gave cash handouts to the poor, but this fuelled inflation that hit them most. The establishment, meanwhile, used Mr Ahmadinejad as a convenient lightning rod to absorb criticism for the regime's failings.
Most of the candidates have also blamed Mr Ahmadinejad for the country's economic woes.
The national currency, the rial, has lost more than two-thirds of its value and inflation has exceeded 30 per cent since sanctions were imposed in early 2012 by the US and the European Union in an effort to force Iran to cut back on its nuclear drive.
However, the candidates said Mr Ahmadinejad's "mismanagement" was as much to blame as the international restrictions on Iran's oil exports and banking system.
A rare voice of high-level, albeit qualified support for Mr Ahmadinejad's legacy has come from Iran's chief nuclear negotiator, Saeed Jalili, who is running in Friday's election. He is a hardline protégé of the supreme leader.
A recent campaign flyer from Mr Jalili's office opined that "Iran's position during Ahmadinejad's era was strengthened". It said: "The poor and deprived masses were given attention and revolutionary values saw growth." There were "weaknesses", however, that Mr Jalili's team "won't neglect".