#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 /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
int fgetlastl(FILE *fd, char *lbuf, int size) {
  int lines = 0;
  if (ftell(fd) != 0)
    fseek(fd, 0, SEEK_SET);
  while (fgets(lbuf, size, fd) != NULL)
    lines++;
  return (lines);
}


x86 assembly (clang, -O0):
<fgetlastl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ftell@plt>
cmp    $0x0,%rax
je     1171 <fgetlastl+0x41>
mov    -0x8(%rbp),%rdi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
jmp    1176 <fgetlastl+0x46>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     119e <fgetlastl+0x6e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1176 <fgetlastl+0x46>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetlastl>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
call   1040 <ftell@plt>
test   %rax,%rax
je     115d <fgetlastl+0x2d>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
mov    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1030 <fgets@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1170 <fgetlastl+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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