#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int ftell(int /*<<< orphan*/ *);
int file_get_size(FILE *fp) {
  int offset, sz;
  int fd;
  struct stat fst;
  if (!fp)
    return -1;
  fd = fileno(fp);
  if (fstat(fd, &fst) == 0)
    return fst.st_size;
  offset = ftell(fp);
  fseek(fp, 0, SEEK_END);
  sz = ftell(fp);
  fseek(fp, offset, SEEK_SET);
  return sz;
}


x86 assembly (clang, -O0):
<file_get_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <file_get_size+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <file_get_size+0x97>
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jne    1190 <file_get_size+0x50>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <file_get_size+0x97>
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11a6 <file_get_size+0x66>
mov    %rdi,%rbx
call   1040 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %rax,%rax
je     11ad <file_get_size+0x6d>
mov    %rbx,%rdi
call   1030 <ftell@plt>
mov    %eax,%r14d
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
mov    %rbx,%rdi
call   1030 <ftell@plt>
mov    %eax,%ebp
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <fseek@plt>
mov    %ebp,%eax
jmp    11b1 <file_get_size+0x71>
mov    $0xffffffff,%eax
jmp    11b1 <file_get_size+0x71>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @file_get_size(i32* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i32* %0, null
br i1 %4, label %19, label %5
5: ; preds = %1
%6 = tail call i32 @fileno(i32* %0)
%7 = call i64 @fstat(i32 %6, %struct.stat* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
br label %19
12: ; preds = %5
%13 = tail call i32 @ftell(i32* %0)
%14 = load i32, i32* @SEEK_END
%15 = tail call i32 @fseek(i32* %0, i32 0, i32 %14)
%16 = tail call i32 @ftell(i32* %0)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %0, i32 %13, i32 %17)
br label %19
19: ; preds = %1, %12, %9
%20 = phi i32 [ %11, %9 ], [ %16, %12 ], [ -1, %1 ]
ret i32 %20
}