#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 */

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ print_usage();
long read_file(char *, unsigned char **);
int /*<<< orphan*/ sm64_update_checksums(unsigned char *);
int /*<<< orphan*/ stderr;
long write_file(char *, unsigned char *, long);
int main(int argc, char *argv[]) {
  unsigned char *rom_data;
  char *file_in;
  char *file_out;
  long length;
  long write_length;
  if (argc < 2) {
    print_usage();
    return EXIT_FAILURE;
  }
  file_in = argv[1];
  if (argc > 2) {
    file_out = argv[2];
  } else {
    file_out = argv[1];
  }
  length = read_file(file_in, &rom_data);
  if (length < 0) {
    fprintf(stderr, "Error reading input file \"%s\"\n", file_in);
    return EXIT_FAILURE;
  }
  sm64_update_checksums(rom_data);
  write_length = write_file(file_out, rom_data, length);
  free(rom_data);
  if (write_length != length) {
    fprintf(stderr, "Error writing to output file \"%s\"\n", file_out);
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


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    1198 <main+0x38>
mov    $0x0,%al
call   1080 <print_usage@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127c <main+0x11c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11bf <main+0x5f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11cb <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <read_file@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1211 <main+0xb1>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe05(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127c <main+0x11c>
mov    -0x18(%rbp),%rdi
call   1040 <sm64_update_checksums@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <write_file@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1270 <main+0x110>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127c <main+0x11c>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     117a <main+0x1a>
xor    %eax,%eax
call   1080 <print_usage@plt>
jmp    1201 <main+0xa1>
mov    0x8(%rsi),%r15
mov    %r15,%r14
cmp    $0x2,%edi
je     118a <main+0x2a>
mov    0x10(%rsi),%r14
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1070 <read_file@plt>
test   %rax,%rax
js     11d4 <main+0x74>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
call   1040 <sm64_update_checksums@plt>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1050 <write_file@plt>
mov    %rax,%r15
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
cmp    %rbx,%r15
jne    11e9 <main+0x89>
mov    0x2dfe(%rip),%rax
jmp    1208 <main+0xa8>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
mov    %r15,%rdx
jmp    11fc <main+0x9c>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error reading input file \22%s\22\0A\00"
@.str.1 = [35 x i8] c"Error writing to output file \22%s\22\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%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 %16
13: ; preds = %2
%14 = call i32 (...) @print_usage()
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %3
br label %59
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %7
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 2
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %8
br label %30
26: ; preds = %16
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
store i8* %29, i8** %8
br label %30
30: ; preds = %26, %22
%31 = load i8*, i8** %7
%32 = call i64 @read_file(i8* %31, i8** %6)
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %7
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
store i32 %39, i32* %3
br label %59
40: ; preds = %30
%41 = load i8*, i8** %6
%42 = call i32 @sm64_update_checksums(i8* %41)
%43 = load i8*, i8** %8
%44 = load i8*, i8** %6
%45 = load i64, i64* %9
%46 = call i64 @write_file(i8* %43, i8* %44, i64 %45)
store i64 %46, i64* %10
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
%49 = load i64, i64* %10
%50 = load i64, i64* %9
%51 = icmp ne i64 %49, %50
br i1 %51, label %52, label %57
52: ; preds = %40
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %8
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %54)
%56 = load i32, i32* @EXIT_FAILURE
store i32 %56, i32* %3
br label %59
57: ; preds = %40
%58 = load i32, i32* @EXIT_SUCCESS
store i32 %58, i32* %3
br label %59
59: ; preds = %57, %52, %35, %13
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error reading input file \22%s\22\0A\00"
@.str.1 = [35 x i8] c"Error writing to output file \22%s\22\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @print_usage()
br label %33
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i32 %0, 2
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
br label %15
15: ; preds = %8, %12
%16 = phi i8* [ %14, %12 ], [ %10, %8 ]
%17 = call i64 @read_file(i8* %10, i8** %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %33
22: ; preds = %15
%23 = load i8*, i8** %3
%24 = call i32 @sm64_update_checksums(i8* %23)
%25 = load i8*, i8** %3
%26 = call i64 @write_file(i8* %16, i8* %25, i64 %17)
%27 = load i8*, i8** %3
%28 = call i32 @free(i8* %27)
%29 = icmp eq i64 %26, %17
br i1 %29, label %33, label %30
30: ; preds = %22
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %33
33: ; preds = %22, %30, %19, %6
%34 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_FAILURE, %19 ], [ @EXIT_FAILURE, %30 ], [ @EXIT_SUCCESS, %22 ]
%35 = load i32, i32* %34
ret i32 %35
}