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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int ace_tu_fs_get_number_of_lines_in_file(char *filepath) {
  FILE *file;
  char *line = NULL;
  ssize_t line_length;
  size_t buffer_length = 0;
  int number_of_lines = 0;
  file = fopen(filepath, "r");
  while ((line_length = getline(&line, &buffer_length, file)) != -1) {
    number_of_lines++;
  }
  fclose(file);
  if (line)
    free(line);
  return number_of_lines;
}


x86 assembly (clang, -O0):
<ace_tu_fs_get_number_of_lines_in_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11a2 <ace_tu_fs_get_number_of_lines_in_file+0x62>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1177 <ace_tu_fs_get_number_of_lines_in_file+0x37>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     11bf <ace_tu_fs_get_number_of_lines_in_file+0x7f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ace_tu_fs_get_number_of_lines_in_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe9c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    $0xffffffff,%ebx
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1060 <getline@plt>
add    $0x1,%ebx
cmp    $0xffffffff,%eax
jne    1180 <ace_tu_fs_get_number_of_lines_in_file+0x40>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11ad <ace_tu_fs_get_number_of_lines_in_file+0x6d>
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @ace_tu_fs_get_number_of_lines_in_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %4
store i64 0, i64* %6
store i32 0, i32* %7
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
br label %10
10: ; preds = %14, %1
%11 = load i32*, i32** %3
%12 = call i32 @getline(i8** %4, i64* %6, i32* %11)
store i32 %12, i32* %5
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
store i32 %16, i32* %7
br label %10
17: ; preds = %10
%18 = load i32*, i32** %3
%19 = call i32 @fclose(i32* %18)
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* %7
ret i32 %26
}

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