#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 checksum(unsigned char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *loadFile(char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ saveFile(char *, unsigned char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: %s savefile", argv[0]);
    exit(1);
  }
  int size;
  unsigned char *buffer = loadFile(argv[1], &size);
  if (buffer == NULL) {
    printf("failed to load %s.\n", argv[1]);
    exit(1);
  }
  int oldsum = (buffer[11] << 8) + buffer[10];
  int newsum = checksum(&buffer[0x34], 0xfa0);
  printf("current checksum = %04x\ncalculated checksum = %04x\n", oldsum,
         newsum);
  buffer[10] = newsum & 0xff;
  buffer[11] = (newsum >> 8) & 0xff;
  saveFile(argv[1], buffer, size);
  free(buffer);
  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    119f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x14(%rbp),%rsi
call   1080 <loadFile@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11df <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
movzbl 0xb(%rax),%eax
shl    $0x8,%eax
mov    -0x20(%rbp),%rcx
movzbl 0xa(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x34,%rdi
mov    $0xfa0,%esi
call   1050 <checksum@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0xa(%rax)
mov    -0x28(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0xb(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <saveFile@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x1,%edi
jle    11d9 <main+0x79>
mov    0x8(%r14),%rdi
lea    0x4(%rsp),%rsi
call   1080 <loadFile@plt>
test   %rax,%rax
je     11e5 <main+0x85>
mov    %rax,%rbx
movzwl 0xa(%rax),%r15d
mov    %rax,%rdi
add    $0x34,%rdi
mov    $0xfa0,%esi
call   1050 <checksum@plt>
mov    %eax,%ebp
lea    0xe83(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %bp,0xa(%rbx)
mov    0x8(%r14),%rdi
mov    0x4(%rsp),%edx
mov    %rbx,%rsi
call   1060 <saveFile@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xe1d(%rip),%rdi
jmp    11f0 <main+0x90>
mov    0x8(%r14),%rsi
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Usage: %s savefile\00"
@.str.1 = [20 x i8] c"failed to load %s.\0A\00"
@.str.2 = [52 x i8] c"current checksum = %04x\0Acalculated checksum = %04x\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
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i8* @loadFile(i8* %21, i32* %6)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %18
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i64 11
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = shl i32 %35, 8
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 10
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = add nsw i32 %36, %40
store i32 %41, i32* %8
%42 = load i8*, i8** %7
%43 = getelementptr inbounds i8, i8* %42, i64 52
%44 = call i32 @checksum(i8* %43, i32 4000)
store i32 %44, i32* %9
%45 = load i32, i32* %8
%46 = load i32, i32* %9
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %46)
%48 = load i32, i32* %9
%49 = and i32 %48, 255
%50 = trunc i32 %49 to i8
%51 = load i8*, i8** %7
%52 = getelementptr inbounds i8, i8* %51, i64 10
store i8 %50, i8* %52
%53 = load i32, i32* %9
%54 = ashr i32 %53, 8
%55 = and i32 %54, 255
%56 = trunc i32 %55 to i8
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i64 11
store i8 %56, i8* %58
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = load i8*, i8** %7
%63 = load i32, i32* %6
%64 = call i32 @saveFile(i8* %61, i8* %62, i32 %63)
%65 = load i8*, i8** %7
%66 = call i32 @free(i8* %65)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"Usage: %s savefile\00"
@.str.1 = [20 x i8] c"failed to load %s.\0A\00"
@.str.2 = [52 x i8] c"current checksum = %04x\0Acalculated checksum = %04x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = bitcast i32* %3 to i8*
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i8* @loadFile(i8* %12, i32* %3)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i8*, i8** %11
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = getelementptr inbounds i8, i8* %13, i64 11
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = shl nuw nsw i32 %22, 8
%24 = getelementptr inbounds i8, i8* %13, i64 10
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = or i32 %23, %26
%28 = getelementptr inbounds i8, i8* %13, i64 52
%29 = call i32 @checksum(i8* %28, i32 4000)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %29)
%31 = trunc i32 %29 to i8
store i8 %31, i8* %24
%32 = lshr i32 %29, 8
%33 = trunc i32 %32 to i8
store i8 %33, i8* %20
%34 = load i8*, i8** %11
%35 = load i32, i32* %3
%36 = call i32 @saveFile(i8* %34, i8* %13, i32 %35)
%37 = call i32 @free(i8* %13)
ret i32 0
}