#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 fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long int getFileSize(FILE *fd) {
  int err;
  long int fsize;
  err = fseek(fd, 0, SEEK_END);
  if (err != 0)
    return -1;
  fsize = ftell(fd);
  if (fsize == -1L)
    return -1;
  rewind(fd);
  return fsize;
}


x86 assembly (clang, -O0):
<getFileSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     116a <getFileSize+0x3a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a0 <getFileSize+0x70>
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    118f <getFileSize+0x5f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a0 <getFileSize+0x70>
mov    -0x10(%rbp),%rdi
call   1030 <rewind@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFileSize>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2ea9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
mov    $0xffffffffffffffff,%rbx
test   %eax,%eax
jne    116f <getFileSize+0x3f>
mov    %r14,%rdi
call   1040 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     116f <getFileSize+0x3f>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <rewind@plt>
mov    %r15,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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