#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_xadd(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:
    (*(short *)tgt_addr) = 0xc00f;
    tgt_addr += 0x2;
    break;
  case 2:
    (*tgt_addr++) = 0x66;
  case 4:
    (*(short *)tgt_addr) = 0xc10f;
    tgt_addr += 0x2;
    break;
  case 8:
    if (x86_64f) {
      (*tgt_addr++) = (0x40 | 0x8 | 0x4);
    } else {
      (*tgt_addr++) = (0x40 | 0x8);
    }
    (*(short *)tgt_addr) = 0xc10f;
    tgt_addr += 0x2;
    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_xadd>:
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_xadd+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_xadd+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     1229 <build_xadd+0x119>
mov    -0x20(%rbp),%rax
lea    0xe73(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x10(%rbp),%rax
movw   $0xc00f,(%rax)
mov    0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,0x10(%rbp)
jmp    1239 <build_xadd+0x129>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0x66,(%rax)
mov    0x10(%rbp),%rax
movw   $0xc10f,(%rax)
mov    0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,0x10(%rbp)
jmp    1239 <build_xadd+0x129>
cmpl   $0x0,-0x14(%rbp)
je     11fd <build_xadd+0xed>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0x4c,(%rax)
jmp    120f <build_xadd+0xff>
mov    0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x10(%rbp)
movb   $0x48,(%rax)
mov    0x10(%rbp),%rax
movw   $0xc10f,(%rax)
mov    0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,0x10(%rbp)
jmp    1239 <build_xadd+0x129>
lea    0xdf0(%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_xadd>:
push   %r15
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %edx,%r15d
mov    0x20(%rsp),%rbx
test   %r9d,%r9d
je     112c <build_xadd+0x1c>
movb   $0xf0,(%rbx)
add    $0x1,%rbx
cmp    $0x20,%esi
jne    1138 <build_xadd+0x28>
movb   $0x67,(%rbx)
add    $0x1,%rbx
movswl %di,%esi
lea    -0x1(%rsi),%eax
cmp    $0x7,%eax
ja     1170 <build_xadd+0x60>
lea    0xeb6(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movw   $0xc00f,(%rbx)
add    $0x2,%rbx
jmp    1195 <build_xadd+0x85>
movb   $0x66,(%rbx)
add    $0x1,%rbx
movw   $0xc10f,(%rbx)
add    $0x2,%rbx
jmp    1195 <build_xadd+0x85>
lea    0xea9(%rip),%rdi
call   1030 <printf@plt>
jmp    1195 <build_xadd+0x85>
test   %r8d,%r8d
setne  %al
shl    $0x2,%al
or     $0x48,%al
mov    %al,(%rbx)
movw   $0xc10f,0x1(%rbx)
add    $0x3,%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_xadd(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 %56 [
i32 1, label %29
i32 2, label %34
i32 4, label %37
i32 8, label %42
]
29: ; preds = %26
%30 = load i8*, i8** %14
%31 = bitcast i8* %30 to i16*
store i16 -16369, i16* %31
%32 = load i8*, i8** %14
%33 = getelementptr inbounds i8, i8* %32, i64 2
store i8* %33, i8** %14
br label %59
34: ; preds = %26
%35 = load i8*, i8** %14
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %14
store volatile i8 102, i8* %35
br label %37
37: ; preds = %26, %34
%38 = load i8*, i8** %14
%39 = bitcast i8* %38 to i16*
store i16 -16113, i16* %39
%40 = load i8*, i8** %14
%41 = getelementptr inbounds i8, i8* %40, i64 2
store i8* %41, i8** %14
br label %59
42: ; preds = %26
%43 = load i32, i32* %12
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i8*, i8** %14
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %14
store volatile i8 76, i8* %46
br label %51
48: ; preds = %42
%49 = load i8*, i8** %14
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %14
store volatile i8 72, i8* %49
br label %51
51: ; preds = %48, %45
%52 = load i8*, i8** %14
%53 = bitcast i8* %52 to i16*
store i16 -16113, i16* %53
%54 = load i8*, i8** %14
%55 = getelementptr inbounds i8, i8* %54, i64 2
store i8* %55, i8** %14
br label %59
56: ; preds = %26
%57 = load i16, i16* %8
%58 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i16 signext %57)
br label %59
59: ; preds = %56, %51, %37, %29
%60 = load i32, i32* %10
%61 = shl i32 %60, 3
%62 = add nsw i32 0, %61
%63 = load i32, i32* %11
%64 = add nsw i32 %62, %63
%65 = trunc i32 %64 to i8
%66 = load i8*, i8** %14
%67 = getelementptr inbounds i8, i8* %66, i32 1
store i8* %67, i8** %14
store volatile i8 %65, i8* %66
%68 = load i8*, i8** %14
ret i8* %68
}

LLVM IR (-O2):
@.str = [51 x i8] c"incorrect mov_size passed to xadd instruction :%d\0A\00"
define i8* @build_xadd(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 %34 [
i32 1, label %19
i32 2, label %22
i32 4, label %24
i32 8, label %28
]
19: ; preds = %16
%20 = bitcast i8* %17 to i16*
store i16 -16369, i16* %20
%21 = getelementptr inbounds i8, i8* %17, i64 2
br label %36
22: ; preds = %16
%23 = getelementptr inbounds i8, i8* %17, i64 1
store volatile i8 102, i8* %17
br label %24
24: ; preds = %16, %22
%25 = phi i8* [ %17, %16 ], [ %23, %22 ]
%26 = bitcast i8* %25 to i16*
store i16 -16113, i16* %26
%27 = getelementptr inbounds i8, i8* %25, i64 2
br label %36
28: ; preds = %16
%29 = icmp eq i32 %4, 0
%30 = select i1 %29, i8 72, i8 76
store volatile i8 %30, i8* %17
%31 = getelementptr inbounds i8, i8* %17, i64 1
%32 = bitcast i8* %31 to i16*
store i16 -16113, i16* %32
%33 = getelementptr inbounds i8, i8* %17, i64 3
br label %36
34: ; preds = %16
%35 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i16 signext %0)
br label %36
36: ; preds = %34, %28, %24, %19
%37 = phi i8* [ %17, %34 ], [ %33, %28 ], [ %27, %24 ], [ %21, %19 ]
%38 = shl i32 %2, 3
%39 = add nsw i32 %38, %3
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds i8, i8* %37, i64 1
store volatile i8 %40, i8* %37
ret i8* %41
}