An undated photo of Mohammed al-Qahtani. He is suspected of planning to participate in the attacks of Sept. 11, 2001, as the so-called 20th hijacker and is now seeking release from the U.S. military prison at Guantanamo Bay, Cuba.

Long before Mohammed al-Qahtani was suspected of planning to participate in the attacks of Sept. 11, 2001, as the 20th hijacker, Saudi police found him naked in a garbage dumpster in Riyadh.

In another psychotic episode that took place in Mecca in 2000, Qahtani was involuntarily committed to a hospital after trying to hurl himself into the street. Doctors at the time said he was delusional and suicidal, according to medical and psychiatric records that were obtained by The Washington Post.

Qahtani tried to enter the United States a month before the 9/11 attacks but was denied entry at Orlando International Airport. He was captured by the Pakistani army at the Afghanistan-Pakistan border in the fall of 2001 and transferred to U.S. custody, arriving at Guantanamo Bay, Cuba, in January 2002, according to military records.

In 2009, Susan J. Crawford, a senior Pentagon legal official and retired judge, told The Post, “We tortured Qahtani. His treatment met the legal definition of torture. And that’s why I did not refer the case” for prosecution at a military commission.

Qahtani’s attorneys will argue Thursday at a military-style parole board hearing that their client’s preexisting mental illness and the abuse he experienced at Guantanamo Bay should favor his release, as the prison is unable to provide adequate medical and psychiatric care.

Saudi Arabia has agreed to resettle him, according to a Saudi Interior Ministry document provided to The Post.

President Obama is attempting to close the U.S. military prison and transfer out as many of the 80 detainees remaining as possible. But the Qahtani case is among the most complex facing the administration because the military thinks he was linked to the 9/11 conspiracy but his ill-treatment and medical issues make a trial all but impossible.

“Mohammed was already mentally ill long before the time when he was alleged to have attempted anything. That fact calls into serious question the fairness of holding him responsible for any actions during that period of his life,” said Ramzi Kassem, a law professor at the City University of New York whose legal clinic represents Qahtani along with Shayana Kadidal, a lawyer with the Center for Constitutional Rights.

Qahtani, 39, is one of the most controversial figures at Guantanamo Bay. Over a 50-day period, he was subjected to constant interrogations marked by extreme sleep deprivation, low temperatures, stress positions and forced nudity as well as being threatened with a military dog. He had to be hospitalized twice with a dangerously low heart rate.

He has schizophrenia, major depression and persistent skin lesions, said Emily Keram, a psychiatrist who recently examined him. In her assessment, she wrote that he “will likely require lifelong mental health care.”

“Qahtani cannot receive effective treatment for his current mental health conditions while he remains in U.S. custody,” Keram concluded in an evaluation that The Post obtained.

Keram and an interpreter met with Qahtani for 39 hours in May. He revealed that he had suffered a head injury after being ejected from a car when he was 8 and was injured twice more in car accidents.

According to Keram’s medical evaluation, Qahtani developed psychotic symptoms in childhood that worsened as he grew older. His family said he once tossed his cellphone out of a car because it was making him “tired.”

In Mecca, he was admitted to the psychiatric unit of the King Adbul Aziz Hospital and treated with antipsychotic medication and a sedative. He told doctors that he wanted to kill himself and that he was delusional, the records show.

Qahtani first traveled to Afghanistan in 2000, where he received training at an al-Qaeda camp before meeting and swearing loyalty to Osama bin Laden, according to military records. The al-Qaeda leader singled him out to participate in the 9/11 attacks, according to the records.

Qahtani’s “psychological and cognitive deficits” made him “vulnerable to ma­nipu­la­tion and coercion,” said Keram, who has previously examined other Guantanamo Bay prisoners.

During the period when he was interrogated in 2002 and 2003, Qahtani thought he was dead and believed that he saw ghosts but that an imaginary bird reassured him he was still alive, Keram reported.

“My interview of him was extremely disruptive to his sense of identity and induced deep feelings of anxiety and shame,” she wrote. “He often wept. Over the days of our interview he reported experiencing increase in the intensity of frequency of PTSD symptoms.”

He told Keram that he had two options when he was being tortured: compliance or suicide. Because he couldn’t kill himself, he told his interrogators what they wanted to hear. She said those statements were neither reliable nor credible.

In 2008, military officials wrote that Qahtani was in good health and “although publicly released records allege detainee was subject to harsh interrogation techniques in the early stages of detention, detainee’s admission of involvement in UBL’s special mission to the US appear to be true and are corroborated in reporting from other sources.”

That same year, Qahtani told an administrative review board at the prison that he was a “different person now than he was in the summer of 2001. The detainee stated he would not have participated in a mission that involved killing women and children.”