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


x86 assembly (clang, -O0):
<count_lines_in_file>:
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)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1175 <count_lines_in_file+0x45>
movl   $0x0,-0x4(%rbp)
jmp    11c5 <count_lines_in_file+0x95>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%eax
je     11b6 <count_lines_in_file+0x86>
mov    -0x1c(%rbp),%eax
movsbl %al,%eax
cmp    $0xa,%eax
jne    11b1 <count_lines_in_file+0x81>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    117e <count_lines_in_file+0x4e>
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):
<count_lines_in_file>:
push   %rbp
push   %r14
push   %rbx
lea    0xec5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1190 <count_lines_in_file+0x60>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    0x2e91(%rip),%r14
mov    $0x1,%ebp
cmp    (%r14),%eax
je     1186 <count_lines_in_file+0x56>
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    (%r14),%eax
jne    1170 <count_lines_in_file+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1192 <count_lines_in_file+0x62>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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