#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ member_0;
};
typedef TYPE_1__ fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
scalar_t__ fgetpos(int /*<<< orphan*/ *, TYPE_1__ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fsetpos(int /*<<< orphan*/ *, TYPE_1__ *);
int ftell(int /*<<< orphan*/ *);
int io_getfilesize(FILE *fp) {
  int file_len = 0;
  fpos_t pos_backup = {0};
  if (fgetpos(fp, &pos_backup))
    return -1;
  if (fseek(fp, 0, SEEK_END))
    return -1;
  file_len = ftell(fp);
  if (fsetpos(fp, &pos_backup))
    return -1;
  return file_len;
}


x86 assembly (clang, -O0):
<io_getfilesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <fgetpos@plt>
cmp    $0x0,%rax
je     1196 <io_getfilesize+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <io_getfilesize+0xa5>
mov    -0x10(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11c0 <io_getfilesize+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <io_getfilesize+0xa5>
mov    -0x10(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <fsetpos@plt>
cmp    $0x0,%rax
je     11ef <io_getfilesize+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <io_getfilesize+0xa5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_getfilesize>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1030 <fgetpos@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    119f <io_getfilesize+0x5f>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
test   %rax,%rax
jne    119f <io_getfilesize+0x5f>
mov    %rbx,%rdi
call   1040 <ftell@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <fsetpos@plt>
neg    %rax
sbb    %ebp,%ebp
or     %r14d,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32 }
@SEEK_END = global i32 0
define i32 @io_getfilesize(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.TYPE_4__
store i32* %0, i32** %3
store i32 0, i32* %4
%6 = bitcast %struct.TYPE_4__* %5 to i8*
%7 = load i32*, i32** %3
%8 = call i64 @fgetpos(i32* %7, %struct.TYPE_4__* %5)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %26
11: ; preds = %1
%12 = load i32*, i32** %3
%13 = load i32, i32* @SEEK_END
%14 = call i64 @fseek(i32* %12, i32 0, i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 -1, i32* %2
br label %26
17: ; preds = %11
%18 = load i32*, i32** %3
%19 = call i32 @ftell(i32* %18)
store i32 %19, i32* %4
%20 = load i32*, i32** %3
%21 = call i64 @fsetpos(i32* %20, %struct.TYPE_4__* %5)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %2
br label %26
24: ; preds = %17
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %24, %23, %16, %10
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32 }
@SEEK_END = global i32 0
define i32 @io_getfilesize(i32* nocapture %0) {
%2 = alloca %struct.TYPE_4__
%3 = bitcast %struct.TYPE_4__* %2 to i8*
%4 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
store i32 0, i32* %4
%5 = call i64 @fgetpos(i32* %0, %struct.TYPE_4__* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i64 @fseek(i32* %0, i32 0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = tail call i32 @ftell(i32* %0)
%13 = call i64 @fsetpos(i32* %0, %struct.TYPE_4__* %2)
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, i32 %12, i32 -1
br label %16
16: ; preds = %11, %7, %1
%17 = phi i32 [ -1, %1 ], [ -1, %7 ], [ %15, %11 ]
ret i32 %17
}