#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
unsigned lengthof(FILE *f) {
  unsigned ret;
  if (fseek(f, 0, SEEK_END) == -1) {
    fprintf(stderr, "fseek failed: %s\n", strerror(errno));
    return -1;
  }
  if ((ret = ftell(f)) == -1) {
    fprintf(stderr, "ftell failed: %s\n", strerror(errno));
    return -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<lengthof>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
cmp    $0xffffffff,%eax
jne    11a1 <lengthof+0x61>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe70(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <lengthof+0xb4>
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11ee <lengthof+0xae>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe35(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <lengthof+0xb4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lengthof>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
cmp    $0xffffffff,%eax
je     1171 <lengthof+0x31>
mov    %rbx,%rdi
call   1040 <ftell@plt>
cmp    $0xffffffff,%eax
jne    11a1 <lengthof+0x61>
lea    0xea3(%rip),%rbx
jmp    1178 <lengthof+0x38>
lea    0xe88(%rip),%rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%ebp
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"fseek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"ftell failed: %s\0A\00"
define i32 @lengthof(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = load i32, i32* @SEEK_END
%7 = call i32 @fseek(i32* %5, i32 0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 -1, i32* %2
br label %25
14: ; preds = %1
%15 = load i32*, i32** %3
%16 = call i32 @ftell(i32* %15)
store i32 %16, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 -1, i32* %2
br label %25
23: ; preds = %14
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %25
25: ; preds = %23, %18, %9
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"fseek failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"ftell failed: %s\0A\00"
define i32 @lengthof(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, -1
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = tail call i32 @ftell(i32* %0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %5, %1
%9 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %5 ]
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* %9, i8* %12)
br label %14
14: ; preds = %8, %5
%15 = phi i32 [ %6, %5 ], [ -1, %8 ]
ret i32 %15
}