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

/* Variables and functions */
int /*<<< orphan*/ EOVERFLOW;
int LONG_MAX;
int /*<<< orphan*/ errno;
int ftello(int /*<<< orphan*/ *);
long ftell(FILE *stream) {
  off_t offset = ftello(stream);
  if (offset == -1)
    return -1;
  if (offset < 0 || offset > LONG_MAX) {
    errno = EOVERFLOW;
    return -1;
  }
  return (long)offset;
}


x86 assembly (clang, -O0):
<ftell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ftello@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    113f <ftell+0x2f>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1182 <ftell+0x72>
cmpl   $0x0,-0x14(%rbp)
jl     115b <ftell+0x4b>
mov    -0x14(%rbp),%eax
mov    0x2e85(%rip),%rcx
cmp    (%rcx),%eax
jle    117a <ftell+0x6a>
mov    0x2e8e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e65(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1182 <ftell+0x72>
movslq -0x14(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftell>:
push   %rax
call   1030 <ftello@plt>
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
cmp    $0xffffffff,%ecx
je     1135 <ftell+0x25>
test   %ecx,%ecx
js     1137 <ftell+0x27>
mov    0x2ea9(%rip),%rdx
cmp    (%rdx),%ecx
jg     1137 <ftell+0x27>
mov    %ecx,%eax
pop    %rcx
ret
mov    0x2eb2(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e89(%rip),%rdx
mov    %ecx,(%rdx)
pop    %rcx
ret

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

LLVM IR (-O2):
@LONG_MAX = global i32 0
@EOVERFLOW = global i32 0
@errno = global i32 0
define i64 @ftell(i32* nocapture %0) {
%2 = tail call i32 @ftello(i32* %0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = icmp slt i32 %2, 0
%6 = load i32, i32* @LONG_MAX
%7 = icmp sgt i32 %2, %6
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = load i32, i32* @EOVERFLOW
store i32 %10, i32* @errno
br label %13
11: ; preds = %4
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %1, %11, %9
%14 = phi i64 [ -1, %9 ], [ %12, %11 ], [ -1, %1 ]
ret i64 %14
}