In January 1985, the CIA withdrew $50 million from a Swiss bank account to buy 40 Oerlikon antiaircraft guns for the rebels fighting the Soviet army in Afghanistan.

But only 11 guns made it to the guerrillas. Somewhere along the way 29 of them, worth $36.25 million, disappeared.

The weapons were stolen from the CIA's secret arms pipeline to Afghanistan.

A six-month Inquirer investigation shows that at least $340 million worth of weapons - and perhaps much more - intended for the Afghan rebels never reached them.

Even the CIA, whose $2 billion covert effort apparently is about to be rewarded by the withdrawal of the Soviets from Afghanistan, estimates that one of every five dollars' worth of weapons destined for the Afghan resistance got sidetracked, stolen or misused.

Many of the CIA's guns got through - but now Islamic fundamentalists among the resistance fighters are stockpiling those weapons, preparing for a power struggle after the Soviets leave Afghanistan. Those who back the Afghan rebels fear the CIA's guns will be used to kill the people they were meant to save.

In addition to arming the rebels against the Soviets, the CIA's pipeline also armed:

* Iranian revolutionaries, who bought or stole Stinger antiaircraft missiles from the Afghan rebels. In October the Iranians fired the Stingers at U.S. helicopters in the Persian Gulf.

* Drug traffickers in Afghanistan and Pakistan, who produce half the heroin used by Americans. They bought weapons from Afghan rebels who play a small but active role in the heroin trade.

* Corrupt Afghan guerrilla leaders, who stole antiaircraft guns, rocket- propelled grenades, AK-47 rifles and other weapons. They sold some in the black market, and kept others for personal use.

* Pakistani soldiers, who stole and sold weapons. A Reagan administration official called the thefts "a kind of commission they deduct for allowing us to transport them through their territory. "

The missing weapons were supposed to be distributed to the seven Afghan resistance groups from their headquarters in Peshawar, Pakistan. But reports by the CIA and the National Security Agency say 20 percent to 30 percent of the arms shipments never reached the battlefront.

Others in the intelligence community think the leakage is far greater.

"You're not going to get arms through Peshawar without a lot of graft. It is not a clean, neat operation," Stansfield Turner, the former CIA director who began the Afghan arms pipeline in 1979, said in an interview. "Weapons are going to fall into the wrong hands. The Pakistanis are going to take something. The various Afghan factions are going to take their share. . . . If we only got 20 percent through, I wouldn't be surprised. "

The Afghan arms pipeline is the largest CIA paramilitary operation in history. The CIA spent eight times as much on the Afghan operation as it did to support the Nicaraguan contras. And the losses alone from the pipeline exceed the total amount - $280 million - spent by the U.S. to support the Nicaraguan contras since 1981.

The consensus among U.S. officials is clear: The covert operation was

worthwhile, whatever the cost. The CIA's mission was to help the Afghans win their jihad, or holy war, and to make the Soviets pay the highest possible price. The weapons that reached the resistance helped the Afghans survive their struggle against a superpower.

And the Afghan jihad became something of a holy war for the Reagan administration, fueled by a deep desire to win a nation back from Soviet domination.

The demoralized Soviet army is ready to leave the nation it devastated. The Soviets have announced that they will begin withdrawing on May 15, if an agreement is reached by March 15 in U.N.-sponsored negotiations in Geneva. The CIA is near its goal.

For this series, part of a continuing investigation of the "black budget" for secret U.S. military and intelligence programs, The Inquirer interviewed 118 people, including officials of the State Department, the Pentagon, the

Drug Enforcement Administration and American intelligence agencies, guerrilla commanders in Afghanistan and government officials in Pakistan. The Inquirer also reviewed CIA field reports, State Department cables, closed-door congressional testimony and other U.S. government documents.

*

The CIA knew the risks of running guns into an ungoverned war zone seething with Islamic fervor. The agency was prepared to accept thefts and diversions in its mission to support the mujaheddin, or holy warriors, of the Afghan resistance.

"It's a cost of doing business," Gregory Treverton, a National Security Council staffer in the Carter administration and a Senate investigator of the CIA in the Ford administration, said of the leaking pipeline. "This is the nature of the beast. "

But there were other costs.

Mujaheddin frequently killed one another in disputes over precious weapons. Resistance attacks on Soviet positions failed for want of ammunition. A U.S.

