Many times I have sat with one of my death row clients as he comes within minutes of execution before we finally secured a stay. With one man in the US we got within 40 minutes of the electric chair four times. It is a misnomer to talk about the "eleventh hour" – one time there were just 58 seconds left before the allotted time. I find it fairly traumatic myself, and have always wondered how the human mind survives – how a sane person avoids tipping over the edge to madness.

In Pakistan, in the case of Imdad Ali, the government began ahead of the game: prison doctors had already diagnosed him as schizophrenic and labelled him "insane". A few nights ago, Ali came agonisingly close to the hangman's noose before the Supreme Court of Pakistan issued a stay. Yet the stay only lasts a week, and the court will soon decide whether the government can have another crack at killing him.

Ali's father was also afflicted with schizophrenia. One night, when Ali was just two years old, his father jumped in front of a train. He thought he was invincible. He wasn't.

Although Ali was too young to remember his father, the fruit often falls close to the tree in many complicated ways. One issue is the degree to which our genes predispose us to certain mental illnesses.

It is a matter very close to my own heart, as my father suffered from bipolar disorder, and I grew up under the damoclean threat that I would one day find that I was, truly, my father's son.

Some 20 years ago, as it became clear that Ali was becoming ill, his family was desperate to find help. But that is not easy to find even if you have the money to pay for it – which they did not. Ali became increasingly withdrawn, talking to himself, talking to inanimate objects, and instructing the sun loudly to shine with less heat. His wife, Safia, tried unsuccessfully to get him into a mental hospital.

Tragically, in 2002, his rational mind in tatters, Ali shot a man – a wholly innocent person from a family held in high regard by Ali's family. It was not Ali who committed murder, but his schizophrenia. The entire neighbourhood remains willing to come forward to testify that Ali was crippled by his illness.

Spending more than a decade on death row has taken an additional toll on Ali's mental health. He has spent years in solitary confinement because his manic ramblings so upset the other prisoners.

How can we punish someone if that person has only the vaguest comprehension of what is being done to him and why?

When Ali's sister went to see him for what was slated to be the last time, she was distraught: "He will never even understand what we're telling him, or what the noose is for. He has no idea that he might die."

I have always had mixed feelings about this: perhaps it is kinder, if we are going to kill people, that they do not understand the barbarism that we are about to visit on them. On the other hand, I have seen mentally ill clients close to death, and – whether they have a full understanding or not – the terror is visceral.

Regardless, we should not compound our revenge. It has been a principle of law for more than 300 years – since Sir Edward Coke wrote it down in 1680 – that if "a mad man is executed, [it would] be a miserable spectacle, both against law, and of extreme inhumanity and cruelty, and can be no example to others." How can we punish someone, he asked, if that person has only the vaguest comprehension of what is being done to him and why?

Pakistan only started executing people again last year. Since then, more than 400 people have gone to the gallows, making it the third most prolific executioner in the world. Yet, regardless what people think about capital punishment, Pakistani and international law are both clear: the death penalty should not apply to mentally ill prisoners.

The courts have the power – in fact, the duty – to enforce this law, and prevent an execution under such sorry circumstances. Such a ruling is vital to the integrity of the Pakistan legal system, as it plays out on the grand stage of the Supreme Court, as much as it is to Imdad's family, as they weep in the wings.

Clive Stafford Smith is founder and director of Reprieve