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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void do_load(uint64_t *destination, void *syscall_handler,
             const char *source_fname) {
  uint64_t load_size, total_size, i;
  uint32_t reloc;
  FILE *fd;
  fd = fopen(source_fname, "rb");
  if (fd == NULL) {
    perror("unable to open input file");
    exit(1);
  }
  fseek(fd, 5 * 8, SEEK_SET);
  if ((fread(&load_size, 8, 1, fd) != 1) ||
      (fread(&total_size, 8, 1, fd) != 1)) {
    perror("unable to read input file header");
    exit(1);
  }
  if ((load_size > total_size) || (total_size > 0x1000000)) {
    fputs("load_size/total_size invalid", stderr);
    exit(1);
  }
  fseek(fd, 0, SEEK_SET);
  if (fread(destination, load_size, 1, fd) != 1) {
    perror("unable to read input payload");
    exit(1);
  }
  for (i = load_size; i < total_size; i += 4) {
    if (fread(&reloc, 4, 1, fd) != 1) {
      perror("unable to read relocation table");
      exit(1);
    }
    if (reloc >= (load_size / 8)) {
      fputs("relocation invalid", stderr);
      exit(1);
    }
    destination[reloc] += (uint64_t)destination;
  }
  assert(ftell(fd) == total_size);
  destination[2] = (uint64_t)syscall_handler;
  fclose(fd);
}


