#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 */
char EOF;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int line_count(FILE *stream) {
  char c;
  int nlines = 0;
  long position;
  if ((position = ftell(stream)) != -1L) {
    rewind(stream);
    while ((c = getc(stream)) != EOF)
      if (c == '\n')
        ++nlines;
    fseek(stream, position, SEEK_SET);
    return nlines;
  } else
    return -1;
}


x86 assembly (clang, -O0):
<line_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
je     11d0 <line_count+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <rewind@plt>
mov    -0x10(%rbp),%rdi
call   1060 <getc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
mov    0x2e5f(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11af <line_count+0x6f>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    11aa <line_count+0x6a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1173 <line_count+0x33>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d7 <line_count+0x97>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<line_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     11ac <line_count+0x6c>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <rewind@plt>
mov    %rbx,%rdi
call   1060 <getc@plt>
mov    0x2e79(%rip),%r15
xor    %ebp,%ebp
cmp    (%r15),%al
je     1196 <line_count+0x56>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1060 <getc@plt>
cmp    (%r15),%al
jne    1180 <line_count+0x40>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fseek@plt>
jmp    11b1 <line_count+0x71>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@EOF = global i8 0
@SEEK_SET = global i32 0
define i32 @line_count(i32* nocapture %0) {
%2 = tail call i64 @ftell(i32* %0)
%3 = icmp eq i64 %2, -1
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = tail call i32 @rewind(i32* %0)
%6 = tail call signext i8 @getc(i32* %0)
%7 = load i8, i8* @EOF
%8 = icmp eq i8 %6, %7
br i1 %8, label %18, label %9
9: ; preds = %4, %9
%10 = phi i8 [ %15, %9 ], [ %6, %4 ]
%11 = phi i32 [ %14, %9 ], [ 0, %4 ]
%12 = icmp eq i8 %10, 10
%13 = zext i1 %12 to i32
%14 = add nuw nsw i32 %11, %13
%15 = tail call signext i8 @getc(i32* %0)
%16 = load i8, i8* @EOF
%17 = icmp eq i8 %15, %16
br i1 %17, label %18, label %9
18: ; preds = %9, %4
%19 = phi i32 [ 0, %4 ], [ %14, %9 ]
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %0, i64 %2, i32 %20)
br label %22
22: ; preds = %1, %18
%23 = phi i32 [ %19, %18 ], [ -1, %1 ]
ret i32 %23
}