Ahmadinejad, who ran on a populist platform that sought to redistribute wealth across Iranian society, had no interest in curtailing Iran's nuclear programme and questioned Israel's right to exist.
In style and substance, he was different from the more moderate Rouhani, who is even-tempered and sophisticated in his dealings with the West and managed to get support from all factions of Iran's elected and unelected Government in negotiating the nuclear deal.
Rouhani enjoys the backing of former President Mohammad Khatami, who served from 1997 to 2005. Khatami is the de facto godfather of Iran's Green Movement, the reformist camp that took to the streets to protest the 2009 re-election of Ahmadinejad.
In part because he favours more liberalisation of Iran's economy and greater engagement with the West, Khatami has broad support among Iran's youth, who want more civil liberties and less government intrusion in their private lives.
He also has the support of those in the international community who would like to see Iran prioritise negotiation, rather than confrontation, with global powers.
Rouhani's chief opponent is the conservative cleric Ebrahim Raisi. Raisi is rumoured to be the IRGC's preferred choice for supreme leader when Khamenei, 77, passes.
While Iran's supreme leader does not explicitly endorse any candidate, it is clear from Khamenei's criticism of Rouhani's foreign and domestic policy that he prefers the President's hardline rival.
Last year Khamenei appointed Raisi to lead Astan Quds Razavi, the most powerful religious foundation in Iran.
Raisi is also the custodian of the Imam Reza shrine in Mashad, the most visited and important holy site in Iran, and has a strong base of support among rural religious poor and hardline clerics.
Raisi hopes to follow in the footsteps of Khamenei, who served two terms as Iran's President before becoming supreme leader. But if he loses the election by a wide margin, that path is less likely, as his backers would struggle to justify his ascension to absolute power when voters didn't even want him to be president.
Raisi has promised his supporters more cash handouts and a redistribution of the country's wealth. He has campaigned on tackling corruption and confronting US policies in the region, which he believes undermine Iran's security.
If this rhetoric sounds familiar, it's because it is. It is the same platform that helped propel Ahmadinejad to victory in 2005. At the time, Iran was at a crossroads, as it is now. It had two terms of Khatami, the reformist.
Its economy was beginning to rebound. Its relationship with its Arab neighbours was improving and diplomatic engagement with the West, particularly with Europe, was becoming the norm rather than the exception.
Domestically there was greater tolerance for diverging points of view as a record number of newspapers had started circulating.
In every respect, Iran took a giant step backward by the time Ahmadinejad's presidency ended. The country became isolated internationally with sanctions that choked its already struggling economy.
The electorate was divided internally as many Iranians viewed Ahmadinejad's 2009 re-election as illegitimate because of allegations of widespread voter fraud.
A win for Rouhani would be a win for moderation. It would mean that Iran's revolution is evolving in ways that could satisfy its people, who want a government willing to meet their everyday needs.
It would also mean that the West has a negotiating partner that wants to bring Iran back in from the diplomatic cold and become a responsible member of the international community. The world will be watching closely.