#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int file_length(char *filename) {
  int lines = 0;
  int ch;
  FILE *f;
  f = fopen(filename, "r");
  while (EOF != (ch = fgetc(f))) {
    if (ch == '\n')
      lines++;
  }
  fclose(f);
  return lines;
}


x86 assembly (clang, -O0):
<file_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
mov    %ecx,-0x10(%rbp)
cmp    %ecx,%eax
je     1194 <file_length+0x64>
cmpl   $0xa,-0x10(%rbp)
jne    118f <file_length+0x5f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1157 <file_length+0x27>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_length>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xec1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r12
mov    0x2e9a(%rip),%r14
mov    (%r14),%r15d
mov    %rax,%rdi
call   1040 <fgetc@plt>
xor    %ebp,%ebp
cmp    %eax,%r15d
je     1189 <file_length+0x59>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebp
mov    (%r14),%ebx
mov    %r12,%rdi
call   1040 <fgetc@plt>
cmp    %eax,%ebx
jne    1170 <file_length+0x40>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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