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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ image_raster_from_game_boy_save_ram(int /*<<< orphan*/ *,
                                                       int /*<<< orphan*/ *,
                                                       size_t);
int /*<<< orphan*/ pgm_from_image_raster(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stderr;
int main(int argc, char const *argv[]) {
  if (argc != 2) {
    printf("Usage: %s <file>\n", argv[0]);
    exit(0);
  }
  FILE *save_file = fopen(argv[1], "r");
  if (save_file == NULL) {
    fprintf(stderr, "Error: could not open file '%s'.\n", argv[1]);
    exit(1);
  }
  uint8_t image_raster[16 * 14 * 8 * 8];
  for (size_t i = 0; i < 30; i++) {
    image_raster_from_game_boy_save_ram(save_file, image_raster, i);
    pgm_from_image_raster(image_raster, i);
  }
  fclose(save_file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ad <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5f(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f7 <main+0x87>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe2c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movq   $0x0,-0xe028(%rbp)
cmpq   $0x1e,-0xe028(%rbp)
1e
jae    1251 <main+0xe1>
mov    -0x18(%rbp),%rdi
lea    -0xe020(%rbp),%rsi
mov    -0xe028(%rbp),%rdx
call   1060 <image_raster_from_game_boy_save_ram@plt>
lea    -0xe020(%rbp),%rdi
mov    -0xe028(%rbp),%rsi
call   1070 <pgm_from_image_raster@plt>
mov    -0xe028(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xe028(%rbp)
jmp    1202 <main+0x92>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0xe030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xe000,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11ea <main+0x7a>
mov    0x8(%rbx),%rdi
lea    0xe83(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1200 <main+0x90>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <image_raster_from_game_boy_save_ram@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <pgm_from_image_raster@plt>
add    $0x1,%rbx
cmp    $0x1e,%rbx
jne    11b0 <main+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0xe000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    (%rbx),%rsi
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe00(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Error: could not open file '%s'.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [14336 x i32]
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
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 ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = load i32*, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %17
store i64 0, i64* %8
br label %32
32: ; preds = %43, %31
%33 = load i64, i64* %8
%34 = icmp ult i64 %33, 30
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = load i32*, i32** %6
%37 = getelementptr inbounds [14336 x i32], [14336 x i32]* %7, i64 0, i64 0
%38 = load i64, i64* %8
%39 = call i32 @image_raster_from_game_boy_save_ram(i32* %36, i32* %37, i64 %38)
%40 = getelementptr inbounds [14336 x i32], [14336 x i32]* %7, i64 0, i64 0
%41 = load i64, i64* %8
%42 = call i32 @pgm_from_image_raster(i32* %40, i64 %41)
br label %43
43: ; preds = %35
%44 = load i64, i64* %8
%45 = add i64 %44, 1
store i64 %45, i64* %8
br label %32
46: ; preds = %32
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Error: could not open file '%s'.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [14336 x i32]
%4 = icmp eq i32 %0, 2
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %10
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = bitcast [14336 x i32]* %3 to i8*
%21 = getelementptr inbounds [14336 x i32], [14336 x i32]* %3, i64 0, i64 0
br label %24
22: ; preds = %24
%23 = call i32 @fclose(i32* %12)
ret i32 0
24: ; preds = %19, %24
%25 = phi i64 [ 0, %19 ], [ %28, %24 ]
%26 = call i32 @image_raster_from_game_boy_save_ram(i32* %12, i32* %21, i64 %25)
%27 = call i32 @pgm_from_image_raster(i32* %21, i64 %25)
%28 = add nuw nsw i64 %25, 1
%29 = icmp eq i64 %28, 30
br i1 %29, label %22, label %24
}