int atomic(void) {
  int old;
  int val = 1;
  char valc = 1;
  _Bool valb = 0;
  unsigned int uval = 1;
  int cmp = 0;
  int *ptrval;
  old = __sync_fetch_and_add(&val, 1);
  old = __sync_fetch_and_sub(&valc, 2);
  old = __sync_fetch_and_min(&val, 3);
  old = __sync_fetch_and_max(&val, 4);
  old = __sync_fetch_and_umin(&uval, 5u);
  old = __sync_fetch_and_umax(&uval, 6u);
  old = __sync_lock_test_and_set(&val, 7);
  old = __sync_swap(&val, 8);
  old = __sync_val_compare_and_swap(&val, 4, 1976);
  old = __sync_bool_compare_and_swap(&val, 4, 1976);
  old = __sync_fetch_and_and(&val, 0x9);
  old = __sync_fetch_and_or(&val, 0xa);
  old = __sync_fetch_and_xor(&val, 0xb);
  old = __sync_add_and_fetch(&val, 1);
  old = __sync_sub_and_fetch(&val, 2);
  old = __sync_and_and_fetch(&valc, 3);
  old = __sync_or_and_fetch(&valc, 4);
  old = __sync_xor_and_fetch(&valc, 5);
  __sync_val_compare_and_swap((void **)0, (void *)0, (void *)0);
  if (__sync_val_compare_and_swap(&valb, 0, 1)) {
    old = 42;
  }
  __sync_bool_compare_and_swap((void **)0, (void *)0, (void *)0);
  __sync_lock_release(&val);
  __sync_lock_release(&ptrval);
  __sync_synchronize();
  return old;
}


