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

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *malloc(int);
int zstrlen(char *);
int flinelen(const char *path) {
  int lines = 0;
  FILE *f = fopen(path, "r");
  char *buff = malloc(1024);
  while (fgets(buff, sizeof(buff), f)) {
    for (int i = 0; i < zstrlen(buff); i++) {
      if (buff[i] == '\n') {
        lines += 1;
      }
    }
  }
  return lines;
}


x86 assembly (clang, -O0):
<flinelen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x8,%esi
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     11ea <flinelen+0xaa>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <zstrlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    11e5 <flinelen+0xa5>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11d2 <flinelen+0x92>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d7 <flinelen+0x97>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1198 <flinelen+0x58>
jmp    1175 <flinelen+0x35>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flinelen>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x8,%esi
mov    %r14,%rdx
call   1030 <fgets@plt>
xor    %r12d,%r12d
jmp    1190 <flinelen+0x50>
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x8,%esi
mov    %r14,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
je     11d2 <flinelen+0x92>
mov    %r15,%rdi
call   1060 <zstrlen@plt>
test   %eax,%eax
jle    1180 <flinelen+0x40>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
cmpb   $0xa,(%r15,%rbx,1)
sete   %al
add    %eax,%r12d
add    $0x1,%rbx
mov    %r15,%rdi
call   1060 <zstrlen@plt>
cltq
cmp    %rax,%rbx
jl     11b0 <flinelen+0x70>
jmp    1180 <flinelen+0x40>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @flinelen(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%4 = tail call i64 @fgets(i8* %3, i32 8, i32* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %26, label %10
6: ; preds = %14, %10
%7 = phi i32 [ %11, %10 ], [ %21, %14 ]
%8 = tail call i64 @fgets(i8* %3, i32 8, i32* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %1, %6
%11 = phi i32 [ %7, %6 ], [ 0, %1 ]
%12 = tail call i32 @zstrlen(i8* %3)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %6
14: ; preds = %10, %14
%15 = phi i64 [ %22, %14 ], [ 0, %10 ]
%16 = phi i32 [ %21, %14 ], [ %11, %10 ]
%17 = getelementptr inbounds i8, i8* %3, i64 %15
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
%20 = zext i1 %19 to i32
%21 = add nsw i32 %16, %20
%22 = add nuw nsw i64 %15, 1
%23 = tail call i32 @zstrlen(i8* %3)
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %22, %24
br i1 %25, label %14, label %6
26: ; preds = %6, %1
%27 = phi i32 [ 0, %1 ], [ %7, %6 ]
ret i32 %27
}