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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(long *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long checksum_fix(FILE *f) {
  unsigned char data = 0;
  unsigned int i = 0;
  unsigned int overflow = 0;
  long checksum = 0;
  size_t size = 0;
  fseek(f, 0, SEEK_END);
  size = ftell(f);
  rewind(f);
  for (i = 0; i < size; ++i) {
    checksum <<= 1;
    fread(&data, sizeof(data), 1, f);
    if (0x0C <= i && 0x0C + 0x04 > i) {
      data = 0;
    }
    checksum += data + overflow;
    overflow = (checksum < 0);
  }
  rewind(f);
  fseek(f, 0x0C, SEEK_CUR);
  fwrite(&checksum, sizeof(checksum), 1, f);
  return checksum;
}


x86 assembly (clang, -O0):
<checksum_fix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x0,-0x9(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x28(%rbp),%rax
jae    122b <checksum_fix+0xdb>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x9(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    $0xc,%eax
cmp    -0x10(%rbp),%eax
ja     11fc <checksum_fix+0xac>
mov    $0x10,%eax
cmp    -0x10(%rbp),%eax
jbe    11fc <checksum_fix+0xac>
movb   $0x0,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,%eax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11af <checksum_fix+0x5f>
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x8(%rbp),%rdi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    $0xc,%esi
call   1060 <fseek@plt>
mov    -0x8(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum_fix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movb   $0x0,0xf(%rsp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %r14,%rdi
call   1050 <ftell@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1040 <rewind@plt>
test   %r12,%r12
je     11f2 <checksum_fix+0xa2>
mov    $0x1,%r13d
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    11c3 <checksum_fix+0x73>
nopl   0x0(%rax)
movb   $0x0,0xf(%rsp)
xor    %eax,%eax
movzbl %al,%eax
or     %rbp,%rbx
add    %rax,%rbx
mov    %rbx,%rbp
shr    $0x3f,%rbp
mov    %r13d,%eax
add    $0x1,%r13d
cmp    %rax,%r12
jbe    11f2 <checksum_fix+0xa2>
lea    -0x1(%r13),%r15d
add    %rbx,%rbx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
and    $0xfffffffc,%r15d
cmp    $0xc,%r15d
je     11a0 <checksum_fix+0x50>
movzbl 0xf(%rsp),%eax
jmp    11a7 <checksum_fix+0x57>
mov    %rbx,0x10(%rsp)
mov    %r14,%rdi
call   1040 <rewind@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0xc,%esi
call   1060 <fseek@plt>
lea    0x10(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
define i64 @checksum_fix(i32* %0) {
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %2
store i8 0, i8* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i64 0, i64* %6
store i64 0, i64* %7
%8 = load i32*, i32** %2
%9 = load i32, i32* @SEEK_END
%10 = call i32 @fseek(i32* %8, i32 0, i32 %9)
%11 = load i32*, i32** %2
%12 = call i64 @ftell(i32* %11)
store i64 %12, i64* %7
%13 = load i32*, i32** %2
%14 = call i32 @rewind(i32* %13)
store i32 0, i32* %4
br label %15
15: ; preds = %42, %1
%16 = load i32, i32* %4
%17 = zext i32 %16 to i64
%18 = load i64, i64* %7
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %45
20: ; preds = %15
%21 = load i64, i64* %6
%22 = shl i64 %21, 1
store i64 %22, i64* %6
%23 = load i32*, i32** %2
%24 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %23)
%25 = load i32, i32* %4
%26 = icmp ule i32 12, %25
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i32, i32* %4
%29 = icmp ugt i32 16, %28
br i1 %29, label %30, label %31
30: ; preds = %27
store i8 0, i8* %3
br label %31
31: ; preds = %30, %27, %20
%32 = load i8, i8* %3
%33 = zext i8 %32 to i32
%34 = load i32, i32* %5
%35 = add i32 %33, %34
%36 = zext i32 %35 to i64
%37 = load i64, i64* %6
%38 = add nsw i64 %37, %36
store i64 %38, i64* %6
%39 = load i64, i64* %6
%40 = icmp slt i64 %39, 0
%41 = zext i1 %40 to i32
store i32 %41, i32* %5
br label %42
42: ; preds = %31
%43 = load i32, i32* %4
%44 = add i32 %43, 1
store i32 %44, i32* %4
br label %15
45: ; preds = %15
%46 = load i32*, i32** %2
%47 = call i32 @rewind(i32* %46)
%48 = load i32*, i32** %2
%49 = load i32, i32* @SEEK_CUR
%50 = call i32 @fseek(i32* %48, i32 12, i32 %49)
%51 = load i32*, i32** %2
%52 = call i32 @fwrite(i64* %6, i32 8, i32 1, i32* %51)
%53 = load i64, i64* %6
ret i64 %53
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
define i64 @checksum_fix(i32* nocapture %0) {
%2 = alloca i8
%3 = alloca i64
store i8 0, i8* %2
%4 = bitcast i64* %3 to i8*
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %0, i32 0, i32 %5)
%7 = tail call i64 @ftell(i32* %0)
%8 = tail call i32 @rewind(i32* %0)
%9 = icmp eq i64 %7, 0
br i1 %9, label %30, label %10
10: ; preds = %1, %21
%11 = phi i64 [ %26, %21 ], [ 0, %1 ]
%12 = phi i32 [ %27, %21 ], [ 0, %1 ]
%13 = phi i64 [ %25, %21 ], [ 0, %1 ]
%14 = shl i64 %13, 1
%15 = call i32 @fread(i8* %2, i32 1, i32 1, i32* %0)
%16 = and i32 %12, -4
%17 = icmp eq i32 %16, 12
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = load i8, i8* %2
br label %21
20: ; preds = %10
store i8 0, i8* %2
br label %21
21: ; preds = %18, %20
%22 = phi i8 [ %19, %18 ], [ 0, %20 ]
%23 = zext i8 %22 to i64
%24 = or i64 %14, %11
%25 = add i64 %24, %23
%26 = lshr i64 %25, 63
%27 = add i32 %12, 1
%28 = zext i32 %27 to i64
%29 = icmp ugt i64 %7, %28
br i1 %29, label %10, label %30
30: ; preds = %21, %1
%31 = phi i64 [ 0, %1 ], [ %25, %21 ]
store i64 %31, i64* %3
%32 = tail call i32 @rewind(i32* %0)
%33 = load i32, i32* @SEEK_CUR
%34 = tail call i32 @fseek(i32* %0, i32 12, i32 %33)
%35 = call i32 @fwrite(i64* %3, i32 8, i32 1, i32* %0)
%36 = load i64, i64* %3
ret i64 %36
}