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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ strchr(char *, char);
int count_lines(FILE *fp) {
  int nl = 0;
  int el = 0;
  char buf[BUFSIZ];
  while (fgets(buf, sizeof(buf), fp) != NULL) {
    if (strchr(buf, '\n')) {
      nl++;
      el = 0;
    } else {
      el = 1;
    }
  }
  return nl + el;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x30(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11c0 <count_lines+0xa0>
mov    -0x28(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11b4 <count_lines+0x94>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
jmp    11bb <count_lines+0x9b>
movl   $0x1,-0x10(%rbp)
jmp    116a <count_lines+0x4a>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2ea8(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11a1 <count_lines+0x81>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,%r13
cmp    $0x1,%rax
sbb    $0xffffffff,%ebx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1170 <count_lines+0x50>
cmp    $0x1,%r13
adc    $0x0,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
define i32 @count_lines(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i32* %0, i32** %2
store i32 0, i32* %3
store i32 0, i32* %4
%7 = load i32, i32* @BUFSIZ
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %5
%10 = alloca i8, i64 %8
store i64 %8, i64* %6
br label %11
11: ; preds = %23, %1
%12 = trunc i64 %8 to i32
%13 = load i32*, i32** %2
%14 = call i32* @fgets(i8* %10, i32 %12, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = call i64 @strchr(i8* %10, i8 signext 10)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
store i32 0, i32* %4
br label %23
22: ; preds = %16
store i32 1, i32* %4
br label %23
23: ; preds = %22, %19
br label %11
24: ; preds = %11
%25 = load i32, i32* %3
%26 = load i32, i32* %4
%27 = add nsw i32 %25, %26
%28 = load i8*, i8** %5
ret i32 %27
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
define i32 @count_lines(i32* nocapture %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32* @fgets(i8* %4, i32 %2, i32* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %19, label %7
7: ; preds = %1, %7
%8 = phi i32 [ %13, %7 ], [ 0, %1 ]
%9 = call i64 @strchr(i8* %4, i8 signext 10)
%10 = icmp eq i64 %9, 0
%11 = xor i1 %10, true
%12 = zext i1 %11 to i32
%13 = add nuw nsw i32 %8, %12
%14 = call i32* @fgets(i8* %4, i32 %2, i32* %0)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %7
16: ; preds = %7
%17 = zext i1 %10 to i32
%18 = add nuw nsw i32 %13, %17
br label %19
19: ; preds = %16, %1
%20 = phi i32 [ %18, %16 ], [ 0, %1 ]
ret i32 %20
}