#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 */
typedef scalar_t__ uint32_t;
typedef int /*<<< orphan*/ tests;
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, scalar_t__,
                           scalar_t__, int, scalar_t__, scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int subtilis_arm_encode_lvl2_imm(int /*<<< orphan*/, scalar_t__ *,
                                 scalar_t__ *);
int prv_test_encode_lvl2_imm(void) {
  struct test {
    int32_t num;
    uint32_t encoded;
    uint32_t encoded2;
    bool ok;
  };
  size_t i;
  struct test tests[] = {
      {257, 1, 0xc01, true},
      {65535, 0xff, 0xcff, true},
      {0x1ffff, 0, 0, false},
  };
  uint32_t encoded;
  uint32_t encoded2;
  bool ok;
  printf("arm_core_encode_lvl2_imm");
  for (i = 0; i < sizeof(tests) / sizeof(struct test); i++) {
    encoded = 0;
    encoded2 = 0;
    ok = subtilis_arm_encode_lvl2_imm(tests[i].num, &encoded, &encoded2);
    if (ok != tests[i].ok || encoded != tests[i].encoded ||
        encoded2 != tests[i].encoded2) {
      fprintf(stderr, "Expected %d %u %u got %d %u %u\n", tests[i].ok,
              tests[i].encoded, tests[i].encoded2, ok, encoded, encoded2);
      goto fail;
    }
  }
  printf(": [OK]\n");
  return 0;
fail:
  printf(": [FAIL]\n");
  return 1;
}


