#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 const *, char *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int _file_size(const char *filename) {
  int ret = 0;
  FILE *f = fopen(filename, "r");
  if (f) {
    if (fseek(f, 0L, SEEK_END) == 0)
      ret = ftell(f);
    fclose(f);
  }
  return ret;
}


x86 assembly (clang, -O0):
<_file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a7 <_file_size+0x67>
mov    -0x18(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <fseek@plt>
cmp    $0x0,%rax
jne    119e <_file_size+0x5e>
mov    -0x18(%rbp),%rdi
call   1040 <ftell@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_file_size>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1188 <_file_size+0x48>
mov    %rax,%rbx
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
test   %rax,%rax
jne    117e <_file_size+0x3e>
mov    %rbx,%rdi
call   1040 <ftell@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    118b <_file_size+0x4b>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
define i32 @_file_size(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i8* %0, i8** %2
store i32 0, 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 %20
9: ; preds = %1
%10 = load i32*, i32** %4
%11 = load i32, i32* @SEEK_END
%12 = call i64 @fseek(i32* %10, i64 0, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32*, i32** %4
%16 = call i32 @ftell(i32* %15)
store i32 %16, i32* %3
br label %17
17: ; preds = %14, %9
%18 = load i32*, i32** %4
%19 = call i32 @fclose(i32* %18)
br label %20
20: ; preds = %17, %1
%21 = load i32, i32* %3
ret i32 %21
}

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 %13, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i64 0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @ftell(i32* %2)
br label %10
10: ; preds = %8, %4
%11 = phi i32 [ %9, %8 ], [ 0, %4 ]
%12 = tail call i32 @fclose(i32* %2)
br label %13
13: ; preds = %10, %1
%14 = phi i32 [ %11, %10 ], [ 0, %1 ]
ret i32 %14
}