The optimizer doesn't work that way. It assumes the code does not do UB and then removes branches that can never be taken if the code executes no UB.
That's different from "This is UB, and now I do ...".
That's different from "This is UB, and now I do ...".