#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int line_count(char file[400]) {
  FILE *fp;
  int c, lines;
  if ((fp = fopen(file, "r")) == NULL) {
    printf("Cannot open file %s \n", file);
    exit(0);
  }
  lines = 0;
  while ((c = fgetc(fp)) != EOF)
    if (c == '\n')
      lines++;
  fclose(fp);
  return lines;
}


x86 assembly (clang, -O0):
<line_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1191 <line_count+0x41>
mov    -0x8(%rbp),%rsi
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%eax
je     11cb <line_count+0x7b>
cmpl   $0xa,-0x14(%rbp)
jne    11c6 <line_count+0x76>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1198 <line_count+0x48>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<line_count>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b6 <line_count+0x66>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    0x2e6e(%rip),%r14
xor    %ebp,%ebp
cmp    (%r14),%eax
je     11a7 <line_count+0x57>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    (%r14),%eax
jne    1190 <line_count+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe45(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"Cannot open file %s \0A\00"
@EOF = global i32 0
define i32 @line_count(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %1
store i32 0, i32* %5
br label %14
14: ; preds = %25, %13
%15 = load i32*, i32** %3
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %4
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %25
25: ; preds = %22, %19
br label %14
26: ; preds = %14
%27 = load i32*, i32** %3
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %5
ret i32 %29
}

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