#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*/ off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
int linecnt(FILE *f) {
  off_t oldpos = ftell(f);
  int l = 0;
  char lbuf[BUFSIZ];
  while (fgets(lbuf, sizeof lbuf, f))
    l++;
  clearerr(f);
  fseek(f, oldpos, 0);
  return (l);
}


x86 assembly (clang, -O0):
<linecnt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x30(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     11ba <linecnt+0x7a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    118f <linecnt+0x4f>
mov    -0x8(%rbp),%rdi
call   1050 <clearerr@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1060 <fseek@plt>
mov    -0x10(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linecnt>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <ftell@plt>
mov    %eax,%r14d
mov    0x2e80(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r13
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    $0xffffffff,%ebx
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1030 <fgets@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1180 <linecnt+0x40>
mov    %r15,%rdi
call   1050 <clearerr@plt>
mov    %r15,%rdi
mov    %r14d,%esi
xor    %edx,%edx
call   1060 <fseek@plt>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
define i32 @linecnt(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i32* %0, i32** %2
%7 = load i32*, i32** %2
%8 = call i32 @ftell(i32* %7)
store i32 %8, i32* %3
store i32 0, i32* %4
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
br label %13
13: ; preds = %18, %1
%14 = trunc i64 %10 to i32
%15 = load i32*, i32** %2
%16 = call i64 @fgets(i8* %12, i32 %14, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
store i32 %20, i32* %4
br label %13
21: ; preds = %13
%22 = load i32*, i32** %2
%23 = call i32 @clearerr(i32* %22)
%24 = load i32*, i32** %2
%25 = load i32, i32* %3
%26 = call i32 @fseek(i32* %24, i32 %25, i32 0)
%27 = load i32, i32* %4
%28 = load i8*, i8** %5
ret i32 %27
}

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