#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
long file_size(const char *fname) {
  long result = 0;
  FILE *f = fopen(fname, "rb");
  char buff[1024];
  for (;;) {
    size_t cnt = fread(buff, 1, 1024, f);
    if (cnt > 0)
      result += cnt;
    else
      break;
  };
  fclose(f);
  return result;
}


x86 assembly (clang, -O0):
<file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeae(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jbe    119e <file_size+0x6e>
mov    -0x428(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11a3 <file_size+0x73>
jmp    11a8 <file_size+0x78>
jmp    115b <file_size+0x2b>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_size>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xebd(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
xor    %ebx,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
add    %rax,%rbx
test   %rax,%rax
jne    1150 <file_size+0x20>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i64 @file_size(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %3
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
br label %9
9: ; preds = %20, %1
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = load i32*, i32** %4
%12 = call i64 @fread(i8* %10, i32 1, i32 1024, i32* %11)
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i64, i64* %6
%17 = load i64, i64* %3
%18 = add i64 %17, %16
store i64 %18, i64* %3
br label %20
19: ; preds = %9
br label %21
20: ; preds = %15
br label %9
21: ; preds = %19
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
%24 = load i64, i64* %3
ret i64 %24
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i64 @file_size(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %5
5: ; preds = %5, %1
%6 = phi i64 [ 0, %1 ], [ %9, %5 ]
%7 = call i64 @fread(i8* %4, i32 1, i32 1024, i32* %3)
%8 = icmp eq i64 %7, 0
%9 = add i64 %7, %6
br i1 %8, label %10, label %5
10: ; preds = %5
%11 = tail call i32 @fclose(i32* %3)
ret i64 %9
}