Inside the ropes, a Midwestern American prepares to confront a local from Northern Thailand. They are in Rangsit Stadium, outside of Bangkok, preparing to battle through five rounds of Muay Thai.

A centuries-old precursor to UFC, the sport is a brutal form of pugilism known as “the art of eight limbs.” Sharp elbows slam into eyes, knee-kicks rumble kidneys, clutches from opponents can feel suffocating. The stale-aired stadium reeks of sweat and liniment oil; wild-eyed fans wager stacks of baht as they cheer on their fighters of choice.

Normally, contenders — sometimes minors fighting for the sake of putting food on family tables — go at it for a couple hundred dollars in baht. But this time, one is fighting to get out of jail.

If the Thai combatant, trim and prison-tattooed Noy Khaopan, wins, he will be granted release six years early from an 11-year murder sentence. Noy, in his mid-20s, is serving his time in Northern Thailand’s Khao Prik Prison, where 60 convicts cram into 18-by-30-foot cells, drugs are rampant and gangs control everything.

If the American, Cody Moberly, a converted Buddhist from Wichita, is victorious, he will emerge with bragging rights, prize money and satisfaction that comes from having defended an innocent who had been slaughtered in cold blood.

He wants to keep Noy in prison — and kick his ass in the process. “It was important for me to head home a winner,” says Moberly, 28. “I felt like I had the victim on my shoulder.”

The road to this moment begins at a modest end-of-school celebration gone awry around 2010 outside the industrial city of Korat, Thailand. It’s recounted in a Showtime documentary, “Prison Fighters: 5 Rounds to Freedom,” premiering Friday night.

Anirut Vanichyaron, handsome and studious, was celebrating his school graduation and had ambitions to be an engineer. He was supposed to go home but friends encouraged him to hit a pub for more festivities.

That’s where trouble began. As Anirut’s father says in the documentary, “Gangsters saw him with a pretty girl and despised him.”

The gang members stared him down from a nearby table, and one of them trailed Anirut out the door, calling for his fellow members to follow him. Noy was among them, and he stabbed Anirut in the lower right side of his head.

Reasons for the attack range from jealousy over the girl to disliking Anarut’s light skin color — nobody knows for sure. The victim was rushed to a nearby hospital where, his mother says in the film, “[Doctors] kept him alive so I could see him one last time.” But medical assistance was moot. “They took him off the oxygen. I couldn’t say a word.”

Noy, a local Muay Thai practitioner, was arrested one day later. His 11-year sentence was soft thanks, possibly, to a bribe from his father and the judge, who accepted the argument that Anirut attacked Noy in a past life. Noy left a nearly 2-year-old son in his parents’ care. They told the boy his father was on a boxing tour.

“They had never met before that night,” Anirut’s mother says in the movie. “Why would you kill somebody you don’t know?”

Five years later, Noy told his side of the story to filmmaker Micah Brown. Sitting crosslegged in a barred prison room, he admits, “I lost control. I was drunk. I killed him. I have to accept the truth.”

Then, over footage of his victim’s parents standing in the rain, honoring their dead son at a Buddhist temple, Noy says, “I don’t need forgiveness from everyone in this world. If my parents forgive me I am satisfied.”

What he really needed was a break. It came his way via an unusual program built into the Thai penal system: Prison Fight, which has been running since 2012.

Prison Fight is the brain-child of Aree Chaloisuk, formerly the director of Klong Pai Prison, two hours north of Bangkok. As explained in the movie, he came up with it to “capitalize on the popularity of a national sport and exploit the rehabilitative power of fighting’s discipline.” Win enough Muay Thai matches — Noy won seven — against outside fighters, and one more gets scheduled to decide the incarcerated man’s fate. Win, and he goes free; lose, and he’s thrown back behind bars to complete his sentence.

If such an arrangement seems flippant to American sensibilities, Muay Thai commentator and photojournalist Rob Cox says it’s different in the Land of Smiles: “Forgiveness is part of the culture. It’s quite prominent here.”

