#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 /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int count_lines(FILE *fp) {
  int lines = 0;
  while (!feof(fp)) {
    char ch = fgetc(fp);
    if (ch == '\n') {
      lines++;
    }
  }
  rewind(fp);
  return lines + 1;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1161 <count_lines+0x31>
jmp    1188 <count_lines+0x58>
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
jne    1183 <count_lines+0x53>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1143 <count_lines+0x13>
mov    -0x8(%rbp),%rdi
call   1030 <rewind@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <feof@plt>
mov    $0x1,%ebp
test   %eax,%eax
jne    1170 <count_lines+0x40>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %eax,%eax
je     1150 <count_lines+0x20>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <rewind@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @count_lines(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8
store i32* %0, i32** %2
store i32 0, i32* %3
br label %5
5: ; preds = %19, %1
%6 = load i32*, i32** %2
%7 = call i32 @feof(i32* %6)
%8 = icmp ne i32 %7, 0
%9 = xor i1 %8, true
br i1 %9, label %10, label %20
10: ; preds = %5
%11 = load i32*, i32** %2
%12 = call signext i8 @fgetc(i32* %11)
store i8 %12, i8* %4
%13 = load i8, i8* %4
%14 = sext i8 %13 to i32
%15 = icmp eq i32 %14, 10
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %19
19: ; preds = %16, %10
br label %5
20: ; preds = %5
%21 = load i32*, i32** %2
%22 = call i32 @rewind(i32* %21)
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
ret i32 %24
}

LLVM IR (-O2):
define i32 @count_lines(i32* nocapture %0) {
%2 = tail call i32 @feof(i32* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %14
4: ; preds = %1, %4
%5 = phi i32 [ %9, %4 ], [ 0, %1 ]
%6 = tail call signext i8 @fgetc(i32* %0)
%7 = icmp eq i8 %6, 10
%8 = zext i1 %7 to i32
%9 = add nuw nsw i32 %5, %8
%10 = tail call i32 @feof(i32* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %4, label %12
12: ; preds = %4
%13 = add nuw i32 %9, 1
br label %14
14: ; preds = %12, %1
%15 = phi i32 [ 1, %1 ], [ %13, %12 ]
%16 = tail call i32 @rewind(i32* %0)
ret i32 %15
}