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


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1182 <checksum+0x72>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1175 <checksum+0x65>
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     1170 <checksum+0x60>
movzbl -0x1a(%rbp),%eax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1134 <checksum+0x24>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    118a <checksum+0x7a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
jle    115f <checksum+0x4f>
mov    %edi,%ebp
xor    %ebx,%ebx
lea    0xe(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
movw   $0x0,0xe(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
movzbl 0xe(%rsp),%ecx
mov    $0x0,%edx
cmovne %rcx,%rdx
add    %rdx,%rbx
test   %eax,%eax
jne    1130 <checksum+0x20>
jmp    1161 <checksum+0x51>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @checksum(i32 %0) {
%2 = alloca [2 x i8]
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %16
4: ; preds = %1
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
br label %7
7: ; preds = %4, %7
%8 = phi i64 [ 0, %4 ], [ %14, %7 ]
store i8 0, i8* %5
store i8 0, i8* %6
%9 = call i32 @read(i32 %0, i8* %5, i32 1)
%10 = icmp eq i32 %9, 0
%11 = load i8, i8* %5
%12 = zext i8 %11 to i64
%13 = select i1 %10, i64 0, i64 %12
%14 = add i64 %13, %8
br i1 %10, label %15, label %7
15: ; preds = %7
br label %16
16: ; preds = %1, %15
%17 = phi i64 [ %14, %15 ], [ 0, %1 ]
ret i64 %17
}