#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 */
char *calloc(scalar_t__, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
char *strcat(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ writeFile(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Usage: %s gameboyFilename\n", argv[0]);
  }
  FILE *gbFile = fopen(argv[1], "rb");
  if (gbFile == NULL) {
    printf("Unable to open %s\n", argv[1]);
    exit(1);
  }
  char *outFilename = calloc(strlen(argv[1]) + strlen(".c"), sizeof(char));
  outFilename = strcat(argv[1], ".c");
  FILE *outFile = fopen(outFilename, "w");
  if (outFile == NULL) {
    printf("Unable to open %s for writing\n", outFilename);
    fclose(gbFile);
    exit(1);
  }
  writeFile(gbFile, outFile);
  fclose(gbFile);
  fclose(outFile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b3 <main+0x33>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe59(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <main+0x74>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
lea    0xe25(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfc(%rip),%rsi
call   1090 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1280 <main+0x100>
mov    -0x20(%rbp),%rsi
lea    0xdce(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <writeFile@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     118c <main+0x1c>
mov    (%rbx),%rsi
lea    0xe79(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
lea    0xe84(%rip),%rsi
call   1080 <fopen@plt>
mov    0x8(%rbx),%rsi
test   %rax,%rax
je     120f <main+0x9f>
mov    %rax,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    0x2(%rax),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
movw   $0x632e,(%rbx,%rax,1)
movb   $0x0,0x2(%rbx,%rax,1)
lea    0xe58(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1225 <main+0xb5>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   1070 <writeFile@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe0a(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: %s gameboyFilename\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"Unable to open %s\0A\00"
@.str.3 = [3 x i8] c".c\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [31 x i8] c"Unable to open %s for writing\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %11, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %16
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i64 @strlen(i8* %32)
%34 = call i64 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%35 = add nsw i64 %33, %34
%36 = call i8* @calloc(i64 %35, i32 1)
store i8* %36, i8** %7
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i8* @strcat(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %42, i32** %8
%43 = load i32*, i32** %8
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %51
45: ; preds = %29
%46 = load i8*, i8** %7
%47 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %29
%52 = load i32*, i32** %6
%53 = load i32*, i32** %8
%54 = call i32 @writeFile(i32* %52, i32* %53)
%55 = load i32*, i32** %6
%56 = call i32 @fclose(i32* %55)
%57 = load i32*, i32** %8
%58 = call i32 @fclose(i32* %57)
ret i32 0
}

LLVM IR (-O2):
@.str = [27 x i8] c"Usage: %s gameboyFilename\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"Unable to open %s\0A\00"
@.str.3 = [3 x i8] c".c\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [31 x i8] c"Unable to open %s for writing\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
%12 = load i8*, i8** %8
br i1 %11, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = tail call i64 @strlen(i8* dereferenceable(1) %12)
%18 = add nsw i64 %17, 2
%19 = tail call i8* @calloc(i64 %18, i32 1)
%20 = load i8*, i8** %8
%21 = tail call i64 @strlen(i8* dereferenceable(1) %20)
%22 = getelementptr i8, i8* %20, i64 %21
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %22, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i64 3, i1 false)
%23 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%27 = tail call i32 @fclose(i32* %10)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %16
%30 = tail call i32 @writeFile(i32* %10, i32* %23)
%31 = tail call i32 @fclose(i32* %10)
%32 = tail call i32 @fclose(i32* %23)
ret i32 0
}