Exactly two decades ago, on August 23, 1996, Osama bin Laden declared war on the United States. At the time, few people paid much attention. But it was the start of what’s now the Twenty Years’ War between the United States and al-Qaeda—a conflict that both sides have ultimately lost.

During the 1980s, bin Laden fought alongside the mujahideen in Afghanistan against the Soviet Union. After the Soviets withdrew, he went home to Saudi Arabia, then moved to Sudan before being expelled and returning to Afghanistan in 1996 to live under Taliban protection. Within a few months of his arrival, he issued a 30-page fatwa, “Declaration of War Against the Americans Occupying the Land of the Two Holy Places,” which was published in a London-based newspaper, Al-Quds Al-Arabi, and faxed to supporters around the world. It was bin Laden’s first public call for a global jihad against the United States. In a rambling text, bin Laden opined on Islamic history, celebrated recent attacks against U.S. forces in Lebanon and Somalia, and recounted a multitude of grievances against the United States, Israel, and their allies. “The people of Islam had suffered from aggression, iniquity and injustice imposed on them by the Jewish-Christian alliance and their collaborators,” he wrote.

His central lament was the presence of U.S. forces in Saudi Arabia, or “the occupation of the land of the two holiest sites.” Following the Iraqi invasion of Kuwait in 1990, bin Laden had offered to defend Saudi Arabia with his Arab legion. But the Saudi royals decided that the U.S. military would be a better bet. Six years later, American soldiers were still in Saudi Arabia in a bid to contain Saddam Hussein. Bin Laden saw the United States as the power behind the throne: the “far enemy” that propped up apostate regimes in the Middle East. Muslims, he wrote, should abandon their petty local fights and unite to drive the Americans out of Saudi Arabia: “destroying, fighting and killing the enemy until, by the Grace of Allah, it is completely defeated.”

And so began the Twenty Years’ War between al-Qaeda and the United States, which has had five distinct eras to date. The first phase, from 1996-2001, was the phony war marked by intermittent hostilities. It took al-Qaeda two years to organize its first major attack against the United States: the August 1998 bombing of U.S. embassies in Kenya and Tanzania, which killed 224 people in total, 12 of them American. The United States responded with a quasi-war against al-Qaeda and its state sponsors, which combined a legal indictment of bin Laden with limited military action, including cruise missile strikes in Afghanistan and Sudan in 1998 that killed at least six al-Qaeda personnel. In 2000, al-Qaeda suicide bombers hit the USS Cole at a port in Yemen, killing 17. The following year, the terrorist group brought the war to the American homeland with the 9/11 attacks, which killed nearly 3,000 people.

The second phase of the Twenty Years’ War, from 2001-2003, was the invasion of Afghanistan, which represented the high point of American optimism about victory. George W. Bush seized the sword, declaring a “war against terrorism,” sweeping aside the Taliban regime and al-Qaeda’s training camps in Afghanistan, and installing a new Afghan government under Hamid Karzai. And Bush also grasped the shield, constructing an entire architecture of domestic defense, including the Department of Homeland Security, which was resourced to the tune of tens of billions of dollars every year.

The third phase, from 2003-2006, was the invasion of Iraq, where American hopes evaporated in the Mesopotamian sun. Bush had argued that only war could sever the purported—and it turned out largely imagined—alliance between Saddam Hussein and al-Qaeda, and liberate an oppressed people. But the overthrow of Saddam’s regime triggered widespread disorder, and led to the rise of an al-Qaeda affiliate, al-Qaeda in Iraq (AQI), which began a murderous campaign of violence. The quagmire in Iraq also eroded the parallel mission in Afghanistan. With American attention focused on Iraq, and only limited U.S. forces in Afghanistan, the Taliban recovered in the south of the country as well as in sanctuaries in Pakistan.

The fourth phase of the Twenty Years’ War, from 2007-2011, was the surge era, a time of fragile recovery. The deployment of U.S. reinforcements in Iraq, together with the “Awakening” movement, which involved Washington allying with Sunni tribes against AQI (by now rebranded as the Islamic State of Iraq), helped to pull Iraq back from the brink of catastrophe. In Afghanistan, Barack Obama ordered a surge of U.S. forces, which nearly tripled troop levels to over 100,000 from 2009-2010. In 2011, U.S. Navy SEALs killed bin Laden in Pakistan. At the end of the year, American troops left Iraq. There was, finally, a sense of closure.

