#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*/ 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 {
      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   1070 <init@plt>
lea    0xe63(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
lea    0x2ecb(%rip),%rdi
call   1080 <gets@plt>
lea    0x2ebf(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    122e <main+0xae>
mov    0x2e03(%rip),%rax
mov    %rax,0x2eac(%rip)
mov    0x2ea5(%rip),%rdi
call   1050 <xmodem_recv@plt>
mov    %rax,0x2e69(%rip)
mov    $0x0,%al
call   1090 <wait@plt>
cmpq   $0x0,0x2e5a(%rip)
jge    121d <main+0x9d>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
jmp    1229 <main+0xa9>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
jmp    1299 <main+0x119>
lea    0x2e4b(%rip),%rdi
lea    0xe2b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1288 <main+0x108>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e03(%rip),%rdi
xor    %esi,%esi
call   10a0 <putxval@plt>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e19(%rip),%rdi
mov    0x2de2(%rip),%rsi
call   1040 <dump@plt>
jmp    1294 <main+0x114>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
jmp    1299 <main+0x119>
jmp    11a2 <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   1060 <init@plt>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
lea    0xe90(%rip),%r14
lea    0x2ed4(%rip),%rbx
mov    $0x64616f6c,%ebp
mov    0x2e38(%rip),%r15
mov    $0x706d7564,%r12d
lea    0xebe(%rip),%r13
jmp    11c8 <main+0x58>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <gets@plt>
mov    0x2e92(%rip),%eax
xor    %ebp,%eax
movzbl 0x2e8d(%rip),%ecx
or     %eax,%ecx
je     1240 <main+0xd0>
mov    0x2e7f(%rip),%eax
xor    %r12d,%eax
movzbl 0x2e79(%rip),%ecx
or     %eax,%ecx
jne    11c0 <main+0x50>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e46(%rip),%rdi
xor    %esi,%esi
call   1090 <putxval@plt>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e54(%rip),%rdi
mov    0x2e25(%rip),%rsi
call   1040 <dump@plt>
jmp    11c8 <main+0x58>
nopw   0x0(%rax,%rax,1)
mov    %r15,0x2e39(%rip)
mov    %r15,%rdi
call   1050 <xmodem_recv@plt>
mov    %rax,0x2e02(%rip)
xor    %eax,%eax
call   1080 <wait@plt>
cmpq   $0x0,0x2df3(%rip)
js     1278 <main+0x108>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
jmp    11c8 <main+0x58>
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
jmp    11c8 <main+0x58>

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 = [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, %34
%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 %34
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 %33
31: ; preds = %20
%32 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %33
33: ; preds = %31, %23
br label %34
34: ; preds = %33, %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.8 = [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, %29, %13, %15
br label %3
29: ; preds = %17
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
br label %28
}