Tens of millions of Pakistanis are preparing to vote in a general election on Wednesday after a campaign tainted by violence and dominated by political controversy.

What happens in this South Asian country of nearly 200 million matters: it is a nuclear-armed rival to India, a key developing economy and one the world's largest Muslim-majority nations.

Here's what you need to know about the election, which has been called the dirtiest in Pakistan's history.

Why it's important
Since independence in 1947, Pakistan has oscillated between civilian and military rule. This election will mark the second time that one civilian government has handed power to another after serving a full term - a historic landmark.

But few in Pakistan are celebrating the strength of its democracy. The run-up to the vote has been marked by tensions between the incumbent Pakistan Muslim League-Nawaz (PML-N) party and the military.

The PML-N complains of a targeted crackdown against it by the powerful security establishment, with the alleged help of the courts. Nearly 17,000 party members are facing criminal cases over breaking unspecified election rules.

The media, meanwhile, have faced virulent censorship and intimidation. Another concern for some Pakistani democrats is the participation in the vote of militant groups.

Many believe the military is up to its old political machinations in favour of its preferred candidates. The Human Rights Commission of Pakistan says there have been "blatant, aggressive and unabashed attempts" to manipulate the polls, with "alarming implications for Pakistan's transition to an effective democracy".

The campaign has also been marred by violent attacks - including an IS-claimed attack in Balochistan on 13 July that killed nearly 150 people.

Who are the key players?

Nawaz Sharif (PML-N), 68

The three-time prime minister was disqualified from office last year after a corruption investigation prompted by the Panama Papers. He went to London to spend time with his ailing wife, but made a dramatic return with his daughter Maryam in early July, despite having been sentenced to 10 years in prison. The pair, convicted for their family's unexplained ownership of luxury London flats, are now behind bars.

He blames the military for conspiring against him because he has openly criticised them and seeks better relations with India. The military denies any role. Nawaz's brother, Shehbaz Sharif, has led the PML-N campaign and will be looking to become the next prime minister.

Key quote: "What credibility will these elections have when the government is taking such drastic action against our people and this crackdown is taking place all over the country?"

Current seats: 182

Imran Khan (PTI), 65

The former star international cricketer entered Pakistani politics more than two decades ago, but he has never run the government. This time, many observers are convinced that he is the military's preferred candidate and that they are working to undermine his rivals. Mr Khan and the military deny any collusion but he told the BBC that the current military chief, Gen Bajwa, "is probably the most pro-democratic man we have ever seen". The PTI is supported by controversial groups, including one linked to al-Qaeda.

Key quote: "Pakistan's issue is nothing to do with liberalism or fundamentalism. Pakistan has an issue of governance."

Current seats: 32

Bilawal Bhutto Zardari (PPP), 29

Oxford-educated Mr Bhutto Zardari is the latest political candidate from a long political dynasty. Both his mother, Benazir Bhutto, and his grandfather Zulfilqar Ali Bhutto, served as prime ministers. Both were also killed - Ms Bhutto by an assassin and her father by an executioner. The 29-year-old PPP leader, who is standing for parliament for the first time, says he wants to implement his mother's vision of a "peaceful, progressive, prosperous, democratic Pakistan". Polls predict the party will finish third.

Key quote: "If all you have to criticise me on is my age or my accent then you really can't defeat me on the issues."

Current seats: 46

Where will the vote be won?
The PML-N's stronghold is Punjab province - Nawaz Sharif's homeland and the country's richest and most populous province. It has more than half the 272 directly-elected seats in the National Assembly and will be the key battleground.

Mr Khan's PTI will have to make serious inroads here in order to win. In the 2013, the party performed well in Khyber Pakhtunkhwa.

Analysts say Mr Bhutto's PPP is popular among the "rural class" and its voter base is concentrated in the southern province of Sindh.

So what might happen?
In the context of an election where two of the three main parties have cast doubt on the legitimacy of the vote, analysts expect a tight race between the Sharifs' PML-N and Mr Khan's PTI. If neither party wins a clear majority, the support of Mr Bhutto Zardari's PPP and other parties could be crucial to forming a governing coalition.

If the PML-N wins, India and the US may breathe a sigh of relief, given Mr Khan's perceived closeness with the military and accusations that he is soft on Islamist extremism. Although Pakistan is a longstanding partner in the US war on terrorism, it's alleged protection of militant groups active in neighbouring Afghanistan has long irked Washington and President Trump has cut off security aid.

If the PTI wins, the PML-N could lead its supporters into the streets - especially if Mr Sharif remains behind bars.

No matter who wins, however, the military will seek to maintain its extremely powerful role in Pakistan.