It could work too. Either code is ok to me. The testscase is very rare and can be achieved only for a small program. So the code speed is not important. I favor a bit your code as it does not change the code presented in IRA and LRA which might be unified someday. So if you are ready to commit your patch before Aldy did it, please do it, Steven.
Steven, go right ahead. Let me know when you commit so I can close the PR. Thanks.