#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*/ fseek(int /*<<< orphan*/ *const, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *const);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *const);
size_t get_filesize(FILE *const restrict file) {
  long size = 0L;
  if (!file)
    return size;
  if (!fseek(file, 0, SEEK_END)) {
    size = ftell(file);
    if (size <= -1)
      return 0L;
    rewind(file);
  }
  return (size_t)size;
}


x86 assembly (clang, -O0):
<get_filesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115c <get_filesize+0x2c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11af <get_filesize+0x7f>
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
cmp    $0x0,%eax
jne    11a7 <get_filesize+0x77>
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jg     119e <get_filesize+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    11af <get_filesize+0x7f>
mov    -0x10(%rbp),%rdi
call   1030 <rewind@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filesize>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     116d <get_filesize+0x3d>
mov    %rdi,%rbx
mov    0x2ea5(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
xor    %esi,%esi
call   1050 <fseek@plt>
test   %eax,%eax
jne    1170 <get_filesize+0x40>
mov    %rbx,%rdi
call   1040 <ftell@plt>
test   %rax,%rax
js     116d <get_filesize+0x3d>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <rewind@plt>
jmp    1170 <get_filesize+0x40>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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