Michael Cohen was at a breaking point. He told friends he was suicidal. He insisted to lawyers he would never go to jail. Most of all, he feared that President Donald Trump, his longtime boss, had forsaken him.

"Basically he needs a little loving and respect booster," one of Cohen's legal advisers at the time, Robert Costello, wrote in a text message to Rudy Giuliani, the president's lead lawyer. "He is not thinking clearly because he feels abandoned."

That was in June. The "booster" from Trump never arrived. And by August, Cohen's relationship with him had gone from fraught to hostile, casting a shadow on the Trump presidency and helping drive multiple criminal investigations into the president's inner circle, including some that continued after the special counsel's work ended.

In the biggest blow to the president personally, federal prosecutors in Manhattan effectively characterized Trump as an unindicted co-conspirator in a criminal case against Cohen involving hush money payments to an adult-film actress. Cohen, and evidence gathered by prosecutors, implicated the president.

Now, as Cohen prepares to head to prison in two weeks, dozens of previously unreported e-mails, text messages and other confidential documents reviewed by the New York Times suggest that his falling out with Trump might have been avoidable.

Missed cues, clashing egos, veiled threats and unaddressed money worries all contributed to Cohen's halting decision to turn on a man he had long idolized and even once vowed to take a bullet for, according to the documents and interviews with people close to the events.

Cohen held out hope for a different outcome until the very end, when he pleaded guilty and confessed to paying the illegal hush money to avert a potential sex scandal during the presidential campaign. Just hours earlier, wracked with indecision, he was still seeking guidance, looking, as one informal adviser put it, "for another way out."

Cohen's anxiety, on display in the documents, played a role in the undoing of his relationship with Trump, as did Costello's lack of success in serving as a bridge to the White House. But also looming large were Giuliani's and Trump's failures to understand the threat that Cohen posed, and their inability — or unwillingness — to put his financial and emotional insecurities to rest.

After the FBI raided Cohen's home, office and hotel room in April 2018, two of Cohen's advisers explored whether the president might be open to a pardon, but Giuliani offered no assurances.

In June, Costello proposed that he and Giuliani meet with Cohen to address his grievances and ease his anxieties. "Are we going to meet Thursday or Friday?" Costello texted Giuliani on a Monday. "I would like to get back to Michael with a response."

But Giuliani did not respond. And when Costello followed up, "Can I get a response on the possible meeting?" Giuliani hesitated, replying, "Not yet because haven't talked to President," who was out of the country.

The next day, Cohen's private admission to friends that he was open to cooperating with prosecutors suddenly appeared in the news. And Cohen relayed his growing displeasure with the Trump camp to Costello, sending the lawyer an article that suggested the president and his allies intended "to discredit Michael Cohen" and commenting in the e-mail that "they are again on a bad path." He also complained to Costello that the president had stopped covering his legal expenses.

Costello, who spoke with the Times after Cohen waived attorney-client privilege in February, said that without Cohen's team and the president's lawyers in sync, it was impossible to navigate the tumultuous relationship.

"What we had here was a failure to communicate," said Costello, who was never formally retained by Cohen. "My thought was a face-to-face meeting among all the lawyers together with Cohen would put everyone on the same channel. The meeting never happened, and the rest is history."

In an interview, Giuliani acknowledged that the Trump team had pulled back from Cohen, saying it did so because prosecutors might have viewed friendly overtures as witness tampering, and because Cohen's legal problems extended beyond his relationship with Trump.

"It seemed like an unfortunate but sensible decision," he said of the Trump team's reticence toward Cohen. "The more I look back at it, the more I wonder if it was inevitable that Michael was going to crack."