#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int count_lines_in_file(FILE *fp, int *max_line_length) {
  int c;
  int line_count = 0;
  int char_count = 0;
  int max_char_count = 0;
  rewind(fp);
  while ((c = fgetc(fp)) != EOF) {
    char_count++;
    if (c == '\n') {
      line_count++;
      if (char_count > max_char_count) {
        max_char_count = char_count;
      }
      char_count = 0;
    }
  }
  if (char_count > max_char_count) {
    max_char_count = char_count;
  }
  rewind(fp);
  if (max_line_length != NULL)
    *max_line_length = max_char_count;
  return line_count;
}


x86 assembly (clang, -O0):
<count_lines_in_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <rewind@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%eax
je     11a3 <count_lines_in_file+0x83>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    119e <count_lines_in_file+0x7e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1197 <count_lines_in_file+0x77>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    114e <count_lines_in_file+0x2e>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    11b5 <count_lines_in_file+0x95>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <rewind@plt>
cmpq   $0x0,-0x10(%rbp)
je     11d2 <count_lines_in_file+0xb2>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines_in_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
mov    %rdi,%r15
call   1030 <rewind@plt>
mov    %r15,%rdi
call   1040 <fgetc@plt>
mov    0x2ea2(%rip),%r12
xor    %r13d,%r13d
mov    $0x0,%ebp
mov    $0x0,%ebx
cmp    (%r12),%eax
je     119c <count_lines_in_file+0x7c>
xor    %r14d,%r14d
xor    %ebx,%ebx
xor    %ecx,%ecx
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x1(%rcx),%ebp
cmp    %ebx,%ecx
mov    %ebp,%ecx
cmovl  %ebx,%ecx
xor    %edx,%edx
cmp    $0xa,%eax
cmove  %ecx,%ebx
sete   %dl
cmove  %r14d,%ebp
add    %edx,%r13d
mov    %r15,%rdi
call   1040 <fgetc@plt>
mov    %ebp,%ecx
cmp    (%r12),%eax
jne    1170 <count_lines_in_file+0x50>
mov    %r15,%rdi
call   1030 <rewind@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     11b4 <count_lines_in_file+0x94>
cmp    %ebx,%ebp
cmovg  %ebp,%ebx
mov    %ebx,(%rax)
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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