#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 */
int /*<<< orphan*/ __xchg_called_with_bad_pointer();
unsigned long xchg_u16(void volatile *, unsigned long);
unsigned long xchg_u32(void volatile *, unsigned long);
unsigned long xchg_u64(void volatile *, unsigned long);
unsigned long xchg_u8(void volatile *, unsigned long);
unsigned long __xchg(unsigned long x, volatile void *ptr, int size) {
  switch (size) {
  case 1:
    return xchg_u8(ptr, x);
  case 2:
    return xchg_u16(ptr, x);
  case 4:
    return xchg_u32(ptr, x);
  case 8:
    return xchg_u64(ptr, x);
  }
  __xchg_called_with_bad_pointer();
  return x;
}


x86 assembly (clang, -O0):
<__xchg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x7,%eax
ja     11e4 <__xchg+0x94>
mov    -0x28(%rbp),%rax
lea    0xe7d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <xchg_u8@plt>
mov    %rax,-0x8(%rbp)
jmp    11f3 <__xchg+0xa3>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <xchg_u16@plt>
mov    %rax,-0x8(%rbp)
jmp    11f3 <__xchg+0xa3>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <xchg_u32@plt>
mov    %rax,-0x8(%rbp)
jmp    11f3 <__xchg+0xa3>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <xchg_u64@plt>
mov    %rax,-0x8(%rbp)
jmp    11f3 <__xchg+0xa3>
mov    $0x0,%al
call   1050 <__xchg_called_with_bad_pointer@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__xchg>:
push   %rbx
mov    %rdi,%rbx
add    $0xffffffff,%edx
cmp    $0x7,%edx
ja     1184 <__xchg+0x34>
lea    0xe9d(%rip),%rax
movslq (%rax,%rdx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %rsi,%rdi
mov    %rbx,%rsi
pop    %rbx
jmp    1070 <xchg_u8@plt>
mov    %rsi,%rdi
mov    %rbx,%rsi
pop    %rbx
jmp    1060 <xchg_u16@plt>
xor    %eax,%eax
call   1050 <__xchg_called_with_bad_pointer@plt>
mov    %rbx,%rax
pop    %rbx
ret
mov    %rsi,%rdi
mov    %rbx,%rsi
pop    %rbx
jmp    1040 <xchg_u32@plt>
mov    %rsi,%rdi
mov    %rbx,%rsi
pop    %rbx
jmp    1030 <xchg_u64@plt>

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

LLVM IR (-O2):
define i64 @__xchg(i64 %0, i8* %1, i32 %2) {
switch i32 %2, label %12 [
i32 1, label %4
i32 2, label %6
i32 4, label %8
i32 8, label %10
]
4: ; preds = %3
%5 = tail call i64 @xchg_u8(i8* %1, i64 %0)
br label %14
6: ; preds = %3
%7 = tail call i64 @xchg_u16(i8* %1, i64 %0)
br label %14
8: ; preds = %3
%9 = tail call i64 @xchg_u32(i8* %1, i64 %0)
br label %14
10: ; preds = %3
%11 = tail call i64 @xchg_u64(i8* %1, i64 %0)
br label %14
12: ; preds = %3
%13 = tail call i32 (...) @__xchg_called_with_bad_pointer()
br label %14
14: ; preds = %12, %10, %8, %6, %4
%15 = phi i64 [ %0, %12 ], [ %11, %10 ], [ %9, %8 ], [ %7, %6 ], [ %5, %4 ]
ret i64 %15
}