The fifth phase, from 2011-2016, was the era of transformation, as once again, U.S. hopes went unrealized. AQI/ISI evolved into ISIS and moved to the center of the global jihadist movement. Misgovernment and sectarian rule in Iraq had alienated Iraqi Sunnis and breathed new life into the ISI. After Syria collapsed into civil war in 2011, ISI crossed the border; in 2013, firmly ensconced in both Iraq and Syria, ISI changed its name to the Islamic State of Iraq and al-Sham (ISIS). The following year, al-Qaeda repudiated its former affiliate. But far from collapsing as an organization, ISIS subsequently swept into northern Iraq and declared a global caliphate.  Meanwhile, in the often-forgotten war in Afghanistan, American troops were withdrawn and the Taliban made steady gains, with the campaign left teetering between stalemate and failure.

Thus neither side won the Twenty Years’ War. Victory would mean achieving core aims at an acceptable cost relative to the benefits. Al-Qaeda did meet some of its goals: With limited resources, bin Laden gained incredible notoriety and inflicted enormous damage on a great power. In 2003, U.S. troops left Saudi Arabia—the key goal outlined in the 1996 manifesto. In 2004, bin Laden released a video that compared the costs of the 9/11 attacks to al-Qaeda versus the United States: “Al-Qaeda spent $500,000 on the event, while America, in the incident and its aftermath, lost—according to the lowest estimate—more than $500 billion, meaning that every dollar of al-Qaeda defeated a million dollars.”

But in a broader analysis, bin Laden failed. Yes, U.S. forces left Saudi Arabia, but they did so voluntarily, after Saddam was toppled. Crucially, al-Qaeda was unable to mobilize Muslims around a strict Islamist identity that transcended other loyalties. As Charles Kurzman showed in his book The Missing Martyrs, after 9/11, fewer than one in every 100,000 Muslims became jihadist terrorists. The vast majority of Muslims completely reject bin Laden’s ideology. And national, tribal, and other local identities remain profoundly important from the Palestinians to the Pakistanis. From 2003-2011, confidence in bin Laden collapsed in many Muslim-majority countries, falling from 59 percent to 26 percent in Indonesia, and from 56 percent to 13 percent in Jordan. In a 2013 poll taken in 11 Muslim countries, a median of just 13 percent had a favorable view of al-Qaeda, whereas 57 percent had an unfavorable view.

Another key al-Qaeda goal was to assume leadership of the global jihadist movement. Today, al-Qaeda affiliates in North Africa and Yemen remain a threat. But history seems to have moved on. Al-Qaeda was a marginal player in the grand drama of the Arab Spring. Al-Qaeda’s former satellite, AQI, morphed into ISIS, broke away, and seized the mantle of global jihad. According to the State Department, in the face of ISIS’s expansion in 2014, “AQ leadership also appeared to lose momentum as the self-styled leader of a global movement.”

Al-Qaeda and ISIS are very different animals. Al-Qaeda is a loose terrorist network focused on launching spectacular attacks to mobilize Muslims, which sometimes relied on host governments like the Taliban. ISIS is simultaneously a terrorist network, an insurgency, and a quasi-state, with tens of thousands of fighters, widespread territorial control, and extensive funding. ISIS doesn’t need to rely on government patronage—it is the government.

Al-Qaeda offers delayed gratification: ISIS provides instant gratification. Bin Laden saw the caliphate as a distant goal. In his declaration of war, he spoke of harnessing Saudi oil wealth in “the forthcoming Islamic State, by Allah’s Grace”—but this was a utopian and long-term vision. ISIS seized land in Syria and Iraq and made the caliphate real. In his 1996 declaration of war, bin Laden promised that Muslim martyrs would receive 72 pure virgins in heaven. ISIS offers sex slaves right now. Front-loading the rewards proved popular. By 2014, an estimated one thousand foreign fighters were joining ISIS every month, far in excess of new al-Qaeda recruits.

Al-Qaeda’s failure in the Twenty Years’ War, however, doesn’t mean the United States was victorious. War is not a sports match where one team wins and the other team loses. Instead, each side has its own separate tally. In the positive column, Washington can point to the absence of terrorist attacks on anything like the scale of 9/11 in the United States after 2001. Global jihad became a far more challenging endeavor, as Washington and its allies clamped down on terrorists’ opportunity to travel, communicate, and trade money and weapons. The United States also succeeded in capturing or killing the majority of al-Qaeda’s core leadership using a range of innovative tactics, including drone strikes and special-operations raids.

But a sober assessment of the last 20 years suggests that the United States lost the broader war. The country wasn’t occupied and there was no surrender. But Americans have paid an exorbitant price for the two-decade campaign in strategic, economic, and moral terms. When terrorists strike a great power, the destructive potential lies not in the act itself but in the great power’s response to the act. In 1914, Serbian terrorists killed Austrian Archduke Franz Ferdinand and his wife. Austria-Hungary used the attack as a pretext for war against Serbia, triggering a cataclysmic conflict, World War I, in which four empires collapsed—the Russian, German, Ottoman, and Austria-Hungarian. Similarly, in the Twenty Years’ War, America’s response has had far greater consequences than al-Qaeda’s attacks.

