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


x86 assembly (clang, -O0):
<getNumLines>:
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   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
je     117d <getNumLines+0x4d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    11a1 <getNumLines+0x71>
cmpl   $0x0,-0x1c(%rbp)
jle    119a <getNumLines+0x6a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    11a6 <getNumLines+0x76>
mov    -0x14(%rbp),%eax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%eax
jne    115e <getNumLines+0x2e>
cmpl   $0xa,-0x14(%rbp)
je     11d5 <getNumLines+0xa5>
cmpl   $0x0,-0x18(%rbp)
je     11d5 <getNumLines+0xa5>
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):
<getNumLines>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xec1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r12
xor    %r14d,%r14d
mov    $0xffffffff,%ebp
mov    0x2e92(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%eax
setne  %cl
add    %ebx,%ecx
cmp    $0xa,%eax
mov    %ecx,%ebx
cmove  %r14d,%ebx
sete   %dl
test   %ecx,%ecx
setg   %cl
and    %dl,%cl
movzbl %cl,%ecx
add    %ecx,%ebp
cmp    (%r15),%eax
jne    1160 <getNumLines+0x30>
cmp    $0xa,%eax
setne  %al
test   %ebp,%ebp
setne  %cl
and    %al,%cl
movzbl %cl,%r14d
add    %ebp,%r14d
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r14d,%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 @getNumLines(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
store i32 -1, i32* %5
store i32 0, i32* %6
br label %9
9: ; preds = %28, %1
%10 = load i32*, i32** %3
%11 = call i32 @fgetc(i32* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
store i32 %16, i32* %6
br label %17
17: ; preds = %14, %9
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %26
26: ; preds = %23, %20
store i32 0, i32* %6
br label %27
27: ; preds = %26, %17
br label %28
28: ; preds = %27
%29 = load i32, i32* %4
%30 = load i32, i32* @EOF
%31 = icmp ne i32 %29, %30
br i1 %31, label %9, label %32
32: ; preds = %28
%33 = load i32, i32* %4
%34 = icmp ne i32 %33, 10
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* %5
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
br label %41
41: ; preds = %38, %35, %32
%42 = load i32*, i32** %3
%43 = call i32 @fclose(i32* %42)
%44 = load i32, i32* %5
ret i32 %44
}

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