#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
unsigned long count_lines_of_file(FILE *fp) {
  unsigned long line_count = 0;
  if (fp == NULL) {
    fclose(fp);
    return 0;
  }
  if (feof(fp))
    rewind(fp);
  if (feof(fp))
    return 0;
  int ch;
  while ((ch = getc(fp)) != EOF)
    if (ch == '\n')
      ++line_count;
  rewind(fp);
  return line_count;
}


x86 assembly (clang, -O0):
<count_lines_of_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <count_lines_of_file+0x35>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fd <count_lines_of_file+0xbd>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1191 <count_lines_of_file+0x51>
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11b1 <count_lines_of_file+0x71>
movq   $0x0,-0x8(%rbp)
jmp    11fd <count_lines_of_file+0xbd>
jmp    11b6 <count_lines_of_file+0x76>
mov    -0x10(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%eax
je     11ec <count_lines_of_file+0xac>
cmpl   $0xa,-0x1c(%rbp)
jne    11e7 <count_lines_of_file+0xa7>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11b6 <count_lines_of_file+0x76>
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines_of_file>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1170 <count_lines_of_file+0x30>
mov    %rdi,%r14
call   1050 <feof@plt>
test   %rax,%rax
je     115f <count_lines_of_file+0x1f>
mov    %r14,%rdi
call   1040 <rewind@plt>
mov    %r14,%rdi
call   1050 <feof@plt>
test   %rax,%rax
je     117b <count_lines_of_file+0x3b>
xor    %ebx,%ebx
jmp    11c0 <count_lines_of_file+0x80>
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <fclose@plt>
jmp    11c0 <count_lines_of_file+0x80>
mov    %r14,%rdi
call   1060 <getc@plt>
mov    0x2e5e(%rip),%r15
cmp    (%r15),%eax
jne    1193 <count_lines_of_file+0x53>
xor    %ebx,%ebx
jmp    11b8 <count_lines_of_file+0x78>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %rcx,%rbx
mov    %r14,%rdi
call   1060 <getc@plt>
cmp    (%r15),%eax
jne    11a0 <count_lines_of_file+0x60>
mov    %r14,%rdi
call   1040 <rewind@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i32 0
define i64 @count_lines_of_file(i32* %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
store i32* %0, i32** %3
store i64 0, i64* %4
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32*, i32** %3
%10 = call i32 @fclose(i32* %9)
store i64 0, i64* %2
br label %40
11: ; preds = %1
%12 = load i32*, i32** %3
%13 = call i64 @feof(i32* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32*, i32** %3
%17 = call i32 @rewind(i32* %16)
br label %18
18: ; preds = %15, %11
%19 = load i32*, i32** %3
%20 = call i64 @feof(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i64 0, i64* %2
br label %40
23: ; preds = %18
br label %24
24: ; preds = %35, %23
%25 = load i32*, i32** %3
%26 = call i32 @getc(i32* %25)
store i32 %26, i32* %5
%27 = load i32, i32* @EOF
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i64, i64* %4
%34 = add i64 %33, 1
store i64 %34, i64* %4
br label %35
35: ; preds = %32, %29
br label %24
36: ; preds = %24
%37 = load i32*, i32** %3
%38 = call i32 @rewind(i32* %37)
%39 = load i64, i64* %4
store i64 %39, i64* %2
br label %40
40: ; preds = %36, %22, %8
%41 = load i64, i64* %2
ret i64 %41
}

LLVM IR (-O2):
@EOF = global i32 0
define i64 @count_lines_of_file(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @fclose(i32* null)
br label %29
5: ; preds = %1
%6 = tail call i64 @feof(i32* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @rewind(i32* %0)
br label %10
10: ; preds = %8, %5
%11 = tail call i64 @feof(i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = tail call i32 @getc(i32* %0)
%15 = load i32, i32* @EOF
%16 = icmp eq i32 %14, %15
br i1 %16, label %26, label %17
17: ; preds = %13, %17
%18 = phi i32 [ %23, %17 ], [ %14, %13 ]
%19 = phi i64 [ %22, %17 ], [ 0, %13 ]
%20 = icmp eq i32 %18, 10
%21 = zext i1 %20 to i64
%22 = add i64 %19, %21
%23 = tail call i32 @getc(i32* %0)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %17
26: ; preds = %17, %13
%27 = phi i64 [ 0, %13 ], [ %22, %17 ]
%28 = tail call i32 @rewind(i32* %0)
br label %29
29: ; preds = %10, %26, %3
%30 = phi i64 [ 0, %3 ], [ %27, %26 ], [ 0, %10 ]
ret i64 %30
}