drug-enforcement agent investigating a heroin kingpin reported finding an antiaircraft gun on the roof of the dealer's fortress. Warring tribesmen in Pakistan's lawless border lands blasted one another's strongholds with rocket- propelled grenades. Criminal gangs in Pakistan's cities armed themselves with automatic rifles once destined for the Afghan battlefield.

The leaking pipeline spilled weapons all over Pakistan, a State Department official in Peshawar observed. "There's been a tremendous proliferation of weapons all over the countryside," he said. "Every shepherd boy guarding his flock has got an AK-47. In the villages, you can rent an RPG (rocket-propelled grenade). "

The CIA lost control of the pipeline from the time its most crucial link was laid.

A look at the map showed there was only one way for the CIA's arms shipments to reach the mujaheddin. The pipeline had to run through Pakistan. Under martial law through 1985, Pakistan was run by generals. Its intelligence agency cooperated with the CIA to ship the weapons to the guerrillas.

But the Pakistani armed forces saw it as their right to take weapons and ammunition from the CIA's shipments, according to sources in Congress, the State Department and the Pentagon.

"The Pakistanis were siphoning off a reasonable amount of the aid we

sent," said Rep. George E. Brown Jr. (D., Calif.), a House intelligence committee member from 1985 through 1987. "They keep good records, but they are no doubt altered to conceal pilferage. "

The pipeline began to hemorrhage in Peshawar, the dusty city near the Afghan border where the rebels have their headquarters-in-exile. Resistance commanders fighting in Afghanistan blame most of the leakage on their political leaders in Peshawar, as do a number of American officials and intelligence analysts.

"There are many temptations in Peshawar," said Gulam Ahmed, a resistance commander interviewed inside Afghanistan. "There are not too many honest people to do the political jobs honestly. A lot of people are putting a lot of money in their pockets. "

Gen. Kemal Matinuddin, director of Pakistan's military think tank in Islamabad, the Institute of Strategic Studies, said: "In a clandestine operation of this sort, you must be prepared for these kinds of corruptions, diversions and losses.

"It is unwise for the U.S. to feel that whatever they send is going to be received," the retired general said. "There are so many agents in between, so many people who can make money along the line. Between the weapons that have been sold to criminal elements and the weapons being stockpiled for a rainy day, the men who handle these weapons have taken advantage of an opportunity given to them. "

Afghan resistance commanders and supporters complained bitterly about promised weapons that never arrived, saying the flood of weaponry sent out by the CIA was reduced to a trickle in Afghanistan.

"There are never enough weapons," said a mujaheddin commander named Zamrey, as he cradled his rifle at a resistance camp near Narang, a rough jumble of mud huts and caves 8,000 feet up in the mountains of Konar province, Afghanistan.

"We know the United States is helping us, the weapons coming from the American people," he said through a translator. "But it is not enough. "

He picked up a handful of stones, symbolizing weapons, to illustrate his point. "This amount should be distributed. " He took a few of the stones in his other hand. "This is what we receive. The Pakistanis are stealing the best of them, and keeping some. In Peshawar they are stealing too. "

The CIA, though aware that it is losing huge amounts of weaponry, has resisted repeated attempts by Congress to audit the operation. Members of the Senate intelligence committee and the House Budget Committee have called for an accounting, but the agency will not comply, saying it must preserve the secrecy of its operations.

Yet weapons diverted and stolen from the CIA pipeline have been found in a remarkable variety of unintended hands.

Last summer, Iran's Revolutionary Guards bought or stole at least 12 Stinger antiaircraft missiles and launchers from an Afghan resistance group loyal to the leader of the guerrilla alliance, Yunis Khalis. Although U.S. intelligence officials knew about the missing Stingers as early as July 1987, they did not disclose it publicly until October, when one of the Stingers was fired from an Iranian gunboat at American helicopters patrolling the Persian Gulf.

When reports that the Iranians had the CIA's Stingers first reached Washington, Sen. Dennis DeConcini (D., Ariz.), a member of the Senate intelligence committee, said that, if the story were true, it meant that "one of our worst enemies may have one of our best weapons in one of the most volatile regions of the world. "

The story was true, although how the Stingers got to Iran remains a mystery. Khalis said the Stingers were stolen; unconfirmed reports from inside Afghanistan said his guerrilla group sold the missiles to the Iranians for $1 million.

