#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
ssize_t get_file_size(FILE *f) {
  if (fseek(f, 0, SEEK_END)) {
    perror("fseek");
    return -1;
  }
  long data_size = ftell(f);
  if (data_size == -1) {
    perror("ftell");
    return -1;
  }
  if (fseek(f, 0, SEEK_SET)) {
    perror("fseek");
    return -1;
  }
  return (ssize_t)data_size;
}


x86 assembly (clang, -O0):
<get_file_size>:
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   1040 <fseek@plt>
cmp    $0x0,%rax
je     1172 <get_file_size+0x42>
lea    0xe9f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <get_file_size+0xaf>
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11a2 <get_file_size+0x72>
lea    0xe75(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <get_file_size+0xaf>
mov    -0x10(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <fseek@plt>
cmp    $0x0,%rax
je     11d8 <get_file_size+0xa8>
lea    0xe39(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <get_file_size+0xaf>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_size>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <fseek@plt>
test   %rax,%rax
jne    1180 <get_file_size+0x50>
mov    %rbx,%rdi
call   1030 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     1189 <get_file_size+0x59>
mov    %rax,%r14
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <fseek@plt>
test   %rax,%rax
jne    1180 <get_file_size+0x50>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe79(%rip),%rdi
jmp    1190 <get_file_size+0x60>
lea    0xe76(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1175 <get_file_size+0x45>

LLVM IR (-O0):
@SEEK_END = global i32 0
@.str = [6 x i8] c"fseek\00"
@.str.1 = [6 x i8] c"ftell\00"
@SEEK_SET = global i32 0
define i32 @get_file_size(i32* %0) {
%2 = alloca i32
%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 %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
11: ; preds = %1
%12 = load i32*, i32** %3
%13 = call i64 @ftell(i32* %12)
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
18: ; preds = %11
%19 = load i32*, i32** %3
%20 = load i32, i32* @SEEK_SET
%21 = call i64 @fseek(i32* %19, i32 0, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
25: ; preds = %18
%26 = load i64, i64* %4
%27 = trunc i64 %26 to i32
store i32 %27, i32* %2
br label %28
28: ; preds = %25, %23, %16, %9
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@.str = [6 x i8] c"fseek\00"
@.str.1 = [6 x i8] c"ftell\00"
@SEEK_SET = global i32 0
define i32 @get_file_size(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 %7, label %5
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %20
7: ; preds = %1
%8 = tail call i64 @ftell(i32* %0)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %20
12: ; preds = %7
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @fseek(i32* %0, i32 0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %20
18: ; preds = %12
%19 = trunc i64 %8 to i32
br label %20
20: ; preds = %10, %16, %18, %5
%21 = phi i32 [ -1, %5 ], [ -1, %10 ], [ -1, %16 ], [ %19, %18 ]
ret i32 %21
}