"McMaster realized we just cannot come back here next time with a binary option - certify or decertify," an exercise Congress requires every 90 days, said one person familiar with the July discussion. "He put his team to work on a range of other options including an decertification option that would involve Congress" and would not immediately break the deal.
That effort - described by seven people familiar with the debate, most of whom insisted on anonymity to describe the confidential discussions - led to a revamping of the US approach to Iran and the nuclear pact that Trump is set to announce this week and which Congressional leaders were briefed on Wednesday. Under the expected announcement, Trump will declare that the deal is not in the US national interest while stopping short of recommending renewed nuclear sanctions.
The deliberations show the extent to which Trump's national security team in recent months has been occupied with navigating the future of the Iran nuclear deal, which Trump repeatedly vowed to throw out as a "disaster" during the campaign. The sometimes angry internal debate also provides another illustration of the way in which Trump's gut impulses and desire for dramatic action have often collided with the subtlety of international diplomacy.
The Iran agreement, brokered by former president Barack Obama, was never designed to do many of the things Trump criticizes it for lacking. Many of his own advisers - and many Republican leaders and key US allies - also see it as a valuable tool in stopping an Iranian nuclear bomb.
The solution is a compromise that retains the agreement but also puts Iran and US allies on notice that Trump is willing to walk away. Meanwhile, Trump is likely to make the case that as the Islamic State terrorist group is weakened, Iran is reasserting itself as the most destructive influence in the Middle East and using the nuclear deal as cover to do so.
"He doesn't want to certify the Iran deal for more domestic reasons than international ones," said Vali Nasr, dean of the Johns Hopkins School of Advanced International Studies. "He doesn't want to certify that any piece of the Obama strategy is working."
Trump is expected to announce new conditions for US participation in the agreement among the US, Iran, Britain France, Germany, Russia and China, and punt the issue to Congress. He may also announce new sanctions or penalties on Iran's powerful Revolutionary Guards Corps.
"You can do both," Trump said Wednesday when asked about certifying or rejecting the deal.
He said he would announce his plan "very shortly," adding in an interview with Fox News, "It's a horrible, horrible embarrassment to our country, and we did it out of weakness, when actually we had great strength."
"We are on a tightrope. We don't know what will happen," said one Western diplomat worried that Trump's action will undermine the international agreement.
As a practical matter, Trump's expected move will place the onus on Congress to decide what to do next. Working with Sen. Tom Cotton, R-Ark., a leading congressional hawk on Iran, the White House would refrain from recommending that Congress reimpose nuclear sanctions that were suspended under the deal.
That would buy time for new legislation codifying Trump's conditions for remaining in the deal formally known as the Joint Comprehensive Plan of Action, a Republican congressional aide said. It would also increase US leverage with European allies who don't want to renegotiate the deal, said the aide, who requested anonymity because Trump has not yet announced his plan.
"To get us on the right foot on the Iran strategy we do need to use this certification decision, this moment, to launch a real effort to plug the holes and the weaknesses in the JCPOA," said the aide.
"We need to send the message that the president does not feel constrained by the JCPOA and does not feel beholden to it," while seeking tougher an extension of the deal's restrictions on Iran's nuclear activities and other modifications.
Cotton laid out that approach in a speech to the Council on Foreign Relations earlier this month, in which he accused Iran of harming US interests in the Middle East and scheming to preserve its ability to eventually produce a bomb.
The speaker and the setting were clear signals that the Iran hard-liner would block for Trump in two ways. By holding off on new sanctions that would bust the deal, Cotton helps Trump refute the claim of sabotage from Democrats and other parties to the agreement. And because of his history of advocating tough measures against Iran, he may help protect the White House from criticism by conservatives who want to do away with the deal.
"It would give a few months or years lead time to give time to get US allies on board with the same restrictions - a unified front that will put lots of pressure on the Iranians" to reopen the deal, said the aide.
Britain, France and Germany, along with the European Union's foreign policy chief, have argued to Congress and the Trump administration that the deal cannot be redone. Iran has said the same.
The pivotal moment in the administration's Iran debate came on July 17, when the president balked when presented with the consensus recommendation of his national security advisers that he should submit the congressional certification. He argued with aides all that afternoon, forcing a postponement of a planned announcement and a rewriting of White House talking points.
The decision was clumsily announced that evening, hours before a legal deadline, along with a declaration that Trump planned to toughen expectations and enforcement.
The administration announced new sanctions on Iran over its ballistic missile program the following day. But only sanctions related to the country's disputed nuclear program are covered by the 2015 deal. Iran claims it has never sought a nuclear weapon and that its nuclear research and development is intended for medicine and energy.
The first certification of Trump's presidency came in April, when Trump was also reluctant but agreed on the grounds that the administration was just beginning a broad review of its Iran strategy and would wait for any major decisions, the people familiar with the debate said.
By July, the president's frustration was evident. He made it clear he felt strong-armed and that the July certification would be his last, several people familiar with the discussion said.
Trump took the internal confrontation public in an interview with the Wall Street Journal in which he said he regretted the decision. The experience also further soured Trump on Tillerson, whom he complained consistently came forward with only "totally conventional" approaches to foreign policy problems, people familiar with Trump's thinking have said.
It would fall to Tillerson and the State Department to try to negotiate new terms for the Iran deal, and ally after ally has bent his ear with arguments that the deal should be preserved as it is.
"The nuclear deal was a crucial agreement that neutralized its nuclear threat," British Foreign Secretary Boris Johnson said Tuesday, following a telephone call with Tillerson. "The U.K. supports the deal and stresses the importance of all parties continuing to uphold their commitments."
Tillerson joined all of Trump's other top national security advisers in recommending last month that Trump decertify the deal as part of a strategy some refer to as "decertify, pressure and fix."
As Trump officials briefed lawmakers Wednesday, two Obama administration architects of the deal, former secretary of state John Kerry and former energy secretary Ernest Moniz, were also on Capitol Hill arguing in defense of the original agreement.
Congress may now do away with the requirement that the president recommit to the deal every 90 days, something that skeptical lawmakers of both parties mandated when Obama negotiated the agreement.