int atomic(void) {
  int old;
  int val = 1;
  char valc = 1;
  _Bool valb = 0;
  unsigned int uval = 1;
  int cmp = 0;
  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_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_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,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
sub    $0x4,%ecx
mov    %ecx,-0x20(%rbp)
mov    $0x3,%ecx
cmovl  %eax,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x1c(%rbp)
test   $0x1,%cl
mov    %eax,-0x18(%rbp)
jne    1170 <atomic+0x70>
jmp    1141 <atomic+0x41>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
sub    $0x5,%ecx
mov    %ecx,-0x2c(%rbp)
mov    $0x4,%ecx
cmovge %eax,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x28(%rbp)
test   $0x1,%cl
mov    %eax,-0x24(%rbp)
jne    11ab <atomic+0xab>
jmp    117c <atomic+0x7c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,%ecx
sub    $0x6,%ecx
mov    %ecx,-0x38(%rbp)
mov    $0x5,%ecx
cmovb  %eax,%ecx
lock cmpxchg %ecx,-0x10(%rbp)
sete   %cl
mov    %eax,-0x34(%rbp)
test   $0x1,%cl
mov    %eax,-0x30(%rbp)
jne    11e6 <atomic+0xe6>
jmp    11b7 <atomic+0xb7>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
sub    $0x7,%ecx
mov    %ecx,-0x44(%rbp)
mov    $0x6,%ecx
cmovae %eax,%ecx
lock cmpxchg %ecx,-0x10(%rbp)
sete   %cl
mov    %eax,-0x40(%rbp)
test   $0x1,%cl
mov    %eax,-0x3c(%rbp)
jne    1221 <atomic+0x121>
jmp    11f2 <atomic+0xf2>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    $0x7,%eax
xchg   %eax,-0x8(%rbp)
mov    %eax,-0x4(%rbp)
mov    $0x4,%eax
mov    %eax,-0x4c(%rbp)
mov    $0x7b8,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
mov    %eax,%edx
mov    -0x4c(%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,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,%ecx
and    $0x9,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x50(%rbp)
test   $0x1,%cl
mov    %eax,-0x48(%rbp)
jne    1288 <atomic+0x188>
jmp    1264 <atomic+0x164>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,%ecx
or     $0xa,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x58(%rbp)
test   $0x1,%cl
mov    %eax,-0x54(%rbp)
jne    12b8 <atomic+0x1b8>
jmp    1294 <atomic+0x194>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    %eax,%ecx
xor    $0xb,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x60(%rbp)
test   $0x1,%cl
mov    %eax,-0x5c(%rbp)
jne    12e8 <atomic+0x1e8>
jmp    12c4 <atomic+0x1c4>
mov    -0x60(%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,-0x61(%rbp)
mov    -0x61(%rbp),%al
mov    %al,%cl
and    $0x3,%cl
lock cmpxchg %cl,-0x9(%rbp)
sete   %cl
mov    %al,-0x62(%rbp)
test   $0x1,%cl
mov    %al,-0x61(%rbp)
jne    1338 <atomic+0x238>
jmp    1314 <atomic+0x214>
mov    -0x62(%rbp),%al
and    $0x3,%al
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x63(%rbp)
mov    -0x63(%rbp),%al
mov    %al,%cl
or     $0x4,%cl
lock cmpxchg %cl,-0x9(%rbp)
sete   %cl
mov    %al,-0x64(%rbp)
test   $0x1,%cl
mov    %al,-0x63(%rbp)
jne    136d <atomic+0x26d>
jmp    1349 <atomic+0x249>
mov    -0x64(%rbp),%al
or     $0x4,%al
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x65(%rbp)
mov    -0x65(%rbp),%al
mov    %al,%cl
xor    $0x5,%cl
lock cmpxchg %cl,-0x9(%rbp)
sete   %cl
mov    %al,-0x66(%rbp)
test   $0x1,%cl
mov    %al,-0x65(%rbp)
jne    13a2 <atomic+0x2a2>
jmp    137e <atomic+0x27e>
mov    -0x66(%rbp),%al
xor    $0x5,%al
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,-0x6c(%rbp)
mov    %eax,%ecx
mov    %rcx,%rax
lock cmpxchg %rcx,0x0
mov    -0x6c(%rbp),%eax
mov    $0x1,%cl
lock cmpxchg %cl,-0xa(%rbp)
sete   %cl
test   $0x1,%al
je     13dd <atomic+0x2dd>
movl   $0x2a,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rax
lock cmpxchg %rcx,0x0
movl   $0x0,-0x8(%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    $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
cs nopw 0x0(%rax,%rax,1)
nop
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
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
%7 = atomicrmw add i32* %2, i32 1 seq_cst
store i32 %7, i32* %1
%8 = atomicrmw sub i8* %3, i8 2 seq_cst
%9 = sext i8 %8 to i32
store i32 %9, i32* %1
%10 = atomicrmw min i32* %2, i32 3 seq_cst
store i32 %10, i32* %1
%11 = atomicrmw max i32* %2, i32 4 seq_cst
store i32 %11, i32* %1
%12 = atomicrmw umin i32* %5, i32 5 seq_cst
store i32 %12, i32* %1
%13 = atomicrmw umax i32* %5, i32 6 seq_cst
store i32 %13, i32* %1
%14 = atomicrmw xchg i32* %2, i32 7 seq_cst
store i32 %14, i32* %1
%15 = cmpxchg i32* %2, i32 4, i32 1976 seq_cst seq_cst
%16 = extractvalue { i32, i1 } %15, 0
store i32 %16, i32* %1
%17 = cmpxchg i32* %2, i32 4, i32 1976 seq_cst seq_cst
%18 = extractvalue { i32, i1 } %17, 1
%19 = zext i1 %18 to i32
store i32 %19, i32* %1
%20 = atomicrmw and i32* %2, i32 9 seq_cst
store i32 %20, i32* %1
%21 = atomicrmw or i32* %2, i32 10 seq_cst
store i32 %21, i32* %1
%22 = atomicrmw xor i32* %2, i32 11 seq_cst
store i32 %22, i32* %1
%23 = atomicrmw add i32* %2, i32 1 seq_cst
%24 = add i32 %23, 1
store i32 %24, i32* %1
%25 = atomicrmw sub i32* %2, i32 2 seq_cst
%26 = sub i32 %25, 2
store i32 %26, i32* %1
%27 = atomicrmw and i8* %3, i8 3 seq_cst
%28 = and i8 %27, 3
%29 = sext i8 %28 to i32
store i32 %29, i32* %1
%30 = atomicrmw or i8* %3, i8 4 seq_cst
%31 = or i8 %30, 4
%32 = sext i8 %31 to i32
store i32 %32, i32* %1
%33 = atomicrmw xor i8* %3, i8 5 seq_cst
%34 = xor i8 %33, 5
%35 = sext i8 %34 to i32
store i32 %35, i32* %1
%36 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
%37 = extractvalue { i64, i1 } %36, 0
%38 = inttoptr i64 %37 to i8*
%39 = cmpxchg i8* %4, i8 0, i8 1 seq_cst seq_cst
%40 = extractvalue { i8, i1 } %39, 0
%41 = trunc i8 %40 to i1
br i1 %41, label %42, label %43
42: ; preds = %0
store i32 42, i32* %1
br label %43
43: ; preds = %42, %0
%44 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
%45 = extractvalue { i64, i1 } %44, 1
store atomic i32 0, i32* %2 release
fence seq_cst
%46 = load i32, i32* %1
ret i32 %46
}

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 = cmpxchg i32* %1, i32 4, i32 1976 seq_cst seq_cst
%15 = cmpxchg i32* %1, i32 4, i32 1976 seq_cst seq_cst
%16 = atomicrmw and i32* %1, i32 9 seq_cst
%17 = atomicrmw or i32* %1, i32 10 seq_cst
%18 = atomicrmw xor i32* %1, i32 11 seq_cst
%19 = atomicrmw add i32* %1, i32 1 seq_cst
%20 = atomicrmw sub i32* %1, i32 2 seq_cst
%21 = atomicrmw and i8* %2, i8 3 seq_cst
%22 = atomicrmw or i8* %2, i8 4 seq_cst
%23 = atomicrmw xor i8* %2, i8 5 seq_cst
%24 = xor i8 %23, 5
%25 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
%26 = cmpxchg i8* %3, i8 0, i8 1 seq_cst seq_cst
%27 = extractvalue { i8, i1 } %26, 0
%28 = and i8 %27, 1
%29 = icmp eq i8 %28, 0
%30 = select i1 %29, i8 %24, i8 42
%31 = sext i8 %30 to i32
%32 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
store atomic i32 0, i32* %1 release
fence seq_cst
ret i32 %31
}