Compilers recognize patterns in code and replace them with efficient alternatives, even when the code is obfuscated. This is done by translating code into an intermediate representation, allowing the compiler to treat different code patterns identically.