#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int ptg_file_lines(const char *filename) {
  FILE *fp;
  static char lnbuf[128];
  int i = 0;
  fp = fopen(filename, "r");
  if (fp == NULL)
    return 0;
  while (fgets(lnbuf, 127, fp) != NULL) {
    i++;
  }
  fclose(fp);
  return i;
}


x86 assembly (clang, -O0):
<ptg_file_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116e <ptg_file_lines+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11af <ptg_file_lines+0x7f>
jmp    1173 <ptg_file_lines+0x43>
mov    -0x18(%rbp),%rdx
lea    0x2ed2(%rip),%rdi
mov    $0x7f,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11a0 <ptg_file_lines+0x70>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1173 <ptg_file_lines+0x43>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptg_file_lines>:
push   %r15
push   %r14
push   %rbx
lea    0xec4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1182 <ptg_file_lines+0x52>
mov    %rax,%r14
mov    $0xffffffff,%ebx
lea    0x2efb(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    $0x7f,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1160 <ptg_file_lines+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1184 <ptg_file_lines+0x54>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@ptg_file_lines.lnbuf = internal global [128 x i8] zeroinitializer
@.str = [2 x i8] c"r\00"
define i32 @ptg_file_lines(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 = icmp eq i32* %2, null
br i1 %3, label %11, label %4
4: ; preds = %1, %4
%5 = phi i32 [ %8, %4 ], [ 0, %1 ]
%6 = tail call i32* @fgets(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @ptg_file_lines.lnbuf, i64 0, i64 0), i32 127, i32* %2)
%7 = icmp eq i32* %6, null
%8 = add nuw nsw i32 %5, 1
br i1 %7, label %9, label %4
9: ; preds = %4
%10 = tail call i32 @fclose(i32* %2)
br label %11
11: ; preds = %1, %9
%12 = phi i32 [ %5, %9 ], [ 0, %1 ]
ret i32 %12
}