int atomic(void) {
  int old;
  int val = 1;
  char valc = 1;
  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_fetch_and_nand(&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);
  old = __sync_nand_and_fetch(&valc, 5);
  __sync_val_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)
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    116c <atomic+0x6c>
jmp    113d <atomic+0x3d>
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    11a7 <atomic+0xa7>
jmp    1178 <atomic+0x78>
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    11e2 <atomic+0xe2>
jmp    11b3 <atomic+0xb3>
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    121d <atomic+0x11d>
jmp    11ee <atomic+0xee>
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    1284 <atomic+0x184>
jmp    1260 <atomic+0x160>
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    12b4 <atomic+0x1b4>
jmp    1290 <atomic+0x190>
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    12e4 <atomic+0x1e4>
jmp    12c0 <atomic+0x1c0>
mov    -0x60(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,%ecx
not    %ecx
mov    %ecx,-0x6c(%rbp)
or     $0xfffffff4,%ecx
lock cmpxchg %ecx,-0x8(%rbp)
sete   %cl
mov    %eax,-0x68(%rbp)
test   $0x1,%cl
mov    %eax,-0x64(%rbp)
jne    1319 <atomic+0x219>
jmp    12f0 <atomic+0x1f0>
mov    -0x68(%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    1369 <atomic+0x269>
jmp    1345 <atomic+0x245>
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    139e <atomic+0x29e>
jmp    137a <atomic+0x27a>
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    13d3 <atomic+0x2d3>
jmp    13af <atomic+0x2af>
mov    -0x72(%rbp),%al
xor    $0x5,%al
movsbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x73(%rbp)
mov    -0x73(%rbp),%al
mov    %al,%cl
not    %cl
mov    %cl,-0x75(%rbp)
or     $0xfa,%cl
lock cmpxchg %cl,-0x9(%rbp)
sete   %cl
mov    %al,-0x74(%rbp)
test   $0x1,%cl
mov    %al,-0x73(%rbp)
jne    140d <atomic+0x30d>
jmp    13e4 <atomic+0x2e4>
mov    -0x74(%rbp),%al
not    %al
or     $0xfa,%al
movsbl %al,%eax
mov    %eax,-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,-0x8(%rsp)
movb   $0x1,-0x9(%rsp)
movl   $0x1,-0x4(%rsp)
lock addl $0x1,-0x8(%rsp)
lock subb $0x2,-0x9(%rsp)
mov    -0x8(%rsp),%eax
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x4,%eax
mov    $0x3,%ecx
cmovl  %eax,%ecx
lock cmpxchg %ecx,-0x8(%rsp)
jne    1130 <atomic+0x30>
mov    -0x8(%rsp),%eax
nopw   0x0(%rax,%rax,1)
cmp    $0x5,%eax
mov    $0x4,%ecx
cmovge %eax,%ecx
lock cmpxchg %ecx,-0x8(%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,-0x8(%rsp)
mov    $0x7b8,%ecx
mov    $0x4,%eax
lock cmpxchg %ecx,-0x8(%rsp)
mov    $0x4,%eax
lock cmpxchg %ecx,-0x8(%rsp)
lock andl $0x9,-0x8(%rsp)
lock orl $0xa,-0x8(%rsp)
lock xorl $0xb,-0x8(%rsp)
mov    -0x8(%rsp),%eax
nopl   (%rax)
mov    %eax,%ecx
not    %ecx
or     $0xfffffff4,%ecx
lock cmpxchg %ecx,-0x8(%rsp)
jne    11e0 <atomic+0xe0>
lock addl $0x1,-0x8(%rsp)
lock subl $0x2,-0x8(%rsp)
lock andb $0x3,-0x9(%rsp)
lock orb $0x4,-0x9(%rsp)
lock xorb $0x5,-0x9(%rsp)
mov    -0x9(%rsp),%cl
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ecx,%edx
not    %dl
or     $0xfa,%dl
mov    %ecx,%eax
lock cmpxchg %dl,-0x9(%rsp)
mov    %eax,%ecx
jne    1220 <atomic+0x120>
xor    %edx,%edx
xor    %eax,%eax
lock cmpxchg %rdx,0x0
not    %cl
or     $0xfa,%cl
movsbl %cl,%eax
movl   $0x0,-0x8(%rsp)
mfence
ret

LLVM IR (-O0):
define i32 @atomic() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
store i32 1, i32* %2
store i8 1, i8* %3
store i32 1, i32* %4
store i32 0, i32* %5
%6 = atomicrmw add i32* %2, i32 1 seq_cst
store i32 %6, i32* %1
%7 = atomicrmw sub i8* %3, i8 2 seq_cst
%8 = sext i8 %7 to i32
store i32 %8, i32* %1
%9 = atomicrmw min i32* %2, i32 3 seq_cst
store i32 %9, i32* %1
%10 = atomicrmw max i32* %2, i32 4 seq_cst
store i32 %10, i32* %1
%11 = atomicrmw umin i32* %4, i32 5 seq_cst
store i32 %11, i32* %1
%12 = atomicrmw umax i32* %4, i32 6 seq_cst
store i32 %12, i32* %1
%13 = atomicrmw xchg i32* %2, i32 7 seq_cst
store i32 %13, i32* %1
%14 = cmpxchg i32* %2, i32 4, i32 1976 seq_cst seq_cst
%15 = extractvalue { i32, i1 } %14, 0
store i32 %15, i32* %1
%16 = cmpxchg i32* %2, i32 4, i32 1976 seq_cst seq_cst
%17 = extractvalue { i32, i1 } %16, 1
%18 = zext i1 %17 to i32
store i32 %18, i32* %1
%19 = atomicrmw and i32* %2, i32 9 seq_cst
store i32 %19, i32* %1
%20 = atomicrmw or i32* %2, i32 10 seq_cst
store i32 %20, i32* %1
%21 = atomicrmw xor i32* %2, i32 11 seq_cst
store i32 %21, i32* %1
%22 = atomicrmw nand 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 = atomicrmw nand i8* %3, i8 5 seq_cst
%37 = and i8 %36, 5
%38 = xor i8 %37, -1
%39 = sext i8 %38 to i32
store i32 %39, i32* %1
%40 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
%41 = extractvalue { i64, i1 } %40, 0
%42 = inttoptr i64 %41 to i8*
store atomic i32 0, i32* %2 release
fence seq_cst
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
define i32 @atomic() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
store i32 1, i32* %1
store i8 1, i8* %2
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = atomicrmw add i32* %1, i32 1 seq_cst
%7 = atomicrmw sub i8* %2, i8 2 seq_cst
%8 = atomicrmw min i32* %1, i32 3 seq_cst
%9 = atomicrmw max i32* %1, i32 4 seq_cst
%10 = atomicrmw umin i32* %3, i32 5 seq_cst
%11 = atomicrmw umax i32* %3, i32 6 seq_cst
%12 = atomicrmw xchg i32* %1, i32 7 seq_cst
%13 = cmpxchg i32* %1, i32 4, i32 1976 seq_cst seq_cst
%14 = cmpxchg i32* %1, i32 4, i32 1976 seq_cst seq_cst
%15 = atomicrmw and i32* %1, i32 9 seq_cst
%16 = atomicrmw or i32* %1, i32 10 seq_cst
%17 = atomicrmw xor i32* %1, i32 11 seq_cst
%18 = atomicrmw nand 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 = atomicrmw nand i8* %2, i8 5 seq_cst
%25 = and i8 %24, 5
%26 = xor i8 %25, -1
%27 = sext i8 %26 to i32
%28 = cmpxchg i64* null, i64 0, i64 0 seq_cst seq_cst
store atomic i32 0, i32* %1 release
fence seq_cst
ret i32 %27
}