Just a week before Britain votes, the prime minister's hope of settling once and for all the country's long-simmering European question with a resounding vote to stay in the EU may be out of reach. Surveys show the country is almost exactly divided, with momentum in recent days for "out."
If Britain does vote to leave - a scenario popularly known as Brexit - analysts say that Cameron would probably be forced to resign, perhaps within hours of the result.
Even if British voters heed Cameron's call to stay in the EU, a narrow victory could leave him vulnerable to a vengeance-fueled coup by pro-Brexit politicians in his party who think the prime minister has played dirty in his no-holds-barred campaign to keep Britain in.
Politics were put on hold Thursday afternoon by the death of Jo Cox, a 41-year-old member of Parliament who was shot and stabbed to death after a meeting with constituents near the northern English city of Leeds. The killing shocked Britain, and both sides of the referendum debate said they would suspend their campaigns until at least the weekend.
But when they resume, Cameron will be on tenuous ground, with just a few days to pull his country back from the brink of Brexit.
The fragility of Cameron's position marks a stunning turnabout for a politician who won a commanding electoral victory just a year ago and who called the EU referendum as a way to unify his fractious party behind his leadership.
"This has turned out worse for Cameron than he ever conceived it could have," said Roger Mortimore, a politics professor at King's College London who directs political analysis at the polling firm Ipsos Mori. "I don't think anyone really saw this coming. It's very clear that David Cameron didn't see it coming."
Among the prime minister's gravest misjudgments, Mortimore said, was that he could rely on the small clique of Oxford-educated politicians who with Cameron form the upper echelon of Conservative Party politics. Instead of loyalty, several have jettisoned the prime minister, and one - the shaggy-haired, populist former London mayor Boris Johnson - has all but declared his intention to topple the man who has led Britain for the past six years.
Johnson and other pro-Brexit dissidents, said professor Tim Bale of Queen Mary University of London, form a "pop-up government in waiting" that is prepared to seize power if the Brexit vote does not go Cameron's way. That dynamic, he said, has made for a particularly nasty campaign.
"Given the division in the party over Europe, there was always going to be some friction," said Bale, who has written books on the Conservatives. "But it's been made worse by the fact that there are an awful lot of personal ambitions at stake here as well."
In recent weeks, as polls have revealed an electorate stubbornly divided on EU membership, both sides have resorted to personal attacks of a sort rarely seen within the highest reaches of a British governing party.
Johnson and his allies have accused Cameron of misleading the public with scary stories about the devastating effect Brexit would have on the country's economy. In campaign appearances, the voluble Johnson has lacerated the prime minister's case for staying in the EUas "propaganda" and "a hoax."
Cameron and his allies have returned fire by calling Johnson out on what they regard as his naked ambition. In a nationally televised debate last week, Cameron loyalist Amber Rudd noted pointedly that "the only number Boris is interested in is the one that says 'Number 10' " - a reference to the door number on the prime minister's Downing Street residence.
Although the acerbic and personalized tone of the debate is new, the profound split within the Conservative Party is not. It dates at least to the 1980s and the reign of Tory icon Margaret Thatcher. Her views on Europe were decidedly mixed, and both sides in the current debate have claimed her backing from beyond the grave.
The question of whether Britain should be part of the EU cuts across the country's left-right political divide. Among Tories, staying in the union appeals to pro-business politicians who favor the benefits of low trade barriers with continental economies. But the party also has a deep nationalistic tradition, and the idea that Britain can be truly sovereign only outside the EU resonates with the Conservative grass roots.
So, too, does the pro-Brexit camp's claim that leaving Europe will allow the United Kingdom to significantly reduce immigration.
When Cameron gambled and promised voters a direct say on the EU in January 2013, Conservatives faced a sharp challenge from their right by the anti-immigration U.K. Independence Party.
By offering a referendum, Cameron thought he could outflank UKIP and mend the long-standing rift within his party. The first part worked, with Conservatives winning an unexpected majority in last year's general election while UKIP remained marginalized.
But the second part appears to have backfired.
"It's come at the price of creating these very public divisions within the party, and possibly setting off this chain of events that is spiraling out of control for him," said Thomas Quinn, who teaches politics at the University of Essex.
If Britain votes to leave the EU on June 23, despite Cameron putting his full weight behind the case for staying in, the prime minister's odds of keeping his job would be "slim to nonexistent," Quinn said.
"He just wouldn't have the authority," Quinn said. "He could be offering his resignation within a few days - if not on the day."
But even if Cameron pulls out a close victory, he could still be in peril, with pro-Brexit Tories blaming him for undermining their long-awaited chance to break free of the EU
Andrew Bridgen, a Tory member of Parliament who favors Brexit, said in an interview that unless Britain votes to stay in the EU by a wide margin, Cameron should step down.
"The prime minister has led a very disingenuous campaign on the most crucial question our country will face in my lifetime," he said. "He's blown his credibility with the electorate."
Others in the party say that Conservatives need to get beyond the damaging clash of personalities and start focusing on the substance of sorting out Britain's place in the world. That will be a challenge regardless how the public votes, said Phillip Lee, a Tory member of Parliament who favors staying in the EU.
"What kind of country do we want to be going forward?" Lee said. "I hope that post-referendum, the debate doesn't stop. Within the Conservative Party, we need to come to terms with what Britain's role in the world ought to be."
But at least in the short term, the struggle for power could drown out any broader discussion.
If Cameron doesn't step down, all it will take is for 50 Conservative members of Parliament - out of a total of 330 - to force a no-confidence vote.
His ouster would trigger a leadership contest in which the Tory faction in Parliament selects two candidates from among its ranks to vie to become the next prime minister. The winner would be chosen by Conservative Party members nationwide. The general public would not get a vote.
Quinn, who has written a book on British party leadership contests, rated Johnson's chances of becoming prime minister by the end of the year at 40 percent.
That is despite the fact that Johnson has alienated parliamentary colleagues and foreign leaders with his over-the-top comments during the campaign.
The publicity-hungry Johnson has drawn comparisons to U.S. presidential candidate Donald Trump for his suggestion that the EU and Hitler both had the same goal, and his insinuation that President Obama is hostile toward Britain because of his "part-Kenyan" heritage.
Nonetheless, Johnson remains popular with the Conservative rank-and-file members who would ultimately pick the prime minister if Cameron steps down.
"He has that X-factor that other politicians don't seem to have," said Bale, the Queen Mary University professor. "But he's hardly a safe pair of hands. I'm not sure he's seen as someone who can lead the party - or, indeed, the country."