#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 */
struct jit_state {
  int dummy;
};
typedef scalar_t__ int64_t;

/* Variables and functions */
scalar_t__ INT32_MAX;
scalar_t__ INT32_MIN;
int /*<<< orphan*/ emit1(struct jit_state *, int);
int /*<<< orphan*/ emit8(struct jit_state *, scalar_t__);
int /*<<< orphan*/ emit_alu64_imm32(struct jit_state *, int, int /*<<< orphan*/,
                                    int, scalar_t__);
int /*<<< orphan*/ emit_basic_rex(struct jit_state *, int, int /*<<< orphan*/,
                                  int);
void emit_load_imm(struct jit_state *state, int dst, int64_t imm) {
  if (imm >= INT32_MIN && imm <= INT32_MAX) {
    emit_alu64_imm32(state, 0xc7, 0, dst, imm);
  } else {
    emit_basic_rex(state, 1, 0, dst);
    emit1(state, 0xb8 | (dst & 7));
    emit8(state, imm);
  }
}


x86 assembly (clang, -O0):
<emit_load_imm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
jl     1197 <emit_load_imm+0x57>
mov    -0x18(%rbp),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
jg     1197 <emit_load_imm+0x57>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    $0xc7,%esi
xor    %edx,%edx
call   1040 <emit_alu64_imm32@plt>
jmp    11cc <emit_load_imm+0x8c>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x1,%esi
xor    %edx,%edx
call   1060 <emit_basic_rex@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
and    $0x7,%esi
or     $0xb8,%esi
call   1030 <emit1@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <emit8@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<emit_load_imm>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e85(%rip),%rax
cmp    %rdx,(%rax)
jg     117c <emit_load_imm+0x3c>
mov    0x2e81(%rip),%rax
cmp    %rbx,(%rax)
jl     117c <emit_load_imm+0x3c>
mov    %r14,%rdi
mov    $0xc7,%esi
xor    %edx,%edx
mov    %ebp,%ecx
mov    %rbx,%r8
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <emit_alu64_imm32@plt>
mov    %r14,%rdi
mov    $0x1,%esi
xor    %edx,%edx
mov    %ebp,%ecx
call   1060 <emit_basic_rex@plt>
and    $0x7,%ebp
or     $0xb8,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <emit1@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <emit8@plt>

LLVM IR (-O0):
%struct.jit_state = type { i32 }
@INT32_MIN = global i64 0
@INT32_MAX = global i64 0
define void @emit_load_imm(%struct.jit_state* %0, i32 %1, i64 %2) {
%4 = alloca %struct.jit_state*
%5 = alloca i32
%6 = alloca i64
store %struct.jit_state* %0, %struct.jit_state** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
%7 = load i64, i64* %6
%8 = load i64, i64* @INT32_MIN
%9 = icmp sge i64 %7, %8
br i1 %9, label %10, label %19
10: ; preds = %3
%11 = load i64, i64* %6
%12 = load i64, i64* @INT32_MAX
%13 = icmp sle i64 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load %struct.jit_state*, %struct.jit_state** %4
%16 = load i32, i32* %5
%17 = load i64, i64* %6
%18 = call i32 @emit_alu64_imm32(%struct.jit_state* %15, i32 199, i32 0, i32 %16, i64 %17)
br label %31
19: ; preds = %10, %3
%20 = load %struct.jit_state*, %struct.jit_state** %4
%21 = load i32, i32* %5
%22 = call i32 @emit_basic_rex(%struct.jit_state* %20, i32 1, i32 0, i32 %21)
%23 = load %struct.jit_state*, %struct.jit_state** %4
%24 = load i32, i32* %5
%25 = and i32 %24, 7
%26 = or i32 184, %25
%27 = call i32 @emit1(%struct.jit_state* %23, i32 %26)
%28 = load %struct.jit_state*, %struct.jit_state** %4
%29 = load i64, i64* %6
%30 = call i32 @emit8(%struct.jit_state* %28, i64 %29)
br label %31
31: ; preds = %19, %14
ret void
}

LLVM IR (-O2):
%struct.jit_state = type { i32 }
@INT32_MIN = global i64 0
@INT32_MAX = global i64 0
define void @emit_load_imm(%struct.jit_state* %0, i32 %1, i64 %2) {
%4 = load i64, i64* @INT32_MIN
%5 = icmp sgt i64 %4, %2
%6 = load i64, i64* @INT32_MAX
%7 = icmp slt i64 %6, %2
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @emit_alu64_imm32(%struct.jit_state* %0, i32 199, i32 0, i32 %1, i64 %2)
br label %17
11: ; preds = %3
%12 = tail call i32 @emit_basic_rex(%struct.jit_state* %0, i32 1, i32 0, i32 %1)
%13 = and i32 %1, 7
%14 = or i32 %13, 184
%15 = tail call i32 @emit1(%struct.jit_state* %0, i32 %14)
%16 = tail call i32 @emit8(%struct.jit_state* %0, i64 %2)
br label %17
17: ; preds = %11, %9
ret void
}