As President Reagan heads into critical months of his second term, his most powerful lieutenants are working to overcome mutual distrust and competition.

The two are White House chief of staff Donald T. Regan and national security affairs adviser Robert C. McFarlane. In the eight months since Regan arrived in the West Wing, the relationship between the two men has been uneasy at best and at times openly hostile, according to informed officials.

Now, with Reagan beginning a crucial stretch of his second term with issues from the budget to U.S.-Soviet relations in the balance, Regan and McFarlane are trying to overcome their differences, associates say. How the president fares this fall in dealing with Congress and the Soviets is sure to be affected by whether these two officials are cooperating. "The curve is going in a positive direction," said a senior White House official close to Regan.

Last week, for example, Regan and McFarlane appeared to be working toward averting an embarrassing defeat for Reagan in Congress on South Africa sanctions.

However, friends and subordinates of both men, describing their differences as both stylistic and procedural, said the conflicts could flare again. The degree to which they have substantive differences is likely to become clearer this fall.

How long Regan and McFarlane remain in the White House may depend on the success of the president's agenda this autumn -- and thus on their ability to work together in coming months.

A string of victories in Congress and a successful summit meeting with Soviet leader Mikhail Gorbachev could have a positive effect on their relationship, associates say. But another string of setbacks like those earlier this year could drive them apart -- and perhaps out of the White House.

The reason such relationships between presidential aides are so closely watched inside the White House is that Reagan tends to delegate much authority to subordinates. Chief of staff Regan has already become one of the most powerful White House assistants in a generation, serving a president who prefers to focus on the big picture, who is often passive in resolving internal conflicts and who eschews detail.

By some informed accounts, the president is aware of the tension between Regan and McFarlane but has not taken any action.

Publicly, he has denied any conflict exists.

"There seems to be a concerted effort, and has been for the last 4 1/2 years, to try and build feuds within the administration," Reagan told Time magazine columnist Hugh Sidey in an interview just after having cancer surgery in July. "I think they thrive on -- some do -- on combat. And there just isn't anything to it.

"I have witnessed no grabs for power on the part of anyone," he said.

But others say the differences between Regan, 66, and McFarlane, 48, have centered on substantive issues and involved competition for responsibility and power.

For example, in early August, they disagreed on preparations for the Gorbachev summit. Officials said McFarlane had proposed that he head a working group to handle summit preparations.

Officials described McFarlane as believing that the Gorbachev meeting could offer Reagan a historic opportunity to seek deep reductions in Soviet offensive missile forces. But, they said, McFarlane is worried that continuing disagreements in the administration over the issue could leave Reagan unprepared to take advantage of such an opportunity at the summit.

Chief of staff Regan resisted the proposal for McFarlane to head a summit working group for several weeks, officials said. Ultimately, they compromised, with Regan and McFarlane being "co-chairmen" of the group. This would assure Regan a voice in substantive issues and also give him power over logistical planning. A key McFarlane deputy on the National Security Council, Jack F. Matlock Jr., is to head a staff-level working group.

Regan has moved recently to install his own people in key posts, including the Office of Management and Budget, the Council of Economic Advisers and key White House staff posts. McFarlane's domain in the West Wing is one of the few Regan has not touched.

Officials describe McFarlane as being irritated that Regan has not exercised tighter control over those he brought into the administration. Specifically, the national security affairs adviser has been angered a number of times by Communications Director Patrick J. Buchanan, who works for Regan, on the issue of South Africa sanctions and the tone and substance of presidential speeches.

Another source of contention was the seemingly zigzag legislative course followed by Regan this year. The White House sometimes antagonized Reagan's GOP allies in both the House and the Senate. The result was, among other things, a hefty slice out of Reagan's defense buildup and the slashing of the MX missile force to 50 missiles. Reagan did, however, win resumption of aid to the anti-Sandinista rebels. McFarlane, son of a Texas congressman and a veteran Capitol Hill assistant who retains strong ties in Congress, confided to friends during the summer his disappointment over Regan's confrontational style with Congress, saying it was counterproductive.

Many Republicans have faulted Regan on this score as well, and the chief of staff seems to have grown more sensitive to the complaints. He went to Capitol Hill twice last week to confer privately with GOP leaders. He was stung by the criticism that he was squandering Reagan's second-term "window of opportunity," one official said, "and now he's actively trying to manage issues" such as South Africa sanctions.

Regan got involved with the South Africa deliberations in part because he feared that the first congressional test for Reagan this fall might be an overwhelming defeat on sanctions legislation that would mar the "fall offensive" he had mapped out on trade, spending and tax issues.

Officials close to Regan said that his early impressions of McFarlane were not positive, and that this shaded his later dealings with the national security affairs adviser. Regan's style is blustery, aggressive and self-confident. But aides said he felt tripped-up at one point after an important overnight foreign policy development when McFarlane telephoned the president with details immediately. According to Regan associates, McFarlane did not telephone the chief of staff. The next morning, one official said, Regan walked into the Oval Office to meet the president very early, not aware of what had happened, and was somewhat embarrassed.

"You just don't do that to Regan," the official said.

According to Regan associates, the episode seemed to telegraph McFarlane's desire to be an independent force from the chief of staff. In this view, McFarlane seemed more willing to challenge or bypass Regan than he had been with the first-term advisers, particularly chief of staff James A. Baker III. One official speculated that McFarlane may have miscalculated and been overly aggressive in his early dealings with Regan, which soured their relationship later.

There have been hints of problems between the two advisers for months. Over the Easter holiday in Santa Barbara, they gave back-to-back, conflicting accounts of what Reagan sought in meeting Gorbachev. Regan told reporters that the president wanted "results" from a meeting "and that's what we're working towards, rather than just having meetings for meetings' sake."

The next day, McFarlane said that before a summit on substantive issues, it would be "useful" to have a limited get-acquainted meeting between Reagan and the new Soviet leader.

For the most part, tension between the two men has been kept from view. One official called it "almost invisible." But it bubbled up in front of other White House officials at a staff meeting in early August over a column by Rowland Evans and Robert Novak. The column suggested that some conservative senators unhappy with McFarlane had urged Regan to immerse himself quickly in national security policy, and had suggested a role for Zbigniew Brzezinski, who was President Jimmy Carter's national security affairs adviser, in reviewing the administration's strategic planning. The column quoted senators as saying that McFarlane did not show the same deference to Regan as he had to Baker, and that he did not brief Regan on major national security documents. McFarlane let a "flash" of his displeasure at Regan show at a senior staff meeting that day, participants said.

The resentment appears to have reached a peak in August, officials said, and both Regan and McFarlane subsequently decided to try to mend fences while the president faces the most crowded and pressing legislative agenda of his White House years.