#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*/ uint64_t;
struct stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ stderr;
uint64_t file_size(const char *filename) {
  FILE *f = fopen(filename, "r");
  if (f == NULL) {
    fprintf(stderr, "Could not open %s for reading!\n", filename);
    return 0;
  }
  struct stat buf;
  int fd = fileno(f);
  if (fstat(fd, &buf) != 0) {
    fprintf(stderr, "Could not stat %s!\n", filename);
    return 0;
  }
  uint64_t size = buf.st_size;
  fclose(f);
  return size;
}


x86 assembly (clang, -O0):
<file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <file_size+0x50>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe73(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fc <file_size+0xac>
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     11e7 <file_size+0x97>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4c(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fc <file_size+0xac>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119a <file_size+0x4a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
je     11b6 <file_size+0x66>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe8a(%rip),%rsi
jmp    11aa <file_size+0x5a>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
xor    %ebp,%ebp
jmp    11c2 <file_size+0x72>
mov    0x8(%rsp),%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Could not open %s for reading!\0A\00"
@.str.2 = [20 x i8] c"Could not stat %s!\0A\00"
define i32 @file_size(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 0, i32* %2
br label %32
16: ; preds = %1
%17 = load i32*, i32** %4
%18 = call i32 @fileno(i32* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i64 @fstat(i32 %19, %struct.stat* %5)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %3
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 0, i32* %2
br label %32
26: ; preds = %16
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%28 = load i32, i32* %27
store i32 %28, i32* %7
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %7
store i32 %31, i32* %2
br label %32
32: ; preds = %26, %22, %12
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Could not open %s for reading!\0A\00"
@.str.2 = [20 x i8] c"Could not stat %s!\0A\00"
define i32 @file_size(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
8: ; preds = %1
%9 = bitcast %struct.stat* %2 to i8*
%10 = tail call i32 @fileno(i32* %3)
%11 = call i64 @fstat(i32 %10, %struct.stat* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %20
16: ; preds = %8
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i32 @fclose(i32* %3)
br label %20
20: ; preds = %16, %13
%21 = phi i32 [ 0, %13 ], [ %18, %16 ]
br label %22
22: ; preds = %20, %5
%23 = phi i32 [ 0, %5 ], [ %21, %20 ]
ret i32 %23
}