https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87683
Bug ID: 87683 Summary: Inline asm input/output operand does not prevent compiler optimization Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: sebastian.hu...@embedded-brains.de Target Milestone: --- I use this macro since 2016 to prevent certain compiler optimizations: #define OBFUSCATE_VARIABLE(var) __asm__("" : "+r" (var)) This was suggested by: https://gcc.gnu.org/ml/gcc/2016-09/msg00115.html It seems that it doesn't prevent some compiler optimizations in combination with the non-null attribute. Consider the following test case: #include <stddef.h> #define OBFUSCATE_VARIABLE(var) __asm__("" : "+r" (var)) int posix_memalign(void **, size_t, size_t) __attribute__((__nonnull__(1))) __attribute__((__alloc_align__(2))) __attribute__((__alloc_size__(3))) __attribute__((__warn_unused_result__)); void a(void); void b(void); void c(void) { int s; void **p; p = 0; OBFUSCATE_VARIABLE(p); s = posix_memalign(p, 16, 16); if (s != 22) { a(); } else { b(); } } GCC 7, 8, 9 unconditionally calls a() with -O2 without any warnings: gcc -O2 -S -Wall -Wextra -pedantic test.c -o - .file "test.c" .text .p2align 4,,15 .globl c .type c, @function c: .LFB0: .cfi_startproc subq $8, %rsp .cfi_def_cfa_offset 16 movl $16, %edx movl $16, %esi xorl %edi, %edi call posix_memalign addq $8, %rsp .cfi_def_cfa_offset 8 jmp a .cfi_endproc .LFE0: .size c, .-c .ident "GCC: (SUSE Linux) 7.3.1 20180920 [gcc-7-branch revision 264438]" .section .note.GNU-stack,"",@progbits If I remove the inline asm, then I get a warning: test.c: In function ācā: test.c:19:4: warning: argument 1 null where non-null expected [-Wnonnull] s = posix_memalign(p, 16, 16); ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~