(Reuters) - Two months before his death in 1989, Iranian leader Ayatollah Ruhollah Khomeini tried to solve a problem unleashed by the revolution he led a decade earlier.

Land and other assets were being seized en masse from purported enemies of the young theocratic state. Khomeini issued a two-paragraph order asking two trusted aides to ensure that much of the proceeds from the sale of the properties would go to charity.

The result was a new organization - known as Setad, or "The Headquarters" - that reported to Iran's supreme leader. As one of the aides later recounted, Setad was intended to oversee the confiscations and then wind down after two years.

Twenty-four years later, Setad is an economic giant. Khomeini's successor as supreme leader, Ayatollah Ali Khamenei, has used it to amass assets worth tens of billions of dollars, rivaling the holdings of the late shah.Setad's portfolio includes banks, farms, cement companies, a licensed contraceptives maker, apartments seized from Iranians living abroad and much more.

Reuters found no evidence that Khamenei puts these assets to personal use. Instead, Setad's holdings underpin his power over Iran.

To make Setad's asset acquisitions possible, governments under Khamenei's watch systematically legitimized the practice of confiscation and gave the organization control over much of the seized wealth, a Reuters investigation has found. The supreme leader, judges and parliament over the years have issued a series of bureaucratic edicts, constitutional interpretations and judicial decisions bolstering Setad. The most recent of these declarations came in June, just after the election of Iran's new president, Hassan Rouhani.

The thinking behind this painstaking legal effort is unclear. The Iranian president's office and the foreign ministry didn't respond to requests for comment. Iran's embassy in the United Arab Emirates issued a statement calling Reuters' findings "scattered and disparate" and said "none has any basis." It didn't elaborate.

Setad's director general of public relations, Hamid Vaezi, said in an email that the Reuters series is "far from realities and is not correct" but didn't go into specifics. He said Setad plans to challenge sanctions imposed on it earlier this year by the U.S. Treasury Department.

But the legal machinations served several purposes. The decrees enabled Setad to beat back rival institutions seeking to take property in the name of the supreme leader. A ruling on the constitutionality of privatizations smoothed Setad's expansion beyond real estate and into owning and investing in companies.

The attention to legal procedure also allows Setad and Khamenei to justify a practice that Khomeini had cited as a reason for overthrowing the shah in 1979: property confiscations. Mohammad Reza Pahlavi, the former king, inherited his fortune from his father, who enriched himself in the first half of the 20th century by expropriating vast amounts of land from his subjects. In October 2010, Khamenei invoked that memory in a speech.

"Our people were living under the pressure of corrupt, tyrannical and greedy governments for many years," Khamenei told officials in the clerical city of Qom, according to an English-language transcript on his official website. The shah's father "grabbed the ownership of any developed piece of land in all parts of the country…. They accumulated wealth. They accumulated property. They accumulated jewelry for themselves."

The Islamic Revolution promised Iranians a new era of justice, governed strictly in accordance with sharia, Islamic law. Khomeini outlined a "Velayat-e Faqih," or Guardianship of the Jurist - a government ruled by a cleric who spurns personal wealth, values the law above all else and rigorously submits himself to it.

"Islamic government … is not a tyranny, where the head of state can deal arbitrarily with the property and lives of the people, making use of them as he wills," Khomeini wrote in a 1970 book.

Iranian attorneys who have battled Setad say the governments under Khamenei's watch have not lived up to those ideals. Instead, they allege, the government makes aggressive use of the law to take property from citizens - in particular, Article 49 of the Iranian constitution, which provides for seizing illicit assets from criminals.

"It is a very powerful tool," said Mohammad Nayyeri, a Britain-based lawyer who worked on several property confiscation cases involving Setad before leaving Iran in 2010. "It opens the door to corruption. There is no limitation. The private ownership and private life of people are not respected."

Setad has emerged as a mainstay for Khamenei. It provides an independent source of revenue to finance his rule even as years of sanctions imposed by the West have squeezed Iran's economy hard. The story of how he used the law to build up Setad is central to understanding how he has managed in some ways to gain even more power than his predecessor.

A ONE-ROOM HOUSE

The supreme leader, now 74 years old, comes from a modest background. He grew up in the holy city of Mashhadin Iran's northeast. His father, Ayatollah Javad Husseini Khamenei, was a religious scholar of ethnic Azeri descent and a prayer leader at a local mosque.

"The house only had one room and a gloomy basement. Whenever a guest came to see my father ... we had to go to the basement until they left," Khamenei is quoted as saying in a biography posted on his official website.

