#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*/ SEEK_SET;
int /*<<< orphan*/ chaz_Util_die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
long fseek(int /*<<< orphan*/ *, long const, int /*<<< orphan*/);
void *ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
long chaz_Util_flength(void *file) {
  FILE *f = (FILE *)file;
  const long bookmark = ftell(f);
  long check_val;
  long len;
  check_val = fseek(f, 0, SEEK_END);
  if (check_val == -1) {
    chaz_Util_die("fseek error : %s\n", strerror(errno));
  }
  len = ftell(f);
  if (len == -1) {
    chaz_Util_die("ftell error : %s\n", strerror(errno));
  }
  check_val = fseek(f, bookmark, SEEK_SET);
  if (check_val == -1) {
    chaz_Util_die("fseek error : %s\n", strerror(errno));
  }
  return len;
}


x86 assembly (clang, -O0):
<chaz_Util_flength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <fseek@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11a2 <chaz_Util_flength+0x62>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe63(%rip),%rdi
call   1060 <chaz_Util_die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11d6 <chaz_Util_flength+0x96>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe41(%rip),%rdi
call   1060 <chaz_Util_die@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
call   1040 <fseek@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1217 <chaz_Util_flength+0xd7>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xdee(%rip),%rdi
call   1060 <chaz_Util_die@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaz_Util_flength>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <ftell@plt>
mov    %rax,%r14
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <fseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    1185 <chaz_Util_flength+0x45>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe82(%rip),%rdi
mov    %eax,%esi
call   1060 <chaz_Util_die@plt>
mov    %rbx,%rdi
call   1030 <ftell@plt>
mov    %rax,%r15
cmp    $0xffffffffffffffff,%rax
jne    11b2 <chaz_Util_flength+0x72>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe67(%rip),%rdi
mov    %eax,%esi
call   1060 <chaz_Util_die@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <fseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    11e8 <chaz_Util_flength+0xa8>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
call   1060 <chaz_Util_die@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@.str = [18 x i8] c"fseek error : %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"ftell error : %s\0A\00"
@SEEK_SET = global i32 0
define i64 @chaz_Util_flength(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to i32*
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = call i8* @ftell(i32* %9)
%11 = ptrtoint i8* %10 to i64
store i64 %11, i64* %4
%12 = load i32*, i32** %3
%13 = load i32, i32* @SEEK_END
%14 = call i64 @fseek(i32* %12, i64 0, i32 %13)
store i64 %14, i64* %5
%15 = load i64, i64* %5
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @chaz_Util_die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %1
%22 = load i32*, i32** %3
%23 = call i8* @ftell(i32* %22)
%24 = ptrtoint i8* %23 to i64
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = icmp eq i64 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @chaz_Util_die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %21
%32 = load i32*, i32** %3
%33 = load i64, i64* %4
%34 = load i32, i32* @SEEK_SET
%35 = call i64 @fseek(i32* %32, i64 %33, i32 %34)
store i64 %35, i64* %5
%36 = load i64, i64* %5
%37 = icmp eq i64 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 @chaz_Util_die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %38, %31
%43 = load i64, i64* %6
ret i64 %43
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@.str = [18 x i8] c"fseek error : %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"ftell error : %s\0A\00"
@SEEK_SET = global i32 0
define i64 @chaz_Util_flength(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = tail call i8* @ftell(i32* %2)
%4 = ptrtoint i8* %3 to i64
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %2, i64 0, i32 %5)
%7 = icmp eq i64 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @chaz_Util_die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %8, %1
%13 = tail call i8* @ftell(i32* %2)
%14 = icmp eq i8* %13, inttoptr (i64 -1 to i8*)
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @chaz_Util_die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %12
%20 = load i32, i32* @SEEK_SET
%21 = tail call i64 @fseek(i32* %2, i64 %4, i32 %20)
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @errno
%25 = tail call i32 @strerror(i32 %24)
%26 = tail call i32 @chaz_Util_die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23, %19
%28 = ptrtoint i8* %13 to i64
ret i64 %28
}