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

/* Variables and functions */
int /*<<< orphan*/ copy_boot_image(uintptr_t, int, int);
int image_read_u32(int, int);
int image_read_u8(int, int);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ reg_write(int, int, int);
uintptr_t load_boot_image(void) {
  uintptr_t dest_addr;
  uint32_t drd_cfg;
  uint8_t clk_div;
  reg_write(0xfb000000, 0x78, 0x0000000b);
  if (image_read_u32(0x80000000, 0x000) != 0xaa550750) {
    panic("Bad image magic0 value");
  }
  if (image_read_u32(0x80000000, 0x004) != 0x424f4f54) {
    panic("Bad image magic1 value");
  }
  clk_div = image_read_u8(0x80000000, 0x10c);
  if (clk_div != 0) {
    reg_write(0xfb000000, 0x00, image_read_u32(0x80000000, 0x108));
    reg_write(0xf0801000, 0x58, clk_div << 6);
  }
  dest_addr = image_read_u32(0x80000000, 0x140);
  if (dest_addr == 0) {
    return 0x80000000 + 0x200;
  }
  copy_boot_image(dest_addr, 0x80000000,
                  image_read_u32(0x80000000, 0x144) + 0x200);
  return dest_addr + 0x200;
}


x86 assembly (clang, -O0):
<load_boot_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0xfb000000,%edi
mov    $0x78,%esi
mov    $0xb,%edx
call   1060 <reg_write@plt>
mov    $0x80000000,%edi
xor    %esi,%esi
call   1040 <image_read_u32@plt>
cmp    $0xaa550750,%eax
je     118f <load_boot_image+0x3f>
lea    0xe76(%rip),%rdi
call   1050 <panic@plt>
mov    $0x80000000,%edi
mov    $0x4,%esi
call   1040 <image_read_u32@plt>
cmp    $0x424f4f54,%eax
je     11b5 <load_boot_image+0x65>
lea    0xe67(%rip),%rdi
call   1050 <panic@plt>
mov    $0x80000000,%edi
mov    $0x10c,%esi
call   1030 <image_read_u8@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1203 <load_boot_image+0xb3>
mov    $0x80000000,%edi
mov    $0x108,%esi
call   1040 <image_read_u32@plt>
mov    %eax,%edx
mov    $0xfb000000,%edi
xor    %esi,%esi
call   1060 <reg_write@plt>
mov    -0x18(%rbp),%edx
shl    $0x6,%edx
mov    $0xf0801000,%edi
mov    $0x58,%esi
call   1060 <reg_write@plt>
mov    $0x80000000,%edi
mov    $0x140,%esi
call   1040 <image_read_u32@plt>
cltq
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1231 <load_boot_image+0xe1>
mov    $0x80000200,%eax
mov    %rax,-0x8(%rbp)
jmp    126c <load_boot_image+0x11c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x80000000,%edi
mov    $0x144,%esi
call   1040 <image_read_u32@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
add    $0x200,%edx
mov    $0x80000000,%esi
call   1070 <copy_boot_image@plt>
mov    -0x10(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_boot_image>:
push   %rbx
mov    $0xfb000000,%edi
mov    $0x78,%esi
mov    $0xb,%edx
call   1060 <reg_write@plt>
mov    $0x80000000,%edi
xor    %esi,%esi
call   1040 <image_read_u32@plt>
cmp    $0xaa550750,%eax
je     1184 <load_boot_image+0x34>
lea    0xe81(%rip),%rdi
call   1050 <panic@plt>
mov    $0x80000000,%edi
mov    $0x4,%esi
call   1040 <image_read_u32@plt>
cmp    $0x424f4f54,%eax
je     11a6 <load_boot_image+0x56>
lea    0xe76(%rip),%rdi
call   1050 <panic@plt>
mov    $0x80000000,%edi
mov    $0x10c,%esi
call   1030 <image_read_u8@plt>
test   %eax,%eax
je     11ec <load_boot_image+0x9c>
mov    %eax,%ebx
mov    $0x80000000,%edi
mov    $0x108,%esi
call   1040 <image_read_u32@plt>
mov    $0xfb000000,%edi
xor    %esi,%esi
mov    %eax,%edx
call   1060 <reg_write@plt>
shl    $0x6,%ebx
mov    $0xf0801000,%edi
mov    $0x58,%esi
mov    %ebx,%edx
call   1060 <reg_write@plt>
mov    $0x80000000,%edi
mov    $0x140,%esi
call   1040 <image_read_u32@plt>
test   %eax,%eax
je     122d <load_boot_image+0xdd>
movslq %eax,%rbx
mov    $0x80000000,%edi
mov    $0x144,%esi
call   1040 <image_read_u32@plt>
lea    0x200(%rax),%edx
mov    %rbx,%rdi
mov    $0x80000000,%esi
call   1070 <copy_boot_image@plt>
add    $0x200,%rbx
jmp    1232 <load_boot_image+0xe2>
mov    $0x80000200,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Bad image magic0 value\00"
@.str.1 = [23 x i8] c"Bad image magic1 value\00"
define i64 @load_boot_image() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @reg_write(i32 -83886080, i32 120, i32 11)
%6 = call i32 @image_read_u32(i32 -2147483648, i32 0)
%7 = icmp ne i32 %6, -1437268144
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @panic(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = call i32 @image_read_u32(i32 -2147483648, i32 4)
%12 = icmp ne i32 %11, 1112493908
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @panic(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = call i32 @image_read_u8(i32 -2147483648, i32 268)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = call i32 @image_read_u32(i32 -2147483648, i32 264)
%21 = call i32 @reg_write(i32 -83886080, i32 0, i32 %20)
%22 = load i32, i32* %4
%23 = shl i32 %22, 6
%24 = call i32 @reg_write(i32 -260042752, i32 88, i32 %23)
br label %25
25: ; preds = %19, %15
%26 = call i32 @image_read_u32(i32 -2147483648, i32 320)
%27 = sext i32 %26 to i64
store i64 %27, i64* %2
%28 = load i64, i64* %2
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i64 2147484160, i64* %1
br label %38
31: ; preds = %25
%32 = load i64, i64* %2
%33 = call i32 @image_read_u32(i32 -2147483648, i32 324)
%34 = add nsw i32 %33, 512
%35 = call i32 @copy_boot_image(i64 %32, i32 -2147483648, i32 %34)
%36 = load i64, i64* %2
%37 = add i64 %36, 512
store i64 %37, i64* %1
br label %38
38: ; preds = %31, %30
%39 = load i64, i64* %1
ret i64 %39
}

LLVM IR (-O2):
@.str = [23 x i8] c"Bad image magic0 value\00"
@.str.1 = [23 x i8] c"Bad image magic1 value\00"
define i64 @load_boot_image() {
%1 = tail call i32 @reg_write(i32 -83886080, i32 120, i32 11)
%2 = tail call i32 @image_read_u32(i32 -2147483648, i32 0)
%3 = icmp eq i32 %2, -1437268144
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 @panic(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = tail call i32 @image_read_u32(i32 -2147483648, i32 4)
%8 = icmp eq i32 %7, 1112493908
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @panic(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = tail call i32 @image_read_u8(i32 -2147483648, i32 268)
%13 = icmp eq i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i32 @image_read_u32(i32 -2147483648, i32 264)
%16 = tail call i32 @reg_write(i32 -83886080, i32 0, i32 %15)
%17 = shl i32 %12, 6
%18 = tail call i32 @reg_write(i32 -260042752, i32 88, i32 %17)
br label %19
19: ; preds = %14, %11
%20 = tail call i32 @image_read_u32(i32 -2147483648, i32 320)
%21 = sext i32 %20 to i64
%22 = icmp eq i32 %20, 0
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = tail call i32 @image_read_u32(i32 -2147483648, i32 324)
%25 = add nsw i32 %24, 512
%26 = tail call i32 @copy_boot_image(i64 %21, i32 -2147483648, i32 %25)
%27 = add nsw i64 %21, 512
br label %28
28: ; preds = %19, %23
%29 = phi i64 [ %27, %23 ], [ 2147484160, %19 ]
ret i64 %29
}