Why not both? Madness and blasphemy at the same time?
In my understanding of traditional Islamic law, people cannot be held accountable for their actions if their considered out their right mind. Blasphemy is generally regarded as a serious offence under most understandings of Shari'a law, but of course this can apply only to people who are capable of acting deliberately, which would not be case if the blasphemer is legally incapacitated by effect of his madness. Since Shari'a often takes account of intentions, blasphemy as such might not apply as a mentally incapacitated person would not be
intentionally blasphemous.
I cannot find the actual sources, but if I remember correctly, there are some cases of people who declared themsleves atheists and were
interned as evidently mentally infirm, rather than punished as blasphemers.
Of course, in such an outlandish case it would get political. The mental illness argument would be used in order to avoid a revolt and replace the Sultan smoothly (even if he is actually perfectly able to reason) while if the court feels a more forceful action is needed, the blasphemy line would be followed.