#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*/ CloseDevice();
int /*<<< orphan*/ EnterBootloader();
unsigned int GetRamSize();
scalar_t__ OpenDevice();
int /*<<< orphan*/ Reset();
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ Set16KMode();
int /*<<< orphan*/ Set8KMode();
int /*<<< orphan*/ WriteData(int /*<<< orphan*/, unsigned char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int getData(int, char *, int);
int getFubar(char *);
int /*<<< orphan*/ hexdump(int, char *, int);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcasecmp(char *, char *);
int main(int argc, char **argv) {
  if (argc != 2 && argc != 3) {
    printf("Usage: c64 <command> [filename]\n");
    printf("Commands:\n");
    printf("     upload <filename>      - Uploads CRT image to C64FC\n");
    printf("     download <filename>    - Downloads image from C64FC (not "
           "implemented)\n");
    printf("     reset                  - Resets the C64\n");
    printf("     clear                  - Clears the entire C64FC, writes 0x0 "
           "to every address\n");
    printf(
        "     hexdump                - Prints a hexdump of the entire cart\n");
    printf("     bootloader             - Enters bootloader mode on cart\n");
    printf("     ramsize                - Prints your EEPROM size\n");
    printf("     chardump <fps>         - Takes stdout from pipe and dump it "
           "on screen\n");
    return (1);
  }
  if (OpenDevice()) {
    printf("C64FC not found\n");
    return (1);
  }
  if ((strcasecmp(argv[1], "reset") == 0) && (argc == 2)) {
    Reset();
  } else if ((strcasecmp(argv[1], "bootloader") == 0) && (argc == 2)) {
    EnterBootloader();
  } else if ((strcasecmp(argv[1], "upload") == 0) && (argc == 3)) {
    int skip = 0;
    FILE *fp = fopen(argv[2], "rb");
    if (!fp) {
      printf("Can't open input file for reading\n");
      return (1);
    }
    fseek(fp, 0L, SEEK_END);
    int len = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    unsigned char *array = malloc(len);
    fread(array, len, 1, fp);
    fclose(fp);
    if (array[0] == 'C' && array[1] == '6' && array[2] == '4') {
      skip = 80;
    }
    printf("Sending data (%d bytes)..\n", len - skip);
    WriteData(0, array + skip, len - skip);
    if (len - skip >= 8192) {
      Set16KMode();
    } else {
      Set8KMode();
    }
    printf("Issuing reset..\n");
    Reset();
  } else if ((strcasecmp(argv[1], "clear") == 0) && (argc == 2)) {
  } else if ((strcasecmp(argv[1], "fubar") == 0) && (argc == 2)) {
    char buff[10];
    int len = getFubar(buff);
    printf("getFubar: %s(%d)\n", buff, len);
    hexdump(0, buff, 10);
  } else if ((strcasecmp(argv[1], "hexdump") == 0) && (argc == 2)) {
    char buff[128];
    printf("Hexdump of cart:\n");
    int i;
    for (i = 0; i < 0x3FFF; i += 128) {
      memset(buff, 0x0, 128);
      int len = getData(i, buff, 128);
      hexdump(i, buff, 128);
    }
  } else if ((strcasecmp(argv[1], "ramsize") == 0) && (argc == 2)) {
    unsigned int size = GetRamSize();
    printf("Your RAM is a %dkb chip\n", size);
  } else {
    printf("WHAT? WHAT\n");
    return (1);
  }
  CloseDevice();
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1305 <main+0xc5>
cmpl   $0x3,-0x8(%rbp)
je     1305 <main+0xc5>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xed1(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xefa(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16da <main+0x49a>
mov    $0x0,%al
call   1120 <OpenDevice@plt>
cmp    $0x0,%rax
je     1330 <main+0xf0>
lea    0xf1b(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16da <main+0x49a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xf0a(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1364 <main+0x124>
cmpl   $0x2,-0x8(%rbp)
jne    1364 <main+0x124>
mov    $0x0,%al
call   10d0 <Reset@plt>
jmp    16cc <main+0x48c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xedc(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1398 <main+0x158>
cmpl   $0x2,-0x8(%rbp)
jne    1398 <main+0x158>
mov    $0x0,%al
call   1030 <EnterBootloader@plt>
jmp    16c7 <main+0x487>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xeb3(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1509 <main+0x2c9>
cmpl   $0x3,-0x8(%rbp)
jne    1509 <main+0x2c9>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe8b(%rip),%rsi
call   1140 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1404 <main+0x1c4>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16da <main+0x49a>
mov    -0x20(%rbp),%rdi
mov    0x2bd9(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1110 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10e0 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2ba7(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1110 <fseek@plt>
mov    -0x24(%rbp),%edi
call   1100 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1090 <fclose@plt>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x43,%eax
jne    149f <main+0x25f>
mov    -0x30(%rbp),%rax
movzbl 0x1(%rax),%eax
cmp    $0x36,%eax
jne    149f <main+0x25f>
mov    -0x30(%rbp),%rax
movzbl 0x2(%rax),%eax
cmp    $0x34,%eax
jne    149f <main+0x25f>
movl   $0x50,-0x14(%rbp)
mov    -0x24(%rbp),%esi
sub    -0x14(%rbp),%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    -0x30(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
sub    -0x14(%rbp),%edx
xor    %edi,%edi
call   1150 <WriteData@plt>
mov    -0x24(%rbp),%eax
sub    -0x14(%rbp),%eax
cmp    $0x2000,%eax
jl     14e8 <main+0x2a8>
mov    $0x0,%al
call   1080 <Set16KMode@plt>
jmp    14ef <main+0x2af>
mov    $0x0,%al
call   1070 <Set8KMode@plt>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    $0x0,%al
call   10d0 <Reset@plt>
jmp    16c2 <main+0x482>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd9b(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1536 <main+0x2f6>
cmpl   $0x2,-0x8(%rbp)
jne    1536 <main+0x2f6>
jmp    16bd <main+0x47d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd74(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1594 <main+0x354>
cmpl   $0x2,-0x8(%rbp)
jne    1594 <main+0x354>
lea    -0x3a(%rbp),%rdi
call   10b0 <getFubar@plt>
mov    %eax,-0x40(%rbp)
lea    -0x3a(%rbp),%rsi
mov    -0x40(%rbp),%edx
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    -0x3a(%rbp),%rsi
xor    %edi,%edi
mov    $0xa,%edx
call   10f0 <hexdump@plt>
jmp    16b8 <main+0x478>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd2e(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1646 <main+0x406>
cmpl   $0x2,-0x8(%rbp)
jne    1646 <main+0x406>
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
movl   $0x0,-0xc4(%rbp)
cmpl   $0x3fff,-0xc4(%rbp)
jge    1641 <main+0x401>
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   10c0 <memset@plt>
mov    -0xc4(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x80,%edx
call   1050 <getData@plt>
mov    %eax,-0xc8(%rbp)
mov    -0xc4(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x80,%edx
call   10f0 <hexdump@plt>
mov    -0xc4(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0xc4(%rbp)
jmp    15d4 <main+0x394>
jmp    16b3 <main+0x473>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xc96(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1694 <main+0x454>
cmpl   $0x2,-0x8(%rbp)
jne    1694 <main+0x454>
mov    $0x0,%al
call   1130 <GetRamSize@plt>
mov    %eax,-0xcc(%rbp)
mov    -0xcc(%rbp),%esi
lea    0xc6b(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    16ae <main+0x46e>
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16da <main+0x49a>
jmp    16b3 <main+0x473>
jmp    16b8 <main+0x478>
jmp    16bd <main+0x47d>
jmp    16c2 <main+0x482>
jmp    16c7 <main+0x487>
jmp    16cc <main+0x48c>
mov    $0x0,%al
call   1160 <CloseDevice@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    1285 <main+0x35>
mov    %rsi,%r15
mov    %edi,%ebx
xor    %eax,%eax
call   1130 <OpenDevice@plt>
test   %rax,%rax
je     1312 <main+0xc2>
lea    0xe48(%rip),%rdi
jmp    12f8 <main+0xa8>
lea    0xe4f(%rip),%rdi
call   1060 <puts@plt>
lea    0xe63(%rip),%rdi
call   1060 <puts@plt>
lea    0xe61(%rip),%rdi
call   1060 <puts@plt>
lea    0xe8e(%rip),%rdi
call   1060 <puts@plt>
lea    0xecd(%rip),%rdi
call   1060 <puts@plt>
lea    0xeee(%rip),%rdi
call   1060 <puts@plt>
lea    0xf35(%rip),%rdi
call   1060 <puts@plt>
lea    0xf6b(%rip),%rdi
call   1060 <puts@plt>
lea    0xf9c(%rip),%rdi
call   1060 <puts@plt>
lea    0xfc6(%rip),%rdi
call   1060 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r15),%r14
lea    0xce3(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcasecmp@plt>
cmp    $0x2,%ebx
jne    1338 <main+0xe8>
test   %rax,%rax
jne    1338 <main+0xe8>
xor    %eax,%eax
call   10e0 <Reset@plt>
jmp    1358 <main+0x108>
lea    0xcc7(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcasecmp@plt>
cmp    $0x2,%ebx
jne    1363 <main+0x113>
test   %rax,%rax
jne    1363 <main+0x113>
xor    %eax,%eax
call   1030 <EnterBootloader@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1170 <CloseDevice@plt>
jmp    1302 <main+0xb2>
lea    0xca7(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcasecmp@plt>
cmp    $0x3,%ebx
jne    144e <main+0x1fe>
test   %rax,%rax
jne    144e <main+0x1fe>
mov    0x10(%r15),%rdi
lea    0xc89(%rip),%rsi
call   1150 <fopen@plt>
test   %rax,%rax
je     14b6 <main+0x266>
mov    %rax,%rbp
mov    0x2c41(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %esi,%esi
call   1120 <fseek@plt>
mov    %rbp,%rdi
call   10f0 <ftell@plt>
mov    %eax,%r14d
mov    0x2c10(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1120 <fseek@plt>
mov    %r14d,%edi
call   1110 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1070 <fread@plt>
mov    %rbp,%rdi
call   10a0 <fclose@plt>
cmpb   $0x43,(%rbx)
jne    1415 <main+0x1c5>
cmpb   $0x36,0x1(%rbx)
jne    1415 <main+0x1c5>
xor    %eax,%eax
cmpb   $0x34,0x2(%rbx)
mov    $0x50,%r15d
cmovne %eax,%r15d
sub    %r15d,%r14d
lea    0xbfc(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
mov    %r15d,%eax
add    %rax,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1160 <WriteData@plt>
xor    %eax,%eax
cmp    $0x2000,%r14d
jl     14c2 <main+0x272>
call   1090 <Set16KMode@plt>
jmp    14c7 <main+0x277>
lea    0xbe1(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcasecmp@plt>
cmp    $0x2,%ebx
jne    146b <main+0x21b>
test   %rax,%rax
je     1358 <main+0x108>
lea    0xbca(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcasecmp@plt>
cmp    $0x2,%ebx
jne    14d8 <main+0x288>
test   %rax,%rax
jne    14d8 <main+0x288>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10c0 <getFubar@plt>
lea    0xbac(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <printf@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1100 <hexdump@plt>
jmp    1358 <main+0x108>
lea    0xbdc(%rip),%rdi
jmp    12f8 <main+0xa8>
call   1080 <Set8KMode@plt>
lea    0xbed(%rip),%rdi
call   1060 <puts@plt>
jmp    132f <main+0xdf>
lea    0xb75(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcasecmp@plt>
cmp    $0x2,%ebx
jne    1542 <main+0x2f2>
test   %rax,%rax
jne    1542 <main+0x2f2>
lea    0xb90(%rip),%rdi
call   1060 <puts@plt>
mov    $0xffffff80,%ebx
mov    %rsp,%rbp
sub    $0xffffff80,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   10d0 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1050 <getData@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1100 <hexdump@plt>
cmp    $0x3f7f,%ebx
jb     1505 <main+0x2b5>
jmp    1358 <main+0x108>
lea    0xb13(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcasecmp@plt>
cmp    $0x2,%ebx
jne    1577 <main+0x327>
test   %rax,%rax
jne    1577 <main+0x327>
xor    %eax,%eax
call   1140 <GetRamSize@plt>
lea    0xafb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
jmp    1358 <main+0x108>
lea    0xaff(%rip),%rdi
jmp    12f8 <main+0xa8>

LLVM IR (-O0):
@.str = [33 x i8] c"Usage: c64 <command> [filename]\0A\00"
@.str.1 = [11 x i8] c"Commands:\0A\00"
@.str.2 = [58 x i8] c" upload <filename> - Uploads CRT image to C64FC\0A\00"
@.str.3 = [76 x i8] c" download <filename> - Downloads image from C64FC (not implemented)\0A\00"
@.str.4 = [46 x i8] c" reset - Resets the C64\0A\00"
@.str.5 = [84 x i8] c" clear - Clears the entire C64FC, writes 0x0 to every address\0A\00"
@.str.6 = [67 x i8] c" hexdump - Prints a hexdump of the entire cart\0A\00"
@.str.7 = [62 x i8] c" bootloader - Enters bootloader mode on cart\0A\00"
@.str.8 = [55 x i8] c" ramsize - Prints your EEPROM size\0A\00"
@.str.9 = [76 x i8] c" chardump <fps> - Takes stdout from pipe and dump it on screen\0A\00"
@.str.10 = [17 x i8] c"C64FC not found\0A\00"
@.str.11 = [6 x i8] c"reset\00"
@.str.12 = [11 x i8] c"bootloader\00"
@.str.13 = [7 x i8] c"upload\00"
@.str.14 = [3 x i8] c"rb\00"
@.str.15 = [35 x i8] c"Can't open input file for reading\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.16 = [27 x i8] c"Sending data (%d bytes)..\0A\00"
@.str.17 = [17 x i8] c"Issuing reset..\0A\00"
@.str.18 = [6 x i8] c"clear\00"
@.str.19 = [6 x i8] c"fubar\00"
@.str.20 = [18 x i8] c"getFubar: %s(%d)\0A\00"
@.str.21 = [8 x i8] c"hexdump\00"
@.str.22 = [18 x i8] c"Hexdump of cart:\0A\00"
@.str.23 = [8 x i8] c"ramsize\00"
@.str.24 = [25 x i8] c"Your RAM is a %dkb chip\0A\00"
@.str.25 = [12 x i8] c"WHAT? WHAT\0A\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 i8*
%10 = alloca [10 x i8]
%11 = alloca i32
%12 = alloca [128 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %32
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 3
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.6, i64 0, i64 0))
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.7, i64 0, i64 0))
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.8, i64 0, i64 0))
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %213
32: ; preds = %18, %2
%33 = call i64 (...) @OpenDevice()
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %3
br label %213
37: ; preds = %32
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i64 @strcasecmp(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* %4
%45 = icmp eq i32 %44, 2
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 (...) @Reset()
br label %211
48: ; preds = %43, %37
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i64 @strcasecmp(i8* %51, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %48
%55 = load i32, i32* %4
%56 = icmp eq i32 %55, 2
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 (...) @EnterBootloader()
br label %210
59: ; preds = %54, %48
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 1
%62 = load i8*, i8** %61
%63 = call i64 @strcasecmp(i8* %62, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %136
65: ; preds = %59
%66 = load i32, i32* %4
%67 = icmp eq i32 %66, 3
br i1 %67, label %68, label %136
68: ; preds = %65
store i32 0, i32* %6
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 2
%71 = load i8*, i8** %70
%72 = call i32* @fopen(i8* %71, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0))
store i32* %72, i32** %7
%73 = load i32*, i32** %7
%74 = icmp ne i32* %73, null
br i1 %74, label %77, label %75
75: ; preds = %68
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.15, i64 0, i64 0))
store i32 1, i32* %3
br label %213
77: ; preds = %68
%78 = load i32*, i32** %7
%79 = load i32, i32* @SEEK_END
%80 = call i32 @fseek(i32* %78, i64 0, i32 %79)
%81 = load i32*, i32** %7
%82 = call i32 @ftell(i32* %81)
store i32 %82, i32* %8
%83 = load i32*, i32** %7
%84 = load i32, i32* @SEEK_SET
%85 = call i32 @fseek(i32* %83, i64 0, i32 %84)
%86 = load i32, i32* %8
%87 = call i8* @malloc(i32 %86)
store i8* %87, i8** %9
%88 = load i8*, i8** %9
%89 = load i32, i32* %8
%90 = load i32*, i32** %7
%91 = call i32 @fread(i8* %88, i32 %89, i32 1, i32* %90)
%92 = load i32*, i32** %7
%93 = call i32 @fclose(i32* %92)
%94 = load i8*, i8** %9
%95 = getelementptr inbounds i8, i8* %94, i64 0
%96 = load i8, i8* %95
%97 = zext i8 %96 to i32
%98 = icmp eq i32 %97, 67
br i1 %98, label %99, label %112
99: ; preds = %77
%100 = load i8*, i8** %9
%101 = getelementptr inbounds i8, i8* %100, i64 1
%102 = load i8, i8* %101
%103 = zext i8 %102 to i32
%104 = icmp eq i32 %103, 54
br i1 %104, label %105, label %112
105: ; preds = %99
%106 = load i8*, i8** %9
%107 = getelementptr inbounds i8, i8* %106, i64 2
%108 = load i8, i8* %107
%109 = zext i8 %108 to i32
%110 = icmp eq i32 %109, 52
br i1 %110, label %111, label %112
111: ; preds = %105
store i32 80, i32* %6
br label %112
112: ; preds = %111, %105, %99, %77
%113 = load i32, i32* %8
%114 = load i32, i32* %6
%115 = sub nsw i32 %113, %114
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.16, i64 0, i64 0), i32 %115)
%117 = load i8*, i8** %9
%118 = load i32, i32* %6
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds i8, i8* %117, i64 %119
%121 = load i32, i32* %8
%122 = load i32, i32* %6
%123 = sub nsw i32 %121, %122
%124 = call i32 @WriteData(i32 0, i8* %120, i32 %123)
%125 = load i32, i32* %8
%126 = load i32, i32* %6
%127 = sub nsw i32 %125, %126
%128 = icmp sge i32 %127, 8192
br i1 %128, label %129, label %131
129: ; preds = %112
%130 = call i32 (...) @Set16KMode()
br label %133
131: ; preds = %112
%132 = call i32 (...) @Set8KMode()
br label %133
133: ; preds = %131, %129
%134 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.17, i64 0, i64 0))
%135 = call i32 (...) @Reset()
br label %209
136: ; preds = %65, %59
%137 = load i8**, i8*** %5
%138 = getelementptr inbounds i8*, i8** %137, i64 1
%139 = load i8*, i8** %138
%140 = call i64 @strcasecmp(i8* %139, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.18, i64 0, i64 0))
%141 = icmp eq i64 %140, 0
br i1 %141, label %142, label %146
142: ; preds = %136
%143 = load i32, i32* %4
%144 = icmp eq i32 %143, 2
br i1 %144, label %145, label %146
145: ; preds = %142
br label %208
146: ; preds = %142, %136
%147 = load i8**, i8*** %5
%148 = getelementptr inbounds i8*, i8** %147, i64 1
%149 = load i8*, i8** %148
%150 = call i64 @strcasecmp(i8* %149, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.19, i64 0, i64 0))
%151 = icmp eq i64 %150, 0
br i1 %151, label %152, label %163
152: ; preds = %146
%153 = load i32, i32* %4
%154 = icmp eq i32 %153, 2
br i1 %154, label %155, label %163
155: ; preds = %152
%156 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%157 = call i32 @getFubar(i8* %156)
store i32 %157, i32* %11
%158 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%159 = load i32, i32* %11
%160 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.20, i64 0, i64 0), i8* %158, i32 %159)
%161 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%162 = call i32 @hexdump(i32 0, i8* %161, i32 10)
br label %207
163: ; preds = %152, %146
%164 = load i8**, i8*** %5
%165 = getelementptr inbounds i8*, i8** %164, i64 1
%166 = load i8*, i8** %165
%167 = call i64 @strcasecmp(i8* %166, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.21, i64 0, i64 0))
%168 = icmp eq i64 %167, 0
br i1 %168, label %169, label %190
169: ; preds = %163
%170 = load i32, i32* %4
%171 = icmp eq i32 %170, 2
br i1 %171, label %172, label %190
172: ; preds = %169
%173 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.22, i64 0, i64 0))
store i32 0, i32* %13
br label %174
174: ; preds = %186, %172
%175 = load i32, i32* %13
%176 = icmp slt i32 %175, 16383
br i1 %176, label %177, label %189
177: ; preds = %174
%178 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%179 = call i32 @memset(i8* %178, i32 0, i32 128)
%180 = load i32, i32* %13
%181 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%182 = call i32 @getData(i32 %180, i8* %181, i32 128)
store i32 %182, i32* %14
%183 = load i32, i32* %13
%184 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%185 = call i32 @hexdump(i32 %183, i8* %184, i32 128)
br label %186
186: ; preds = %177
%187 = load i32, i32* %13
%188 = add nsw i32 %187, 128
store i32 %188, i32* %13
br label %174
189: ; preds = %174
br label %206
190: ; preds = %169, %163
%191 = load i8**, i8*** %5
%192 = getelementptr inbounds i8*, i8** %191, i64 1
%193 = load i8*, i8** %192
%194 = call i64 @strcasecmp(i8* %193, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.23, i64 0, i64 0))
%195 = icmp eq i64 %194, 0
br i1 %195, label %196, label %203
196: ; preds = %190
%197 = load i32, i32* %4
%198 = icmp eq i32 %197, 2
br i1 %198, label %199, label %203
199: ; preds = %196
%200 = call i32 (...) @GetRamSize()
store i32 %200, i32* %15
%201 = load i32, i32* %15
%202 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.24, i64 0, i64 0), i32 %201)
br label %205
203: ; preds = %196, %190
%204 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.25, i64 0, i64 0))
store i32 1, i32* %3
br label %213
205: ; preds = %199
br label %206
206: ; preds = %205, %189
br label %207
207: ; preds = %206, %155
br label %208
208: ; preds = %207, %145
br label %209
209: ; preds = %208, %133
br label %210
210: ; preds = %209, %57
br label %211
211: ; preds = %210, %46
%212 = call i32 (...) @CloseDevice()
store i32 0, i32* %3
br label %213
213: ; preds = %211, %203, %75, %35, %21
%214 = load i32, i32* %3
ret i32 %214
}

LLVM IR (-O2):
@.str.11 = [6 x i8] c"reset\00"
@.str.12 = [11 x i8] c"bootloader\00"
@.str.13 = [7 x i8] c"upload\00"
@.str.14 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.16 = [27 x i8] c"Sending data (%d bytes)..\0A\00"
@.str.18 = [6 x i8] c"clear\00"
@.str.19 = [6 x i8] c"fubar\00"
@.str.20 = [18 x i8] c"getFubar: %s(%d)\0A\00"
@.str.21 = [8 x i8] c"hexdump\00"
@.str.23 = [8 x i8] c"ramsize\00"
@.str.24 = [25 x i8] c"Your RAM is a %dkb chip\0A\00"
@str = [11 x i8] c"WHAT? WHAT\00"
@str.26 = [17 x i8] c"Hexdump of cart:\00"
@str.27 = [34 x i8] c"Can't open input file for reading\00"
@str.28 = [16 x i8] c"Issuing reset..\00"
@str.29 = [16 x i8] c"C64FC not found\00"
@str.30 = [32 x i8] c"Usage: c64 <command> [filename]\00"
@str.31 = [10 x i8] c"Commands:\00"
@str.32 = [57 x i8] c" upload <filename> - Uploads CRT image to C64FC\00"
@str.33 = [75 x i8] c" download <filename> - Downloads image from C64FC (not implemented)\00"
@str.34 = [45 x i8] c" reset - Resets the C64\00"
@str.35 = [83 x i8] c" clear - Clears the entire C64FC, writes 0x0 to every address\00"
@str.36 = [66 x i8] c" hexdump - Prints a hexdump of the entire cart\00"
@str.37 = [61 x i8] c" bootloader - Enters bootloader mode on cart\00"
@str.38 = [54 x i8] c" ramsize - Prints your EEPROM size\00"
@str.39 = [75 x i8] c" chardump <fps> - Takes stdout from pipe and dump it on screen\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca [128 x i8]
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 2
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.30, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.31, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.32, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @str.33, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.34, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([83 x i8], [83 x i8]* @str.35, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @str.36, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.37, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.38, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @str.39, i64 0, i64 0))
br label %124
18: ; preds = %2
%19 = tail call i64 (...) @OpenDevice()
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.29, i64 0, i64 0))
br label %124
23: ; preds = %18
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = tail call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.11, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
%28 = icmp eq i32 %0, 2
%29 = and i1 %28, %27
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = tail call i32 (...) @Reset()
br label %122
32: ; preds = %23
%33 = tail call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
%35 = and i1 %28, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = tail call i32 (...) @EnterBootloader()
br label %122
38: ; preds = %32
%39 = tail call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
%41 = icmp eq i32 %0, 3
%42 = and i1 %41, %40
br i1 %42, label %43, label %85
43: ; preds = %38
%44 = getelementptr inbounds i8*, i8** %1, i64 2
%45 = load i8*, i8** %44
%46 = tail call i32* @fopen(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0))
%47 = icmp eq i32* %46, null
br i1 %47, label %83, label %48
48: ; preds = %43
%49 = load i32, i32* @SEEK_END
%50 = tail call i32 @fseek(i32* %46, i64 0, i32 %49)
%51 = tail call i32 @ftell(i32* %46)
%52 = load i32, i32* @SEEK_SET
%53 = tail call i32 @fseek(i32* %46, i64 0, i32 %52)
%54 = tail call i8* @malloc(i32 %51)
%55 = tail call i32 @fread(i8* %54, i32 %51, i32 1, i32* %46)
%56 = tail call i32 @fclose(i32* %46)
%57 = load i8, i8* %54
%58 = icmp eq i8 %57, 67
br i1 %58, label %59, label %68
59: ; preds = %48
%60 = getelementptr inbounds i8, i8* %54, i64 1
%61 = load i8, i8* %60
%62 = icmp eq i8 %61, 54
br i1 %62, label %63, label %68
63: ; preds = %59
%64 = getelementptr inbounds i8, i8* %54, i64 2
%65 = load i8, i8* %64
%66 = icmp eq i8 %65, 52
%67 = select i1 %66, i32 80, i32 0
br label %68
68: ; preds = %63, %59, %48
%69 = phi i32 [ 0, %59 ], [ 0, %48 ], [ %67, %63 ]
%70 = sub nsw i32 %51, %69
%71 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.16, i64 0, i64 0), i32 %70)
%72 = zext i32 %69 to i64
%73 = getelementptr inbounds i8, i8* %54, i64 %72
%74 = tail call i32 @WriteData(i32 0, i8* %73, i32 %70)
%75 = icmp sgt i32 %70, 8191
br i1 %75, label %76, label %78
76: ; preds = %68
%77 = tail call i32 (...) @Set16KMode()
br label %80
78: ; preds = %68
%79 = tail call i32 (...) @Set8KMode()
br label %80
80: ; preds = %76, %78
%81 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.28, i64 0, i64 0))
%82 = tail call i32 (...) @Reset()
br label %122
83: ; preds = %43
%84 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.27, i64 0, i64 0))
br label %124
85: ; preds = %38
%86 = tail call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.18, i64 0, i64 0))
%87 = icmp eq i64 %86, 0
%88 = and i1 %28, %87
br i1 %88, label %122, label %89
89: ; preds = %85
%90 = tail call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.19, i64 0, i64 0))
%91 = icmp eq i64 %90, 0
%92 = and i1 %28, %91
br i1 %92, label %93, label %98
93: ; preds = %89
%94 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%95 = call i32 @getFubar(i8* %94)
%96 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.20, i64 0, i64 0), i8* %94, i32 %95)
%97 = call i32 @hexdump(i32 0, i8* %94, i32 10)
br label %122
98: ; preds = %89
%99 = tail call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.21, i64 0, i64 0))
%100 = icmp eq i64 %99, 0
%101 = and i1 %28, %100
br i1 %101, label %102, label %113
102: ; preds = %98
%103 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%104 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.26, i64 0, i64 0))
br label %105
105: ; preds = %102, %105
%106 = phi i32 [ 0, %102 ], [ %110, %105 ]
%107 = call i32 @memset(i8* %103, i32 0, i32 128)
%108 = call i32 @getData(i32 %106, i8* %103, i32 128)
%109 = call i32 @hexdump(i32 %106, i8* %103, i32 128)
%110 = add nuw nsw i32 %106, 128
%111 = icmp ult i32 %106, 16255
br i1 %111, label %105, label %112
112: ; preds = %105
br label %122
113: ; preds = %98
%114 = tail call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.23, i64 0, i64 0))
%115 = icmp eq i64 %114, 0
%116 = and i1 %28, %115
br i1 %116, label %117, label %120
117: ; preds = %113
%118 = tail call i32 (...) @GetRamSize()
%119 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.24, i64 0, i64 0), i32 %118)
br label %122
120: ; preds = %113
%121 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %124
122: ; preds = %80, %36, %85, %112, %117, %93, %30
%123 = call i32 (...) @CloseDevice()
br label %124
124: ; preds = %83, %122, %120, %21, %7
%125 = phi i32 [ 1, %7 ], [ 1, %21 ], [ 0, %122 ], [ 1, %83 ], [ 1, %120 ]
ret i32 %125
}