#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 */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long int length_of_file(char *file_name) {
  FILE *file_to_be_measured;
  long int counter = 0;
  if (file_name != NULL) {
    if ((file_to_be_measured = fopen(file_name, "r")) != NULL) {
      while (fgetc(file_to_be_measured) != EOF)
        counter++;
      counter++;
    }
    fclose(file_to_be_measured);
  }
  return counter + 1;
}


x86 assembly (clang, -O0):
<length_of_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11b1 <length_of_file+0x81>
mov    -0x8(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11a8 <length_of_file+0x78>
jmp    1172 <length_of_file+0x42>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    0x2e66(%rip),%rcx
cmp    (%rcx),%rax
je     119c <length_of_file+0x6c>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1172 <length_of_file+0x42>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<length_of_file>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     117b <length_of_file+0x4b>
lea    0xebf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
xor    %ebx,%ebx
test   %rax,%rax
je     1171 <length_of_file+0x41>
mov    0x2e91(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <fgetc@plt>
add    $0x1,%rbx
cmp    (%r15),%rax
jne    1160 <length_of_file+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    117d <length_of_file+0x4d>
xor    %ebx,%ebx
add    $0x1,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i64 0
define i64 @length_of_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %4
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %26
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %23
11: ; preds = %7
br label %12
12: ; preds = %17, %11
%13 = load i32*, i32** %3
%14 = call i64 @fgetc(i32* %13)
%15 = load i64, i64* @EOF
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i64, i64* %4
%19 = add nsw i64 %18, 1
store i64 %19, i64* %4
br label %12
20: ; preds = %12
%21 = load i64, i64* %4
%22 = add nsw i64 %21, 1
store i64 %22, i64* %4
br label %23
23: ; preds = %20, %7
%24 = load i32*, i32** %3
%25 = call i32 @fclose(i32* %24)
br label %26
26: ; preds = %23, %1
%27 = load i64, i64* %4
%28 = add nsw i64 %27, 1
ret i64 %28
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i64 0
define i64 @length_of_file(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %15, label %3
3: ; preds = %1
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %3, %6
%7 = phi i64 [ %11, %6 ], [ 1, %3 ]
%8 = tail call i64 @fgetc(i32* %4)
%9 = load i64, i64* @EOF
%10 = icmp eq i64 %8, %9
%11 = add i64 %7, 1
br i1 %10, label %12, label %6
12: ; preds = %6, %3
%13 = phi i64 [ 0, %3 ], [ %7, %6 ]
%14 = tail call i32 @fclose(i32* %4)
br label %15
15: ; preds = %12, %1
%16 = phi i64 [ %13, %12 ], [ 0, %1 ]
%17 = add nsw i64 %16, 1
ret i64 %17
}