#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 scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ EOVERFLOW;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
long filelength(int fd) {
  off_t cur, len;
  if ((cur = lseek(fd, 0, SEEK_CUR)) == (off_t)-1)
    return -1;
  if ((len = lseek(fd, 0, SEEK_END)) == (off_t)-1)
    return -1;
  if (lseek(fd, cur, SEEK_SET) == (off_t)-1)
    return -1;
  long res = (long)len;
  if (res < 0) {
    errno = EOVERFLOW;
    return -1;
  }
  return res;
}


x86 assembly (clang, -O0):
<filelength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2eb3(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <lseek@plt>
mov    %rax,-0x18(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    114b <filelength+0x3b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e1 <filelength+0xd1>
mov    -0xc(%rbp),%edi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <lseek@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    117b <filelength+0x6b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e1 <filelength+0xd1>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11a7 <filelength+0x97>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e1 <filelength+0xd1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11d9 <filelength+0xc9>
mov    0x2e2f(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfe(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e1 <filelength+0xd1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filelength>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2eb8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    $0xffffffffffffffff,%r14
cmp    $0xffffffffffffffff,%rax
je     118e <filelength+0x7e>
mov    %rax,%rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     118e <filelength+0x7e>
mov    %rax,%r15
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     118e <filelength+0x7e>
mov    %r15,%r14
test   %r15,%r15
jns    118e <filelength+0x7e>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define i64 @filelength(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = load i32, i32* @SEEK_CUR
%9 = call i64 @lseek(i32 %7, i64 0, i32 %8)
store i64 %9, i64* %4
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i64 -1, i64* %2
br label %33
12: ; preds = %1
%13 = load i32, i32* %3
%14 = load i32, i32* @SEEK_END
%15 = call i64 @lseek(i32 %13, i64 0, i32 %14)
store i64 %15, 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 %33
18: ; preds = %12
%19 = load i32, i32* %3
%20 = load i64, i64* %4
%21 = load i32, i32* @SEEK_SET
%22 = call i64 @lseek(i32 %19, i64 %20, i32 %21)
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %18
store i64 -1, i64* %2
br label %33
25: ; preds = %18
%26 = load i64, i64* %5
store i64 %26, i64* %6
%27 = load i64, i64* %6
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* @EOVERFLOW
store i32 %30, i32* @errno
store i64 -1, i64* %2
br label %33
31: ; preds = %25
%32 = load i64, i64* %6
store i64 %32, i64* %2
br label %33
33: ; preds = %31, %29, %24, %17, %11
%34 = load i64, i64* %2
ret i64 %34
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define i64 @filelength(i32 %0) {
%2 = load i32, i32* @SEEK_CUR
%3 = tail call i64 @lseek(i32 %0, i64 0, i32 %2)
%4 = icmp eq i64 %3, -1
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @lseek(i32 %0, i64 0, i32 %6)
%8 = icmp eq i64 %7, -1
br i1 %8, label %17, label %9
9: ; preds = %5
%10 = load i32, i32* @SEEK_SET
%11 = tail call i64 @lseek(i32 %0, i64 %3, i32 %10)
%12 = icmp eq i64 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = icmp slt i64 %7, 0
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = load i32, i32* @EOVERFLOW
store i32 %16, i32* @errno
br label %17
17: ; preds = %15, %13, %9, %5, %1
%18 = phi i64 [ -1, %1 ], [ -1, %5 ], [ -1, %9 ], [ -1, %15 ], [ %7, %13 ]
ret i64 %18
}