#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;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ getFilenameExt(char *);
scalar_t__ malloc(long);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
unsigned char toupper(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  FILE *fout, *fin, *fbase;
  if (argc < 3 || argc > 4) {
    printf("Usage:\n");
    printf("SongInserter [output.gb] [song.gbs] (uses default base ROM)\n");
    printf("SongInserter [output.gb] [song.gbs] [base_rom.gb]\n");
    return 0;
  } else {
    printf("Starting...\n");
    if (argc == 4) {
      if (strcmp(getFilenameExt(argv[3]), ".gb") != 0) {
        printf("ERROR: Base ROM must have the file name extension '.gb'\n");
        return 1;
      }
      fbase = fopen(argv[3], "rb");
    } else {
      fbase = fopen("rom.gb", "rb");
    }
    if (strcmp(getFilenameExt(argv[2]), ".gbs") != 0) {
      printf(
          "ERROR: The input song must have the file name extension '.gbs'\n");
      return 1;
    }
    fin = fopen(argv[2], "rb");
    if (strcmp(getFilenameExt(argv[1]), ".gb") != 0) {
      printf("ERROR: The output ROM must have the file name extension '.gb'\n");
      return 1;
    }
    fout = fopen(argv[1], "wb");
    if (fbase == NULL) {
      printf("ERROR: Failed to open base ROM '%s'\n",
             argc == 4 ? argv[3] : "rom.gb");
      return 1;
    }
    if (fin == NULL) {
      printf("ERROR: Failed to open gbs file '%s'\n", argv[2]);
      return 1;
    }
    if (fout == NULL) {
      printf("ERROR: Failed to open output file '%s'\n", argv[1]);
      return 1;
    }
    fseek(fbase, 0, SEEK_END);
    long fsize_rom = ftell(fbase);
    if (fsize_rom < 0x80000) {
      printf("ERROR: Base ROM is too small. It should be 0x%x (%i) bytes or "
             "more.\n",
             0x80000, 0x80000);
      return 1;
    }
    unsigned char *rom =
        (unsigned char *)malloc(fsize_rom * sizeof(unsigned char));
    fseek(fbase, 0, SEEK_SET);
    fread(rom, 1, fsize_rom, fbase);
    fclose(fbase);
    printf("Writing song data\n");
    fseek(fin, 0, SEEK_END);
    long song_data_size = ftell(fin);
    if (song_data_size > 0x7C000 - 0x500) {
      printf("ERROR: Cannot insert song without overwriting graphics data.\n");
      free(rom);
      return 1;
    }
    fseek(fin, 0x70, SEEK_SET);
    fread(&(rom[0x500]), 1, song_data_size, fin);
    printf("Writing song name\n");
    fseek(fin, 0x10, SEEK_SET);
    for (int i = 0; i < 18; i++) {
      unsigned char c = toupper(fgetc(fin));
      rom[0x7C715 + i] = c == 0 ? ' ' : c;
    }
    printf("Writing song author\n");
    fseek(fin, 0x30, SEEK_SET);
    for (int i = 0; i < 18; i++) {
      unsigned char c = toupper(fgetc(fin));
      rom[0x7C729 + i] = c == 0 ? ' ' : c;
    }
    printf("Writing buffer to ROM\n");
    fwrite(rom, 1, fsize_rom, fout);
    fclose(fin);
    fclose(fout);
    free(rom);
    printf("Success!\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11fa <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    1230 <main+0x60>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x4,-0x8(%rbp)
jne    12a4 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <getFilenameExt@plt>
mov    %eax,%edi
lea    0xe66(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1287 <main+0xb7>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe28(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x28(%rbp)
jmp    12bb <main+0xeb>
lea    0xe16(%rip),%rdi
lea    0xe0c(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <getFilenameExt@plt>
mov    %eax,%edi
lea    0xdf7(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12fa <main+0x12a>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdb5(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <getFilenameExt@plt>
mov    %eax,%edi
lea    0xd9c(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1351 <main+0x181>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdec(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    13bd <main+0x1ed>
cmpl   $0x4,-0x8(%rbp)
jne    138f <main+0x1bf>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    139f <main+0x1cf>
lea    0xd2b(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    139f <main+0x1cf>
mov    -0x58(%rbp),%rsi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
cmpq   $0x0,-0x20(%rbp)
jne    13ea <main+0x21a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
cmpq   $0x0,-0x18(%rbp)
jne    1417 <main+0x247>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
mov    -0x28(%rbp),%rdi
mov    0x2bc6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x80000,-0x30(%rbp)
jge    1467 <main+0x297>
lea    0xd74(%rip),%rdi
mov    $0x80000,%edx
mov    %edx,%esi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
mov    -0x30(%rbp),%rdi
shl    $0x0,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2b55(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2b24(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x7bb00,-0x40(%rbp)
jle    150b <main+0x33b>
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    16b3 <main+0x4e3>
mov    -0x20(%rbp),%rdi
mov    0x2ac2(%rip),%rax
mov    (%rax),%edx
mov    $0x70,%esi
call   10d0 <fseek@plt>
mov    -0x38(%rbp),%rdi
add    $0x500,%rdi
mov    -0x40(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2a80(%rip),%rax
mov    (%rax),%edx
mov    $0x10,%esi
call   10d0 <fseek@plt>
movl   $0x0,-0x44(%rbp)
cmpl   $0x12,-0x44(%rbp)
jge    15cf <main+0x3ff>
mov    -0x20(%rbp),%rdi
call   1080 <fgetc@plt>
mov    %eax,%edi
call   1040 <toupper@plt>
mov    %al,-0x45(%rbp)
movzbl -0x45(%rbp),%eax
cmp    $0x0,%eax
jne    15a2 <main+0x3d2>
mov    $0x20,%eax
mov    %eax,-0x5c(%rbp)
jmp    15a9 <main+0x3d9>
movzbl -0x45(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    %al,%dl
mov    -0x38(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x7c715,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    156b <main+0x39b>
lea    0xc94(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x29f0(%rip),%rax
mov    (%rax),%edx
mov    $0x30,%esi
call   10d0 <fseek@plt>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x12,-0x4c(%rbp)
jge    165f <main+0x48f>
mov    -0x20(%rbp),%rdi
call   1080 <fgetc@plt>
mov    %eax,%edi
call   1040 <toupper@plt>
mov    %al,-0x4d(%rbp)
movzbl -0x4d(%rbp),%eax
cmp    $0x0,%eax
jne    1632 <main+0x462>
mov    $0x20,%eax
mov    %eax,-0x60(%rbp)
jmp    1639 <main+0x469>
movzbl -0x4d(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
mov    %al,%dl
mov    -0x38(%rbp),%rax
mov    -0x4c(%rbp),%ecx
add    $0x7c729,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    15fb <main+0x42b>
lea    0xc19(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
lea    0xbf1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
lea    -0x5(%rbp),%eax
cmp    $0xfffffffd,%eax
ja     1220 <main+0x40>
lea    0x1022(%rip),%rdi
call   1050 <puts@plt>
lea    0x101d(%rip),%rdi
call   1050 <puts@plt>
lea    0x104d(%rip),%rdi
call   1050 <puts@plt>
xor    %ebp,%ebp
jmp    12da <main+0xfa>
mov    %rsi,%rbx
lea    0xe9f(%rip),%rdi
call   1050 <puts@plt>
cmp    $0x4,%ebp
jne    1259 <main+0x79>
mov    0x18(%rbx),%rdi
call   10c0 <getFilenameExt@plt>
lea    0xdc2(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1262 <main+0x82>
lea    0xf8f(%rip),%rdi
jmp    12d0 <main+0xf0>
lea    0xda3(%rip),%rdi
jmp    1266 <main+0x86>
mov    0x18(%rbx),%rdi
lea    0xd93(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%r12
mov    0x10(%rbx),%rdi
call   10c0 <getFilenameExt@plt>
lea    0xd85(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     129a <main+0xba>
lea    0xf0f(%rip),%rdi
jmp    12d0 <main+0xf0>
mov    0x10(%rbx),%rdi
lea    0xd5b(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,%r13
mov    0x8(%rbx),%rdi
call   10c0 <getFilenameExt@plt>
lea    0xd49(%rip),%rsi
mov    %eax,%edi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12eb <main+0x10b>
lea    0xe99(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
lea    0xd19(%rip),%rsi
call   10f0 <fopen@plt>
test   %r12,%r12
je     134a <main+0x16a>
test   %r13,%r13
je     1355 <main+0x175>
mov    %rax,%r14
test   %rax,%rax
je     1372 <main+0x192>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    %r12,%rdi
call   10b0 <ftell@plt>
cmp    $0x7ffff,%rax
jg     1389 <main+0x1a9>
lea    0xd4d(%rip),%rdi
mov    $0x80000,%esi
mov    $0x80000,%edx
xor    %eax,%eax
call   1080 <printf@plt>
jmp    12d5 <main+0xf5>
cmp    $0x4,%ebp
jne    1362 <main+0x182>
mov    0x18(%rbx),%rsi
jmp    1369 <main+0x189>
mov    0x10(%rbx),%rsi
lea    0xcd7(%rip),%rdi
jmp    137d <main+0x19d>
lea    0xc9a(%rip),%rsi
lea    0xca2(%rip),%rdi
jmp    137d <main+0x19d>
mov    0x8(%rbx),%rsi
lea    0xcdf(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    12d5 <main+0xf5>
mov    %rax,%rdi
mov    %rax,%rbx
call   10d0 <malloc@plt>
mov    %rax,%r15
mov    0x2c3a(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    $0x1,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,(%rsp)
mov    %rbx,%rdx
mov    %r12,%rcx
call   1060 <fread@plt>
mov    %r12,%rdi
call   1070 <fclose@plt>
lea    0xd00(%rip),%rdi
call   1050 <puts@plt>
mov    0x2c07(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    %r13,%rdi
call   10b0 <ftell@plt>
cmp    $0x7bb01,%rax
jl     1416 <main+0x236>
lea    0xd28(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12da <main+0xfa>
mov    %rax,%rbx
mov    0x2bb8(%rip),%r12
mov    (%r12),%edx
mov    %r13,%rdi
mov    $0x70,%esi
call   10e0 <fseek@plt>
mov    %r15,%rdi
add    $0x500,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1060 <fread@plt>
lea    0xc95(%rip),%rdi
call   1050 <puts@plt>
mov    (%r12),%edx
mov    %r13,%rdi
mov    $0x10,%esi
call   10e0 <fseek@plt>
mov    $0x7c715,%ebx
mov    $0x20,%ebp
mov    %r13,%rdi
call   1090 <fgetc@plt>
mov    %eax,%edi
call   1040 <toupper@plt>
test   %al,%al
movzbl %al,%eax
cmove  %ebp,%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0x7c727,%rbx
jne    1472 <main+0x292>
lea    0xc58(%rip),%rdi
call   1050 <puts@plt>
mov    (%r12),%edx
mov    %r13,%rdi
mov    $0x30,%esi
call   10e0 <fseek@plt>
mov    $0x7c729,%ebx
mov    $0x20,%ebp
mov    %r13,%rdi
call   1090 <fgetc@plt>
mov    %eax,%edi
call   1040 <toupper@plt>
test   %al,%al
movzbl %al,%eax
cmove  %ebp,%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0x7c73b,%rbx
jne    14c1 <main+0x2e1>
lea    0xc1d(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    (%rsp),%rdx
mov    %r14,%rcx
call   1100 <fwrite@plt>
mov    %r13,%rdi
call   1070 <fclose@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xbfb(%rip),%rdi
jmp    1214 <main+0x34>

LLVM IR (-O0):
@.str = [8 x i8] c"Usage:\0A\00"
@.str.1 = [61 x i8] c"SongInserter [output.gb] [song.gbs] (uses default base ROM)\0A\00"
@.str.2 = [51 x i8] c"SongInserter [output.gb] [song.gbs] [base_rom.gb]\0A\00"
@.str.3 = [13 x i8] c"Starting...\0A\00"
@.str.4 = [4 x i8] c".gb\00"
@.str.5 = [57 x i8] c"ERROR: Base ROM must have the file name extension '.gb'\0A\00"
@.str.6 = [3 x i8] c"rb\00"
@.str.7 = [7 x i8] c"rom.gb\00"
@.str.8 = [5 x i8] c".gbs\00"
@.str.9 = [64 x i8] c"ERROR: The input song must have the file name extension '.gbs'\0A\00"
@.str.10 = [63 x i8] c"ERROR: The output ROM must have the file name extension '.gb'\0A\00"
@.str.11 = [3 x i8] c"wb\00"
@.str.12 = [37 x i8] c"ERROR: Failed to open base ROM '%s'\0A\00"
@.str.13 = [37 x i8] c"ERROR: Failed to open gbs file '%s'\0A\00"
@.str.14 = [40 x i8] c"ERROR: Failed to open output file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.15 = [69 x i8] c"ERROR: Base ROM is too small. It should be 0x%x (%i) bytes or more.\0A\00"
@SEEK_SET = global i32 0
@.str.16 = [19 x i8] c"Writing song data\0A\00"
@.str.17 = [62 x i8] c"ERROR: Cannot insert song without overwriting graphics data.\0A\00"
@.str.18 = [19 x i8] c"Writing song name\0A\00"
@.str.19 = [21 x i8] c"Writing song author\0A\00"
@.str.20 = [23 x i8] c"Writing buffer to ROM\0A\00"
@.str.21 = [10 x i8] c"Success!\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 i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8
%14 = alloca i32
%15 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 3
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 4
br i1 %20, label %21, label %25
21: ; preds = %18, %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %219
25: ; preds = %18
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 4
br i1 %28, label %29, label %43
29: ; preds = %25
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @getFilenameExt(i8* %32)
%34 = call i64 @strcmp(i32 %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %219
38: ; preds = %29
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %42, i32** %8
br label %45
43: ; preds = %25
%44 = call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %44, i32** %8
br label %45
45: ; preds = %43, %38
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = call i32 @getFilenameExt(i8* %48)
%50 = call i64 @strcmp(i32 %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %219
54: ; preds = %45
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32* @fopen(i8* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %58, i32** %7
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = call i32 @getFilenameExt(i8* %61)
%63 = call i64 @strcmp(i32 %62, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %54
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %3
br label %219
67: ; preds = %54
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = call i32* @fopen(i8* %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0))
store i32* %71, i32** %6
%72 = load i32*, i32** %8
%73 = icmp eq i32* %72, null
br i1 %73, label %74, label %85
74: ; preds = %67
%75 = load i32, i32* %4
%76 = icmp eq i32 %75, 4
br i1 %76, label %77, label %81
77: ; preds = %74
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 3
%80 = load i8*, i8** %79
br label %82
81: ; preds = %74
br label %82
82: ; preds = %81, %77
%83 = phi i8* [ %80, %77 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), %81 ]
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.12, i64 0, i64 0), i8* %83)
store i32 1, i32* %3
br label %219
85: ; preds = %67
%86 = load i32*, i32** %7
%87 = icmp eq i32* %86, null
br i1 %87, label %88, label %93
88: ; preds = %85
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 2
%91 = load i8*, i8** %90
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.13, i64 0, i64 0), i8* %91)
store i32 1, i32* %3
br label %219
93: ; preds = %85
%94 = load i32*, i32** %6
%95 = icmp eq i32* %94, null
br i1 %95, label %96, label %101
96: ; preds = %93
%97 = load i8**, i8*** %5
%98 = getelementptr inbounds i8*, i8** %97, i64 1
%99 = load i8*, i8** %98
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.14, i64 0, i64 0), i8* %99)
store i32 1, i32* %3
br label %219
101: ; preds = %93
%102 = load i32*, i32** %8
%103 = load i32, i32* @SEEK_END
%104 = call i32 @fseek(i32* %102, i32 0, i32 %103)
%105 = load i32*, i32** %8
%106 = call i64 @ftell(i32* %105)
store i64 %106, i64* %9
%107 = load i64, i64* %9
%108 = icmp slt i64 %107, 524288
br i1 %108, label %109, label %111
109: ; preds = %101
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.15, i64 0, i64 0), i32 524288, i32 524288)
store i32 1, i32* %3
br label %219
111: ; preds = %101
%112 = load i64, i64* %9
%113 = mul i64 %112, 1
%114 = call i64 @malloc(i64 %113)
%115 = inttoptr i64 %114 to i8*
store i8* %115, i8** %10
%116 = load i32*, i32** %8
%117 = load i32, i32* @SEEK_SET
%118 = call i32 @fseek(i32* %116, i32 0, i32 %117)
%119 = load i8*, i8** %10
%120 = load i64, i64* %9
%121 = load i32*, i32** %8
%122 = call i32 @fread(i8* %119, i32 1, i64 %120, i32* %121)
%123 = load i32*, i32** %8
%124 = call i32 @fclose(i32* %123)
%125 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.16, i64 0, i64 0))
%126 = load i32*, i32** %7
%127 = load i32, i32* @SEEK_END
%128 = call i32 @fseek(i32* %126, i32 0, i32 %127)
%129 = load i32*, i32** %7
%130 = call i64 @ftell(i32* %129)
store i64 %130, i64* %11
%131 = load i64, i64* %11
%132 = icmp sgt i64 %131, 506624
br i1 %132, label %133, label %137
133: ; preds = %111
%134 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.17, i64 0, i64 0))
%135 = load i8*, i8** %10
%136 = call i32 @free(i8* %135)
store i32 1, i32* %3
br label %219
137: ; preds = %111
%138 = load i32*, i32** %7
%139 = load i32, i32* @SEEK_SET
%140 = call i32 @fseek(i32* %138, i32 112, i32 %139)
%141 = load i8*, i8** %10
%142 = getelementptr inbounds i8, i8* %141, i64 1280
%143 = load i64, i64* %11
%144 = load i32*, i32** %7
%145 = call i32 @fread(i8* %142, i32 1, i64 %143, i32* %144)
%146 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.18, i64 0, i64 0))
%147 = load i32*, i32** %7
%148 = load i32, i32* @SEEK_SET
%149 = call i32 @fseek(i32* %147, i32 16, i32 %148)
store i32 0, i32* %12
br label %150
150: ; preds = %172, %137
%151 = load i32, i32* %12
%152 = icmp slt i32 %151, 18
br i1 %152, label %153, label %175
153: ; preds = %150
%154 = load i32*, i32** %7
%155 = call i32 @fgetc(i32* %154)
%156 = call zeroext i8 @toupper(i32 %155)
store i8 %156, i8* %13
%157 = load i8, i8* %13
%158 = zext i8 %157 to i32
%159 = icmp eq i32 %158, 0
br i1 %159, label %160, label %161
160: ; preds = %153
br label %164
161: ; preds = %153
%162 = load i8, i8* %13
%163 = zext i8 %162 to i32
br label %164
164: ; preds = %161, %160
%165 = phi i32 [ 32, %160 ], [ %163, %161 ]
%166 = trunc i32 %165 to i8
%167 = load i8*, i8** %10
%168 = load i32, i32* %12
%169 = add nsw i32 509717, %168
%170 = sext i32 %169 to i64
%171 = getelementptr inbounds i8, i8* %167, i64 %170
store i8 %166, i8* %171
br label %172
172: ; preds = %164
%173 = load i32, i32* %12
%174 = add nsw i32 %173, 1
store i32 %174, i32* %12
br label %150
175: ; preds = %150
%176 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.19, i64 0, i64 0))
%177 = load i32*, i32** %7
%178 = load i32, i32* @SEEK_SET
%179 = call i32 @fseek(i32* %177, i32 48, i32 %178)
store i32 0, i32* %14
br label %180
180: ; preds = %202, %175
%181 = load i32, i32* %14
%182 = icmp slt i32 %181, 18
br i1 %182, label %183, label %205
183: ; preds = %180
%184 = load i32*, i32** %7
%185 = call i32 @fgetc(i32* %184)
%186 = call zeroext i8 @toupper(i32 %185)
store i8 %186, i8* %15
%187 = load i8, i8* %15
%188 = zext i8 %187 to i32
%189 = icmp eq i32 %188, 0
br i1 %189, label %190, label %191
190: ; preds = %183
br label %194
191: ; preds = %183
%192 = load i8, i8* %15
%193 = zext i8 %192 to i32
br label %194
194: ; preds = %191, %190
%195 = phi i32 [ 32, %190 ], [ %193, %191 ]
%196 = trunc i32 %195 to i8
%197 = load i8*, i8** %10
%198 = load i32, i32* %14
%199 = add nsw i32 509737, %198
%200 = sext i32 %199 to i64
%201 = getelementptr inbounds i8, i8* %197, i64 %200
store i8 %196, i8* %201
br label %202
202: ; preds = %194
%203 = load i32, i32* %14
%204 = add nsw i32 %203, 1
store i32 %204, i32* %14
br label %180
205: ; preds = %180
%206 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.20, i64 0, i64 0))
%207 = load i8*, i8** %10
%208 = load i64, i64* %9
%209 = load i32*, i32** %6
%210 = call i32 @fwrite(i8* %207, i32 1, i64 %208, i32* %209)
%211 = load i32*, i32** %7
%212 = call i32 @fclose(i32* %211)
%213 = load i32*, i32** %6
%214 = call i32 @fclose(i32* %213)
%215 = load i8*, i8** %10
%216 = call i32 @free(i8* %215)
%217 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.21, i64 0, i64 0))
br label %218
218: ; preds = %205
store i32 0, i32* %3
br label %219
219: ; preds = %218, %133, %109, %96, %88, %82, %65, %52, %36, %21
%220 = load i32, i32* %3
ret i32 %220
}

LLVM IR (-O2):
@.str.4 = [4 x i8] c".gb\00"
@.str.6 = [3 x i8] c"rb\00"
@.str.7 = [7 x i8] c"rom.gb\00"
@.str.8 = [5 x i8] c".gbs\00"
@.str.11 = [3 x i8] c"wb\00"
@.str.12 = [37 x i8] c"ERROR: Failed to open base ROM '%s'\0A\00"
@.str.13 = [37 x i8] c"ERROR: Failed to open gbs file '%s'\0A\00"
@.str.14 = [40 x i8] c"ERROR: Failed to open output file '%s'\0A\00"
@SEEK_END = global i32 0
@.str.15 = [69 x i8] c"ERROR: Base ROM is too small. It should be 0x%x (%i) bytes or more.\0A\00"
@SEEK_SET = global i32 0
@str = [12 x i8] c"Starting...\00"
@str.22 = [18 x i8] c"Writing song data\00"
@str.23 = [18 x i8] c"Writing song name\00"
@str.24 = [20 x i8] c"Writing song author\00"
@str.25 = [22 x i8] c"Writing buffer to ROM\00"
@str.26 = [9 x i8] c"Success!\00"
@str.27 = [61 x i8] c"ERROR: Cannot insert song without overwriting graphics data.\00"
@str.28 = [62 x i8] c"ERROR: The output ROM must have the file name extension '.gb'\00"
@str.29 = [63 x i8] c"ERROR: The input song must have the file name extension '.gbs'\00"
@str.30 = [56 x i8] c"ERROR: Base ROM must have the file name extension '.gb'\00"
@str.31 = [7 x i8] c"Usage:\00"
@str.32 = [60 x i8] c"SongInserter [output.gb] [song.gbs] (uses default base ROM)\00"
@str.33 = [50 x i8] c"SongInserter [output.gb] [song.gbs] [base_rom.gb]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.31, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str.32, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.33, i64 0, i64 0))
br label %124
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%11 = icmp eq i32 %0, 4
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = getelementptr inbounds i8*, i8** %1, i64 3
%14 = load i8*, i8** %13
%15 = tail call i32 @getFilenameExt(i8* %14)
%16 = tail call i64 @strcmp(i32 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.30, i64 0, i64 0))
br label %124
20: ; preds = %12
%21 = load i8*, i8** %13
br label %22
22: ; preds = %9, %20
%23 = phi i8* [ %21, %20 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), %9 ]
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i32 @getFilenameExt(i8* %26)
%28 = tail call i64 @strcmp(i32 %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %22
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @str.29, i64 0, i64 0))
br label %124
32: ; preds = %22
%33 = load i8*, i8** %25
%34 = tail call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = tail call i32 @getFilenameExt(i8* %36)
%38 = tail call i64 @strcmp(i32 %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %32
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str.28, i64 0, i64 0))
br label %124
42: ; preds = %32
%43 = load i8*, i8** %35
%44 = tail call i32* @fopen(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0))
%45 = icmp eq i32* %24, null
br i1 %45, label %46, label %53
46: ; preds = %42
br i1 %11, label %47, label %50
47: ; preds = %46
%48 = getelementptr inbounds i8*, i8** %1, i64 3
%49 = load i8*, i8** %48
br label %50
50: ; preds = %46, %47
%51 = phi i8* [ %49, %47 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), %46 ]
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.12, i64 0, i64 0), i8* %51)
br label %124
53: ; preds = %42
%54 = icmp eq i32* %34, null
br i1 %54, label %55, label %58
55: ; preds = %53
%56 = load i8*, i8** %25
%57 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.13, i64 0, i64 0), i8* %56)
br label %124
58: ; preds = %53
%59 = icmp eq i32* %44, null
br i1 %59, label %60, label %63
60: ; preds = %58
%61 = load i8*, i8** %35
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.14, i64 0, i64 0), i8* %61)
br label %124
63: ; preds = %58
%64 = load i32, i32* @SEEK_END
%65 = tail call i32 @fseek(i32* %24, i32 0, i32 %64)
%66 = tail call i64 @ftell(i32* %24)
%67 = icmp slt i64 %66, 524288
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str.15, i64 0, i64 0), i32 524288, i32 524288)
br label %124
70: ; preds = %63
%71 = tail call i64 @malloc(i64 %66)
%72 = inttoptr i64 %71 to i8*
%73 = load i32, i32* @SEEK_SET
%74 = tail call i32 @fseek(i32* %24, i32 0, i32 %73)
%75 = tail call i32 @fread(i8* %72, i32 1, i64 %66, i32* %24)
%76 = tail call i32 @fclose(i32* %24)
%77 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.22, i64 0, i64 0))
%78 = load i32, i32* @SEEK_END
%79 = tail call i32 @fseek(i32* %34, i32 0, i32 %78)
%80 = tail call i64 @ftell(i32* %34)
%81 = icmp sgt i64 %80, 506624
br i1 %81, label %82, label %85
82: ; preds = %70
%83 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.27, i64 0, i64 0))
%84 = tail call i32 @free(i8* %72)
br label %124
85: ; preds = %70
%86 = load i32, i32* @SEEK_SET
%87 = tail call i32 @fseek(i32* %34, i32 112, i32 %86)
%88 = getelementptr inbounds i8, i8* %72, i64 1280
%89 = tail call i32 @fread(i8* %88, i32 1, i64 %80, i32* %34)
%90 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.23, i64 0, i64 0))
%91 = load i32, i32* @SEEK_SET
%92 = tail call i32 @fseek(i32* %34, i32 16, i32 %91)
br label %97
93: ; preds = %97
%94 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.24, i64 0, i64 0))
%95 = load i32, i32* @SEEK_SET
%96 = tail call i32 @fseek(i32* %34, i32 48, i32 %95)
br label %114
97: ; preds = %85, %97
%98 = phi i64 [ 0, %85 ], [ %105, %97 ]
%99 = tail call i32 @fgetc(i32* %34)
%100 = tail call zeroext i8 @toupper(i32 %99)
%101 = icmp eq i8 %100, 0
%102 = select i1 %101, i8 32, i8 %100
%103 = add nuw nsw i64 %98, 509717
%104 = getelementptr inbounds i8, i8* %72, i64 %103
store i8 %102, i8* %104
%105 = add nuw nsw i64 %98, 1
%106 = icmp eq i64 %105, 18
br i1 %106, label %93, label %97
107: ; preds = %114
%108 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.25, i64 0, i64 0))
%109 = tail call i32 @fwrite(i8* %72, i32 1, i64 %66, i32* %44)
%110 = tail call i32 @fclose(i32* %34)
%111 = tail call i32 @fclose(i32* %44)
%112 = tail call i32 @free(i8* %72)
%113 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.26, i64 0, i64 0))
br label %124
114: ; preds = %93, %114
%115 = phi i64 [ 0, %93 ], [ %122, %114 ]
%116 = tail call i32 @fgetc(i32* %34)
%117 = tail call zeroext i8 @toupper(i32 %116)
%118 = icmp eq i8 %117, 0
%119 = select i1 %118, i8 32, i8 %117
%120 = add nuw nsw i64 %115, 509737
%121 = getelementptr inbounds i8, i8* %72, i64 %120
store i8 %119, i8* %121
%122 = add nuw nsw i64 %115, 1
%123 = icmp eq i64 %122, 18
br i1 %123, label %107, label %114
124: ; preds = %68, %107, %82, %60, %55, %50, %40, %30, %18, %5
%125 = phi i32 [ 0, %5 ], [ 1, %18 ], [ 1, %30 ], [ 1, %40 ], [ 1, %50 ], [ 1, %55 ], [ 1, %60 ], [ 1, %68 ], [ 1, %82 ], [ 0, %107 ]
ret i32 %125
}