Whatever the circumstances, there are close ties between the fundamentalist factions of the mujaheddin and their Iranian neighbors. Many mujaheddin see Iran's Ayatollah Ruhollah Khomeini as a hero, not the dangerous zealot the West sees him to be. Travelers inside Afghanistan report seeing the ayatollah's picture in mujaheddin trenches. Iran is thought to have agents in Pakistan seeking to strengthen the country's influence with Afghan resistance leaders.

And Iranian officials supported the mujaheddin by opening up a new supply route for the CIA. As White House documents released in the Iran-contra investigation show, the Iranians agreed to transship to the mujaheddin some of the TOW antitank missiles the United States sold to Iran in 1985 and 1986.

*

U.S. Drug Enforcement Administration agents in Pakistan say there is another threat to the United States stemming from the leaking pipeline. They say weapons shipped by the CIA have fallen into the hands of some of the world's biggest heroin traffickers.

DEA agents and Pakistani government officials said the kingpins of the heroin trade along the Pakistan-Afghanistan border - the source of three tons of heroin reaching the United States each year - have armed themselves with automatic weapons, rocket-propelled grenades and even antiaircraft guns diverted from the CIA supply line.

The rugged border land is literally lawless. In those tribal areas of Pakistan, thousands of weapons and tons of opium flow in over smugglers' trails from Afghanistan. The opium is refined into heroin in heavily guarded fortresses. The heroin flows around the world. The weapons stay with the heroin dealers.

"Every doper down-country has got RPGs and AK-47s," a U.S. drug- enforcement official in Pakistan said. "I mean, dope peddlers anywhere in the world are going to have guns, but these guys are armed to the teeth. . . . . Where do you think those weapons come from? Allah? "

DEA agents and other U.S. officials in Pakistan also said there was little question some Afghan rebels were involved in heroin smuggling.

"I don't doubt that the mujaheddin carry dope across the border," said one U.S. narcotics official. "We talk to the commanders about it. But they say, 'I need those people - I can't alienate them. ' We've been terrified all along of discovering a really big drug conspiracy among the mujaheddin. If it ever happens - look out. There'll be hell to pay. "

"Law enforcement in this country is completely outgunned," said one U.S.

drug-enforcement agent. "There's no question that a lot of these guns are coming from our friends. And somebody's making a lot of money. "

Virtually no heroin was refined in Pakistan before 1979, he said. "Now Pakistan produces and transships more heroin than the rest of the world combined."

*

The politics of Pakistan posed a particularly thorny problem for the CIA.

Relations between the United States and Pakistan were at an all-time low at the hour of the Soviet invasion of Afghanistan. In April 1979, Pakistan's military ruler, Gen. Mohammad Zia ul-Haq, ignored world protest and ordered the execution of the president he had overthrown, Zulfikar Ali Bhutto. Zia jailed opponents and banned dissent.

To make matters worse, the CIA learned that summer that Pakistan was secretly using smuggled technology to build a nuclear weapon - the first ''Islamic bomb. " Under U.S. laws aimed at stemming the spread of nuclear weapons, President Jimmy Carter cut off military aid to the strategic ally on Afghanistan's eastern flank.

Then, in November 1979, anti-American mobs burned the U.S. Embassy and American cultural centers in Pakistan.

The Soviets invaded Afghanistan during the last week of 1979. Less than two months later, Carter's national security adviser, Zbigniew Brzezinski, and the CIA station chief in Pakistan - working out of temporary quarters after the destruction of the embassy - met with Zia's intelligence chief, Gen. Aktar Abdul Rehman.

The message from the White House was that U.S. military and economic aid would be restored if Zia would help ship the CIA's weapons through Pakistan to the Afghan resistance.

"When Zia heard that, it was 'all the way with CIA,' " said a Pakistani familiar with the meetings.

Zia agreed to provide the vital link in the CIA's pipeline. But he imposed a strict condition.

Zia demanded that once the weapons reached Pakistan, they would be under Pakistan's control - specifically, the control of Zia's military intelligence unit, the Inter-Services Intelligence Directorate (ISID), a kind of combination CIA, FBI and secret police.

The CIA agreed to Zia's terms. The agency would fly the weapons into Pakistani airfields and ship them into Pakistani ports on the Arabian Sea. And then the ISID would take charge.

"The Pakistanis feel this gives them protection," Sen. DeConcini said. ''But it also gives them a way to filter out weapons if they want to. It's a contrived way for us to give more (military) assistance to Pakistan. "