The young Ali Khamenei was known as Seyyed Ali, a title used by families who claim descent from the Prophet Mohammad. While he chose the same calling as his father, Khamenei embraced radically different views.

The elder Khamenei was a traditionalist opposed to mixing religion and politics. The son took up with the growing Islamist revolutionary cause. One relative says the young cleric appeared as interested in the movement's political dimension as its spiritual side.

"He came across as a modernist or progressive cleric," said Mahmoud Moradkhani, a nephew who opposes Khamenei's rule and lives in exile in Paris. "He was not a part of the fundamentalists ... and only followed the revolutionary movement."

In the early 1960s Khamenei studied under Khomeini in Qom. The young cleric began agitating against the shah's pro-Western regime. In 1963, Khamenei served his first of many terms in prison when, at the age of 24, he was detained by security forces for his political activities. Later that year he was imprisoned for ten days in his home city of Mashhad, where he was put under "severe torture," according to his official biography.

People who knew Khamenei in his formative years describe a complex man. He was an uncompromising revolutionary and Islamist ideologue, but he also had a sociable side, with a sense of humor.

"He has maybe two different personalities," said Houshang Asadi, a dissident journalist who spent months sharing a prison cell with Khamenei in 1974. Asadi recalled one occasion when Khamenei "stood under the window in the little cell, talking to God and crying." On other occasions, "he was a simple man like me. We had different opinions about many things, but we argued, we talked, we laughed, we were joking."

Five years later came the revolution. The shah fled, Khomeini returned in triumph from exile, the clerics emerged pre-eminent. According to historians, Khamenei, then 40, helped found the Islamic Republic Party and went on to serve in increasingly powerful posts.

In 1979, months after taking power, Khomeini pushed through a new constitution that enshrined his concept of the Guardianship of the Jurist, a republic ruled by a leader who is the foremost expert in Islamic law. Among the charter's 175 articles were two that would prove instrumental for Setad. One, Article 45, which deals with public property, gave the government the right to use "abandoned" land and "property of undetermined ownership."

Even more important in the end was Article 49, which allows for the confiscation of wealth acquired through criminal activities. It prescribed broad safeguards against arbitrary confiscations. In practice, this law rarely was followed.

"Article 49 is so broadly written that it allows confiscation and expropriation on the flimsiest of excuses," said Shaul Bakhash, an Iran historian at George Mason University in Virginia. He says property of his own was confiscated by court order in 1992.

Wholesale expropriation of wealth became a hallmark of the early republic. It was a chaotic time. Iraq invaded Iran in 1980. Years of brutal trench warfare followed. There were shortages of rice, milk, meat and fuel. The new regime faced internal threats, carrying out tit-for-tat killings as it consolidated power. And the new state began seizing assets from the deposed royal family and other perceived enemies.

"Bonyads," or foundations, were among the first beneficiaries of this transfer of wealth. One of the biggest was Bonyad Mostazafan, or the Foundation of the Oppressed, which took over many of the royal family's assets. It remains in business. A Mostazafan official did not respond to a request for an interview.

"This situation in reality created a struggle and a sort of competition between the revolutionary units and institutions … for each to find the best properties and introduce them to the court as candidates for confiscation," said Hossein Raeesi, a human-rights attorney who practiced in Iran for 20 years and handled some property confiscation cases.

According to a study published in 1989 in the International Journal of Middle East Studies, in 1982 Bonyad Mostazafan held 2,786 real-estate properties it obtained by court-sanctioned confiscation. Its real estate included a building on Fifth Avenue in Manhattan built in the 1970s by the shah's charity, the Pahlavi Foundation.

The revolutionaries also began turning on some of their own. In 1981, Khamenei helped lead a successful effort to impeach Abolhassan Banisadr, who in 1980 had become the republic's first elected president. Khamenei, then in parliament, introduced a 14-point list of reasons to drive out Banisadr. Less than 17 months after taking office, Banisadr was declared an enemy of the state and fled to Paris, where he now lives.

Later that year, Khamenei survived a bomb attack by a leftist insurgent group that disabled his right arm. The explosives were placed in a tape recorder in the Abuzar mosque in Tehran where he was giving a speech. Two months later, Banisadr's successor as president was assassinated by the same group. In October 1981, Khamenei was elected overwhelmingly as the republic's third president.

"IN THE NAME OF THE REVOLUTION"

