#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int file_goto_last_line(const char *fpath, char *line, int line_size) {
  FILE *fp;
  int nline = 0;
  memset(line, 0, line_size);
  fp = fopen(fpath, "r");
  if (fp) {
    while (!feof(fp)) {
      if (NULL == fgets(line, line_size, fp)) {
        break;
      }
      nline++;
    }
    fclose(fp);
  }
  return nline;
}


x86 assembly (clang, -O0):
<file_goto_last_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f4 <file_goto_last_line+0xa4>
jmp    119c <file_goto_last_line+0x4c>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ba <file_goto_last_line+0x6a>
jmp    11eb <file_goto_last_line+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11dd <file_goto_last_line+0x8d>
jmp    11eb <file_goto_last_line+0x9b>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119c <file_goto_last_line+0x4c>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_goto_last_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %ebp,%ebp
mov    %rsi,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xe8d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ca <file_goto_last_line+0x7a>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    11c2 <file_goto_last_line+0x72>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11c2 <file_goto_last_line+0x72>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11a0 <file_goto_last_line+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @file_goto_last_line(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i32 @memset(i8* %9, i32 0, i32 %10)
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %35
16: ; preds = %3
br label %17
17: ; preds = %29, %16
%18 = load i32*, i32** %7
%19 = call i32 @feof(i32* %18)
%20 = icmp ne i32 %19, 0
%21 = xor i1 %20, true
br i1 %21, label %22, label %32
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = load i32*, i32** %7
%26 = call i32* @fgets(i8* %23, i32 %24, i32* %25)
%27 = icmp eq i32* null, %26
br i1 %27, label %28, label %29
28: ; preds = %22
br label %32
29: ; preds = %22
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
br label %17
32: ; preds = %28, %17
%33 = load i32*, i32** %7
%34 = call i32 @fclose(i32* %33)
br label %35
35: ; preds = %32, %3
%36 = load i32, i32* %8
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @file_goto_last_line(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %21, label %7
7: ; preds = %3
%8 = tail call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %7, %14
%11 = phi i32 [ %15, %14 ], [ 0, %7 ]
%12 = tail call i32* @fgets(i8* %1, i32 %2, i32* %5)
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = add nuw nsw i32 %11, 1
%16 = tail call i32 @feof(i32* %5)
%17 = icmp eq i32 %16, 0
br i1 %17, label %10, label %18
18: ; preds = %14, %10, %7
%19 = phi i32 [ 0, %7 ], [ %11, %10 ], [ %15, %14 ]
%20 = tail call i32 @fclose(i32* %5)
br label %21
21: ; preds = %18, %3
%22 = phi i32 [ %19, %18 ], [ 0, %3 ]
ret i32 %22
}