x86 assembly (clang, -O0):
<prv_test_encode_lvl2_imm>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    -0x70(%rbp),%rdi
lea    0xeaa(%rip),%rsi
mov    $0x60,%edx
call   1050 <memcpy@plt>
lea    0xef9(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x3,-0x10(%rbp)
jae    128b <prv_test_encode_lvl2_imm+0x14b>
movq   $0x0,-0x78(%rbp)
movq   $0x0,-0x80(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x70(%rbp),%rax
shl    $0x5,%rcx
add    %rcx,%rax
mov    (%rax),%edi
lea    -0x78(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1060 <subtilis_arm_encode_lvl2_imm@plt>
mov    %eax,-0x84(%rbp)
mov    -0x84(%rbp),%eax
mov    -0x10(%rbp),%rdx
lea    -0x70(%rbp),%rcx
shl    $0x5,%rdx
add    %rdx,%rcx
cmp    0x18(%rcx),%eax
jne    120b <prv_test_encode_lvl2_imm+0xcb>
mov    -0x78(%rbp),%rax
mov    -0x10(%rbp),%rdx
lea    -0x70(%rbp),%rcx
shl    $0x5,%rdx
add    %rdx,%rcx
cmp    0x8(%rcx),%rax
jne    120b <prv_test_encode_lvl2_imm+0xcb>
mov    -0x80(%rbp),%rax
mov    -0x10(%rbp),%rdx
lea    -0x70(%rbp),%rcx
shl    $0x5,%rdx
add    %rdx,%rcx
cmp    0x10(%rcx),%rax
je     1275 <prv_test_encode_lvl2_imm+0x135>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rcx
lea    -0x70(%rbp),%rax
shl    $0x5,%rcx
add    %rcx,%rax
mov    0x18(%rax),%edx
mov    -0x10(%rbp),%rcx
lea    -0x70(%rbp),%rax
shl    $0x5,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rsi
lea    -0x70(%rbp),%rax
shl    $0x5,%rsi
add    %rsi,%rax
mov    0x10(%rax),%r8
mov    -0x84(%rbp),%r9d
mov    -0x78(%rbp),%r10
mov    -0x80(%rbp),%rax
lea    0xe17(%rip),%rsi
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <fprintf@plt>
jmp    12a3 <prv_test_encode_lvl2_imm+0x163>
jmp    127a <prv_test_encode_lvl2_imm+0x13a>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1174 <prv_test_encode_lvl2_imm+0x34>
lea    0xe07(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <prv_test_encode_lvl2_imm+0x176>
lea    0xdf7(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prv_test_encode_lvl2_imm>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xf13(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    $0x101,%edi
call   1060 <subtilis_arm_encode_lvl2_imm@plt>
mov    $0x1,%ebx
xor    %edx,%edx
cmp    $0x1,%eax
jne    123b <prv_test_encode_lvl2_imm+0xfb>
cmpq   $0x1,0x10(%rsp)
mov    $0x1,%ebp
jne    1242 <prv_test_encode_lvl2_imm+0x102>
cmpq   $0xc01,0x8(%rsp)
jne    1242 <prv_test_encode_lvl2_imm+0x102>
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    $0xffff,%edi
call   1060 <subtilis_arm_encode_lvl2_imm@plt>
mov    $0x1,%edx
cmp    $0x1,%eax
jne    1247 <prv_test_encode_lvl2_imm+0x107>
cmpq   $0xff,0x10(%rsp)
jne    1242 <prv_test_encode_lvl2_imm+0x102>
cmpq   $0xcff,0x8(%rsp)
mov    $0x1,%eax
jne    1247 <prv_test_encode_lvl2_imm+0x107>
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    $0x1ffff,%edi
call   1060 <subtilis_arm_encode_lvl2_imm@plt>
mov    $0x2,%edx
test   %eax,%eax
jne    1297 <prv_test_encode_lvl2_imm+0x157>
mov    0x10(%rsp),%rcx
xor    %eax,%eax
or     0x8(%rsp),%rcx
jne    1297 <prv_test_encode_lvl2_imm+0x157>
lea    0xe62(%rip),%rdi
xor    %ebx,%ebx
jmp    1289 <prv_test_encode_lvl2_imm+0x149>
mov    $0x1,%ebp
jmp    1247 <prv_test_encode_lvl2_imm+0x107>
mov    $0x1,%eax
mov    0x2d92(%rip),%rcx
mov    (%rcx),%edi
shl    $0x5,%rdx
lea    0xda5(%rip),%rsi
mov    0x8(%rdx,%rsi,1),%rcx
mov    0x10(%rdx,%rsi,1),%r8
lea    0xe0d(%rip),%rsi
mov    %ebp,%edx
mov    %eax,%r9d
push   0x8(%rsp)
push   0x18(%rsp)
call   1050 <fprintf@plt>
add    $0x10,%rsp
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
xor    %ebp,%ebp
jmp    1247 <prv_test_encode_lvl2_imm+0x107>

LLVM IR (-O0):
%struct.test = type { i32, i64, i64, i32 }
@__const.prv_test_encode_lvl2_imm.tests = [3 x %struct.test] [%struct.test { i32 257, i64 1, i64 3073, i32 1 }, %struct.test { i32 65535, i64 255, i64 3327, i32 1 }, %struct.test { i32 131071, i64 0, i64 0, i32 0 }]
@.str = [25 x i8] c"arm_core_encode_lvl2_imm\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Expected %d %u %u got %d %u %u\0A\00"
@.str.2 = [8 x i8] c": [OK]\0A\00"
@.str.3 = [10 x i8] c": [FAIL]\0A\00"
define i32 @prv_test_encode_lvl2_imm() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [3 x %struct.test]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = bitcast [3 x %struct.test]* %3 to i8*
%8 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %2
br label %9
9: ; preds = %57, %0
%10 = load i64, i64* %2
%11 = icmp ult i64 %10, 3
br i1 %11, label %12, label %60
12: ; preds = %9
store i64 0, i64* %4
store i64 0, i64* %5
%13 = load i64, i64* %2
%14 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* %3, i64 0, i64 %13
%15 = getelementptr inbounds %struct.test, %struct.test* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @subtilis_arm_encode_lvl2_imm(i32 %16, i64* %4, i64* %5)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = load i64, i64* %2
%20 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* %3, i64 0, i64 %19
%21 = getelementptr inbounds %struct.test, %struct.test* %20, i32 0, i32 3
%22 = load i32, i32* %21
%23 = icmp ne i32 %18, %22
br i1 %23, label %38, label %24
24: ; preds = %12
%25 = load i64, i64* %4
%26 = load i64, i64* %2
%27 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* %3, i64 0, i64 %26
%28 = getelementptr inbounds %struct.test, %struct.test* %27, i32 0, i32 1
%29 = load i64, i64* %28
%30 = icmp ne i64 %25, %29
br i1 %30, label %38, label %31
31: ; preds = %24
%32 = load i64, i64* %5
%33 = load i64, i64* %2
%34 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* %3, i64 0, i64 %33
%35 = getelementptr inbounds %struct.test, %struct.test* %34, i32 0, i32 2
%36 = load i64, i64* %35
%37 = icmp ne i64 %32, %36
br i1 %37, label %38, label %56
38: ; preds = %31, %24, %12
%39 = load i32, i32* @stderr
%40 = load i64, i64* %2
%41 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* %3, i64 0, i64 %40
%42 = getelementptr inbounds %struct.test, %struct.test* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = load i64, i64* %2
%45 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* %3, i64 0, i64 %44
%46 = getelementptr inbounds %struct.test, %struct.test* %45, i32 0, i32 1
%47 = load i64, i64* %46
%48 = load i64, i64* %2
%49 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* %3, i64 0, i64 %48
%50 = getelementptr inbounds %struct.test, %struct.test* %49, i32 0, i32 2
%51 = load i64, i64* %50
%52 = load i32, i32* %6
%53 = load i64, i64* %4
%54 = load i64, i64* %5
%55 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %43, i64 %47, i64 %51, i32 %52, i64 %53, i64 %54)
br label %62
56: ; preds = %31
br label %57
57: ; preds = %56
%58 = load i64, i64* %2
%59 = add i64 %58, 1
store i64 %59, i64* %2
br label %9
60: ; preds = %9
%61 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %64
62: ; preds = %38
%63 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %64
64: ; preds = %62, %60
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.test = type { i32, i64, i64, i32 }
@__const.prv_test_encode_lvl2_imm.tests = [3 x %struct.test] [%struct.test { i32 257, i64 1, i64 3073, i32 1 }, %struct.test { i32 65535, i64 255, i64 3327, i32 1 }, %struct.test { i32 131071, i64 0, i64 0, i32 0 }]
@.str = [25 x i8] c"arm_core_encode_lvl2_imm\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Expected %d %u %u got %d %u %u\0A\00"
@str = [7 x i8] c": [OK]\00"
@str.4 = [9 x i8] c": [FAIL]\00"
define i32 @prv_test_encode_lvl2_imm() {
%1 = alloca i64
%2 = alloca i64
%3 = bitcast i64* %1 to i8*
%4 = bitcast i64* %2 to i8*
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %1
store i64 0, i64* %2
%6 = call i32 @subtilis_arm_encode_lvl2_imm(i32 257, i64* %1, i64* %2)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i64, i64* %1
%10 = icmp eq i64 %9, 1
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 3073
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %26, label %14
14: ; preds = %38, %35, %29, %26, %8, %0
%15 = phi i64 [ 0, %8 ], [ 0, %0 ], [ 1, %26 ], [ 1, %29 ], [ 2, %35 ], [ 2, %38 ]
%16 = phi i32 [ 1, %8 ], [ %6, %0 ], [ %27, %26 ], [ 1, %29 ], [ %36, %35 ], [ 0, %38 ]
%17 = phi i32 [ 1, %8 ], [ 1, %0 ], [ 1, %26 ], [ 1, %29 ], [ 0, %35 ], [ 0, %38 ]
%18 = load i32, i32* @stderr
%19 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* @__const.prv_test_encode_lvl2_imm.tests, i64 0, i64 %15, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds [3 x %struct.test], [3 x %struct.test]* @__const.prv_test_encode_lvl2_imm.tests, i64 0, i64 %15, i32 2
%22 = load i64, i64* %21
%23 = load i64, i64* %1
%24 = load i64, i64* %2
%25 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %17, i64 %20, i64 %22, i32 %16, i64 %23, i64 %24)
br label %44
26: ; preds = %8
store i64 0, i64* %1
store i64 0, i64* %2
%27 = call i32 @subtilis_arm_encode_lvl2_imm(i32 65535, i64* %1, i64* %2)
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %14
29: ; preds = %26
%30 = load i64, i64* %1
%31 = icmp eq i64 %30, 255
%32 = load i64, i64* %2
%33 = icmp eq i64 %32, 3327
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %35, label %14
35: ; preds = %29
store i64 0, i64* %1
store i64 0, i64* %2
%36 = call i32 @subtilis_arm_encode_lvl2_imm(i32 131071, i64* %1, i64* %2)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %14
38: ; preds = %35
%39 = load i64, i64* %1
%40 = icmp eq i64 %39, 0
%41 = load i64, i64* %2
%42 = icmp eq i64 %41, 0
%43 = select i1 %40, i1 %42, i1 false
br i1 %43, label %44, label %14
44: ; preds = %38, %14
%45 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @str.4, i64 0, i64 0), %14 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0), %38 ]
%46 = phi i32 [ 1, %14 ], [ 0, %38 ]
%47 = call i32 @puts(i8* dereferenceable(1) %45)
ret i32 %46
}