In 1982, with factions and organizations feuding over properties, Khomeini tried to control the chaos, issuing a decree that banned confiscations without a judge's order. "It is unacceptable and intolerable that in the name of the revolution and being revolutionary-minded, God forbid, an injustice should be done to someone," Khomeini declared in the order.

Two years later, in 1984, parliament created a special class of property confiscation courts - dubbed "Article 49 courts" - in each of Iran's provinces. They were a branch of the Revolutionary Courts, which had been established to dispense justice to purported enemies of the republic.

The Article 49 courts continue to operate today, but they failed to end the free-for-all. "In practice, the establishment of the Article 49 courts systematized and continued the expropriations, and even today the confiscations continue," Raeesi said.

The war with Iraq ended in 1988. It left hundreds of thousands of Iranian soldiers and civilians dead. That meant many mouths to feed.

In April 1989, Khomeini issued his brief order that marked the genesis of Setad, whose full name in Persian is "Setad Ejraiye Farmane Hazrate Emam" - the Headquarters for Executing the Order of the Imam.

He directed two senior officials to take over all "sales, servicing and managing" of properties "of unknown ownership and without owners." The revenues were to be spent on sharia issues "and as much as possible" to help seven bonyads and charities he named. The officials were to use the money to support "the families of the martyrs, veterans, the missing, prisoners of war and the downtrodden."

At this point, though he had been president for more than seven years, Khamenei wasn't counted among Iran's first rank of power brokers. The presidency had been stripped of key powers during Banisadr's short rule, because he was viewed as a threat to Iran's new clerical elite.

After Khomeini's death in June 1989, Iran's Assembly of Experts selected Khamenei as his successor. Some senior clerics regarded him as unqualified for the job of supreme leader. He hadn't achieved the required rank among Shi'ite clerics that the constitution stipulated. But key supporters backed a constitutional amendment that sealed his elevation, and Khamenei took office in July.

One of his early acts was to order a cleric to follow up on the creation of Setad. The new organization's decisions, he said in an edict that September, "should be just and based on sharia and without negligence."

The new supreme leader quickly set about cultivating the Islamic Revolutionary Guard Corps, an elite military group that had emerged from the war as one of Iran's most powerful institutions.

Droves of its soldiers had returned in 1988 from the battlefield and were seeking opportunity. He began securing their support by helping them receive lucrative reconstruction contracts.

According to Mohsen Sazegara, a co-founder of the Revolutionary Guards who is now in exile in the United States, Khamenei allowed the Guards to enter the construction business. That opening eventually enabled an engineering division of the Guards to evolve into a major conglomerate.

In time the Guards became a pillar of Khamenei's power. So too did Setad.

By the early 1990s, courts were seizing assets and turning them over to Setad, according to documents reviewed by Reuters. The organization began keeping funds from property sales, rather than redistributing all the proceeds. It isn't clear when Setad began retaining funds or what percentage of the revenue it keeps.

Khamenei, meanwhile, took pains to show Iranians that he was above exploiting his high office. On a visit to his boyhood home in Mashhad in August 1995, he told the story of how while he was president, a next-door neighbor erected a tall building overlooking his yard.

"As a result, my mother could no longer step in the yard without wearing a chador," a long garment worn by devout women in Iran. He asked the neighbor to reconsider, according to an account by Khamenei on his official website, but the owner wouldn't listen.

Khamenei dropped the matter. "Such events bear testimony to the fact that worldly positions and financial means never cause individuals to regard themselves as distinct from the general public, or that they are entitled to live in greater comfort," Khamenei said.

In 1997, the reformist Mohammad Khatami was elected president. Iran's judiciary soon moved to shield Setadfrom scrutiny. The General Inspection Office is an anti-corruption body that monitors many Iranian institutions and reports to the head of the judiciary, who is appointed by Khamenei. That year, a government legal commission declared that the GIO had no right to inspect Setad unless the supreme leader requested it to do so.

Setad still faced competitors. Among its main rivals for seized assets, according to attorney Mohammad Nayyeri, was another organization: Sazemane Jamavari va Forooshe Amvale Tamliki, or Department for the Collection and Sale of Acquired Property, which is controlled by the Ministry of Economic Affairs and Finance. In 2000, the judiciary adopted a bylaw granting Setad exclusive authority over property taken in the name of the supreme leader.

AN EXPEDIENT RULING

Setad began expanding into corporate investments, taking advantage of another legal initiative by Khamenei.

In 2004, Khamenei ordered a review of Article 44 of the constitution, which mandates state ownership of critical industries. The Expediency Council, a state advisory body appointed by the supreme leader, issued a new interpretation of Article 44 allowing the privatization of major industries.

