* Planned election follows major gains for Assad's forces
* Rebels driven from central Syria, but most provinces contested
* Foreign fighters on both sides complicate picture
By Dominic Evans and Mariam Karouny
BEIRUT, May 18 (Reuters) - From his base in Damascus, Bashar al-Assad can contemplate a broad sweep of Syria clawed back from rebels who once threatened to drive him out. The capital which they targeted is now plastered with posters inviting Syrians to reelect him president.
Powerful foreign allies have helped Assad hold or retake a chain of cities which form the north-south backbone of the country, keep his grip on the Mediterranean coast to the west and restore control over the Lebanese border.
The culmination of that slow, grinding military turnaround came last week with the final withdrawal of rebel fighters from Homs city, a month before the presidential election in which Assad faces no serious challenge.
His foes dismiss the June 3 vote as a farce, saying the huge areas still beyond his command make a credible vote impossible, but the fact that authorities can consider a notionally countrywide ballot reveals their growing confidence.
One of the two candidates officially approved to run against Assad said the overwhelming majority of Syrians would be able to vote, downplaying the fighting that still kills around 200 people a day and the almost three million who have fled.
"In the middle of the country the situation is perfect for election. On the coast the situation is very good. In the southern part of Syria the situation is getting better," said Hassan al-Nouri, a U.S.-educated former minister of state.
The military respite has come at a cost. Assad's foreign Shi'ite supporters have often taken the lead in battle, leaving his own forces to play a peripheral role against rebels who are themselves increasingly directed by outside Sunni powers.
Whoever pulls the strings, though, the long term momentum is clear.
Rebels have fought Assad's forces in Homs city since the early days of the uprising in 2011. Until a year ago they held territory along the main highway from Homs to Damascus and controlled the capital's eastern and southern suburbs.
Now that they have pulled out of Syria's third biggest city, battered by years of bombardment, siege and retreat, Assad's hold over the heart of the country is tighter than it has ever been since protests against his rule turned to armed insurgency.
On the fringes, rebels still pose a deadly challenge, holding parts of Aleppo and Deraa at the northern and southern tips of that backbone of Syrian cities.
Most of the northern border with Turkey is also in rebel hands, as are swathes of northern Syria, the eastern oilfields and farmlands, and southern areas close to Jordanian border and the Golan Heights.
Assad's enemies make much of the fact that the territory under his control may only account for a third of the country, but it forms an increasingly coherent core, linked by secure road connections, where a semblances of normality exists and the great majority of the population now lives.
By contrast the rebel-held land - riven with internecine fighting and battered by waves of Assad's aerial bombardment - offers neither security to the population nor a military platform to strike against his strongholds.
The three-year conflict has killed at least 150,000 people, but the relentless nature of the devastation and the lack of a single frontline marked by advances and retreats means the war often seems deadlocked.
In reality multiple battles are fought on a local level, the small scale of their ebb and flow making little impact on the broader war. With the possible exception of Tartous, one of two Mediterranean strongholds of Assad's Alawite minority, not one of Syria's 14 provinces is totally controlled by either side.
"It's a fluid form of stalemate," said one military official in the region who closely monitors the Syrian conflict. "The regime holds the strategic upper hand while the rebels fight tactical battles on the fringes."
Assad has been helped by the multiple fractures in rebel ranks. Rival al Qaeda groups are at each others' throats and the Western-backed coalition known as the Free Syrian Army (FSA) has been eclipsed by more Islamist brigades.
"Bashar has the upper hand on the ground. As long as the FSA exists he will always have the upper hand," said a fighter from the Islamic State in Iraq and the Levant (ISIL), highlighting the mutual animosity which plagues Assad's enemies.
ISIL, a breakaway al Qaeda faction, is battling the official al Qaeda branch in Syria, the Nusra Front. It has also clashed with the powerful Islamic Front and fighters from the FSA.
ISIL's rebel opponents say the group is more intent on carving out a jihadist heartland in rebel territory than it is on fighting Assad, who in turn has carefully avoided targeting ISIL in government air strikes, they say.
In his final speech to the U.N. Security Council before he steps down in despair, international mediator Lakhdar Brahimi lamented on Tuesday that there are "probably well over a thousand" rebel groups involved in Syria's complex war.
"Peace in Syria will not happen and be sustainable without a much more intimate understanding of the military map in Syria, and a deeper understanding of their interests, goals and potential to engage in a political process," he said. "We have not developed that understanding to a sufficiently high level."
LEBANON BORDER SEALED
Complicating the battle is the presence on both sides of thousands of foreign fighters. The mainly Sunni Muslim rebels, who are backed by Sunni regional powers, have been joined by Sunni jihadi fighters from across the world.
Assad, whose Alawite community is an offshoot of Shi'ite Islam, has been reinforced by Iraqi Shi'ite fighters and the Lebanese Shi'ite group Hezbollah, and supported by Shi'ite Iran and his main arms supplier and diplomatic supporters, Russia.
Hezbollah's role has been crucial over the last 12 months in consolidating control over the Qalamoun region close to the Lebanese border, overlooking the road linking Damascus to Homs.
"Qalamoun is now completely free," said a military source close to the pro-Assad forces. "Five important towns which were the main strongholds of the gunmen are now under the control of the government."
Thirty crossing points with Lebanon which had been used to supply rebel arms and fighters have been sealed along 124 km (78 miles) of border, he said, a crucial element in Assad's consolidation of power over the centre of the country.
A rebel offensive launched from Turkey into northern Latakia, the other Mediterranean Alawite bastion, seized ground two months ago not far from the ancestral Assad town of Qardaha, but petered out and lost most of its early gains.
Similarly, rebels have launched several attacks on Assad's forces around Aleppo - Syria's biggest city before much of it was reduced to ruins - but probably hold less territory there than they did after storming the city in the summer of 2012.
"What has been under government control still is," the military source said, describing both the rebel assaults around Aleppo and Latakia as failures.
The battle around Aleppo, as in most parts of the country, is far from straightforward. Fighters often have a better understanding with their foes than their brothers-in-arms.
"Those on the frontline are thugs and traders who are willing to sell and buy," a Nusra Front gunman said dismissively of his fellow insurgents. "Sometimes they make deals with Assad's forces, and sometimes they fight."
BARREL BOMBS AND TUNNEL BOMBS
Assad's air force has bombarded rebel-held areas of Aleppo and other cities with barrel bombs, crude explosives packed into cylinders or barrels and rolled out of the backs of helicopters from a great height to avoid rebel fire from the ground.
The resulting damage is largely indiscriminate and has been condemned by rights groups and Assad's international foes who say thousands of civilians have been killed in the bombardment.
The United States and France have also said preliminary evidence suggests Syrian government forces have used chlorine gas in the last two months - in violation of a treaty Damascus signed up to as part of a deal to eliminate its chemical arsenal after deadly sarin attacks around Damascus in August last year.
Trying to level the battlefield, rebels have acquired heavy weaponry - some supplied from Gulf Arab allies and some seized from Syria's army - including sophisticated rockets and tanks.
Those weapons have taken a heavy toll on Assad's forces. Figures collated by the Syrian Observatory for Human Rights monitoring group suggest that more pro-Assad fighters (58,000) were killed than rebels (38,000) by the beginning of April.
Rebels have been gaining ground in the heavily militarised south, seizing army positions between the city of Deraa and the town of Quneitra close to the Israeli-occupied Golan Heights.
Lacking air power, they have also turned underground, detonating three huge 'tunnel bombs' under targets in the northern provinces of Aleppo and Idlib in the last two weeks.
With video footage showing columns of earth flying into the sky like smoke, an Islamic Front commander said from inside Syria they would not be the last. "We will use this strategy whenever there is a target that is difficult for us to reach."
(Editing by Philippa Fletcher)