#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*/ dump(unsigned char *, long);
char *elf_load(unsigned char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ init();
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ putxval(unsigned long, int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ wait();
long xmodem_recv(unsigned char *);
int main(void) {
  static char buf[16];
  static long size = -1;
  static unsigned char *loadbuf = ((void *)0);
  char *entry_point;
  void (*f)(void);
  extern int buffer_start;
  init();
  puts("kzload (kozos boot loader) started.\n");
  while (1) {
    puts("kzload> ");
    gets(buf);
    if (!strcmp(buf, "load")) {
      loadbuf = (char *)(&buffer_start);
      size = xmodem_recv(loadbuf);
      wait();
      if (size < 0) {
        puts("\nXMODEM receive error!\n");
      } else {
        puts("\nXMODEM receive succeeded.\n");
      }
    } else if (!strcmp(buf, "dump")) {
      puts("size: ");
      putxval(size, 0);
      puts("\n");
      dump(loadbuf, size);
    } else if (!strcmp(buf, "run")) {
      entry_point = elf_load(loadbuf);
      if (!entry_point) {
        puts("run error!\n");
      } else {
        puts("starting from entry point: ");
        putxval((unsigned long)entry_point, 0);
        puts("\n");
        f = (void (*)(void))entry_point;
        f();
      }
    } else {
      puts("unknown.\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <init@plt>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
lea    0x2ebb(%rip),%rdi
call   1090 <gets@plt>
lea    0x2eaf(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    123e <main+0xae>
mov    0x2df3(%rip),%rax
mov    %rax,0x2e9c(%rip)
mov    0x2e95(%rip),%rdi
call   1060 <xmodem_recv@plt>
mov    %rax,0x2e61(%rip)
mov    $0x0,%al
call   10a0 <wait@plt>
cmpq   $0x0,0x2e52(%rip)
jge    122d <main+0x9d>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
jmp    1239 <main+0xa9>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
jmp    1329 <main+0x199>
lea    0x2e3b(%rip),%rdi
lea    0xe1b(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1298 <main+0x108>
lea    0xe0b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfb(%rip),%rdi
xor    %esi,%esi
call   10b0 <putxval@plt>
lea    0xdce(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e09(%rip),%rdi
mov    0x2dda(%rip),%rsi
call   1040 <dump@plt>
jmp    1324 <main+0x194>
lea    0x2de1(%rip),%rdi
lea    0xdcd(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1313 <main+0x183>
mov    0x2dd5(%rip),%rdi
call   1050 <elf_load@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    12e0 <main+0x150>
lea    0xda1(%rip),%rdi
call   1030 <puts@plt>
jmp    130e <main+0x17e>
lea    0xd9c(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   10b0 <putxval@plt>
lea    0xd4b(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
call   *-0x18(%rbp)
jmp    131f <main+0x18f>
lea    0xd85(%rip),%rdi
call   1030 <puts@plt>
jmp    1324 <main+0x194>
jmp    1329 <main+0x199>
jmp    11b2 <main+0x22>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <init@plt>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
lea    0xe80(%rip),%r14
lea    0x2ed4(%rip),%rbx
mov    $0x64616f6c,%ebp
mov    0x2e28(%rip),%r15
mov    $0x706d7564,%r12d
jmp    11f9 <main+0x79>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e8d(%rip),%rdi
xor    %esi,%esi
call   10a0 <putxval@plt>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ea3(%rip),%rdi
mov    0x2e6c(%rip),%rsi
call   1040 <dump@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1080 <gets@plt>
mov    0x2e71(%rip),%eax
xor    %ebp,%eax
movzbl 0x2e6c(%rip),%ecx
or     %eax,%ecx
je     1280 <main+0x100>
mov    0x2e5e(%rip),%eax
xor    %r12d,%eax
movzbl 0x2e58(%rip),%ecx
or     %eax,%ecx
je     11c0 <main+0x40>
cmpl   $0x6e7572,0x2e46(%rip)
jne    12b8 <main+0x138>
mov    0x2e4d(%rip),%rdi
call   1050 <elf_load@plt>
test   %rax,%rax
je     12da <main+0x15a>
mov    %rax,%r13
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
xor    %esi,%esi
call   10a0 <putxval@plt>
lea    0xdd8(%rip),%rdi
call   1030 <puts@plt>
call   *%r13
jmp    11f9 <main+0x79>
xchg   %ax,%ax
mov    %r15,0x2e09(%rip)
mov    %r15,%rdi
call   1060 <xmodem_recv@plt>
mov    %rax,0x2dca(%rip)
xor    %eax,%eax
call   1090 <wait@plt>
cmpq   $0x0,0x2dbb(%rip)
js     12c9 <main+0x149>
lea    0xd9d(%rip),%rdi
call   1030 <puts@plt>
jmp    11f9 <main+0x79>
lea    0xddc(%rip),%rdi
call   1030 <puts@plt>
jmp    11f9 <main+0x79>
lea    0xd63(%rip),%rdi
call   1030 <puts@plt>
jmp    11f9 <main+0x79>
lea    0xd92(%rip),%rdi
call   1030 <puts@plt>
jmp    11f9 <main+0x79>

LLVM IR (-O0):
@main.buf = internal global [16 x i8] zeroinitializer
@main.size = internal global i64 -1
@main.loadbuf = internal global i8* null
@.str = [37 x i8] c"kzload (kozos boot loader) started.\0A\00"
@.str.1 = [9 x i8] c"kzload> \00"
@.str.2 = [5 x i8] c"load\00"
@buffer_start = external global i32
@.str.3 = [24 x i8] c"\0AXMODEM receive error!\0A\00"
@.str.4 = [28 x i8] c"\0AXMODEM receive succeeded.\0A\00"
@.str.5 = [5 x i8] c"dump\00"
@.str.6 = [7 x i8] c"size: \00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [4 x i8] c"run\00"
@.str.9 = [12 x i8] c"run error!\0A\00"
@.str.10 = [28 x i8] c"starting from entry point: \00"
@.str.11 = [10 x i8] c"unknown.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca void ()*
store i32 0, i32* %1
%4 = call i32 (...) @init()
%5 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %0, %57
%7 = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @gets(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0))
%9 = call i32 @strcmp(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp ne i32 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %6
store i8* bitcast (i32* @buffer_start to i8*), i8** @main.loadbuf
%12 = load i8*, i8** @main.loadbuf
%13 = call i64 @xmodem_recv(i8* %12)
store i64 %13, i64* @main.size
%14 = call i32 (...) @wait()
%15 = load i64, i64* @main.size
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %21
19: ; preds = %11
%20 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %19, %17
br label %57
22: ; preds = %6
%23 = call i32 @strcmp(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %22
%26 = call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%27 = load i64, i64* @main.size
%28 = call i32 @putxval(i64 %27, i32 0)
%29 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%30 = load i8*, i8** @main.loadbuf
%31 = load i64, i64* @main.size
%32 = call i32 @dump(i8* %30, i64 %31)
br label %56
33: ; preds = %22
%34 = call i32 @strcmp(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %53, label %36
36: ; preds = %33
%37 = load i8*, i8** @main.loadbuf
%38 = call i8* @elf_load(i8* %37)
store i8* %38, i8** %2
%39 = load i8*, i8** %2
%40 = icmp ne i8* %39, null
br i1 %40, label %43, label %41
41: ; preds = %36
%42 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %52
43: ; preds = %36
%44 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%45 = load i8*, i8** %2
%46 = ptrtoint i8* %45 to i64
%47 = call i32 @putxval(i64 %46, i32 0)
%48 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%49 = load i8*, i8** %2
%50 = bitcast i8* %49 to void ()*
store void ()* %50, void ()** %3
%51 = load void ()*, void ()** %3
br label %52
52: ; preds = %43, %41
br label %55
53: ; preds = %33
%54 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
br label %55
55: ; preds = %53, %52
br label %56
56: ; preds = %55, %25
br label %57
57: ; preds = %56, %21
br label %6
}

LLVM IR (-O2):
@main.buf = internal global [16 x i8] zeroinitializer
@main.size = internal unnamed_addr global i64 -1
@main.loadbuf = internal unnamed_addr global i8* null
@.str = [37 x i8] c"kzload (kozos boot loader) started.\0A\00"
@.str.1 = [9 x i8] c"kzload> \00"
@.str.2 = [5 x i8] c"load\00"
@buffer_start = external global i32
@.str.3 = [24 x i8] c"\0AXMODEM receive error!\0A\00"
@.str.4 = [28 x i8] c"\0AXMODEM receive succeeded.\0A\00"
@.str.5 = [5 x i8] c"dump\00"
@.str.6 = [7 x i8] c"size: \00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.9 = [12 x i8] c"run error!\0A\00"
@.str.10 = [28 x i8] c"starting from entry point: \00"
@.str.11 = [10 x i8] c"unknown.\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @init()
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %3
3: ; preds = %28, %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @gets(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0))
%6 = tail call i32 @bcmp(i8* dereferenceable(5) getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0), i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %3
store i8* bitcast (i32* @buffer_start to i8*), i8** @main.loadbuf
%9 = tail call i64 @xmodem_recv(i8* bitcast (i32* @buffer_start to i8*))
store i64 %9, i64* @main.size
%10 = tail call i32 (...) @wait()
%11 = load i64, i64* @main.size
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %28
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %28
17: ; preds = %3
%18 = tail call i32 @bcmp(i8* dereferenceable(5) getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0), i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64 5)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%22 = load i64, i64* @main.size
%23 = tail call i32 @putxval(i64 %22, i32 0)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%25 = load i8*, i8** @main.loadbuf
%26 = load i64, i64* @main.size
%27 = tail call i32 @dump(i8* %25, i64 %26)
br label %28
28: ; preds = %20, %36, %38, %44, %13, %15
br label %3
29: ; preds = %17
%30 = load i32, i32* bitcast ([16 x i8]* @main.buf to i32*)
%31 = icmp eq i32 %30, 7239026
br i1 %31, label %32, label %44
32: ; preds = %29
%33 = load i8*, i8** @main.loadbuf
%34 = tail call i8* @elf_load(i8* %33)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %28
38: ; preds = %32
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%40 = ptrtoint i8* %34 to i64
%41 = tail call i32 @putxval(i64 %40, i32 0)
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%43 = bitcast i8* %34 to void ()*
tail call void %43()
br label %28
44: ; preds = %29
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
br label %28
}