#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int i, c, id;
  char buf[1024];
  if ((id = open("boot/boot", O_RDONLY, 0)) < 0)
    die("Unable to open 'boot'");
  if (read(id, buf, 512) != 512)
    die("Boot block must be exactly 512 bytes!");
  close(id);
  if ((*(unsigned short *)(buf + 510)) != 0xAA55)
    die("Boot block hasn't got boot flag (0xAA55)");
  i = write(1, buf, 512);
  if (i != 512)
    die("Write call failed");
  if ((id = open("boot/setup", O_RDONLY, 0)) < 0)
    die("Unable to open 'setup'");
  for (i = 0; (c = read(id, buf, sizeof buf)) > 0; i += c)
    if (write(1, buf, c) != c)
      die("Write call failed");
  close(id);
  if ((id = open("tool/kbinary", O_RDONLY, 0)) < 0)
    die("Unable to open 'kernel'");
  for (i = 0; (c = read(id, buf, sizeof buf)) > 0; i += c)
    if (write(1, buf, c) != c)
      die("Write call failed");
  close(id);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1198 <main+0x48>
lea    0xe77(%rip),%rdi
call   1070 <die@plt>
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0x200,%eax
je     11c3 <main+0x73>
lea    0xe62(%rip),%rdi
call   1070 <die@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movzwl -0x222(%rbp),%eax
cmp    $0xaa55,%eax
je     11e9 <main+0x99>
lea    0xe62(%rip),%rdi
call   1070 <die@plt>
lea    -0x420(%rbp),%rsi
mov    $0x1,%edi
mov    $0x200,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x200,-0x14(%rbp)
je     121b <main+0xcb>
lea    0xe59(%rip),%rdi
call   1070 <die@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    124a <main+0xfa>
lea    0xe47(%rip),%rdi
call   1070 <die@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    12ad <main+0x15d>
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     129a <main+0x14a>
lea    0xdda(%rip),%rdi
call   1070 <die@plt>
jmp    129f <main+0x14f>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1251 <main+0x101>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12e4 <main+0x194>
lea    0xdd1(%rip),%rdi
call   1070 <die@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1347 <main+0x1f7>
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     1334 <main+0x1e4>
lea    0xd40(%rip),%rdi
call   1070 <die@plt>
jmp    1339 <main+0x1e9>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    12eb <main+0x19b>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e82(%rip),%r12
mov    (%r12),%esi
lea    0xe8f(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    118a <main+0x3a>
lea    0xe85(%rip),%rdi
call   1070 <die@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0x200,%eax
je     11ac <main+0x5c>
lea    0xe79(%rip),%rdi
call   1070 <die@plt>
mov    %ebx,%edi
call   1040 <close@plt>
movzwl 0x1fe(%rsp),%eax
cmp    $0xaa55,%eax
je     11ce <main+0x7e>
lea    0xe7d(%rip),%rdi
call   1070 <die@plt>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    $0x200,%edx
call   1030 <write@plt>
cmp    $0x200,%eax
je     11f3 <main+0xa3>
lea    0xe81(%rip),%rdi
call   1070 <die@plt>
mov    (%r12),%esi
lea    0xe83(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1218 <main+0xc8>
lea    0xe79(%rip),%rdi
call   1070 <die@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1273 <main+0x123>
mov    %eax,%ebp
mov    %rsp,%rbx
lea    0xe37(%rip),%r14
jmp    1256 <main+0x106>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1273 <main+0x123>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1240 <main+0xf0>
mov    %r14,%rdi
call   1070 <die@plt>
jmp    1240 <main+0xf0>
mov    %r15d,%edi
call   1040 <close@plt>
mov    (%r12),%esi
lea    0xe1d(%rip),%rdi
xor    %edx,%edx
call   1060 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    12a0 <main+0x150>
lea    0xe15(%rip),%rdi
call   1070 <die@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1303 <main+0x1b3>
mov    %eax,%ebx
mov    %rsp,%rbp
lea    0xdaf(%rip),%r14
jmp    12e6 <main+0x196>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1303 <main+0x1b3>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     12d0 <main+0x180>
mov    %r14,%rdi
call   1070 <die@plt>
jmp    12d0 <main+0x180>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"boot/boot\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Unable to open 'boot'\00"
@.str.2 = [38 x i8] c"Boot block must be exactly 512 bytes!\00"
@.str.3 = [41 x i8] c"Boot block hasn't got boot flag (0xAA55)\00"
@.str.4 = [18 x i8] c"Write call failed\00"
@.str.5 = [11 x i8] c"boot/setup\00"
@.str.6 = [23 x i8] c"Unable to open 'setup'\00"
@.str.7 = [13 x i8] c"tool/kbinary\00"
@.str.8 = [24 x i8] c"Unable to open 'kernel'\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10, i32 0)
store i32 %11, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* %8
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 512)
%19 = icmp ne i32 %18, 512
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @die(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32, i32* %8
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%26 = getelementptr inbounds i8, i8* %25, i64 510
%27 = bitcast i8* %26 to i16*
%28 = load i16, i16* %27
%29 = zext i16 %28 to i32
%30 = icmp ne i32 %29, 43605
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %22
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%35 = call i32 @write(i32 1, i8* %34, i32 512)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp ne i32 %36, 512
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %41, i32 0)
store i32 %42, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
store i32 0, i32* %6
br label %47
47: ; preds = %61, %46
%48 = load i32, i32* %8
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%50 = call i32 @read(i32 %48, i8* %49, i32 1024)
store i32 %50, i32* %7
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %65
52: ; preds = %47
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%54 = load i32, i32* %7
%55 = call i32 @write(i32 1, i8* %53, i32 %54)
%56 = load i32, i32* %7
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %58, %52
br label %61
61: ; preds = %60
%62 = load i32, i32* %7
%63 = load i32, i32* %6
%64 = add nsw i32 %63, %62
store i32 %64, i32* %6
br label %47
65: ; preds = %47
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* @O_RDONLY
%69 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %68, i32 0)
store i32 %69, i32* %8
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %65
%72 = call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %73
73: ; preds = %71, %65
store i32 0, i32* %6
br label %74
74: ; preds = %88, %73
%75 = load i32, i32* %8
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%77 = call i32 @read(i32 %75, i8* %76, i32 1024)
store i32 %77, i32* %7
%78 = icmp sgt i32 %77, 0
br i1 %78, label %79, label %92
79: ; preds = %74
%80 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%81 = load i32, i32* %7
%82 = call i32 @write(i32 1, i8* %80, i32 %81)
%83 = load i32, i32* %7
%84 = icmp ne i32 %82, %83
br i1 %84, label %85, label %87
85: ; preds = %79
%86 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %87
87: ; preds = %85, %79
br label %88
88: ; preds = %87
%89 = load i32, i32* %7
%90 = load i32, i32* %6
%91 = add nsw i32 %90, %89
store i32 %91, i32* %6
br label %74
92: ; preds = %74
%93 = load i32, i32* %8
%94 = call i32 @close(i32 %93)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"boot/boot\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Unable to open 'boot'\00"
@.str.2 = [38 x i8] c"Boot block must be exactly 512 bytes!\00"
@.str.3 = [41 x i8] c"Boot block hasn't got boot flag (0xAA55)\00"
@.str.4 = [18 x i8] c"Write call failed\00"
@.str.5 = [11 x i8] c"boot/setup\00"
@.str.6 = [23 x i8] c"Unable to open 'setup'\00"
@.str.7 = [13 x i8] c"tool/kbinary\00"
@.str.8 = [24 x i8] c"Unable to open 'kernel'\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = call i32 @read(i32 %6, i8* %4, i32 512)
%12 = icmp eq i32 %11, 512
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @die(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = tail call i32 @close(i32 %6)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 510
%18 = bitcast i8* %17 to i16*
%19 = load i16, i16* %18
%20 = icmp eq i16 %19, -21931
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = tail call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = call i32 @write(i32 1, i8* %4, i32 512)
%25 = icmp eq i32 %24, 512
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = load i32, i32* @O_RDONLY
%30 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32 %29, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = call i32 @read(i32 %30, i8* %4, i32 1024)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %34, %43
%38 = phi i32 [ %44, %43 ], [ %35, %34 ]
%39 = call i32 @write(i32 1, i8* %4, i32 %38)
%40 = icmp eq i32 %39, %38
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %37, %41
%44 = call i32 @read(i32 %30, i8* %4, i32 1024)
%45 = icmp sgt i32 %44, 0
br i1 %45, label %37, label %46
46: ; preds = %43, %34
%47 = tail call i32 @close(i32 %30)
%48 = load i32, i32* @O_RDONLY
%49 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %48, i32 0)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = tail call i32 @die(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %53
53: ; preds = %51, %46
%54 = call i32 @read(i32 %49, i8* %4, i32 1024)
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %65
56: ; preds = %53, %62
%57 = phi i32 [ %63, %62 ], [ %54, %53 ]
%58 = call i32 @write(i32 1, i8* %4, i32 %57)
%59 = icmp eq i32 %58, %57
br i1 %59, label %62, label %60
60: ; preds = %56
%61 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %56, %60
%63 = call i32 @read(i32 %49, i8* %4, i32 1024)
%64 = icmp sgt i32 %63, 0
br i1 %64, label %56, label %65
65: ; preds = %62, %53
%66 = tail call i32 @close(i32 %49)
ret i32 0
}