#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_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(short *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, short);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ write(char *, char *);
int main(int argc, char *argv[]) {
  char ch;
  FILE *romfile;
  int short checksum = 0;
  int size;
  if (argc != 2) {
    puts("Usage: checksumgen flippedrom.bin\n");
  } else {
    romfile = fopen(argv[1], "r+b");
    if (!romfile) {
      write("Can't open %s\n", argv[1]);
      return 1;
    }
    fseek(romfile, 0, SEEK_SET);
    for (size = 0; size < 0x80; size++) {
      checksum += fgetc(romfile);
    }
    fseek(romfile, 0x82, SEEK_SET);
    for (size = 0; size < 0x20000 - 0x82; size++) {
      checksum += fgetc(romfile);
    }
    printf("Checksum: %X\n", checksum & 0xFFFF);
    fseek(romfile, 0x80, SEEK_SET);
    fwrite(&checksum, sizeof(int short), 1, romfile);
    fclose(romfile);
  }
  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)
movw   $0x0,-0x22(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b7 <main+0x37>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
jmp    12e8 <main+0x168>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fa <main+0x7a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe3e(%rip),%rdi
call   1040 <write@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ef <main+0x16f>
mov    -0x20(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x80,-0x28(%rbp)
jge    1248 <main+0xc8>
mov    -0x20(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %rax,%rcx
movswq -0x22(%rbp),%rax
add    %rcx,%rax
mov    %ax,-0x22(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1215 <main+0x95>
mov    -0x20(%rbp),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    $0x82,%esi
call   1080 <fseek@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x1ff7e,-0x28(%rbp)
jge    1299 <main+0x119>
mov    -0x20(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %rax,%rcx
movswq -0x22(%rbp),%rax
add    %rcx,%rax
mov    %ax,-0x22(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1266 <main+0xe6>
movswl -0x22(%rbp),%eax
and    $0xffff,%eax
lea    0xd8d(%rip),%rdi
movswl %ax,%esi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rcx
lea    -0x22(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1254 <main+0xd4>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe85(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1264 <main+0xe4>
mov    %rax,%r15
mov    0x2e2a(%rip),%r14
mov    (%r14),%edx
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    $0x80,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1070 <fgetc@plt>
add    %eax,%ebp
add    $0xffffffff,%ebx
jne    11d0 <main+0x50>
mov    (%r14),%edx
mov    %r15,%rdi
mov    $0x82,%esi
call   1080 <fseek@plt>
mov    $0x1ff7e,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1070 <fgetc@plt>
add    %eax,%ebp
add    $0xffffffff,%ebx
jne    1200 <main+0x80>
mov    %bp,0x6(%rsp)
movswl %bp,%esi
lea    0xe18(%rip),%rdi
call   1060 <printf@plt>
mov    (%r14),%edx
mov    %r15,%rdi
mov    $0x80,%esi
call   1080 <fseek@plt>
lea    0x6(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    1260 <main+0xe0>
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1279 <main+0xf9>
mov    0x8(%r14),%rsi
lea    0xdb8(%rip),%rdi
call   1040 <write@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Usage: checksumgen flippedrom.bin\0A\00"
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [15 x i8] c"Can't open %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [14 x i8] c"Checksum: %X\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32*
%8 = alloca i16
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i16 0, i16* %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %73
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %7
%19 = load i32*, i32** %7
%20 = icmp ne i32* %19, null
br i1 %20, label %26, label %21
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @write(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 1, i32* %3
br label %74
26: ; preds = %14
%27 = load i32*, i32** %7
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
store i32 0, i32* %9
br label %30
30: ; preds = %40, %26
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 128
br i1 %32, label %33, label %43
33: ; preds = %30
%34 = load i32*, i32** %7
%35 = call i64 @fgetc(i32* %34)
%36 = load i16, i16* %8
%37 = sext i16 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i16
store i16 %39, i16* %8
br label %40
40: ; preds = %33
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
br label %30
43: ; preds = %30
%44 = load i32*, i32** %7
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %44, i32 130, i32 %45)
store i32 0, i32* %9
br label %47
47: ; preds = %57, %43
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 130942
br i1 %49, label %50, label %60
50: ; preds = %47
%51 = load i32*, i32** %7
%52 = call i64 @fgetc(i32* %51)
%53 = load i16, i16* %8
%54 = sext i16 %53 to i64
%55 = add nsw i64 %54, %52
%56 = trunc i64 %55 to i16
store i16 %56, i16* %8
br label %57
57: ; preds = %50
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %47
60: ; preds = %47
%61 = load i16, i16* %8
%62 = sext i16 %61 to i32
%63 = and i32 %62, 65535
%64 = trunc i32 %63 to i16
%65 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i16 signext %64)
%66 = load i32*, i32** %7
%67 = load i32, i32* @SEEK_SET
%68 = call i32 @fseek(i32* %66, i32 128, i32 %67)
%69 = load i32*, i32** %7
%70 = call i32 @fwrite(i16* %8, i32 2, i32 1, i32* %69)
%71 = load i32*, i32** %7
%72 = call i32 @fclose(i32* %71)
br label %73
73: ; preds = %60, %12
store i32 0, i32* %3
br label %74
74: ; preds = %73, %21
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [35 x i8] c"Usage: checksumgen flippedrom.bin\0A\00"
@.str.1 = [4 x i8] c"r+b\00"
@.str.2 = [15 x i8] c"Can't open %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [14 x i8] c"Checksum: %X\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %44
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i8*, i8** %9
%15 = tail call i32 @write(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %44
16: ; preds = %8
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %11, i32 0, i32 %17)
br label %19
19: ; preds = %16, %19
%20 = phi i32 [ 0, %16 ], [ %25, %19 ]
%21 = phi i16 [ 0, %16 ], [ %24, %19 ]
%22 = tail call i64 @fgetc(i32* %11)
%23 = trunc i64 %22 to i16
%24 = add i16 %21, %23
%25 = add nuw nsw i32 %20, 1
%26 = icmp eq i32 %25, 128
br i1 %26, label %27, label %19
27: ; preds = %19
%28 = load i32, i32* @SEEK_SET
%29 = tail call i32 @fseek(i32* %11, i32 130, i32 %28)
br label %30
30: ; preds = %27, %30
%31 = phi i32 [ 0, %27 ], [ %36, %30 ]
%32 = phi i16 [ %24, %27 ], [ %35, %30 ]
%33 = tail call i64 @fgetc(i32* %11)
%34 = trunc i64 %33 to i16
%35 = add i16 %32, %34
%36 = add nuw nsw i32 %31, 1
%37 = icmp eq i32 %36, 130942
br i1 %37, label %38, label %30
38: ; preds = %30
store i16 %35, i16* %3
%39 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i16 signext %35)
%40 = load i32, i32* @SEEK_SET
%41 = tail call i32 @fseek(i32* %11, i32 128, i32 %40)
%42 = call i32 @fwrite(i16* %3, i32 2, i32 1, i32* %11)
%43 = tail call i32 @fclose(i32* %11)
br label %44
44: ; preds = %6, %38, %13
%45 = phi i32 [ 1, %13 ], [ 0, %38 ], [ 0, %6 ]
ret i32 %45
}