#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 number_of_lines_in_file(char *filename) {
  FILE *myfile = fopen(filename, "r");
  int ch, number_of_lines = 0;
  do {
    ch = fgetc(myfile);
    if (ch == '\n')
      number_of_lines++;
  } while (ch != EOF);
  if (ch != '\n' && number_of_lines != 0)
    number_of_lines++;
  fclose(myfile);
  return number_of_lines;
}


x86 assembly (clang, -O0):
<number_of_lines_in_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    1176 <number_of_lines_in_file+0x46>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117b <number_of_lines_in_file+0x4b>
mov    -0x14(%rbp),%eax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%eax
jne    1157 <number_of_lines_in_file+0x27>
cmpl   $0xa,-0x14(%rbp)
je     11aa <number_of_lines_in_file+0x7a>
cmpl   $0x0,-0x18(%rbp)
je     11aa <number_of_lines_in_file+0x7a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<number_of_lines_in_file>:
push   %rbp
push   %r14
push   %rbx
lea    0xec5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    0x2e9c(%rip),%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebp
cmp    (%r14),%eax
jne    1150 <number_of_lines_in_file+0x20>
cmp    $0xa,%eax
setne  %al
test   %ebp,%ebp
setne  %cl
and    %al,%cl
movzbl %cl,%r14d
add    %ebp,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i32 @number_of_lines_in_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%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** %3
store i32 0, i32* %5
br label %8
8: ; preds = %17, %1
%9 = load i32*, i32** %3
%10 = call i32 @fgetc(i32* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 10
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %5
br label %16
16: ; preds = %13, %8
br label %17
17: ; preds = %16
%18 = load i32, i32* %4
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %18, %19
br i1 %20, label %8, label %21
21: ; preds = %17
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %30
30: ; preds = %27, %24, %21
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* %5
ret i32 %33
}

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