#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 */
unsigned int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
unsigned int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int countLines(const char *path) {
  FILE *file = fopen(path, "r");
  unsigned lines = 0;
  unsigned c;
  unsigned last = '\n';
  while (EOF != (c = fgetc(file))) {
    if (c == '\n' && last != '\n') {
      ++lines;
    }
    last = c;
  }
  fclose(file);
  return lines;
}


x86 assembly (clang, -O0):
<countLines>:
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   $0x0,-0x14(%rbp)
movl   $0xa,-0x1c(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
mov    %ecx,-0x18(%rbp)
cmp    %ecx,%eax
je     11ab <countLines+0x7b>
cmpl   $0xa,-0x18(%rbp)
jne    11a0 <countLines+0x70>
cmpl   $0xa,-0x1c(%rbp)
je     11a0 <countLines+0x70>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    115e <countLines+0x2e>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countLines>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
lea    0xec1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r12
mov    0x2e9a(%rip),%r14
mov    (%r14),%r15d
mov    %rax,%rdi
call   1040 <fgetc@plt>
xor    %ebp,%ebp
cmp    %eax,%r15d
je     1197 <countLines+0x67>
xor    %ebp,%ebp
mov    $0xa,%ecx
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebx
cmp    $0xa,%eax
sete   %al
cmp    $0xa,%ecx
setne  %cl
and    %al,%cl
movzbl %cl,%eax
add    %eax,%ebp
mov    (%r14),%r15d
mov    %r12,%rdi
call   1040 <fgetc@plt>
mov    %ebx,%ecx
cmp    %eax,%r15d
jne    1170 <countLines+0x40>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %ebp,%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 @countLines(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 0, i32* %4
store i32 10, i32* %6
br label %9
9: ; preds = %23, %1
%10 = load i32, i32* @EOF
%11 = load i32*, i32** %3
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* %5
%13 = icmp ne i32 %10, %12
br i1 %13, label %14, label %25
14: ; preds = %9
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %4
%22 = add i32 %21, 1
store i32 %22, i32* %4
br label %23
23: ; preds = %20, %17, %14
%24 = load i32, i32* %5
store i32 %24, i32* %6
br label %9
25: ; preds = %9
%26 = load i32*, i32** %3
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %4
ret i32 %28
}

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