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


x86 assembly (clang, -O0):
<file_line_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1167 <file_line_count+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <file_line_count+0x83>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jne    118d <file_line_count+0x5d>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1192 <file_line_count+0x62>
mov    -0x1c(%rbp),%eax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%eax
jne    116e <file_line_count+0x3e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_line_count>:
push   %r15
push   %r14
push   %rbx
lea    0xec4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1181 <file_line_count+0x51>
mov    %rax,%r14
xor    %ebx,%ebx
mov    0x2e96(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebx
cmp    (%r15),%eax
jne    1160 <file_line_count+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1186 <file_line_count+0x56>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i32 @file_line_count(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 = icmp eq i32* %2, null
br i1 %3, label %14, label %4
4: ; preds = %1, %4
%5 = phi i32 [ %9, %4 ], [ 0, %1 ]
%6 = tail call i32 @fgetc(i32* %2)
%7 = icmp eq i32 %6, 10
%8 = zext i1 %7 to i32
%9 = add nuw nsw i32 %5, %8
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %6, %10
br i1 %11, label %12, label %4
12: ; preds = %4
%13 = tail call i32 @fclose(i32* %2)
br label %14
14: ; preds = %1, %12
%15 = phi i32 [ %9, %12 ], [ -1, %1 ]
ret i32 %15
}