#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*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
void fix_checksum(const char *filename) {
  FILE *file = fopen(filename, "r+b");
  fseek(file, 0x08, SEEK_SET);
  int sum = 0;
  for (int i = 0x08; i < 0x88; i += 2) {
    int x = 0;
    fread(&x, 2, 1, file);
    sum += x;
  }
  sum &= 0xFFFF;
  fseek(file, 0x06, SEEK_SET);
  fwrite(&sum, 2, 1, file);
  fclose(file);
}


x86 assembly (clang, -O0):
<fix_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    $0x8,%esi
call   1050 <fseek@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x8,-0x18(%rbp)
cmpl   $0x88,-0x18(%rbp)
jge    11d7 <fix_checksum+0x87>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    1195 <fix_checksum+0x45>
mov    -0x14(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
call   1050 <fseek@plt>
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_checksum>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
mov    0x2e6e(%rip),%r15
mov    (%r15),%edx
mov    %rax,%rdi
mov    $0x8,%esi
call   1050 <fseek@plt>
xor    %ebp,%ebp
mov    $0x6,%ebx
lea    0x8(%rsp),%r14
xchg   %ax,%ax
movl   $0x0,0x8(%rsp)
mov    %r14,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
add    0x8(%rsp),%ebp
add    $0x2,%ebx
cmp    $0x86,%ebx
jb     1190 <fix_checksum+0x40>
movzwl %bp,%eax
mov    %eax,0xc(%rsp)
mov    (%r15),%edx
mov    %r12,%rdi
mov    $0x6,%esi
call   1050 <fseek@plt>
lea    0xc(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1070 <fwrite@plt>
mov    %r12,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@SEEK_SET = global i32 0
define void @fix_checksum(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = load i32, i32* @SEEK_SET
%11 = call i32 @fseek(i32* %9, i32 8, i32 %10)
store i32 0, i32* %4
store i32 8, i32* %5
br label %12
12: ; preds = %21, %1
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 136
br i1 %14, label %15, label %24
15: ; preds = %12
store i32 0, i32* %6
%16 = load i32*, i32** %3
%17 = call i32 @fread(i32* %6, i32 2, i32 1, i32* %16)
%18 = load i32, i32* %6
%19 = load i32, i32* %4
%20 = add nsw i32 %19, %18
store i32 %20, i32* %4
br label %21
21: ; preds = %15
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 2
store i32 %23, i32* %5
br label %12
24: ; preds = %12
%25 = load i32, i32* %4
%26 = and i32 %25, 65535
store i32 %26, i32* %4
%27 = load i32*, i32** %3
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 6, i32 %28)
%30 = load i32*, i32** %3
%31 = call i32 @fwrite(i32* %4, i32 2, i32 1, i32* %30)
%32 = load i32*, i32** %3
%33 = call i32 @fclose(i32* %32)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@SEEK_SET = global i32 0
define void @fix_checksum(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @fseek(i32* %4, i32 8, i32 %5)
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
br label %15
9: ; preds = %15
%10 = and i32 %20, 65535
store i32 %10, i32* %2
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %4, i32 6, i32 %11)
%13 = call i32 @fwrite(i32* %2, i32 2, i32 1, i32* %4)
%14 = tail call i32 @fclose(i32* %4)
ret void
15: ; preds = %1, %15
%16 = phi i32 [ 8, %1 ], [ %21, %15 ]
%17 = phi i32 [ 0, %1 ], [ %20, %15 ]
store i32 0, i32* %3
%18 = call i32 @fread(i32* %3, i32 2, i32 1, i32* %4)
%19 = load i32, i32* %3
%20 = add nsw i32 %17, %19
%21 = add nuw nsw i32 %16, 2
%22 = icmp ult i32 %16, 134
br i1 %22, label %15, label %9
}