#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(unsigned char const *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(unsigned char *, int, char *, long long);
int checksum_file(const unsigned char *name, unsigned char *buf, int size) {
  FILE *f = fopen(name, "r");
  if (!f)
    return -1;
  long long sum = 0;
  int c;
  while ((c = getc(f)) != EOF) {
    sum += c;
  }
  fclose(f);
  f = NULL;
  snprintf(buf, size, "%016llx", sum);
  return 0;
}


x86 assembly (clang, -O0):
<checksum_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117e <checksum_file+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <checksum_file+0xa1>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x34(%rbp)
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%eax
je     11b2 <checksum_file+0x72>
movslq -0x34(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1186 <checksum_file+0x46>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
lea    0xe2d(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
lea    0xeab(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a9 <checksum_file+0x69>
mov    %rax,%rbp
xor    %eax,%eax
mov    0x2e7d(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <getc@plt>
movslq %eax,%rcx
lea    (%rbx,%rcx,1),%rax
cmp    (%r12),%ecx
jne    1170 <checksum_file+0x30>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe6b(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
xor    %eax,%eax
jmp    11ae <checksum_file+0x6e>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [8 x i8] c"%016llx\00"
define i32 @checksum_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i32 -1, i32* %4
br label %34
16: ; preds = %3
store i64 0, i64* %9
br label %17
17: ; preds = %22, %16
%18 = load i32*, i32** %8
%19 = call i32 @getc(i32* %18)
store i32 %19, i32* %10
%20 = load i32, i32* @EOF
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = load i64, i64* %9
%26 = add nsw i64 %25, %24
store i64 %26, i64* %9
br label %17
27: ; preds = %17
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
store i32* null, i32** %8
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = load i64, i64* %9
%33 = call i32 @snprintf(i8* %30, i32 %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 %32)
store i32 0, i32* %4
br label %34
34: ; preds = %27, %15
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [8 x i8] c"%016llx\00"
define i32 @checksum_file(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3, %6
%7 = phi i64 [ %12, %6 ], [ 0, %3 ]
%8 = tail call i32 @getc(i32* %4)
%9 = load i32, i32* @EOF
%10 = icmp eq i32 %8, %9
%11 = sext i32 %8 to i64
%12 = add nsw i64 %7, %11
br i1 %10, label %13, label %6
13: ; preds = %6
%14 = tail call i32 @fclose(i32* %4)
%15 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 %7)
br label %16
16: ; preds = %3, %13
%17 = phi i32 [ 0, %13 ], [ -1, %3 ]
ret i32 %17
}