Clang is able to convert the code to a jump table, though GCC can't.
https://c.godbolt.org/z/Wq65j4rrM
> What would it take to make the compiler optimize this code meaningfully?
To make GCC compile the code meaningfully, it suffices to add a bunch of else statements:
https://c.godbolt.org/z/z4KvxnbfG
In fact, GCC gets very clever at -O3.
(what matters is whether the compiler can turn the sequence of ifs into a switch statement, then both compilers have special logic to generate a lookup table.)