The attacks on Ahmadinejad signal a turnaround from 2009, when Khamenei supported his re-election, which the reformist opposition said was rigged, igniting eight months of violent street unrest, the worst in the Islamic Republic's history.
The government put the uprising down by force, but Arab uprisings since then have revealed that authoritarian states can be vulnerable to popular anger fuelled by economic hardship.
Leading reformists are not taking part in next month's poll, which they say will not be free or fair. Opposition leaders defeated in the 2009 presidential vote, Mirhossein Mousavi and Mehdi Karoubi, have been under house arrest for a year.
The process of vetting candidates, which includes checks on loyalty to Iran's constitution and clerical leadership, is rooted in a theological debate once confined to Shi'ite religious seminaries but now increasingly out in the open.
Hardline supporters of Khamenei accuse Ahmadinejad's camp of
pursuing an "Iranian" school of Islam, an ideology which some see as an inappropriate mix of religion and nationalism.
Ahmadinejad accuses his rivals of breaking laws by insulting him and has threatened them with jail. Defaming top officials is a crime punishable by a maximum two-year prison sentence.
Politics in Iran are largely defined by attempts to claim the legacy of the late Ayatollah Ruhollah Khomeini, father of the revolution. Both camps portray themselves as the most capable of defending his commitment to Islam and social justice.
While Khamenei is only Iran's second supreme leader, meant to be a neutral arbiter in political life and ultimate Islamic authority in matters of state, presidents have come and gone.
Khamenei, who survived an assassination attempt in 1981, may hope to secure the presidency for one of his own acolytes: his adviser Ali Akbar Velayati or Foreign Minister Ali Akbar Salehi.
Velayati was foreign minister during the Iran-Iraq war in the 1980s and shares Khamenei's policy views, while Salehi has been a driving force behind Iran's nuclear program.
Western sanctions aimed at forcing Iran to make nuclear concessions have started to hurt energy and food imports, but many Iranians blame Ahmadinejad's policies for soaring prices.
They say his cuts in food and fuel subsidies, replaced with direct monthly payments of around $38 per person, have fuelled inflation, officially running at around 20 percent, although some economists say it is more like 50 percent.
"Those in towns and villages support Ahmadinejad. They fear that if the rival camp wins, the subsidy cash distribution will be stopped," said one analyst who declined to be named.
Ahmadinejad has also been tainted by Iran's biggest fraud scandal, made public with Khamenei's approval. Dozens have been arrested over the alleged siphoning of $2.6 billion of state funds. Five of those detained face execution. The president denies any government wrongdoing.
The feud among hardliners risks backfiring on both camps if voters, soured by the 2009 presidential election, ignore polls that offer them a limited choice in a strictly monitored system.
"In major cities, the vote is more politicized. However, I believe the turnout will not be very high as many people are still disappointed over the 2009 vote," said Farahvashian.
(Additional reporting by Ramin Mostafavi; Writing by Parisa Hafezi; Editing by Alistair Lyon)