Skip to content

Instantly share code, notes, and snippets.

@m1lkweed
Created May 19, 2022 11:35
Show Gist options
  • Save m1lkweed/8b5b2e3a56e1a9b7e130de46a0404d99 to your computer and use it in GitHub Desktop.
Save m1lkweed/8b5b2e3a56e1a9b7e130de46a0404d99 to your computer and use it in GitHub Desktop.
Preserve dead code during compilation
// 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