#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*/ fatal(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
int file_line_count(char *file) {
  int ch = 0;
  int linecnt = 0;
  FILE *fptr = fopen(file, "r");
  if (!fptr) {
    fprintf(stderr, "fn = %s\n", file);
    fatal("file_line_count: open failed");
  }
  while ((ch = fgetc(fptr)) != EOF)
    if (ch == '\n')
      linecnt++;
  fclose(fptr);
  return linecnt;
}


x86 assembly (clang, -O0):
<file_line_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ae <file_line_count+0x5e>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe65(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe62(%rip),%rdi
call   1070 <fatal@plt>
jmp    11b3 <file_line_count+0x63>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%eax
je     11e6 <file_line_count+0x96>
cmpl   $0xa,-0xc(%rbp)
jne    11e1 <file_line_count+0x91>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11b3 <file_line_count+0x63>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_line_count>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118f <file_line_count+0x3f>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
lea    0xe81(%rip),%rdi
call   1070 <fatal@plt>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    0x2e4a(%rip),%r14
xor    %ebp,%ebp
cmp    (%r14),%eax
je     11c7 <file_line_count+0x77>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    (%r14),%eax
jne    11b0 <file_line_count+0x60>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [9 x i8] c"fn = %s\0A\00"
@.str.2 = [29 x i8] c"file_line_count: open failed\00"
@EOF = global i32 0
define i32 @file_line_count(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
%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** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %2
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = call i32 @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %10, %1
br label %16
16: ; preds = %27, %15
%17 = load i32*, i32** %5
%18 = call i32 @fgetc(i32* %17)
store i32 %18, i32* %3
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %27
27: ; preds = %24, %21
br label %16
28: ; preds = %16
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %4
ret i32 %31
}

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