#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 */
long SEEK_END;
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, long);
long ftell(int /*<<< orphan*/ *);
long get_filesize(FILE *f) {
  int errorcode = 0;
  long size;
  long original_offset = 0;
  if (f) {
    original_offset = ftell(f);
    if (original_offset == -1)
      return -1;
    errorcode = fseek(f, 0, SEEK_END);
    if (errorcode < 0)
      return -1;
    size = ftell(f);
    errorcode = fseek(f, 0, original_offset);
    if (errorcode < 0)
      return -1;
    return size;
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_filesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11dd <get_filesize+0xbd>
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    116b <get_filesize+0x4b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e5 <get_filesize+0xc5>
mov    -0x10(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%rdx
xor    %esi,%esi
call   1040 <fseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119a <get_filesize+0x7a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e5 <get_filesize+0xc5>
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1040 <fseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d0 <get_filesize+0xb0>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e5 <get_filesize+0xc5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e5 <get_filesize+0xc5>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filesize>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0xffffffffffffffff,%r14
test   %rdi,%rdi
je     1182 <get_filesize+0x62>
mov    %rdi,%rbx
call   1030 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1182 <get_filesize+0x62>
mov    %rax,%r15
mov    0x2e9c(%rip),%rax
mov    (%rax),%rdx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <fseek@plt>
test   %eax,%eax
js     1182 <get_filesize+0x62>
mov    %rbx,%rdi
call   1030 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <fseek@plt>
test   %eax,%eax
mov    $0xffffffffffffffff,%r14
cmovns %r12,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_END = global i64 0
define i64 @get_filesize(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i32* %0, i32** %3
store i32 0, i32* %4
store i64 0, i64* %6
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %33
9: ; preds = %1
%10 = load i32*, i32** %3
%11 = call i64 @ftell(i32* %10)
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %9
store i64 -1, i64* %2
br label %34
15: ; preds = %9
%16 = load i32*, i32** %3
%17 = load i64, i64* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i64 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i64 -1, i64* %2
br label %34
22: ; preds = %15
%23 = load i32*, i32** %3
%24 = call i64 @ftell(i32* %23)
store i64 %24, i64* %5
%25 = load i32*, i32** %3
%26 = load i64, i64* %6
%27 = call i32 @fseek(i32* %25, i32 0, i64 %26)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %22
store i64 -1, i64* %2
br label %34
31: ; preds = %22
%32 = load i64, i64* %5
store i64 %32, i64* %2
br label %34
33: ; preds = %1
store i64 -1, i64* %2
br label %34
34: ; preds = %33, %31, %30, %21, %14
%35 = load i64, i64* %2
ret i64 %35
}

LLVM IR (-O2):
@SEEK_END = global i64 0
define i64 @get_filesize(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = tail call i64 @ftell(i32* %0)
%5 = icmp eq i64 %4, -1
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = load i64, i64* @SEEK_END
%8 = tail call i32 @fseek(i32* %0, i32 0, i64 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %6
%11 = tail call i64 @ftell(i32* %0)
%12 = tail call i32 @fseek(i32* %0, i32 0, i64 %4)
%13 = icmp slt i32 %12, 0
%14 = select i1 %13, i64 -1, i64 %11
br label %15
15: ; preds = %1, %10, %6, %3
%16 = phi i64 [ -1, %3 ], [ -1, %6 ], [ %14, %10 ], [ -1, %1 ]
ret i64 %16
}