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


x86 assembly (clang, -O0):
<lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116e <lines+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11d4 <lines+0xa4>
jmp    1173 <lines+0x43>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
mov    0x2e5f(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11af <lines+0x7f>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    11aa <lines+0x7a>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1173 <lines+0x43>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
je     11ce <lines+0x9e>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lines>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119d <lines+0x6d>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebx
mov    0x2e8c(%rip),%r15
xor    %ebp,%ebp
cmp    (%r15),%al
je     1189 <lines+0x59>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
cmp    $0xa,%bl
sete   %al
add    %eax,%ebp
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebx
cmp    (%r15),%al
jne    1170 <lines+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    $0xa,%bl
setne  %al
add    %ebp,%eax
jmp    119f <lines+0x6f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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