x86 assembly (clang, -O0):
<do_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d9 <do_load+0x49>
lea    0xe39(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
mov    $0x28,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1230 <do_load+0xa0>
mov    -0x30(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1246 <do_load+0xb6>
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     125f <do_load+0xcf>
cmpl   $0x1000000,-0x20(%rbp)
jle    127e <do_load+0xee>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
lea    0xdcf(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     12c6 <do_load+0x136>
lea    0xda4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    136d <do_load+0x1dd>
mov    -0x30(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     130e <do_load+0x17e>
lea    0xd79(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jl     134b <do_load+0x1bb>
mov    0x2cad(%rip),%rax
mov    (%rax),%esi
lea    0xd5c(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
add    (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
jmp    12cc <do_load+0x13c>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
cmp    -0x20(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_load>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe55(%rip),%rsi
mov    %rdx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1321 <do_load+0x191>
mov    %rax,%r12
mov    0x2e12(%rip),%r13
mov    0x0(%r13),%edx
mov    %rax,%rdi
mov    $0x28,%esi
call   1080 <fseek@plt>
lea    0xc(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1318 <do_load+0x188>
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1318 <do_load+0x188>
mov    %rbx,0x10(%rsp)
mov    0xc(%rsp),%ebx
mov    0x8(%rsp),%ebp
cmp    %ebp,%ebx
jg     132a <do_load+0x19a>
cmp    $0x1000001,%ebp
jge    132a <do_load+0x19a>
mov    0x0(%r13),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    133c <do_load+0x1ac>
cmp    %ebp,%ebx
jge    12af <do_load+0x11f>
lea    0x7(%rbx),%r14d
test   %ebx,%ebx
cmovns %ebx,%r14d
sar    $0x3,%r14d
lea    0x4(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12e3 <do_load+0x153>
movslq 0x4(%rsp),%rax
cmp    %r14d,%eax
jge    12f9 <do_load+0x169>
add    %r15d,(%r15,%rax,4)
add    $0x4,%ebx
cmp    %ebp,%ebx
jl     1280 <do_load+0xf0>
mov    %r12,%rdi
call   1070 <ftell@plt>
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1060 <assert@plt>
mov    0x10(%rsp),%rax
mov    %eax,0x8(%r15)
mov    %r12,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xd8e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
lea    0xd8f(%rip),%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xcfe(%rip),%rdi
jmp    12ea <do_load+0x15a>
lea    0xcdb(%rip),%rdi
jmp    12ea <do_load+0x15a>
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
lea    0xd04(%rip),%rdi
jmp    1309 <do_load+0x179>
lea    0xd18(%rip),%rdi
jmp    12ea <do_load+0x15a>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"unable to open input file\00"
@SEEK_SET = global i32 0
@.str.2 = [33 x i8] c"unable to read input file header\00"
@.str.3 = [29 x i8] c"load_size/total_size invalid\00"
@stderr = global i32 0
@.str.4 = [29 x i8] c"unable to read input payload\00"
@.str.5 = [32 x i8] c"unable to read relocation table\00"
@.str.6 = [19 x i8] c"relocation invalid\00"
define void @do_load(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %3
%20 = load i32*, i32** %11
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 40, i32 %21)
%23 = load i32*, i32** %11
%24 = call i32 @fread(i32* %7, i32 8, i32 1, i32* %23)
%25 = icmp ne i32 %24, 1
br i1 %25, label %30, label %26
26: ; preds = %19
%27 = load i32*, i32** %11
%28 = call i32 @fread(i32* %8, i32 8, i32 1, i32* %27)
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %26, %19
%31 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = load i32, i32* %7
%35 = load i32, i32* %8
%36 = icmp sgt i32 %34, %35
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = load i32, i32* %8
%39 = icmp sgt i32 %38, 16777216
br i1 %39, label %40, label %44
40: ; preds = %37, %33
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = load i32*, i32** %11
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @fseek(i32* %45, i32 0, i32 %46)
%48 = load i32*, i32** %4
%49 = load i32, i32* %7
%50 = load i32*, i32** %11
%51 = call i32 @fread(i32* %48, i32 %49, i32 1, i32* %50)
%52 = icmp ne i32 %51, 1
br i1 %52, label %53, label %56
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %44
%57 = load i32, i32* %7
store i32 %57, i32* %9
br label %58
58: ; preds = %87, %56
%59 = load i32, i32* %9
%60 = load i32, i32* %8
%61 = icmp slt i32 %59, %60
br i1 %61, label %62, label %90
62: ; preds = %58
%63 = load i32*, i32** %11
%64 = call i32 @fread(i32* %10, i32 4, i32 1, i32* %63)
%65 = icmp ne i32 %64, 1
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %62
%70 = load i32, i32* %10
%71 = load i32, i32* %7
%72 = sdiv i32 %71, 8
%73 = icmp sge i32 %70, %72
br i1 %73, label %74, label %78
74: ; preds = %69
%75 = load i32, i32* @stderr
%76 = call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %69
%79 = load i32*, i32** %4
%80 = ptrtoint i32* %79 to i32
%81 = load i32*, i32** %4
%82 = load i32, i32* %10
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i32, i32* %81, i64 %83
%85 = load i32, i32* %84
%86 = add nsw i32 %85, %80
store i32 %86, i32* %84
br label %87
87: ; preds = %78
%88 = load i32, i32* %9
%89 = add nsw i32 %88, 4
store i32 %89, i32* %9
br label %58
90: ; preds = %58
%91 = load i32*, i32** %11
%92 = call i32 @ftell(i32* %91)
%93 = load i32, i32* %8
%94 = icmp eq i32 %92, %93
%95 = zext i1 %94 to i32
%96 = call i32 @assert(i32 %95)
%97 = load i8*, i8** %5
%98 = ptrtoint i8* %97 to i32
%99 = load i32*, i32** %4
%100 = getelementptr inbounds i32, i32* %99, i64 2
store i32 %98, i32* %100
%101 = load i32*, i32** %11
%102 = call i32 @fclose(i32* %101)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"unable to open input file\00"
@SEEK_SET = global i32 0
@.str.2 = [33 x i8] c"unable to read input file header\00"
@.str.3 = [29 x i8] c"load_size/total_size invalid\00"
@stderr = global i32 0
@.str.4 = [29 x i8] c"unable to read input payload\00"
@.str.5 = [32 x i8] c"unable to read relocation table\00"
@.str.6 = [19 x i8] c"relocation invalid\00"
define void @do_load(i32* %0, i8* %1, i8* nocapture %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %10, i32 40, i32 %16)
%18 = call i32 @fread(i32* %4, i32 8, i32 1, i32* %10)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @fread(i32* %5, i32 8, i32 1, i32* %10)
%22 = icmp eq i32 %21, 1
br i1 %22, label %26, label %23
23: ; preds = %20, %15
%24 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %20
%27 = load i32, i32* %4
%28 = load i32, i32* %5
%29 = icmp sgt i32 %27, %28
%30 = icmp sgt i32 %28, 16777216
%31 = or i1 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = tail call i32 @fputs(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %26
%37 = load i32, i32* @SEEK_SET
%38 = tail call i32 @fseek(i32* %10, i32 0, i32 %37)
%39 = tail call i32 @fread(i32* %0, i32 %27, i32 1, i32* %10)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %47
41: ; preds = %36
%42 = icmp slt i32 %27, %28
br i1 %42, label %43, label %71
43: ; preds = %41
%44 = sdiv i32 %27, 8
%45 = ptrtoint i32* %0 to i64
%46 = trunc i64 %45 to i32
br label %50
47: ; preds = %36
%48 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %43, %64
%51 = phi i32 [ %27, %43 ], [ %69, %64 ]
%52 = call i32 @fread(i32* %6, i32 4, i32 1, i32* %10)
%53 = icmp eq i32 %52, 1
br i1 %53, label %57, label %54
54: ; preds = %50
%55 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
%56 = tail call i32 @exit(i32 1)
unreachable
57: ; preds = %50
%58 = load i32, i32* %6
%59 = icmp slt i32 %58, %44
br i1 %59, label %64, label %60
60: ; preds = %57
%61 = load i32, i32* @stderr
%62 = tail call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %61)
%63 = tail call i32 @exit(i32 1)
unreachable
64: ; preds = %57
%65 = sext i32 %58 to i64
%66 = getelementptr inbounds i32, i32* %0, i64 %65
%67 = load i32, i32* %66
%68 = add nsw i32 %67, %46
store i32 %68, i32* %66
%69 = add nsw i32 %51, 4
%70 = icmp slt i32 %69, %28
br i1 %70, label %50, label %71
71: ; preds = %64, %41
%72 = tail call i32 @ftell(i32* %10)
%73 = icmp eq i32 %72, %28
%74 = zext i1 %73 to i32
%75 = tail call i32 @assert(i32 %74)
%76 = ptrtoint i8* %1 to i64
%77 = trunc i64 %76 to i32
%78 = getelementptr inbounds i32, i32* %0, i64 2
store i32 %77, i32* %78
%79 = tail call i32 @fclose(i32* %10)
ret void
}