#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*/ printf(char *, short);
volatile char *build_xchg(short mov_size, int addr_size, int src_reg,
                          int mem_reg, int x86_64f, int lock_en,
                          volatile char *tgt_addr) {
  if (lock_en) {
    (*tgt_addr++) = 0xf0;
  }
  if (addr_size == 32) {
    (*tgt_addr++) = 0x67;
  }
  switch (mov_size) {
  case 1:
    (*(char *)tgt_addr) = 0x86;
    tgt_addr += 0x1;
    break;
  case 2:
    (*tgt_addr++) = 0x66;
  case 4:
    (*(char *)tgt_addr) = 0x87;
    tgt_addr += 0x1;
    break;
  case 8:
    if (x86_64f) {
      (*tgt_addr++) = (0x40 | 0x8 | 0x4);
    } else {
      (*tgt_addr++) = (0x40 | 0x8);
    }
    (*(char *)tgt_addr) = 0x87;
    tgt_addr += 0x1;
    break;
  default:
    printf("incorrect mov_size passed to xadd instruction :%d\n", mov_size);
  }
  (*tgt_addr++) = ((0x00) + (src_reg << 0x3) + mem_reg);
  return (tgt_addr);
}


x86 assembly (clang, -O0):
<build_xchg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    0x10(%rbp),%rdi
mov    %ax,-0x2(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8d,-0x14(%rbp)
mov    %r9d,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1150 <build_xchg+0x40>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0xf0,(%rax)
cmpl   $0x20,-0x8(%rbp)
jne    116c <build_xchg+0x5c>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0x67,(%rax)
movswl -0x2(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x7,%eax
ja     1223 <build_xchg+0x113>
mov    -0x20(%rbp),%rax
lea    0xe73(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x10(%rbp),%rax
movb   $0x86,(%rax)
mov    0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,0x10(%rbp)
jmp    1233 <build_xchg+0x123>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0x66,(%rax)
mov    0x10(%rbp),%rax
movb   $0x87,(%rax)
mov    0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,0x10(%rbp)
jmp    1233 <build_xchg+0x123>
cmpl   $0x0,-0x14(%rbp)
je     11f9 <build_xchg+0xe9>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0x4c,(%rax)
jmp    120b <build_xchg+0xfb>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0x48,(%rax)
mov    0x10(%rbp),%rax
movb   $0x87,(%rax)
mov    0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,0x10(%rbp)
jmp    1233 <build_xchg+0x123>
lea    0xdf6(%rip),%rdi
movswl -0x2(%rbp),%esi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
shl    $0x3,%eax
add    $0x0,%eax
add    -0x10(%rbp),%eax
mov    %al,%cl
mov    0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,0x10(%rbp)
mov    %cl,(%rax)
mov    0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_xchg>:
push   %r15
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    0x20(%rsp),%rbx
test   %r9d,%r9d
je     112c <build_xchg+0x1c>
movb   $0xf0,(%rbx)
add    $0x1,%rbx
cmp    $0x20,%esi
jne    1138 <build_xchg+0x28>
movb   $0x67,(%rbx)
add    $0x1,%rbx
movswl %di,%esi
lea    -0x1(%rsi),%eax
cmp    $0x7,%eax
ja     116c <build_xchg+0x5c>
lea    0xeb6(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movb   $0x86,(%rbx)
add    $0x1,%rbx
jmp    118f <build_xchg+0x7f>
movb   $0x66,(%rbx)
add    $0x1,%rbx
movb   $0x87,(%rbx)
add    $0x1,%rbx
jmp    118f <build_xchg+0x7f>
lea    0xead(%rip),%rdi
call   1030 <printf@plt>
jmp    118f <build_xchg+0x7f>
test   %r8d,%r8d
setne  %al
shl    $0x2,%al
or     $0x48,%al
mov    %al,(%rbx)
movb   $0x87,0x1(%rbx)
add    $0x2,%rbx
lea    (%r14,%r15,8),%eax
mov    %al,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [51 x i8] c"incorrect mov_size passed to xadd instruction :%d\0A\00"
define i8* @build_xchg(i16 signext %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i8* %6) {
%8 = alloca i16
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i16 %0, i16* %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i8* %6, i8** %14
%15 = load i32, i32* %13
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = load i8*, i8** %14
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %14
store volatile i8 -16, i8* %18
br label %20
20: ; preds = %17, %7
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, 32
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %14
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %14
store volatile i8 103, i8* %24
br label %26
26: ; preds = %23, %20
%27 = load i16, i16* %8
%28 = sext i16 %27 to i32
switch i32 %28, label %53 [
i32 1, label %29
i32 2, label %33
i32 4, label %36
i32 8, label %40
]
29: ; preds = %26
%30 = load i8*, i8** %14
store i8 -122, i8* %30
%31 = load i8*, i8** %14
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8* %32, i8** %14
br label %56
33: ; preds = %26
%34 = load i8*, i8** %14
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %14
store volatile i8 102, i8* %34
br label %36
36: ; preds = %26, %33
%37 = load i8*, i8** %14
store i8 -121, i8* %37
%38 = load i8*, i8** %14
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8* %39, i8** %14
br label %56
40: ; preds = %26
%41 = load i32, i32* %12
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8*, i8** %14
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %14
store volatile i8 76, i8* %44
br label %49
46: ; preds = %40
%47 = load i8*, i8** %14
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %14
store volatile i8 72, i8* %47
br label %49
49: ; preds = %46, %43
%50 = load i8*, i8** %14
store i8 -121, i8* %50
%51 = load i8*, i8** %14
%52 = getelementptr inbounds i8, i8* %51, i64 1
store i8* %52, i8** %14
br label %56
53: ; preds = %26
%54 = load i16, i16* %8
%55 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i16 signext %54)
br label %56
56: ; preds = %53, %49, %36, %29
%57 = load i32, i32* %10
%58 = shl i32 %57, 3
%59 = add nsw i32 0, %58
%60 = load i32, i32* %11
%61 = add nsw i32 %59, %60
%62 = trunc i32 %61 to i8
%63 = load i8*, i8** %14
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %14
store volatile i8 %62, i8* %63
%65 = load i8*, i8** %14
ret i8* %65
}

LLVM IR (-O2):
@.str = [51 x i8] c"incorrect mov_size passed to xadd instruction :%d\0A\00"
define i8* @build_xchg(i16 signext %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i8* %6) {
%8 = icmp eq i32 %5, 0
br i1 %8, label %11, label %9
9: ; preds = %7
%10 = getelementptr inbounds i8, i8* %6, i64 1
store volatile i8 -16, i8* %6
br label %11
11: ; preds = %9, %7
%12 = phi i8* [ %10, %9 ], [ %6, %7 ]
%13 = icmp eq i32 %1, 32
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %12, i64 1
store volatile i8 103, i8* %12
br label %16
16: ; preds = %14, %11
%17 = phi i8* [ %15, %14 ], [ %12, %11 ]
%18 = sext i16 %0 to i32
switch i32 %18, label %31 [
i32 1, label %19
i32 2, label %21
i32 4, label %23
i32 8, label %26
]
19: ; preds = %16
store i8 -122, i8* %17
%20 = getelementptr inbounds i8, i8* %17, i64 1
br label %33
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %17, i64 1
store volatile i8 102, i8* %17
br label %23
23: ; preds = %16, %21
%24 = phi i8* [ %17, %16 ], [ %22, %21 ]
store i8 -121, i8* %24
%25 = getelementptr inbounds i8, i8* %24, i64 1
br label %33
26: ; preds = %16
%27 = icmp eq i32 %4, 0
%28 = select i1 %27, i8 72, i8 76
store volatile i8 %28, i8* %17
%29 = getelementptr inbounds i8, i8* %17, i64 1
store i8 -121, i8* %29
%30 = getelementptr inbounds i8, i8* %17, i64 2
br label %33
31: ; preds = %16
%32 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i16 signext %0)
br label %33
33: ; preds = %31, %26, %23, %19
%34 = phi i8* [ %17, %31 ], [ %30, %26 ], [ %25, %23 ], [ %20, %19 ]
%35 = shl i32 %2, 3
%36 = add nsw i32 %35, %3
%37 = trunc i32 %36 to i8
%38 = getelementptr inbounds i8, i8* %34, i64 1
store volatile i8 %37, i8* %34
ret i8* %38
}