#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
scalar_t__ calloc(long, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_multiboot(int, char *, long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int serial_open(char *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("filename required\n");
    return 0;
  }
  int fd = serial_open("/dev/tty.usbmodem12341");
  if (fd < 0) {
    printf("Couldn't open serial\n");
    return 1;
  }
  FILE *gba_file = fopen(argv[1], "rb");
  if (!gba_file) {
    printf("Couldn't open file\n");
    return 1;
  }
  fseek(gba_file, 0, SEEK_END);
  long gba_file_size = ftell(gba_file);
  rewind(gba_file);
  printf("To send file of length %lu\n", gba_file_size);
  char *data = (char *)calloc(gba_file_size + 0x10, 1);
  size_t read_size = fread(data, 1, gba_file_size, gba_file);
  if (read_size != gba_file_size) {
    printf("Failed to read file: (%zu/%zu)", read_size, gba_file_size);
    return 1;
  }
  fclose(gba_file);
  do_multiboot(fd, data, gba_file_size);
  free(data);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11fa <main+0x3a>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1337 <main+0x177>
lea    0xe12(%rip),%rdi
call   10c0 <serial_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    122d <main+0x6d>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x177>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe04(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    126a <main+0xaa>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x177>
mov    -0x20(%rbp),%rdi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <rewind@plt>
mov    -0x28(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    $0x1,%esi
call   10a0 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1306 <main+0x146>
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x177>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <do_multiboot@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jg     11f0 <main+0x20>
lea    0xe99(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
jmp    12e6 <main+0x116>
mov    %rsi,%rbx
lea    0xe06(%rip),%rdi
call   10d0 <serial_open@plt>
test   %eax,%eax
js     12b6 <main+0xe6>
mov    %eax,%r14d
mov    0x8(%rbx),%rdi
lea    0xe02(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
je     12bf <main+0xef>
mov    %rax,%rbx
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    %rbx,%rdi
call   10c0 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1090 <rewind@plt>
lea    0xdc7(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x10(%r15),%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
call   10b0 <calloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    %r15,%rax
jne    12d2 <main+0x102>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1070 <do_multiboot@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
jmp    11e9 <main+0x19>
lea    0xdab(%rip),%rdi
jmp    12c6 <main+0xf6>
lea    0xd8f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    12e6 <main+0x116>
lea    0xd5d(%rip),%rdi
mov    %rax,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"filename required\0A\00"
@.str.1 = [23 x i8] c"/dev/tty.usbmodem12341\00"
@.str.2 = [22 x i8] c"Couldn't open serial\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [20 x i8] c"Couldn't open file\0A\00"
@SEEK_END = global i32 0
@.str.5 = [28 x i8] c"To send file of length %lu\0A\00"
@.str.6 = [31 x i8] c"Failed to read file: (%zu/%zu)\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 i64
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %66
15: ; preds = %2
%16 = call i32 @serial_open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %66
21: ; preds = %15
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %25, i32** %7
%26 = load i32*, i32** %7
%27 = icmp ne i32* %26, null
br i1 %27, label %30, label %28
28: ; preds = %21
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %66
30: ; preds = %21
%31 = load i32*, i32** %7
%32 = load i32, i32* @SEEK_END
%33 = call i32 @fseek(i32* %31, i32 0, i32 %32)
%34 = load i32*, i32** %7
%35 = call i64 @ftell(i32* %34)
store i64 %35, i64* %8
%36 = load i32*, i32** %7
%37 = call i32 @rewind(i32* %36)
%38 = load i64, i64* %8
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %38)
%40 = load i64, i64* %8
%41 = add nsw i64 %40, 16
%42 = call i64 @calloc(i64 %41, i32 1)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = load i64, i64* %8
%46 = load i32*, i32** %7
%47 = call i64 @fread(i8* %44, i32 1, i64 %45, i32* %46)
store i64 %47, i64* %10
%48 = load i64, i64* %10
%49 = load i64, i64* %8
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %55
51: ; preds = %30
%52 = load i64, i64* %10
%53 = load i64, i64* %8
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i64 %52, i64 %53)
store i32 1, i32* %3
br label %66
55: ; preds = %30
%56 = load i32*, i32** %7
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %6
%59 = load i8*, i8** %9
%60 = load i64, i64* %8
%61 = call i32 @do_multiboot(i32 %58, i8* %59, i64 %60)
%62 = load i8*, i8** %9
%63 = call i32 @free(i8* %62)
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
store i32 0, i32* %3
br label %66
66: ; preds = %55, %51, %28, %19, %13
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str.1 = [23 x i8] c"/dev/tty.usbmodem12341\00"
@.str.3 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.5 = [28 x i8] c"To send file of length %lu\0A\00"
@.str.6 = [31 x i8] c"Failed to read file: (%zu/%zu)\00"
@str = [19 x i8] c"Couldn't open file\00"
@str.7 = [21 x i8] c"Couldn't open serial\00"
@str.8 = [18 x i8] c"filename required\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %36
6: ; preds = %2
%7 = tail call i32 @serial_open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
br label %36
11: ; preds = %6
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %36
18: ; preds = %11
%19 = load i32, i32* @SEEK_END
%20 = tail call i32 @fseek(i32* %14, i32 0, i32 %19)
%21 = tail call i64 @ftell(i32* %14)
%22 = tail call i32 @rewind(i32* %14)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i64 %21)
%24 = add nsw i64 %21, 16
%25 = tail call i64 @calloc(i64 %24, i32 1)
%26 = inttoptr i64 %25 to i8*
%27 = tail call i64 @fread(i8* %26, i32 1, i64 %21, i32* %14)
%28 = icmp eq i64 %27, %21
br i1 %28, label %31, label %29
29: ; preds = %18
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i64 %27, i64 %21)
br label %36
31: ; preds = %18
%32 = tail call i32 @fclose(i32* %14)
%33 = tail call i32 @do_multiboot(i32 %7, i8* %26, i64 %21)
%34 = tail call i32 @free(i8* %26)
%35 = tail call i32 @close(i32 %7)
br label %36
36: ; preds = %9, %29, %31, %16, %4
%37 = phi i32 [ 0, %4 ], [ 1, %9 ], [ 1, %16 ], [ 1, %29 ], [ 0, %31 ]
ret i32 %37
}