#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*/ SEEK_END;
int /*<<< orphan*/ _dbg(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char const *);
int file_size(const char *filename) {
  FILE *file = NULL;
  int result = -1;
  if (!(file = fopen(filename, "r"))) {
    perror(filename);
    return result;
  }
  fseek(file, 0, SEEK_END);
  result = ftell(file);
  if (file)
    fclose(file);
  if (0)
    _dbg("file size: %d\n", result);
  return result;
}


x86 assembly (clang, -O0):
<file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119d <file_size+0x4d>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <file_size+0x87>
mov    -0x18(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1040 <ftell@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d1 <file_size+0x81>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_size>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1197 <file_size+0x47>
mov    %rax,%rbx
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
mov    %rbx,%rdi
call   1040 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    1192 <file_size+0x42>

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

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