"What was the goal?" he asked in a 2011 speech. "The goal was to create a competitive economy with the presence of the private sector and its investments in the economy of the country."

In 2006, with the country facing a ballooning budget deficit under its new hardline president, Mahmoud Ahmadinejad, Khamenei issued an executive order to privatize 80 percent of the shares of some state-owned companies. The targets included banks, insurers and oil-and-gas firms. The step, he said, would change the "government's role from direct ownership and management of companies to policymaking, guidance and monitoring."

In 2009, Setad emerged as a victor in Iran's biggest state asset sale, the privatization of Telecommunication Co of Iran (TCI).

Through a subsidiary, Setad held a 38 percent stake in a consortium that was awarded majority control of the telecommunications provider, Iran's largest, according to Setad documents seen by Reuters. The other big winner was the Revolutionary Guards, which controlled most of the winning consortium.

Even before then, Setad had been drawing attention from the reformist wing of the establishment. During Khatami's second term, moderate members of parliament sought to investigate Setad, according to Nayyeri. TheGuardian Council, a body of conservative clerics and jurists who are directly or indirectly appointed by Khamenei, issued a declaration that Setad was beyond parliament's authority, Nayyeri said.

Elections in 2008 brought a strongly conservative parliament deeply loyal to Khamenei. In one of its first steps, parliament amended its bylaws to limit its own power to audit institutions under the supreme leader's supervision, except with his permission.

"This is the reason why no one knows what is going on inside these organizations," says Sazegara, the Guards co-founder.

In 2009, hundreds of thousands of Iranians in the so-called Green Movement protested the re-election of President Ahmadinejad. Amid the unrest, one of the two men who established Setad spoke out against the organization.

That man, Mehdi Karoubi, had emerged as a major reformist politician, serving as speaker of parliament and making unsuccessful bids for the presidency in 2005 and 2009. After the 2009 vote, he wrote a letter to a longtime rival of Khamenei in which he reported that Khomeini had intended Setad to last just two years.

Karoubi went on to accuse Setad and Khamenei's other major power base, the Revolutionary Guards, of corruption. In an apparent reference to the TCI privatization, he wrote that they "put the shares of a government ministry in their own name within half an hour. And in the name of privatization they create another saga that continues and completes the recent saga of the presidential elections?"

Karoubi has been under house arrest in Iran since 2011 and couldn't be reached for comment.

Khamenei is sensitive to suggestions that he and his appointees receive special treatment under the law. "No one is above supervision," he has said in a speech, according to a page on his website titled "The Supreme Leader's View of Supervision." "Even the leader is not above supervision, let alone the organizations linked to the leader," he said. "Therefore, everyone should receive supervision, including those who govern the country. Government by its very nature entails accumulation of power and wealth. That is to say, national wealth and social and political power are entrusted to a few government officials. As a result, they must be supervised."

Driving home the point, he added: "I welcome supervision, and I am strongly opposed to evading it. Personally, the more supervision I receive, the happier I will be."

Today, Khamenei's power in some respects exceeds that of his predecessor. He lacks the religious authority of Khomeini but has far greater resources at his disposal.

Khomeini operated from a modest house in northern Tehran with a small staff.

Khamenei lives in a large compound in Tehran. The grounds contain a variety of buildings, including a large hall where Khamenei gives speeches. Setad helps to finance his administrative offices, which are known as Beite Rahbar, the Leader's House, according to a former senior Setad employee and other people familiar with its operations. It employs about 500 people, many recruited from the Guards and security services.

Setad itself remains veiled from the public eye. It is led today by Mohammad Mokhber, a Khamenei loyalist who is also the chairman of Iran's Sina Bank, according to its website. The European Union sanctioned him in 2010 but lifted the sanctions last year, without any explanation.

Setad reveals little about its income, expenditures or staffing. Its headquarters is in a large gray concrete building with small windows in the heart of Tehran's commercial district.

"You won't even notice it even if you are walking by," a former employee said.

In June, Supreme Leader Khamenei congratulated Rouhani upon his election as president. That same month, the chief justice, a Khamenei appointee, issued a fresh declaration of support for Setad. According to the Iranian Students' News Agency, Justice Sadeq Larijani told lower courts that Setad's old rival, the Department for the Collection and Sale of Acquired Property, had no authority to take property in the supreme leader's name.

Setad, the justice reminded the courts, "is the only authorized organization in the issue of property related to the supreme leader."