x86 assembly (clang, -O0):
<atomic>:
push   %rbp
mov    %rsp,%rbp
movl   $0x1,-0x8(%rbp)
movb   $0x1,-0x9(%rbp)
movb   $0x0,-0xa(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x1,%eax
lock xadd %eax,-0x8(%rbp)
mov    %eax,-0x4(%rbp)
mov    $0xfe,%al
lock xadd %al,-0x9(%rbp)
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
sub    $0x4,%ecx
mov    %ecx,-0x2c(%rbp)
mov    $0x3,%ecx
cmovl  %eax,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x28(%rbp)
test   $0x1,%cl
mov    %eax,-0x24(%rbp)
jne    1170 <atomic+0x70>
jmp    1141 <atomic+0x41>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,%ecx
sub    $0x5,%ecx
mov    %ecx,-0x38(%rbp)
mov    $0x4,%ecx
cmovge %eax,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x34(%rbp)
test   $0x1,%cl
mov    %eax,-0x30(%rbp)
jne    11ab <atomic+0xab>
jmp    117c <atomic+0x7c>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
sub    $0x6,%ecx
mov    %ecx,-0x44(%rbp)
mov    $0x5,%ecx
cmovb  %eax,%ecx
lock cmpxchg %ecx,-0x10(%rbp)
sete   %cl
mov    %eax,-0x40(%rbp)
test   $0x1,%cl
mov    %eax,-0x3c(%rbp)
jne    11e6 <atomic+0xe6>
jmp    11b7 <atomic+0xb7>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,%ecx
sub    $0x7,%ecx
mov    %ecx,-0x50(%rbp)
mov    $0x6,%ecx
cmovae %eax,%ecx
lock cmpxchg %ecx,-0x10(%rbp)
sete   %cl
mov    %eax,-0x4c(%rbp)
test   $0x1,%cl
mov    %eax,-0x48(%rbp)
jne    1221 <atomic+0x121>
jmp    11f2 <atomic+0xf2>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    $0x7,%eax
xchg   %eax,-0x8(%rbp)
mov    %eax,-0x4(%rbp)
mov    $0x8,%eax
xchg   %eax,-0x8(%rbp)
mov    %eax,-0x4(%rbp)
mov    $0x4,%eax
mov    %eax,-0x58(%rbp)
mov    $0x7b8,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
mov    %eax,%edx
mov    -0x58(%rbp),%eax
mov    %edx,-0x4(%rbp)
lock cmpxchg %ecx,-0x8(%rbp)
mov    %eax,%ecx
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,%ecx
and    $0x9,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x5c(%rbp)
test   $0x1,%cl
mov    %eax,-0x54(%rbp)
jne    1293 <atomic+0x193>
jmp    126f <atomic+0x16f>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,%ecx
or     $0xa,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x64(%rbp)
test   $0x1,%cl
mov    %eax,-0x60(%rbp)
jne    12c3 <atomic+0x1c3>
jmp    129f <atomic+0x19f>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%eax
mov    %eax,%ecx
xor    $0xb,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x6c(%rbp)
test   $0x1,%cl
mov    %eax,-0x68(%rbp)
jne    12f3 <atomic+0x1f3>
jmp    12cf <atomic+0x1cf>
mov    -0x6c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    $0x1,%eax
lock xadd %eax,-0x8(%rbp)
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    $0xfffffffe,%eax
lock xadd %eax,-0x8(%rbp)
sub    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x6d(%rbp)
mov    -0x6d(%rbp),%al
mov    %al,%cl
and    $0x3,%cl
lock cmpxchg %cl,-0x9(%rbp)
sete   %cl
mov    %al,-0x6e(%rbp)
test   $0x1,%cl
mov    %al,-0x6d(%rbp)
jne    1343 <atomic+0x243>
jmp    131f <atomic+0x21f>
mov    -0x6e(%rbp),%al
and    $0x3,%al
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x6f(%rbp)
mov    -0x6f(%rbp),%al
mov    %al,%cl
or     $0x4,%cl
lock cmpxchg %cl,-0x9(%rbp)
sete   %cl
mov    %al,-0x70(%rbp)
test   $0x1,%cl
mov    %al,-0x6f(%rbp)
jne    1378 <atomic+0x278>
jmp    1354 <atomic+0x254>
mov    -0x70(%rbp),%al
or     $0x4,%al
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
mov    %al,%cl
xor    $0x5,%cl
lock cmpxchg %cl,-0x9(%rbp)
sete   %cl
mov    %al,-0x72(%rbp)
test   $0x1,%cl
mov    %al,-0x71(%rbp)
jne    13ad <atomic+0x2ad>
jmp    1389 <atomic+0x289>
mov    -0x72(%rbp),%al
xor    $0x5,%al
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,-0x78(%rbp)
mov    %eax,%ecx
mov    %rcx,%rax
lock cmpxchg %rcx,0x0
mov    -0x78(%rbp),%eax
mov    $0x1,%cl
lock cmpxchg %cl,-0xa(%rbp)
sete   %cl
test   $0x1,%al
je     13e8 <atomic+0x2e8>
movl   $0x2a,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rax
lock cmpxchg %rcx,0x0
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
mfence
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<atomic>:
movl   $0x1,-0xc(%rsp)
movb   $0x1,-0xd(%rsp)
movb   $0x0,-0x5(%rsp)
movl   $0x1,-0x4(%rsp)
lock addl $0x1,-0xc(%rsp)
lock subb $0x2,-0xd(%rsp)
mov    -0xc(%rsp),%eax
nopw   0x0(%rax,%rax,1)
cmp    $0x4,%eax
mov    $0x3,%ecx
cmovl  %eax,%ecx
lock cmpxchg %ecx,-0xc(%rsp)
jne    1130 <atomic+0x30>
mov    -0xc(%rsp),%eax
nopw   0x0(%rax,%rax,1)
cmp    $0x5,%eax
mov    $0x4,%ecx
cmovge %eax,%ecx
lock cmpxchg %ecx,-0xc(%rsp)
jne    1150 <atomic+0x50>
mov    -0x4(%rsp),%eax
nopw   0x0(%rax,%rax,1)
cmp    $0x6,%eax
mov    $0x5,%ecx
cmovb  %eax,%ecx
lock cmpxchg %ecx,-0x4(%rsp)
jne    1170 <atomic+0x70>
mov    -0x4(%rsp),%eax
nopw   0x0(%rax,%rax,1)
cmp    $0x7,%eax
mov    $0x6,%ecx
cmovae %eax,%ecx
lock cmpxchg %ecx,-0x4(%rsp)
jne    1190 <atomic+0x90>
mov    $0x7,%eax
xchg   %eax,-0xc(%rsp)
mov    $0x8,%eax
xchg   %eax,-0xc(%rsp)
mov    $0x7b8,%ecx
mov    $0x4,%eax
lock cmpxchg %ecx,-0xc(%rsp)
mov    $0x4,%eax
lock cmpxchg %ecx,-0xc(%rsp)
lock andl $0x9,-0xc(%rsp)
lock orl $0xa,-0xc(%rsp)
lock xorl $0xb,-0xc(%rsp)
lock addl $0x1,-0xc(%rsp)
lock subl $0x2,-0xc(%rsp)
lock andb $0x3,-0xd(%rsp)
lock orb $0x4,-0xd(%rsp)
mov    -0xd(%rsp),%cl
xchg   %ax,%ax
mov    %ecx,%edx
xor    $0x5,%dl
mov    %ecx,%eax
lock cmpxchg %dl,-0xd(%rsp)
mov    %eax,%ecx
jne    1200 <atomic+0x100>
xor    %esi,%esi
xor    %eax,%eax
lock cmpxchg %rsi,0x0
mov    $0x1,%dl
xor    %eax,%eax
lock cmpxchg %dl,-0x5(%rsp)
xor    $0x5,%cl
test   $0x1,%al
movzbl %cl,%eax
mov    $0x2a,%ecx
cmove  %eax,%ecx
xor    %eax,%eax
lock cmpxchg %rsi,0x0
movsbl %cl,%eax
movl   $0x0,-0xc(%rsp)
mfence
ret

LLVM IR (-O0):
define i32 @atomic() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 1, i32* %2
store i8 1, i8* %3
store i8 0, i8* %4
store i32 1, i32* %5
store i32 0, i32* %6
%8 = atomicrmw add i32* %2, i32 1 seq_cst
store i32 %8, i32* %1
%9 = atomicrmw sub i8* %3, i8 2 seq_cst
%10 = sext i8 %9 to i32
store i32 %10, i32* %1
%11 = atomicrmw min i32* %2, i32 3 seq_cst
store i32 %11, i32* %1
%12 = atomicrmw max i32* %2, i32 4 seq_cst
store i32 %12, i32* %1
%13 = atomicrmw umin i32* %5, i32 5 seq_cst
store i32 %13, i32* %1
%14 = atomicrmw umax i32* %5, i32 6 seq_cst
store i32 %14, i32* %1
%15 = atomicrmw xchg i32* %2, i32 7 seq_cst
store i32 %15, i32* %1
%16 = atomicrmw xchg i32* %2, i32 8 seq_cst
store i32 %16, i32* %1
%17 = cmpxchg i32* %2, i32 4, i32 1976 seq_cst seq_cst
%18 = extractvalue { i32, i1 } %17, 0
store i32 %18, i32* %1
%19 = cmpxchg i32* %2, i32 4, i32 1976 seq_cst seq_cst
%20 = extractvalue { i32, i1 } %19, 1
%21 = zext i1 %20 to i32
store i32 %21, i32* %1
%22 = atomicrmw and i32* %2, i32 9 seq_cst
store i32 %22, i32* %1
%23 = atomicrmw or i32* %2, i32 10 seq_cst
store i32 %23, i32* %1
%24 = atomicrmw xor i32* %2, i32 11 seq_cst
store i32 %24, i32* %1
%25 = atomicrmw add i32* %2, i32 1 seq_cst
%26 = add i32 %25, 1
store i32 %26, i32* %1
%27 = atomicrmw sub i32* %2, i32 2 seq_cst
%28 = sub i32 %27, 2
store i32 %28, i32* %1
%29 = atomicrmw and i8* %3, i8 3 seq_cst
%30 = and i8 %29, 3
%31 = sext i8 %30 to i32
store i32 %31, i32* %1
%32 = atomicrmw or i8* %3, i8 4 seq_cst
%33 = or i8 %32, 4
%34 = sext i8 %33 to i32
store i32 %34, i32* %1
%35 = atomicrmw xor i8* %3, i8 5 seq_cst
%36 = xor i8 %35, 5
%37 = sext i8 %36 to i32
store i32 %37, i32* %1
%38 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
%39 = extractvalue { i64, i1 } %38, 0
%40 = inttoptr i64 %39 to i8*
%41 = cmpxchg i8* %4, i8 0, i8 1 seq_cst seq_cst
%42 = extractvalue { i8, i1 } %41, 0
%43 = trunc i8 %42 to i1
br i1 %43, label %44, label %45
44: ; preds = %0
store i32 42, i32* %1
br label %45
45: ; preds = %44, %0
%46 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
%47 = extractvalue { i64, i1 } %46, 1
store atomic i32 0, i32* %2 release
%48 = bitcast i32** %7 to i64*
store atomic i64 0, i64* %48 release
fence seq_cst
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
define i32 @atomic() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i8
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
store i32 1, i32* %1
store i8 1, i8* %2
store i8 0, i8* %3
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = atomicrmw add i32* %1, i32 1 seq_cst
%8 = atomicrmw sub i8* %2, i8 2 seq_cst
%9 = atomicrmw min i32* %1, i32 3 seq_cst
%10 = atomicrmw max i32* %1, i32 4 seq_cst
%11 = atomicrmw umin i32* %4, i32 5 seq_cst
%12 = atomicrmw umax i32* %4, i32 6 seq_cst
%13 = atomicrmw xchg i32* %1, i32 7 seq_cst
%14 = atomicrmw xchg i32* %1, i32 8 seq_cst
%15 = cmpxchg i32* %1, i32 4, i32 1976 seq_cst seq_cst
%16 = cmpxchg i32* %1, i32 4, i32 1976 seq_cst seq_cst
%17 = atomicrmw and i32* %1, i32 9 seq_cst
%18 = atomicrmw or i32* %1, i32 10 seq_cst
%19 = atomicrmw xor i32* %1, i32 11 seq_cst
%20 = atomicrmw add i32* %1, i32 1 seq_cst
%21 = atomicrmw sub i32* %1, i32 2 seq_cst
%22 = atomicrmw and i8* %2, i8 3 seq_cst
%23 = atomicrmw or i8* %2, i8 4 seq_cst
%24 = atomicrmw xor i8* %2, i8 5 seq_cst
%25 = xor i8 %24, 5
%26 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
%27 = cmpxchg i8* %3, i8 0, i8 1 seq_cst seq_cst
%28 = extractvalue { i8, i1 } %27, 0
%29 = and i8 %28, 1
%30 = icmp eq i8 %29, 0
%31 = select i1 %30, i8 %25, i8 42
%32 = sext i8 %31 to i32
%33 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
store atomic i32 0, i32* %1 release
fence seq_cst
ret i32 %32
}