#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*/ SEEK_SET;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int numLines(FILE *file) {
  int count = 0;
  char buffer[1024];
  int curr = ftell(file);
  fseek(file, 0, SEEK_SET);
  while (fgets(buffer, 1024, file) != NULL) {
    count++;
  }
  fseek(file, curr, SEEK_SET);
  return count;
}


x86 assembly (clang, -O0):
<numLines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x400,%esi
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     1196 <numLines+0x66>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1169 <numLines+0x39>
mov    -0x8(%rbp),%rdi
mov    -0x414(%rbp),%esi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
mov    -0xc(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numLines>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
call   1040 <ftell@plt>
mov    %eax,%r14d
mov    0x2e8f(%rip),%r12
mov    (%r12),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
mov    $0xffffffff,%ebp
mov    %rsp,%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1030 <fgets@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1170 <numLines+0x40>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <fseek@plt>
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i32 @numLines(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32* %0, i32** %2
store i32 0, i32* %3
%6 = load i32*, i32** %2
%7 = call i32 @ftell(i32* %6)
store i32 %7, i32* %5
%8 = load i32*, i32** %2
%9 = load i32, i32* @SEEK_SET
%10 = call i32 @fseek(i32* %8, i32 0, i32 %9)
br label %11
11: ; preds = %16, %1
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32* @fgets(i8* %12, i32 1024, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %11
19: ; preds = %11
%20 = load i32*, i32** %2
%21 = load i32, i32* %5
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %20, i32 %21, i32 %22)
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i32 @numLines(i32* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @ftell(i32* %0)
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @fseek(i32* %0, i32 0, i32 %5)
br label %7
7: ; preds = %7, %1
%8 = phi i32 [ 0, %1 ], [ %11, %7 ]
%9 = call i32* @fgets(i8* %3, i32 1024, i32* %0)
%10 = icmp eq i32* %9, null
%11 = add nuw nsw i32 %8, 1
br i1 %10, label %12, label %7
12: ; preds = %7
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @fseek(i32* %0, i32 %4, i32 %13)
ret i32 %8
}