I needed a lawyer for a misdemeanor charge (cop was having a bad day, the other cops that arrived on the scene seemed like they would have let me off with a warning but it was not their call). I was certainly guilty by the letter of the law and had no defense but the lawyer I was referred to from a friend of a friend negotiated with the prosecution and I plead guilty to a civil ordinance violation for disturbing the peace (basically a traffic ticket). He was able to do that because he knew what to bargain for based off of similar cases in my state.
However, if it doesn’t get worked out pre-trial, that negotiation process still happens in the court room. When I met my lawyer, he mentioned a recent case he handled where the defendant had committed murder. There was no question that he did it, but the lawyer was proud that he was able to convince the court that his defendant was not of sound mind, which was the difference between going untreated and spending twenty-to-life in a state prison or spending that time getting treatment in a mental ward. While the latter isn’t exactly a cushy place to be, it is certainly better than being mentally unstable in gen pop.
It's also not entirely easy to get found not fit to stand trial - it's not just you and your lawyer and a friendly psychiatrist - the prosecution has the right to call its own mental health professionals and they are required to have access to the defendant, and more.
Aside from that, the law must take into consideration one’s ability to reason because that is the method in which a jury finds facts.. based on reasonability. You wouldn’t convict someone who suffered from a mental handicap to a punishment that was equal to someone who did the same with full faculties, right?
You probably have "not of sound mind" and "temporary insanity" confused.
> Mr. Greer : I can get a lawyer any time I want. I don't need you for that. We're not sitting here for forty five minutes for a god damned referral.
> Michael Clayton : I don't know what Walter promised you but...
> Mr. Greer : A miracle worker. That's Walter on the phone twenty minutes ago. Direct quote, okay, "Hang tight, I'm sending you a miracle worker."
> Michael Clayton : Well he misspoke.
> Mr. Greer : About what? That you're the firms fixer? Or that you're any good at it?
[explodes in anger]
> Mr. Greer : The guy was RUNNING. In the STREET! You take that, you add the fog, you add the light, you add the... the angle. What the fuck is he doing running in the middle of the street at midnight? You answer me that, huh?
[Mrs. Greer throws a glass across the room, there's a long pause]
> Mr. Greer : What if someone had stolen the car? Huh? Happens all the time.
> Michael Clayton : Cops like hit and runs. They work them hard and they clear them fast. Right now there's a DCI unit pulling paint chips off a guard rail. Tomorrow they're going to be looking for the owner of a custom painted hand rubbed Jaguar XJ12. If the guy you hit, if he got a look at the plates? It won't even take that long.
[the phone rings]
> Michael Clayton : There's no play here. There's no angle. There's no champagne room. I'm not a miracle worker, I'm a janitor. The math on this is simple. The smaller the mess the easier it is for me to clean up.
Well, this is a pretty big. You don't want to serve more time than what is required, this is why we have lawyers.