The last three years dramatically reshaped the oil map in Iraq, OPEC's second-largest producer. The dust from the military campaign against the Islamic State of Iraq and the Levant (ISIL, also known as ISIS) will take weeks if not months to settle, and the Iraqi government's grip on disputed and oil-rich Kirkuk, which the Iraqi Kurds recently vacated, remains infirm.

But when it comes to oil, it is clear that the conflict has left some stakeholders better off than others.

The ISIL factor The Baghdad government got off to a very poor start. Even before ISIL stole the headlines with its dramatic conquest of Mosul in June 2014, the "terror" group's operations were damaging Iraq's oil industry. More than three months earlier, ISIL attacks had brought oil exports through the Iraq-Turkey Pipeline through which Iraq used to export the bulk of Kirkuk's 550,000 barrels per day (bpd) production, to a complete halt.

Repair crews, afraid to reach the sites of leaks caused by explosives - even with army escorts - dubbed a stretch of the pipeline's path Tora Bora, after the infamous stronghold of the Taliban and al-Qaeda in Afghanistan.

Iraq's losses mounted when ISIL took over Mosul. ISIL fighters captured Ajil and Himrin oilfields in Salaheddin province and Qayyarah and three others in Nineveh province. The production potential of these fields, 72,000 bpd under ideal conditions, was rather minuscule from Baghdad's perspective - the country at that time was exporting nearly 2.6 million bpd.

But the real damage was in enabling ISIL to finance its war machine. It was able to generate an estimated $45 million a month selling the oil from these fields, and others in Syria, through a labyrinth of oil refining and smuggling operations. The windfall allowed it, for a while, to pay its fighters generously by local standards and keep its murderous campaign going for three long years.

Indirectly, the impact was more profound, more geopolitically significant. In the confusion following the fall of Mosul, Kurdish Peshmerga belonging to the Kurdistan Regional Government (KRG) took over the prized Kirkuk fields. The largest of the fields, Avana and Bai Hassan, were swiftly integrated into the KRG's oil production system, while others continued to be operated by the Baghdad-controlled North Oil Company.

Baghdad and the KRG made a short-lived deal in December 2014 under which Baghdad would pay the KRG 17 percent of the national budget in exchange for 550,000 bpd (250,000 bpd from fields inside Kurdistan proper, and 300,000 bpd from Kirkuk fields under KRG control). The deal faltered within months as both sides accused each other of falling short of meeting their commitments under it.

The KRG started using Kirkuk's crude to shore up its oil exports independent of Baghdad. By July of 2015, the KRG's exports, which had been about 125,000 bpd before the fall of Mosul, quadrupled (pdf), allowing the KRG to generate almost $4bn during the second half of 2015.

KRG's oil gains and losses In the Middle East, oil fuels more than just engines. It can fuel a drive for independence.

The KRG, whose position was boosted with the possession of Kirkuk, grew more confident in its ability to be economically self-reliant and steadily asserted its autonomy from Baghdad. Hardly two weeks had passed since ISIL took Mosul when KRG President Masoud Barzani told CNN during a June 23 interview that "The Kurdistan people should seize the opportunity now to determine their future."

The KRG's position appeared to be getting stronger as Baghdad lacked a proper response. Internally, however, there were undercurrents of disagreement and resentment towards the ruling Kurdistan Democratic Party (KDP) for dominating the region's affairs and managing the oil wealth without consulting other parties. The tensions were palpable in September 2016 when the Patriotic Union of Kurdistan (PUK), the KDP's junior partner in control of much of Kirkuk, reportedly threatened to cut oil flows and strike its own oil export deal with Iran. In the following months, the PUK would reiterate its threat, this time with a show of military force.

Nonetheless, the cash-hungry KRG was seeking more oil deals with powerful external actors both to generate much-needed revenue and hedge against possible retribution by Baghdad, Tehran or Ankara - all wary of the idea of an independent Kurdish nation.

One party that has been pulled into the squabble, or willingly inserted itself, is Russia. Since the beginning of 2017, there have been reports about state-owned Rosneft negotiating deals with the KRG, including buying a majority stake in the KRG oil export pipeline and potentially building another for natural gas export. This set of deals with Rosneft purportedly totals some $3.5bn, of which Rosneft has already paid out $1.3bn.

Securing Russian investment in its pipeline was perhaps a smart insurance policy that the KRG bought to make Baghdad and Ankara think twice before taking draconian measures to shut it down altogether.

For observers willing to overlook the perilous lack of internal cohesion, the KRG, as it geared up to hold a referendum on independence in September, seemed well on its way to become the Middle East's youngest state.

But the KRG miscalculated. Tensions between the Baghdad and Erbil governments culminated in the October 17 takeover of Kirkuk by the Iraqi military and Popular Mobilization Units (PMUs). The Iraqi advance would've been unimaginable if not for the PUK's decision to cooperate with Baghdad, effectively torpedoing a decade-old strategic relationship with the KDP. The contested Kirkuk fields of Avana and Bai Hassan were once again under Baghdad's control.

Overnight, half of the KRG oil exports evaporated, taking with it years of planning, and dealing a painful setback to the decades-old aspiration for independence.

Baghdad resurgent Baghdad almost immediately sought to translate the territorial gains into oil exports and revenue. Two days after the Iraqi military and PMUs walked into Kirkuk, the Iraqi oil minister called on British Petroleum (BP) to help develop Kirkuk fields. The federal oil ministry also announced a plan to repair its own northern pipelines so that it can divert the oil away from the KRG's pipeline.

There's a problem though. Pipeline repairs, by Baghdad's estimates, need at least three months. News of a plan to build a completely new pipeline may indicate that repairs aren't even feasible.

Baghdad hasn't figured out exactly what to do with all the Kirkuk oil yet.

Enter Iran. The eastern neighbour, which for years stood jealous of KRG oil dealings with Turkey, is going to make some modest gains from the KRG's plight. Desperate for an evacuation route, Iraq agreed to start trucking 15,000 bpd from Kirkuk to an Iranian refinery in exchange for Iranian oil delivered to Iraq's south.

In all this drama, the overall supply picture did not skip a beat. Baghdad managed to boost its oil exports from its southern fields to reach about 3.35 million bpd in October, offsetting the shortfall from Kirkuk. The sales generated $5.5bn in revenue, its highest monthly figure in almost three years.

The KRG is not completely out of luck. It remains in control of several fields inside Kurdistan proper, still producing about 300,000 bpd, of which about 250,000 bpd continue to flow through the Kurdistan-Turkey pipeline.

For a month now there has been a standoff between pro-government Iraqi forces and Peshmerga forces at the Faysh Khabur border crossing, where the Kurdish export pipeline enters Turkey. Baghdad wants to assume control over all crossings to bring all northern exports, including from fields inside Kurdistan's uncontested border, under state control - and bring the KRG to its knees.

This would entail Baghdad agreeing in return to provide for the financial needs of the Kurdish provinces from federal coffers. In theory, an agreement is possible. But there's more at stake than just civil servants' salaries and funds for schools or roads.

For the KRG, to maintain a revenue stream independent of Baghdad's chokehold is an existential-level question. There is also the looming question of what happens to the KRG lenders, including Russia. Western companies may have little in the way of recourse, but there's no telling what tricks the Kremlin might have up its sleeve to collect on its awkwardly-timed investments in Kurdish black gold.