#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t file_length(char *filename) {
  int length = -1;
  FILE *file = fopen(filename, "r");
  if (file != NULL) {
    fseek(file, 0, SEEK_END);
    length = ftell(file);
    fclose(file);
  }
  fprintf(stderr, "error unknown file : (%s)\n", filename);
  return length;
}


x86 assembly (clang, -O0):
<file_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ab <file_length+0x5b>
mov    -0x18(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1040 <fprintf@plt>
movslq -0xc(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_length>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1195 <file_length+0x45>
mov    %rax,%rbx
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
movslq %ebp,%rbx
jmp    119c <file_length+0x4c>
mov    $0xffffffffffffffff,%rbx
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"error unknown file : (%s)\0A\00"
define i64 @file_length(i8* %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 %10, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %2, i32 0, i32 %5)
%7 = tail call i32 @ftell(i32* %2)
%8 = tail call i32 @fclose(i32* %2)
%9 = sext i32 %7 to i64
br label %10
10: ; preds = %4, %1
%11 = phi i64 [ %9, %4 ], [ -1, %1 ]
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
ret i64 %11
}