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


x86 assembly (clang, -O0):
<ffcount_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    1168 <ffcount_lines+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <ffcount_lines+0xb2>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jne    1199 <ffcount_lines+0x69>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119e <ffcount_lines+0x6e>
mov    -0x24(%rbp),%eax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%eax
jne    1177 <ffcount_lines+0x47>
cmpl   $0xa,-0x24(%rbp)
je     11d1 <ffcount_lines+0xa1>
cmpq   $0x0,-0x20(%rbp)
je     11d1 <ffcount_lines+0xa1>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ffcount_lines>:
push   %r15
push   %r14
push   %rbx
lea    0xec4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1197 <ffcount_lines+0x67>
mov    %rax,%r15
xor    %ebx,%ebx
mov    0x2e96(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1040 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %rcx,%rbx
cmp    (%r14),%eax
jne    1160 <ffcount_lines+0x30>
cmp    $0xa,%eax
setne  %al
test   %rbx,%rbx
setne  %cl
and    %al,%cl
movzbl %cl,%r14d
add    %rbx,%r14
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    119a <ffcount_lines+0x6a>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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