#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *strrchr(char *, char);
int count_lines(char *filename) {
  FILE *file = fopen(filename, "r");
  int count = 0;
  char buf[4096];
  while (fgets(buf, 4096, file) != NULL) {
    if (strrchr(buf, '\n') != NULL)
      count++;
  }
  return count;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11a2 <count_lines+0x72>
lea    -0x1020(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strrchr@plt>
cmp    $0x0,%rax
je     119d <count_lines+0x6d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <count_lines+0x2a>
mov    -0x14(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
lea    0xebe(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1199 <count_lines+0x69>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <strrchr@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1170 <count_lines+0x40>
mov    %ebp,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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