Walgreens Boots Alliance Inc. (NASDAQ: WBA) agreed to acquire Rite Aid Corp. (NYSE: RAD) for about $9.4 billion in cash in a transaction to further expand the company’s role in the distribution of medications in the U.S.

The Rite Aid transaction, announced Tuesday, would combine the second- and third-largest drugstore chains in the U.S., with a total of about 12,800 locations, helping Walgreens vault past market leader CVS Health Corp. The acquisition will add to Walgreens’ earnings beginning a full year after completion and will produce more than $1 billion in savings from cost overlaps, the companies said Tuesday in a statement. Including debt, the deal is valued at $17.2 billion, they said.

The company also reported fiscal fourth-quarter earnings showed how it reaps cost savings from mergers. Walgreens said it has saved $799 million in fiscal 2015 after combining with Alliance Boots GmbH last year.

Rite Aid shares fell 8.2 percent to $7.96 in early trading Wednesday, below Walgreens’ offer of $9 a share, reflecting speculation that the transaction will face antitrust scrutiny. They had risen as high as $8.74 earlier after the Wall Street Journal reported the companies were close to a deal. Walgreens shares rose 1.4 percent to $96.50.

The pending acquisition comes after Walgreens announced plans earlier in 2015 to sell a majority stake in its infusion services business to Madison Dearborn Partners. The division provides bleeding and nutrition home treatment services.

Rite Aid’s stock had slumped 29 percent since Sept. 16, after the company lowered profit and revenue forecasts for the year, giving Walgreens a potential opportunity to make an offer. The price represents a 48 percent premium to Rite Aid’s closing price on Monday.

Speculation that Walgreens would pursue Rite Aid rose to a crescendo in March after billionaire Stefano Pessina, who took over in January as interim chief executive officer at Walgreens after it acquired Alliance Boots, said he envisioned doing his next big deal in the U.S.

In addition to expanding its drugstore locations, the Rite Aid deal gives Walgreens its first foray into the business of managing drug benefits for insurers and employers, an area where rival CVS is a leader. Rite Aid entered that business by acquiring Envision Pharmaceutical Services Inc. for about $2 billion this year.

If Walgreens aims to get bigger in drug-benefit management, it could use EnvisionRX to acquire other small competitors to build that business, said Ross Muken, an analyst at Evercore ISI who advises holding the shares.

Rite Aid CEO John Standley is eligible to receive $23.4 million if he’s terminated after the deal, according to data compiled by Bloomberg based on Walgreens’ offer price per share.

Shares of drug distributor McKesson Corp. dropped 4.2 percent, while AmerisourceBergen Corp., Walgreens’ distribution partner for medications, jumped 4.2 percent. Rite Aid represents about $18 billion of McKesson’s revenue, according to Evercore ISI.

On Wednesday, Walgreens reported fiscal fourth-quarter earnings that beat analysts’ estimates. Excluding one-time items, earnings were 88 cents a share, beating the 81-cent average of analysts’ estimates compiled by Bloomberg. Walgreens’ pharmacies filled 222 million prescriptions in the quarter, up 4.6 percent from a year before.

While Pessina had made clear his appetite for deals, he had been cagey about what kind of acquisition he might pursue. He has said only that the company wanted to participate in what he saw as the inevitable consolidation of the long distribution chain that delivers drugs from manufacturers to patients in the U.S.

While Pessina prowled for a deal, CVS stayed busy. In May, Walgreens’ top rival agreed to purchase nursing-home pharmacy Omnicare Inc. for $12.7 billion, expanding services to the country’s elderly. Less than four weeks later, CVS struck a deal to acquire Target Corp.’s pharmacies and clinics for $1.9 billion, putting its brand, which includes OneMinute Clinics, in retail locations across 47 states.

"Their biggest competitor in the U.S. has been getting bigger," said Jonathan Palmer, an analyst at Bloomberg Intelligence.

Walgreens already has a foothold in the drug-distribution business after it signed a 10-year agreement with AmerisourceBergen in 2013 and became the company’s third-largest shareholder.

Today’s Walgreens was built by acquisition. Pessina developed Bern, Switzerland-based Alliance Boots into a powerhouse through more than three decades of mergers before selling it to Walgreen Co. last year. He’s now the company’s largest shareholder with a 13 percent stake.

Walgreens will revamp Rite Aid’s stores to focus on health, wellness and beauty, just as it has been remaking its own stores since the Alliance Boots deal, the company said. Rite Aid will continue to operate under its own brand at first, though eventually the plan is to integrate the two chains.

The acquirer plans to finance the deal with cash and new debt, it said. UBS AG was Walgreens’ financial adviser, with Simpson Thacher & Bartlett LP and Weil Gotshal & Manges LLP providing legal advice. Citigroup Inc. gave financial advice to Rite Aid, with legal counsel from Skadden Arps Slate Meagher & Flom LLP and Jones Day. Gibson Dunn & Crutcher LLP is representing UBS.

 --With assistance from Anders Melin and Melissa Mittelman in New York.