When Pakistan Prime Minister Imran Khan released a photo of a luncheon with Bill Gates last month, social media users noticed something odd: The round table had 13 seats, but only a dozen men.
The vacant space contained a ghost-like figure who appeared to be conversing with others around him, raising questions about whether the image had been doctored. Shortly afterward, local news outlets reported that the country's new spy chief, Lieutenant General Nadeem Anjum, had been erased out of the shot.
The drama began four months earlier, when army chief Qamar Javed Bajwa appointed Anjum to lead the Inter-Services Intelligence, or ISI, which oversees Pakistan's internal security. Mr Khan then delayed the appointment and publicly voiced support for General Faiz Hameed, widely seen as his ally, to stay in the role. After a standoff lasting several weeks, the army chief got his way.
Pakistan's civilian leaders have long clashed with the military, which has ruled the country for about half of its history. Yet if anything, Mr Khan has been criticized for being too close to the army since he promised to oversee a "New Pakistan" rid of corruption and favoritism following his 2018 election win.
His relationship with Faiz Hameed drew particular scrutiny. While the law says the premier appoints the ISI chief on the recommendation of the military, the opposition questioned Khan's motives: Nawaz Sharif, a three-time prime minister, accused Faiz Hameed of orchestrating his ouster on corruption charges in 2017 and swinging the election a year later.
Mr Khan's own actions didn't help. Besides seeking to keep Faiz Hameed at the ISI, the prime minister broke taboos by mentioning a private discussion with the army chief at a public rally, countering the military's own claims that it doesn't interfere in politics.
"Naming the army publicly on political forums is the biggest mistake this government has committed," said Shaista Tabassum, former head of the international relations department at the University of Karachi. Mr Khan and his ministers, she said, "have been publicly dragging the army into politics, saying things like the army is very much behind us or that we enjoy the support of the army chief."
That served as the backdrop for last month's luncheon with Gates, who was in Pakistan to promote a campaign to eradicate polio. Unlike his predecessor, Anjum ordered the media to avoid any pictures or videos of him -- leading to the strange altered image of the luncheon with the Microsoft Corp. founder.
The unusual episode provides a glimpse into Khan's behind-the-scenes tussle over military promotions that has underpinned a raft of troubles facing the 69-year-old former cricket star. A unified opposition is vying to oust him in a confidence vote in the next few days, as Asia's second-fastest inflation jeopardizes his chances to become the first prime minister in Pakistan's 75-year history to complete a full term in office.
Even if Mr Khan stays on, his high-stakes showdown with top generals risks leading to months of instability that could determine whether the world's fifth-most populous nation shifts even further toward China and Russia or leans back to the US and Europe.
The Gates photograph provided a vivid example of how the military was now acting "neutral" toward Mr Khan, signaling to Pakistan's political parties that he no longer had establishment support. Last year, the army's tacit backing helped Khan survive a similar challenge when he was forced to test his majority in parliament.
In one example of how that works on the ground, intelligence officers would often call up certain politicians that criticized Khan on television talk shows and warn them to stay quiet. Now that's no longer the case, according to a person familiar with the matter who asked not to be identified given the sensitivity of the issue.
Mr Khan's office and Pakistan's Foreign Ministry didn't reply to requests for comments. Pakistan security sources called allegations that the army or its affiliated institutions affected the outcome of the 2018 election "baseless and unfounded." They reiterated that the army has "nothing to do with politics" and blasted claims to the contrary as "disinformation." The Bill & Melinda Gates Foundation referred queries to Mr Khan's office.
For the military, referred to locally as "the establishment," Mr Khan once represented stability -- especially as the economy recovered from a pandemic-induced contraction. Top generals had a say in every element of the premier's administration, from foreign policy and security matters to economic decisions. Mr Bajwa and other generals regularly held private meetings with top business people and policy makers.
But the relationship began to deteriorate, both over Mr Khan's involvement in military promotions and souring relations with the US Reports said Pakistan's military, once a top recipient of American arms, has sought a more balanced foreign policy after becoming increasingly reliant on China for weapons.
Ties got off to a bad start just days after Joe Biden's inauguration, when a Pakistan court ordered the release of four men who had earlier been convicted of decapitating Wall Street Journal bureau chief Daniel Pearl in 2002. The case drew outrage from the White House, where a decade earlier Biden sat next to Barack Obama watching Navy SEALs secretly enter Pakistan and kill Osama bin Laden.
Biden didn't invite Mr Khan to his climate summit last April and wouldn't speak to him on the phone. Relations got worse as the Taliban took power in Afghanistan, with Khan saying the militant group had "broken the shackles of slavery."
Biden appeared to offer an olive branch last year when he invited Mr Khan to join his democracy summit in December. But the Pakistan leader snubbed the request in a move welcomed by China, which has funded projects in the nation valued at more than $25 billion. Mr Khan has since boosted ties with Russia, holding the first top-level meeting in more than two decades with Vladimir Putin just hours after the Russian leader invaded Ukraine.
Shehbaz Sharif, who leads the main opposition party and is poised to take power if Mr Khan is ousted, has vowed to improve ties with the US and European Union if he wins. He has said the army has been staying neutral ahead of the confidence vote, a notable claim given his older brother was ousted in a 1999 coup. Nawaz Sharif is currently self-exiled in London after being convicted in a corruption case he calls politically motivated.
The 342-member National Assembly will start a debate on the opposition's no-confidence motion on Thursday, with a vote expected over the weekend. This week Mr Khan lost his slim majority in the chamber after two coalition allies withdrew support for his government.
Ahead of the vote, Mr Khan has vowed to stay on. He rallied thousands of supporters in Islamabad last Sunday and claimed "foreign forces" were out to remove him.
Still, a Gallup poll last month showed Mr Khan's approval rating has dropped to 36% from 40% in 2018, while Nawaz Sharif's had more than doubled to 55% in that time. In December, Mr Khan lost a local election in a stronghold it had ruled for eight years, while lawmakers from his party have sought to leave ahead of the vote.
A big reason is the economy. Mr Khan has grappled with some of Asia's fastest price increases for a few years now while managing a $6 billion program with the International Monetary Fund that calls for tax increases set to further boost the cost of living. Mr Khan this month unexpectedly cut fuel and electricity prices to pacify public anger, disregarding the IMF agreement.
A win for Mr Khan would help him silence critics who say he can only win with the support of the army. A loss, on the other hand, could help him deflect blame for the economic slowdown ahead of national elections that must be held by August 2023.
"Nobody will be going to say in future he is selected or he came to power with their support," Zafar Nawaz Jaspal, a professor at the Quaid-i-Azam University in Islamabad. "This would in a way become political mileage for Khan in the next elections."
Even so, there's one more big question mark if Khan stays as prime minister: Will he allow Bajwa, the general he sparred with, to extend his time as army chief when his term expires in November? Reports indicate Mr Khan wants to instead install Hameed, the former ISI chief, as a powerful friend.
Such a move "will trigger a new controversy in Pakistani politics and within the military," said Hasan Askari Rizvi, a Lahore-based analyst who has written several books on the nation's army. Even so, he said, Khan only has himself to blame for his current troubles with the military.
"Khan can't maintain human relations -- he creates unnecessary rifts," Rizvi said. "The military is at distance and will maintain it."
Track Latest News Live on NDTV.com and get news updates from India and around the world