Facing the prospect of millions, or even billions, of dollars in potential liabilities tied to the nation's opioid epidemic, one drugmaker has a nimble plan: spin those liabilities into a separate company.

Mallinckrodt Pharmaceuticals, the U.K.-based drugmaker best known for selling a $1 billion-a-year medicine called H.P. Acthar Gel, told investors in December it planned to separate its business into two: one will contain Acthar and other branded medicines; the other, the company's specialty generic drugs — including oxycodone and hydrocodone.

Mallinckrodt was revealed last week to be the largest manufacturer of those opioid painkillers in a Drug Enforcement Agency database that tracked the flow of the pills from 2006 to 2012 into cities and communities across the U.S., a period when overdoses of prescription opioids claimed almost 100,000 lives.

The database is a key part of a massive legal case centered in Cleveland, where 2,000 states, cities and other entities are suing drugmakers, distributors and others that profited off the opioid epidemic. The plaintiffs hope to recoup some of the costs of the financial toll on families and communities. One estimate of how much plaintiffs are seeking: $480 billion. That's almost twice the size of the 1998 settlement with tobacco companies.

The size of a potential settlement or any fines, which would be split among the defendants, is unclear. Wells Fargo analyst David Maris said one legal expert suggested more than $100 billion "is in the realm of possibility," but most analysts are reluctant to assign a dollar figure to potential opioid liabilities. One analyst, Mizuho Group's Irina Koffler, said she's "skeptical [Mallinckrodt] will pay much at all."

But just the prospect has weighed on the stocks of several manufacturers of opioid painkillers. Mallinckrodt has lost more than two-thirds of its market value in the last year — almost 20 percent just in the last week. Another maker of opioid drugs, Insys Therapeutics, recently filed for bankruptcy after reaching a $225 million settlement with the Department of Justice on civil and criminal charges linked to the crisis.

Ahead of a case in Oklahoma, Purdue Pharma and Teva Pharmaceuticals reached settlements with the state of $270 million and $85 million, respectively. Johnson & Johnson was left as the sole defendant, and analysts say the judge's decision — expected within weeks — could be a bellwether for the liabilities of other opioid makers.

Berenberg Capital Markets' Patrick Trucchio estimates Mallinckrodt's liabilities could be as much as $5 billion in a "worst-case scenario." In a CNBC interview, he said his "base case" is closer to $1.5 billion, paid over 10 years.

But if all goes according to plan, it won't be the current iteration of Mallinckrodt on the hook for any legal liabilities. Mallinckrodt will rename itself Sonorant, and, before the end of this year, it will spin off its generics business. That unit would keep the Mallinckrodt name and take with it any opioid liabilities.

The company, in announcing the planned split last year, didn't focus on the transfer of liabilities. It instead emphasized that the split makes strategic sense, enabling the two already distinct businesses to "pursue independent growth strategies" that "have the potential to unlock and increase value over the long term."

More recently, speaking directly to analysts and investors, company executives have said the spinout should take with it the risks around opioid liabilities.

"So, when we spin — and we spend tons of time with outside counsel on this — we're very comfortable that that exposure legally separates from [the remaining company,]" Mallinckrodt Chief Financial Officer Bryan Reasons told Raymond James analyst Elliot Wilbur at the bank's health-care conference in June, responding to a question about opioid liabilities. "We're comfortable that Mallinckrodt has a very clean legal structure to spin these things off legally."

That potential indemnification matters because what companies end up paying, if anything, could come down to what they're able to pay, said Trucchio. He explained his $5 billion estimate for potential opioid liabilities assumes they're determined on that basis, and that both Mallinckrodt and Sonorant, in a "worst-case scenario," would be liable. If Sonorant is indemnified, what Mallinckrodt could pay would be significantly less.

Mallinckrodt's executives declined to discuss the matter, pointing instead to public documents.

The company wouldn't be the first to try to separate itself from any legal risks from selling opioids through a sale or spin.

In 2014, consumer products giant Reckitt Benckiser, which sells brands such as Clearasil acne products and Enfamil baby formula, spun its pharmaceuticals unit into a company it called Indivior ("a fusion of the words individual and endeavor," it said at the time).

Indivior sells Suboxone Film, a form of opioid used to treat opioid addiction. In April, Indivior was indicted by the Department of Justice for allegedly lying to doctors and others about the safety of the medicine and diverting patients addicted to opioids to doctors it knew prescribed more than most.

Its former parent company didn't escape unscathed; Reckitt Benckiser agreed this month to pay $1.4 billion to resolve a federal investigation into its involvement. The Justice Department said it was the largest recovery in a case concerning an opioid drug in U.S. history.

But for a consumer products company whose brand translates into consumer trust — and sales — that's a win, according to Berenberg's Trucchio.

"As a consumer products business, your brand is a meaningful portion of the valuation, and something where certainly you don't want to have your brand put together with this opioid epidemic," Trucchio said. "For Reckitt, from their perspective on this, they're able to look forward and move on from this litigation."

For another company, Allergan, the separation of its generics business — and any associated potential opioid liabilities to come — was the result of a well-timed sale.

Allergan's Actavis unit was the second-largest manufacturer cited in the DEA database last week, revealed after a years-long legal battle by The Washington Post and the parent company of the Charleston Gazette-Mail in West Virginia. Drug companies and the government opposed its full release, according to the Post, the companies citing trade secrets and the Justice Department concerned about ongoing investigations.

The data showed Actavis accounted for almost 35% of the 76 billion pills distributed in the U.S. between 2006 and 2012, behind Mallinckrodt subsidary SpecGx's almost 38%.

But any opioid liabilities, according to Allergan, would instead be borne by Israeli drug giant Teva, which acquired the Actavis generics business in 2016 for $40 billion.

A Teva spokeswoman confirmed the Actavis unit is now an affiliate of Teva, and said "Teva has not conspired, failed to report suspicious orders or contributed to the abuse of opioids in the U.S."

To be sure, it's not clear that ridding themselves of potential opioid penalties were drivers of Allergan's and Reckitt Benckiser's sale and spinoff; both cases had a clear strategic rationale beyond any legal considerations. For Allergan, the sale facilitated a move away from generic drugs as that business was beginning to decline. For Reckitt Benckiser, the spinoff formally separated a prescription drugs business from one focused on consumer products.

Separating themselves from those risks, though, has been a clear benefit.

Mallinckrodt, which emphasizes it has been considering how to separate its two businesses for at least two years, also has a clear strategic reason for the spinoff, Berenberg analyst Trucchio said. But company executives also take pains to make clear to investors what happens to any legal risks from opioids in its planned spin.

From the question-and-answer session with investors at the JPMorgan Healthcare Conference in January:

"Just curious, what's the ability for legacy Mallinckrodt to indemnify itself for the generics, then, in case there is some sort of settlement of legal liability on the opioids?" one investor asked.

Mallinckrodt CEO Mark Trudeau replied: "With regards to the opioid litigation risk, which today is not a liability, it's a risk, those risks would follow the assets, which would go with the SpinCo business."

Mallinckrodt has said it expects to complete the spinoff this year. The Cleveland case is set to begin in October if no settlement is reached.

— CNBC's Leanne Miller contributed to this article.