#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int ntohl(int);
int /*<<< orphan*/ stderr;
unsigned int affs_checksum(FILE *fff, long begin, long end, int max) {
  unsigned int checksum = 0;
  int temp_int, i;
  long size;
  long saved_location;
  saved_location = ftell(fff);
  size = end - begin;
  if (max != 0)
    if (size > max)
      size = max;
  size = size / 4;
  fprintf(stderr, "Checksum size %ld going from %lx to %lx\n", size, begin,
          end);
  fseek(fff, begin, SEEK_SET);
  for (i = 0; i < size; i++) {
    fread(&temp_int, 4, 1, fff);
    checksum += ntohl(temp_int);
    fprintf(stderr, "(%x %x)", checksum, ntohl(temp_int));
  }
  fprintf(stderr, "\n");
  fseek(fff, saved_location, SEEK_SET);
  return (0xffffffff - checksum) + 1;
}


x86 assembly (clang, -O0):
<affs_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11af <affs_checksum+0x5f>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jle    11aa <affs_checksum+0x5a>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11af <affs_checksum+0x5f>
mov    -0x30(%rbp),%rax
mov    $0x4,%ecx
cqto
idiv   %rcx
mov    %rax,-0x30(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
movl   $0x0,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    1272 <affs_checksum+0x122>
mov    -0x8(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x24(%rbp),%edi
call   1070 <ntohl@plt>
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <ntohl@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1201 <affs_checksum+0xb1>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
mov    $0xffffffff,%eax
sub    -0x20(%rbp),%eax
add    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<affs_checksum>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%r15
call   1050 <ftell@plt>
mov    %rbp,%rcx
mov    %rbp,%r8
sub    %r12,%rcx
movslq %ebx,%rdx
cmp    %rdx,%rcx
mov    %rcx,%r13
cmovg  %rdx,%r13
test   %edx,%edx
cmove  %rcx,%r13
lea    0x3(%r13),%rbx
test   %r13,%r13
cmovns %r13,%rbx
mov    %rax,0x10(%rsp)
sar    $0x2,%rbx
mov    0x2e3b(%rip),%r14
mov    (%r14),%edi
lea    0xe51(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <fseek@plt>
cmp    $0x4,%r13
jl     1227 <affs_checksum+0xd7>
lea    0xc(%rsp),%r12
lea    0xe45(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    0xc(%rsp),%edi
call   1070 <ntohl@plt>
add    %eax,%ebp
mov    (%r14),%edi
mov    %r13,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0xffffffffffffffff,%rbx
jne    11f0 <affs_checksum+0xa0>
mov    (%r14),%edi
lea    0xdf6(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    0x10(%rsp),%rsi
call   1060 <fseek@plt>
neg    %ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"Checksum size %ld going from %lx to %lx\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [8 x i8] c"(%x %x)\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @affs_checksum(i32* %0, i64 %1, i64 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 %3, i32* %8
store i32 0, i32* %9
%14 = load i32*, i32** %5
%15 = call i64 @ftell(i32* %14)
store i64 %15, i64* %13
%16 = load i64, i64* %7
%17 = load i64, i64* %6
%18 = sub nsw i64 %16, %17
store i64 %18, i64* %12
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %4
%22 = load i64, i64* %12
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = icmp sgt i64 %22, %24
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
store i64 %28, i64* %12
br label %29
29: ; preds = %26, %21
br label %30
30: ; preds = %29, %4
%31 = load i64, i64* %12
%32 = sdiv i64 %31, 4
store i64 %32, i64* %12
%33 = load i32, i32* @stderr
%34 = load i64, i64* %12
%35 = load i64, i64* %6
%36 = load i64, i64* %7
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i64 %34, i64 %35, i64 %36)
%38 = load i32*, i32** %5
%39 = load i64, i64* %6
%40 = load i32, i32* @SEEK_SET
%41 = call i32 @fseek(i32* %38, i64 %39, i32 %40)
store i32 0, i32* %11
br label %42
42: ; preds = %59, %30
%43 = load i32, i32* %11
%44 = sext i32 %43 to i64
%45 = load i64, i64* %12
%46 = icmp slt i64 %44, %45
br i1 %46, label %47, label %62
47: ; preds = %42
%48 = load i32*, i32** %5
%49 = call i32 @fread(i32* %10, i32 4, i32 1, i32* %48)
%50 = load i32, i32* %10
%51 = call i32 @ntohl(i32 %50)
%52 = load i32, i32* %9
%53 = add i32 %52, %51
store i32 %53, i32* %9
%54 = load i32, i32* @stderr
%55 = load i32, i32* %9
%56 = load i32, i32* %10
%57 = call i32 @ntohl(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %55, i32 %57)
br label %59
59: ; preds = %47
%60 = load i32, i32* %11
%61 = add nsw i32 %60, 1
store i32 %61, i32* %11
br label %42
62: ; preds = %42
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%65 = load i32*, i32** %5
%66 = load i64, i64* %13
%67 = load i32, i32* @SEEK_SET
%68 = call i32 @fseek(i32* %65, i64 %66, i32 %67)
%69 = load i32, i32* %9
%70 = sub i32 -1, %69
%71 = add i32 %70, 1
ret i32 %71
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"Checksum size %ld going from %lx to %lx\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [8 x i8] c"(%x %x)\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @affs_checksum(i32* nocapture %0, i64 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i64 @ftell(i32* %0)
%8 = sub nsw i64 %2, %1
%9 = icmp eq i32 %3, 0
%10 = sext i32 %3 to i64
%11 = icmp sgt i64 %8, %10
%12 = select i1 %11, i64 %10, i64 %8
%13 = select i1 %9, i64 %8, i64 %12
%14 = sdiv i64 %13, 4
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i64 %14, i64 %1, i64 %2)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %0, i64 %1, i32 %17)
%19 = icmp sgt i64 %13, 3
br i1 %19, label %20, label %31
20: ; preds = %4, %20
%21 = phi i64 [ %29, %20 ], [ 0, %4 ]
%22 = phi i32 [ %26, %20 ], [ 0, %4 ]
%23 = call i32 @fread(i32* %5, i32 4, i32 1, i32* %0)
%24 = load i32, i32* %5
%25 = tail call i32 @ntohl(i32 %24)
%26 = add i32 %25, %22
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %25)
%29 = add nuw nsw i64 %21, 1
%30 = icmp eq i64 %29, %14
br i1 %30, label %31, label %20
31: ; preds = %20, %4
%32 = phi i32 [ 0, %4 ], [ %26, %20 ]
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @SEEK_SET
%36 = tail call i32 @fseek(i32* %0, i64 %7, i32 %35)
%37 = sub i32 0, %32
ret i32 %37
}