Let’s turn first to the United States on offense: the invasions of Afghanistan and Iraq. Recounting the costs is numbing: over 7,000 Americans killed, tens of thousands of soldiers seriously wounded, trillions of dollars expended, and over 100,000 civilian deaths in Iraq alone. And there’s the wider impact of spending on America’s debt, of enhanced interrogation and torture on the U.S. global image and ethical standing, and of seemingly endless quagmires on domestic political unity.

In an interview, Zbigniew Brzezinski, the national security advisor to President Jimmy Carter, told me, “The Iraq War was unnecessary, self-damaging, demoralizing, delegitimizing, and governed primarily by simplistic military assumptions that didn’t take into account the regional mosaic in which Iraq operates and the internal mosaic inside Iraq.”

The eclipse of al-Qaeda by ISIS is a loss for al-Qaeda but not a gain for the United States. ISIS is an even more ruthless and capable adversary.

If we consider the United States on defense, the success of the homeland-security complex in making Americans safer is highly debatable. A trillion dollars has poured into counter-terrorism programs, but to what end? There have been some genuine payoffs. The FBI, for example, has far greater resources to find terrorists before they attack, watch lists and databases of global threats are much improved since 9/11, and aircraft are physically safer with hardened cockpit doors.

But as Steven Brill described in The Atlantic, the spigot of homeland security expenditure also produced a carnival of waste, endless turf wars between bloated federal agencies—and, in many cases, remarkably little additional security. Tens of billions of dollars were poured into programs like FirstNet, a telecommunications system for first responders, which may never be built. After 9/11 there was a vast increase in the number of armed air marshals on planes. But Brill notes that more air marshals have been arrested themselves (for example, for drunk driving), than have carried out arrests in airports or onboard a plane. In 2015, undercover tests found that airport screeners across the country failed to detect explosives and weapons about 95 percent of the time. We can’t rerun the tape of history, but it’s plausible that with a more slimmed-down homeland security apparatus, terrorists would have killed few if any additional Americans.

Another core U.S. goal is to avoid the contest becoming a civilizational clash between the West and Islam. If that happens, the United States will be at war with the entire Muslim world, and very likely, will be facing decisive failure. Bin Laden never succeeded in rallying Muslims into a single internationalist bloc. But in the United States, there’s a creeping Islamophobia that serves to lump Muslims together and could unintentionally advance bin Laden’s vision. After 9/11, George W. Bush visited the Islamic Center in Washington D.C., and declared that “Islam is peace.” But in recent years, the GOP has become more explicitly Islamophobic, epitomized by Donald Trump’s proposal to ban all Muslims from entering the United States.

So if neither al-Qaeda nor the United States won the Twenty Years’ War, who did? The winners were Iran and China. The United States removed not one, but two, of Iran’s adversaries, the Taliban regime in Afghanistan and Saddam Hussein in Iraq. Tehran subsequently became one of the most influential players in Iraqi politics. The American blood and treasure expended in the Middle East also accelerated the point at which China will catch up to the United States economically.

But the main combatants in the struggle lost for similar reasons: They were hobbled by ideology. Al-Qaeda’s vision of austere Wahhabi Islam and endless global jihad is profoundly unappealing to the vast majority of Muslims. But ideology also shaped U.S. strategy, sometimes in dangerous ways. American idealism is one of the country’s most attractive qualities, central to its moral standing and “soft power.” But idealism also helped to frame the Twenty Years’ War as a struggle between good and evil, which required grandiose goals to topple regimes and build beacons of freedom in the Middle East. It also encouraged Americans to lump terrorists and rogue states together into a big bucket of bad guys. At the same time, Americans are also hostile to the whole notion of nation-building, often seeing stabilization missions as a kind of big-government welfarism, and not something that the country’s warriors should be doing. In a recent foreign-policy speech, Donald Trump said, “ISIS will be gone if I’m elected president,” but at the same time, the United States will be “getting out of the nation-building business.” This combination of beliefs is as American as apple pie.

As a result, the United States is an impatient crusader: eager to smite tyrants and terrorists but unwilling to invest the time and resources needed to win the peace. In Afghanistan and Iraq, Washington went to war with a short-term mindset, set on defeating the evildoers, rather than thinking about how to handle the messy consequences.

After five eras—phony war, optimism, catastrophe, recovery, transformation—it’s a sign of America’s inability to achieve victory that the Twenty Years’ War label will probably only last for one more year. Barring an unlikely collapse of al-Qaeda and ISIS, the contest will enter its 21st year next August.

Al-Qaeda’s extremist beliefs and lack of capabilities meant it was always vulnerable to burning bright and then fading. For the United States, as the vastly greater power, the primary danger was self-inflicted injury. Al-Qaeda can never defeat the United States. Only Americans can do that.