#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);
int /*<<< orphan*/ elf_load(unsigned char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ init();
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ putxval(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);
  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")) {
      elf_load(loadbuf);
    } else {
      puts("unknown.\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%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    12db <main+0x14b>
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    12d6 <main+0x146>
lea    0x2de1(%rip),%rdi
lea    0xdcd(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12c5 <main+0x135>
mov    0x2dd5(%rip),%rdi
call   1050 <elf_load@plt>
jmp    12d1 <main+0x141>
lea    0xdab(%rip),%rdi
call   1030 <puts@plt>
jmp    12d6 <main+0x146>
jmp    12db <main+0x14b>
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,%r13d
mov    0x2e27(%rip),%r15
mov    $0x706d7564,%ebp
lea    0xe84(%rip),%r12
jmp    1205 <main+0x85>
nopw   0x0(%rax,%rax,1)
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e7d(%rip),%rdi
xor    %esi,%esi
call   10a0 <putxval@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    0x2e97(%rip),%rdi
mov    0x2e60(%rip),%rsi
call   1040 <dump@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1080 <gets@plt>
mov    0x2e65(%rip),%eax
xor    %r13d,%eax
movzbl 0x2e5f(%rip),%ecx
or     %eax,%ecx
je     1260 <main+0xe0>
mov    0x2e51(%rip),%eax
xor    %ebp,%eax
movzbl 0x2e4c(%rip),%ecx
or     %eax,%ecx
je     11d0 <main+0x50>
cmpl   $0x6e7572,0x2e3a(%rip)
jne    1298 <main+0x118>
mov    0x2e41(%rip),%rdi
call   1050 <elf_load@plt>
jmp    1205 <main+0x85>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,0x2e29(%rip)
mov    %r15,%rdi
call   1060 <xmodem_recv@plt>
mov    %rax,0x2dea(%rip)
xor    %eax,%eax
call   1090 <wait@plt>
cmpq   $0x0,0x2ddb(%rip)
js     12a9 <main+0x129>
lea    0xdbd(%rip),%rdi
call   1030 <puts@plt>
jmp    1205 <main+0x85>
lea    0xdd4(%rip),%rdi
call   1030 <puts@plt>
jmp    1205 <main+0x85>
lea    0xd83(%rip),%rdi
call   1030 <puts@plt>
jmp    1205 <main+0x85>

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 = [10 x i8] c"unknown.\0A\00"
define i32 @main() {
%1 = alloca i32
store i32 0, i32* %1
%2 = call i32 (...) @init()
%3 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %4
4: ; preds = %0, %41
%5 = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @gets(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @main.buf, i64 0, i64 0))
%7 = 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))
%8 = icmp ne i32 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %4
store i8* bitcast (i32* @buffer_start to i8*), i8** @main.loadbuf
%10 = load i8*, i8** @main.loadbuf
%11 = call i64 @xmodem_recv(i8* %10)
store i64 %11, i64* @main.size
%12 = call i32 (...) @wait()
%13 = load i64, i64* @main.size
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %19
17: ; preds = %9
%18 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
br label %41
20: ; preds = %4
%21 = 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))
%22 = icmp ne i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %20
%24 = call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%25 = load i64, i64* @main.size
%26 = call i32 @putxval(i64 %25, i32 0)
%27 = call i32 @puts(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%28 = load i8*, i8** @main.loadbuf
%29 = load i64, i64* @main.size
%30 = call i32 @dump(i8* %28, i64 %29)
br label %40
31: ; preds = %20
%32 = 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))
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i8*, i8** @main.loadbuf
%36 = call i32 @elf_load(i8* %35)
br label %39
37: ; preds = %31
%38 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
br label %40
40: ; preds = %39, %23
br label %41
41: ; preds = %40, %19
br label %4
}

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 = [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, %35, %32, %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 %35
32: ; preds = %29
%33 = load i8*, i8** @main.loadbuf
%34 = tail call i32 @elf_load(i8* %33)
br label %28
35: ; preds = %29
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
br label %28
}