#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*/ SEEK_SET;
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
long _get_file_size(FILE *fp) {
  long current_position = ftell(fp);
  if (current_position == -1) {
    return -1;
  }
  if (fseek(fp, 0, SEEK_END) != 0) {
    return -2;
  }
  long file_size = ftell(fp);
  if (file_size == -1) {
    return -3;
  }
  if (fseek(fp, current_position, SEEK_SET) != 0) {
    return -4;
  }
  return file_size;
}


x86 assembly (clang, -O0):
<_get_file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    1151 <_get_file_size+0x31>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d8 <_get_file_size+0xb8>
mov    -0x10(%rbp),%rdi
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <fseek@plt>
cmp    $0x0,%rax
je     117e <_get_file_size+0x5e>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    11d8 <_get_file_size+0xb8>
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11a3 <_get_file_size+0x83>
movq   $0xfffffffffffffffd,-0x8(%rbp)
ff
jmp    11d8 <_get_file_size+0xb8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
call   1040 <fseek@plt>
cmp    $0x0,%rax
je     11d0 <_get_file_size+0xb0>
movq   $0xfffffffffffffffc,-0x8(%rbp)
ff
jmp    11d8 <_get_file_size+0xb8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_file_size>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     118d <_get_file_size+0x6d>
mov    %rax,%r14
mov    0x2eab(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <fseek@plt>
mov    %rax,%rcx
mov    $0xfffffffffffffffe,%rax
test   %rcx,%rcx
jne    119d <_get_file_size+0x7d>
mov    %rbx,%rdi
call   1030 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1196 <_get_file_size+0x76>
mov    %rax,%r15
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <fseek@plt>
test   %rax,%rax
mov    $0xfffffffffffffffc,%rax
cmove  %r15,%rax
jmp    119d <_get_file_size+0x7d>
mov    $0xffffffffffffffff,%rax
jmp    119d <_get_file_size+0x7d>
mov    $0xfffffffffffffffd,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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