#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
unsigned long cmpxchg_386_u16(void volatile *, unsigned long, unsigned long);
unsigned long cmpxchg_386_u32(void volatile *, unsigned long, unsigned long);
unsigned long cmpxchg_386_u8(void volatile *, unsigned long, unsigned long);
unsigned long cmpxchg_386(volatile void *ptr, unsigned long old,
                          unsigned long new, int size) {
  switch (size) {
  case 1:
    return cmpxchg_386_u8(ptr, old, new);
  case 2:
    return cmpxchg_386_u16(ptr, old, new);
  case 4:
    return cmpxchg_386_u32(ptr, old, new);
  }
  return old;
}


x86 assembly (clang, -O0):
<cmpxchg_386>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x1,%eax
je     117d <cmpxchg_386+0x4d>
jmp    115b <cmpxchg_386+0x2b>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     1197 <cmpxchg_386+0x67>
jmp    116c <cmpxchg_386+0x3c>
mov    -0x28(%rbp),%eax
sub    $0x4,%eax
je     11b1 <cmpxchg_386+0x81>
jmp    11cb <cmpxchg_386+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <cmpxchg_386_u8@plt>
mov    %rax,-0x8(%rbp)
jmp    11d3 <cmpxchg_386+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <cmpxchg_386_u16@plt>
mov    %rax,-0x8(%rbp)
jmp    11d3 <cmpxchg_386+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <cmpxchg_386_u32@plt>
mov    %rax,-0x8(%rbp)
jmp    11d3 <cmpxchg_386+0xa3>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmpxchg_386>:
cmp    $0x4,%ecx
je     1144 <cmpxchg_386+0x14>
cmp    $0x2,%ecx
je     1149 <cmpxchg_386+0x19>
cmp    $0x1,%ecx
jne    114e <cmpxchg_386+0x1e>
jmp    1040 <cmpxchg_386_u8@plt>
jmp    1050 <cmpxchg_386_u32@plt>
jmp    1030 <cmpxchg_386_u16@plt>
mov    %rsi,%rax
ret

LLVM IR (-O0):
define i64 @cmpxchg_386(i8* %0, i64 %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%10 = load i32, i32* %9
switch i32 %10, label %26 [
i32 1, label %11
i32 2, label %16
i32 4, label %21
]
11: ; preds = %4
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = load i64, i64* %8
%15 = call i64 @cmpxchg_386_u8(i8* %12, i64 %13, i64 %14)
store i64 %15, i64* %5
br label %28
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = load i64, i64* %7
%19 = load i64, i64* %8
%20 = call i64 @cmpxchg_386_u16(i8* %17, i64 %18, i64 %19)
store i64 %20, i64* %5
br label %28
21: ; preds = %4
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = load i64, i64* %8
%25 = call i64 @cmpxchg_386_u32(i8* %22, i64 %23, i64 %24)
store i64 %25, i64* %5
br label %28
26: ; preds = %4
%27 = load i64, i64* %7
store i64 %27, i64* %5
br label %28
28: ; preds = %26, %21, %16, %11
%29 = load i64, i64* %5
ret i64 %29
}

LLVM IR (-O2):
define i64 @cmpxchg_386(i8* %0, i64 %1, i64 %2, i32 %3) {
switch i32 %3, label %11 [
i32 1, label %5
i32 2, label %7
i32 4, label %9
]
5: ; preds = %4
%6 = tail call i64 @cmpxchg_386_u8(i8* %0, i64 %1, i64 %2)
br label %11
7: ; preds = %4
%8 = tail call i64 @cmpxchg_386_u16(i8* %0, i64 %1, i64 %2)
br label %11
9: ; preds = %4
%10 = tail call i64 @cmpxchg_386_u32(i8* %0, i64 %1, i64 %2)
br label %11
11: ; preds = %4, %9, %7, %5
%12 = phi i64 [ %10, %9 ], [ %8, %7 ], [ %6, %5 ], [ %1, %4 ]
ret i64 %12
}