#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;
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long get_file_length_and_rewind(FILE *f) {
  long sz;
  if (fseek(f, 0, SEEK_END))
    return -1;
  if ((sz = ftell(f)) < 0)
    return -1;
  rewind(f);
  return sz;
}


x86 assembly (clang, -O0):
<get_file_length_and_rewind>:
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>
cmp    $0x0,%rax
je     1167 <get_file_length_and_rewind+0x37>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    119c <get_file_length_and_rewind+0x6c>
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    118b <get_file_length_and_rewind+0x5b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    119c <get_file_length_and_rewind+0x6c>
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_file_length_and_rewind>:
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   %rax,%rax
jne    116f <get_file_length_and_rewind+0x3f>
mov    %r14,%rdi
call   1040 <ftell@plt>
test   %rax,%rax
js     116f <get_file_length_and_rewind+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 @get_file_length_and_rewind(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = load i32, i32* @SEEK_END
%7 = call i64 @fseek(i32* %5, i32 0, i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i64 -1, i64* %2
br label %19
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = call i64 @ftell(i32* %11)
store i64 %12, i64* %4
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i64 -1, i64* %2
br label %19
15: ; preds = %10
%16 = load i32*, i32** %3
%17 = call i32 @rewind(i32* %16)
%18 = load i64, i64* %4
store i64 %18, i64* %2
br label %19
19: ; preds = %15, %14, %9
%20 = load i64, i64* %2
ret i64 %20
}

LLVM IR (-O2):
@SEEK_END = global i32 0
define i64 @get_file_length_and_rewind(i32* nocapture %0) {
%2 = load i32, i32* @SEEK_END
%3 = tail call i64 @fseek(i32* %0, i32 0, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = tail call i64 @ftell(i32* %0)
%7 = icmp slt i64 %6, 0
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
}