Over the years, the head of the ISID - first Rehman, today Gen. Hamid Gul - met regularly with the CIA station chief to discuss logistics. But the ISID was in control.

It transported and distributed the weapons to the resistance leaders, whom Zia had given haven in Peshawar. It decided which of the rival guerrilla groups got which weapons, and in what quantities. And it favored the most radical, fundamentalist and anti-Western of the groups, according to senior Western diplomats in Islamabad.

An "absurdly small" number of American intelligence personnel oversaw the arms shipments on the most crucial leg of their journey, according to Sen. Gordon J. Humphrey (R., N.H.), an avid supporter of the mujaheddin.

Once the ISID took charge of the CIA's weapons, the troops assigned to deliver them were members of a special Pakistani army unit, the National Logistics Cell.

That is where the leakage began, according to American officials, Western diplomats and resistance commanders in Afghanistan. Pakistani military men - exactly how many is unclear - have been convicted and jailed in Pakistan on charges of stealing and selling the CIA's arms.

The leakage grew when the pipeline reached Peshawar and ISID agents doled out the weapons to the political leadership of the Afghan resistance. Many of those leaders - according to mujaheddin commanders in Afghanistan and U.S. officials in Pakistan - are politicians first and foremost, more interested in personal power than in the grit and danger of the battlefield.

While their troops and commanders eke out hard lives in Afghanistan's mountains and deserts, or in the teeming refugee camps inside Pakistan, the political leaders have fine villas in Peshawar and fleets of vehicles at their command.

"What goes wrong is the human element," said Gen. Matinuddin, the Pakistani military strategist. "Commanders are laying down their lives, while their (political) leaders live in comfortable homes in Peshawar. "

To help finance this standard of living, the resistance politicians have sold and traded the CIA's weapons for cash, cars and commodities, U.S. officials in Pakistan said.

"Sure, there's leakage," said a senior Western diplomat in Pakistan. "If a commander has plenty of weapons, and he needs money for transportation or food or whatever, he's going to be pragmatic about it. Not much we can do or say about that."

*

Every American official with direct knowledge of the operation interviewed agreed that the CIA's pipeline leaks. They disagreed only on the extent of the leakage.

The lowest estimate comes from CIA officials, who told members of the congressional intelligence committees in 1986 that perhaps 20 percent of the weapons shipped to the Afghan resistance were being diverted.

Another study, conducted in 1984 and 1985 by the National Security Agency, the intelligence agency responsible for global electronic eavesdropping, estimated that up to 30 percent of the CIA's weapons were being diverted - with some winding up in Iran.

A State Department official in Pakistan had a higher estimate. "If 50 percent of everything we send gets through, it's a miracle," he said.

Former CIA director Turner, who began the covert arms pipeline, was even more pessimistic. "I wouldn't figure it would be a 20-to-30-percent loss," said Turner. "If we only got 20 percent through, I wouldn't be surprised if that was in the ballpark. The percentage has got to be pretty low. "

Turner's estimate would place the losses to date at more than $1 billion.

The CIA spent roughly $1.7 billion on the Afghan operation through September 30, 1987, the end of the federal fiscal year. (Between $700 million and $1 billion is available for the Afghan pipeline this year, according to intelligence sources and members of Congress. )

If the CIA is correct in saying that 20 percent of the arms were diverted, $340 million worth was lost. By comparison, the Reagan administration has spent no more than $280 million to support the Nicaraguan contras since 1981.

In other words, the losses from the pipeline could have more than financed the entire contra operation for seven years.

In the world of CIA covert operations, the leaking pipeline - the loss of hundreds of millions of dollars of weapons - is seen as an acceptable cost, a risk worth taking.

"Can you sign off on those kind of losses?" asked DeConcini, a Senate intelligence committee member briefed on the Afghan action. "I guess this country has said yes. "

A senior State Department official overseeing the Afghan operation said: ''It was assumed that some (weapons) might be lost, and this was taken into account. It was assumed that some Stingers might fall into the wrong hands. It was anticipated that the consequences of the losses would not be so great. "

"It's perfectly acceptable to get a small return on the dollar in an operation like this," said former CIA director Turner. "But you've got to keep your eyes open. You've got to ask yourself if the cost of Stingers going astray, if the cost of what's being stolen, is more than it's worth. You have to ask yourself: 'Am I being taken beyond all reason?' "