#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*/ fprintf(int /*<<< orphan*/, char *, short);
int /*<<< orphan*/ stderr;
volatile char *build_mov_immediate_to_register(short mov_size, int imm_value,
                                               int dest_reg,
                                               volatile char *tgt_addr) {
  if (mov_size == 2) {
    (*tgt_addr++) = 0x66;
  }
  switch (mov_size) {
  case 1:
    (*(short *)tgt_addr) = (imm_value << 8) | (0xB0 + dest_reg);
    tgt_addr += 0x2;
    break;
  case 4:
    (*(short *)tgt_addr) = (imm_value << 8) | (0xB8 + dest_reg);
    tgt_addr += 0x2;
    break;
  default:
    fprintf(stderr,
            "ERROR: Incorrect size (%d) passed to register to register move\n",
            mov_size);
    return (NULL);
  }
  return (tgt_addr);
}


x86 assembly (clang, -O0):
<build_mov_immediate_to_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0xa(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movswl -0xa(%rbp),%eax
cmp    $0x2,%eax
jne    1148 <build_mov_immediate_to_register+0x38>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x66,(%rax)
movswl -0xa(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x1,%eax
je     116e <build_mov_immediate_to_register+0x5e>
jmp    115d <build_mov_immediate_to_register+0x4d>
mov    -0x24(%rbp),%eax
sub    $0x4,%eax
je     119a <build_mov_immediate_to_register+0x8a>
jmp    11c6 <build_mov_immediate_to_register+0xb6>
mov    -0x10(%rbp),%eax
shl    $0x8,%eax
mov    -0x14(%rbp),%ecx
add    $0xb0,%ecx
or     %ecx,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    11ec <build_mov_immediate_to_register+0xdc>
mov    -0x10(%rbp),%eax
shl    $0x8,%eax
mov    -0x14(%rbp),%ecx
add    $0xb8,%ecx
or     %ecx,%eax
mov    %ax,%cx
mov    -0x20(%rbp),%rax
mov    %cx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
jmp    11ec <build_mov_immediate_to_register+0xdc>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
movswl -0xa(%rbp),%edx
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f4 <build_mov_immediate_to_register+0xe4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_mov_immediate_to_register>:
push   %rax
mov    %rcx,%rax
movswl %di,%ecx
cmp    $0x2,%di
jne    1122 <build_mov_immediate_to_register+0x12>
movb   $0x66,(%rax)
jmp    1133 <build_mov_immediate_to_register+0x23>
cmp    $0x1,%ecx
je     114e <build_mov_immediate_to_register+0x3e>
cmp    $0x4,%ecx
jne    1133 <build_mov_immediate_to_register+0x23>
mov    $0xb8,%ecx
jmp    1153 <build_mov_immediate_to_register+0x43>
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
lea    0xebd(%rip),%rsi
mov    %ecx,%edx
call   1030 <fprintf@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    $0xb0,%ecx
shl    $0x8,%esi
add    %edx,%ecx
or     %esi,%ecx
mov    %cx,(%rax)
add    $0x2,%rax
pop    %rcx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [64 x i8] c"ERROR: Incorrect size (%d) passed to register to register move\0A\00"
define i8* @build_mov_immediate_to_register(i16 signext %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i16 %0, i16* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%10 = load i16, i16* %6
%11 = sext i16 %10 to i32
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = load i8*, i8** %9
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %9
store volatile i8 102, i8* %14
br label %16
16: ; preds = %13, %4
%17 = load i16, i16* %6
%18 = sext i16 %17 to i32
switch i32 %18, label %41 [
i32 1, label %19
i32 4, label %30
]
19: ; preds = %16
%20 = load i32, i32* %7
%21 = shl i32 %20, 8
%22 = load i32, i32* %8
%23 = add nsw i32 176, %22
%24 = or i32 %21, %23
%25 = trunc i32 %24 to i16
%26 = load i8*, i8** %9
%27 = bitcast i8* %26 to i16*
store i16 %25, i16* %27
%28 = load i8*, i8** %9
%29 = getelementptr inbounds i8, i8* %28, i64 2
store i8* %29, i8** %9
br label %45
30: ; preds = %16
%31 = load i32, i32* %7
%32 = shl i32 %31, 8
%33 = load i32, i32* %8
%34 = add nsw i32 184, %33
%35 = or i32 %32, %34
%36 = trunc i32 %35 to i16
%37 = load i8*, i8** %9
%38 = bitcast i8* %37 to i16*
store i16 %36, i16* %38
%39 = load i8*, i8** %9
%40 = getelementptr inbounds i8, i8* %39, i64 2
store i8* %40, i8** %9
br label %45
41: ; preds = %16
%42 = load i32, i32* @stderr
%43 = load i16, i16* %6
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i16 signext %43)
store i8* null, i8** %5
br label %47
45: ; preds = %30, %19
%46 = load i8*, i8** %9
store i8* %46, i8** %5
br label %47
47: ; preds = %45, %41
%48 = load i8*, i8** %5
ret i8* %48
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [64 x i8] c"ERROR: Incorrect size (%d) passed to register to register move\0A\00"
define i8* @build_mov_immediate_to_register(i16 signext %0, i32 %1, i32 %2, i8* %3) {
%5 = icmp eq i16 %0, 2
br i1 %5, label %6, label %7
6: ; preds = %4
store volatile i8 102, i8* %3
br label %10
7: ; preds = %4
%8 = sext i16 %0 to i32
switch i32 %8, label %10 [
i32 1, label %13
i32 4, label %9
]
9: ; preds = %7
br label %13
10: ; preds = %6, %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i16 signext %0)
br label %21
13: ; preds = %7, %9
%14 = phi i32 [ 184, %9 ], [ 176, %7 ]
%15 = shl i32 %1, 8
%16 = add nsw i32 %14, %2
%17 = or i32 %16, %15
%18 = trunc i32 %17 to i16
%19 = bitcast i8* %3 to i16*
store i16 %18, i16* %19
%20 = getelementptr inbounds i8, i8* %3, i64 2
br label %21
21: ; preds = %13, %10
%22 = phi i8* [ null, %10 ], [ %20, %13 ]
ret i8* %22
}