Prior to Prison Fight, a similar set-up resulted in a boxer leaving prison early and going on to represent Thailand in the 2008 Beijing Olympics. Splashiest of all was the release of Sirimongkol “Oh” Singwancha, a two-time WBC champion and Thailand’s most famous fighter. But they were in for relatively minor offenses — Singwancha, for example, had been sentenced to 20 years for having $300 worth of speed in his shirt pocket.

‘I was a little bit in awe to be fighting for the guy he had killed. I knew I would put all of my heart into the fight.’

It all adds up to a philosophical question. “Can violent men redeem themselves through violent acts?” says Mark Kriegel, the documentary’s writer/producer and a former sportswriter for The Post. “It’s possible but it’s complicated. You wonder about the government letting guys fight their way out of prison. But you also need to realize that the creation myth of Muay Thai has to do with a Thai prince being captured and beating up guys for his freedom.

“I’ve covered the biggest fights of the last quarter-century,” adds Kriegel. “What’s typically at stake is money and male fear of humiliation. This was about real freedom, so the stakes were higher.”

Plus, there was the delicate situation of dealing with the victim’s parents. “They did not [initially] know that Noy was part of the fight-to-get-out program,” Brown says. “When we told them, the father wanted to kill Noy. The mother was inconsolably in pain. They don’t think it’s fair for [the program to apply to] a crime like this one.”

The director made a point of not telling them where the fight would be taking place: “I feared that the father would do something crazy.”

And how did they find the man who would decide Noy’s fate?

Cody Moberly didn’t go to Bangkok for an easy life. In 2007 he was a 19-year-old kid who drank too much and had an aimless life, working as a plumber’s helper in Kansas.

“Then I saw a documentary on Muay Thai, started training and knew I would eventually go to Thailand,” he says.

That year he made his first foray to Bangkok and enrolled in a rigorous Muay Thai training camp. Moberly took to it enough that the combat sport has became his life and his livelihood, and he has been bouncing between Thailand and Kansas since 2007. “I kept reminding myself that diamonds are built under pressure,” he says.

Moberly was known to the promoter of the prison fight. He decided that the American and Thai would match up well, both angling for a few hundred-dollars in prize money. “I went to the gym one day, and my trainer told me I would be fighting against Noy,” Moberly says.

“I had been in Thailand for so long that nothing surprised me anymore,” he adds. “Then I thought about it a little and it messed with my head, the fact that he had killed someone. Quickly, though, I got things into perspective. The promoter told me it would not be an easy fight.

“I was a little bit in awe to be fighting for the guy he had killed. I knew I would put all of my heart into the fight.”

On fight night, Noy walks into the arena, without shackles and holding his mongkol, the sacred, circular headpiece Muay Thai fighters don before matches. Upping the ante, his son, now seven, sits in the audience.

Seeing his opponent for the first time, Moberly looks into the camera and speaks with surprise in his voice. “He seems like a normal kid. It’s hard to envision him stabbing someone.”

In the ring, the two fighters touch gloves. (Note: Spoilers follow.) Moberly and Noy deploy all eight limbs in a high-impact match that has Noy getting thrown to the mat and losing the first round. He suffers another fall in round two before speeding up and decking Moberly. He takes it, wins the third round and gives up the fourth to Moberly.

The fight is decided by the fifth and final round. Despite Moberly’s momentum, Noy manages to take it down, assisted by a punch to the face that collapses his opponent for a final time.

Noy leaves the ring with dignity and a tight smile of satisfaction.

Soon after, as promised, he gets his royal pardon. After considering a career as a professional Muay Thai fighter, he winds up forsaking a life of violence for something steadier and less glamorous: He goes to work with his parents at the small fried chicken stand that they own.

Kriegel sounds impressed by the decision, but acknowledges that it will take years to get a hint of much Noy has learned and changed. “The idea of redemption is relative — what crime and punishment means is ambiguous,” Kriegel says.

“What’s really in this guy’s heart? You don’t know and can never know.”