#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_SET;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int cl_flength(FILE *f) {
  int length;
  long pos = ftell(f);
  for (length = 0; !feof(f); length++)
    fgetc(f);
  fseek(f, pos, SEEK_SET);
  return length - 1;
}


x86 assembly (clang, -O0):
<cl_flength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    117e <cl_flength+0x3e>
jmp    1195 <cl_flength+0x55>
mov    -0x8(%rbp),%rdi
call   1030 <fgetc@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1160 <cl_flength+0x20>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cl_flength>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1040 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <feof@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1187 <cl_flength+0x47>
mov    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1030 <fgetc@plt>
mov    %rbx,%rdi
call   1050 <feof@plt>
add    $0x1,%ebp
test   %eax,%eax
je     1170 <cl_flength+0x30>
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fseek@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @cl_flength(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i64
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = call i64 @ftell(i32* %5)
store i64 %6, i64* %4
store i32 0, i32* %3
br label %7
7: ; preds = %15, %1
%8 = load i32*, i32** %2
%9 = call i32 @feof(i32* %8)
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = load i32*, i32** %2
%14 = call i32 @fgetc(i32* %13)
br label %15
15: ; preds = %12
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
store i32 %17, i32* %3
br label %7
18: ; preds = %7
%19 = load i32*, i32** %2
%20 = load i64, i64* %4
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %19, i64 %20, i32 %21)
%23 = load i32, i32* %3
%24 = sub nsw i32 %23, 1
ret i32 %24
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @cl_flength(i32* nocapture %0) {
%2 = tail call i64 @ftell(i32* %0)
%3 = tail call i32 @feof(i32* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %1, %5
%6 = phi i32 [ %8, %5 ], [ 0, %1 ]
%7 = tail call i32 @fgetc(i32* %0)
%8 = add nuw nsw i32 %6, 1
%9 = tail call i32 @feof(i32* %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %5, label %11
11: ; preds = %5, %1
%12 = phi i32 [ -1, %1 ], [ %6, %5 ]
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %0, i64 %2, i32 %13)
ret i32 %12
}