Created
May 19, 2022 11:35
-
-
Save m1lkweed/8b5b2e3a56e1a9b7e130de46a0404d99 to your computer and use it in GitHub Desktop.
Preserve dead code during compilation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// works on all architectures and optimization levels in GCC and clang | |
// Dead code is optimized like running code and may be removed if the | |
// compiler can prove that no branches will ever reach it. | |
#define preserve_dead_code(body) ({ \ | |
__label__ label; \ | |
asm goto(""::::label); \ | |
if(0)label:{body} \ | |
&&label; \ | |
}) | |
int main(){ | |
preserve_dead_code( //will always be preserved | |
puts("Hello, World!"); | |
); | |
if(0){ | |
preserve_dead_code( //will be removed | |
puts("Lorem ipsum"); | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment