#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 read(int, unsigned char *, int);
unsigned long long reverseChecksum(int file, int length) {
  if (file > 0) {
    unsigned long long sum = 0;
    unsigned char buffer[2];
    int c = 1;
    int count = 0;
    while (c != 0 && count < (length - 8)) {
      buffer[0] = 0;
      buffer[1] = 0;
      c = read(file, buffer, 1);
      if (c != 0) {
        sum += (unsigned long long)buffer[0];
        count++;
      }
    }
    return sum;
  }
  return 0;
}


x86 assembly (clang, -O0):
<reverseChecksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11bb <reverseChecksum+0xab>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x25(%rbp)
je     115e <reverseChecksum+0x4e>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x8,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    116e <reverseChecksum+0x5e>
jmp    11ae <reverseChecksum+0x9e>
movb   $0x0,-0x1a(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11a9 <reverseChecksum+0x99>
movzbl -0x1a(%rbp),%eax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113e <reverseChecksum+0x2e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c3 <reverseChecksum+0xb3>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverseChecksum>:
test   %edi,%edi
jle    1183 <reverseChecksum+0x73>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
xor    %ebx,%ebx
cmp    $0x9,%esi
jl     1186 <reverseChecksum+0x76>
mov    %edi,%r12d
add    $0xfffffff8,%r15d
lea    0xe(%rsp),%r14
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbp
movw   $0x0,0xe(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%esi
xor    %edx,%edx
test   %eax,%eax
setne  %cl
movzbl 0xe(%rsp),%edi
mov    $0x0,%eax
cmovne %rdi,%rax
add    %rbp,%rax
test   %esi,%esi
je     1188 <reverseChecksum+0x78>
mov    %cl,%dl
add    %edx,%ebx
cmp    %r15d,%ebx
jl     1140 <reverseChecksum+0x30>
jmp    1188 <reverseChecksum+0x78>
xor    %eax,%eax
ret
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @reverseChecksum(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca [2 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %42
12: ; preds = %2
store i64 0, i64* %6
store i32 1, i32* %8
store i32 0, i32* %9
br label %13
13: ; preds = %39, %12
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* %9
%18 = load i32, i32* %5
%19 = sub nsw i32 %18, 8
%20 = icmp slt i32 %17, %19
br label %21
21: ; preds = %16, %13
%22 = phi i1 [ false, %13 ], [ %20, %16 ]
br i1 %22, label %23, label %40
23: ; preds = %21
%24 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
store i8 0, i8* %24
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 1
store i8 0, i8* %25
%26 = load i32, i32* %4
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 1)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %23
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%33 = load i8, i8* %32
%34 = zext i8 %33 to i64
%35 = load i64, i64* %6
%36 = add i64 %35, %34
store i64 %36, i64* %6
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
br label %39
39: ; preds = %31, %23
br label %13
40: ; preds = %21
%41 = load i64, i64* %6
store i64 %41, i64* %3
br label %43
42: ; preds = %2
store i64 0, i64* %3
br label %43
43: ; preds = %42, %40
%44 = load i64, i64* %3
ret i64 %44
}

LLVM IR (-O2):
define i64 @reverseChecksum(i32 %0, i32 %1) {
%3 = alloca [2 x i8]
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %28
5: ; preds = %2
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%7 = add nsw i32 %1, -8
%8 = icmp sgt i32 %1, 8
br i1 %8, label %9, label %26
9: ; preds = %5
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
br label %11
11: ; preds = %9, %11
%12 = phi i32 [ 0, %9 ], [ %22, %11 ]
%13 = phi i64 [ 0, %9 ], [ %19, %11 ]
store i8 0, i8* %6
store i8 0, i8* %10
%14 = call i32 @read(i32 %0, i8* %6, i32 1)
%15 = icmp eq i32 %14, 0
%16 = load i8, i8* %6
%17 = zext i8 %16 to i64
%18 = select i1 %15, i64 0, i64 %17
%19 = add i64 %18, %13
%20 = xor i1 %15, true
%21 = zext i1 %20 to i32
%22 = add nuw nsw i32 %12, %21
%23 = icmp ne i32 %14, 0
%24 = icmp slt i32 %22, %7
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %11, label %26
26: ; preds = %11, %5
%27 = phi i64 [ 0, %5 ], [ %19, %11 ]
br label %28
28: ; preds = %2, %26
%29 = phi i64 [ %27, %26 ], [ 0, %2 ]
ret i64 %29
}