#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int nlines(char *inputFileName) {
  FILE *inputStream;
  inputStream = fopen(inputFileName, "r");
  if (inputStream == NULL) {
    printf("Could not open the file %s for reading.\n", inputFileName);
    return -5;
  }
  int N = 0;
  int nChars = -1;
  char currentChar = ' ';
  char lastChar;
  while (!feof(inputStream)) {
    lastChar = currentChar;
    currentChar = fgetc(inputStream);
    nChars++;
    if (currentChar == '\n')
      N++;
  }
  if (lastChar != '\n' && nChars != 0)
    N++;
  fclose(inputStream);
  return N;
}


x86 assembly (clang, -O0):
<nlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <nlines+0x47>
mov    -0x10(%rbp),%rsi
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    122c <nlines+0xdc>
movl   $0x0,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movb   $0x20,-0x21(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11c7 <nlines+0x77>
jmp    11fd <nlines+0xad>
mov    -0x21(%rbp),%al
mov    %al,-0x22(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x21(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    11f8 <nlines+0xa8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a9 <nlines+0x59>
movsbl -0x22(%rbp),%eax
cmp    $0xa,%eax
je     121d <nlines+0xcd>
cmpl   $0x0,-0x20(%rbp)
je     121d <nlines+0xcd>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlines>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1182 <nlines+0x32>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     1198 <nlines+0x48>
mov    $0x1,%al
xor    %r12d,%r12d
jmp    11dc <nlines+0x8c>
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xfffffffb,%ebx
jmp    11f3 <nlines+0xa3>
xor    %r12d,%r12d
mov    $0x20,%bl
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%r15d
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebx
xor    %eax,%eax
cmp    $0xa,%bl
sete   %al
add    %eax,%r12d
mov    %r14,%rdi
call   1060 <feof@plt>
add    $0xffffffff,%ebp
test   %eax,%eax
je     11b0 <nlines+0x60>
test   %ebp,%ebp
setne  %al
cmp    $0xa,%r15b
setne  %cl
and    %al,%cl
movzbl %cl,%ebx
add    %r12d,%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [41 x i8] c"Could not open the file %s for reading.\0A\00"
define i32 @nlines(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -5, i32* %2
br label %49
16: ; preds = %1
store i32 0, i32* %5
store i32 -1, i32* %6
store i8 32, i8* %7
br label %17
17: ; preds = %34, %16
%18 = load i32*, i32** %4
%19 = call i32 @feof(i32* %18)
%20 = icmp ne i32 %19, 0
%21 = xor i1 %20, true
br i1 %21, label %22, label %35
22: ; preds = %17
%23 = load i8, i8* %7
store i8 %23, i8* %8
%24 = load i32*, i32** %4
%25 = call signext i8 @fgetc(i32* %24)
store i8 %25, i8* %7
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
store i32 %27, i32* %6
%28 = load i8, i8* %7
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %34
34: ; preds = %31, %22
br label %17
35: ; preds = %17
%36 = load i8, i8* %8
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 10
br i1 %38, label %39, label %45
39: ; preds = %35
%40 = load i32, i32* %6
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %45
45: ; preds = %42, %39, %35
%46 = load i32*, i32** %4
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* %5
store i32 %48, i32* %2
br label %49
49: ; preds = %45, %13
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [41 x i8] c"Could not open the file %s for reading.\0A\00"
define i32 @nlines(i8* %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 %7, label %4
4: ; preds = %1
%5 = tail call i32 @feof(i32* %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %9, label %22
7: ; preds = %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %31
9: ; preds = %4, %9
%10 = phi i8 [ %13, %9 ], [ 32, %4 ]
%11 = phi i32 [ %14, %9 ], [ -1, %4 ]
%12 = phi i32 [ %17, %9 ], [ 0, %4 ]
%13 = tail call signext i8 @fgetc(i32* %2)
%14 = add nsw i32 %11, 1
%15 = icmp eq i8 %13, 10
%16 = zext i1 %15 to i32
%17 = add nuw nsw i32 %12, %16
%18 = tail call i32 @feof(i32* %2)
%19 = icmp eq i32 %18, 0
br i1 %19, label %9, label %20
20: ; preds = %9
%21 = icmp ne i32 %14, 0
br label %22
22: ; preds = %20, %4
%23 = phi i32 [ 0, %4 ], [ %17, %20 ]
%24 = phi i1 [ true, %4 ], [ %21, %20 ]
%25 = phi i8 [ undef, %4 ], [ %10, %20 ]
%26 = icmp ne i8 %25, 10
%27 = select i1 %26, i1 %24, i1 false
%28 = zext i1 %27 to i32
%29 = add nuw nsw i32 %23, %28
%30 = tail call i32 @fclose(i32* %2)
br label %31
31: ; preds = %22, %7
%32 = phi i32 [ -5, %7 ], [ %29, %22 ]
ret i32 %32
}