#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*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int countLines(FILE *fp) {
  if (fp == NULL) {
    return -1;
  }
  char *buffer = (char *)malloc(sizeof(char) * (40959 + 1));
  fread(buffer, 40959 + 1, 1, fp);
  int lineCount = 0;
  for (int i = 0; i < 40959 + 1; i++) {
    if (buffer[i] == '\n') {
      lineCount++;
    }
  }
  free(buffer);
  return lineCount;
}


x86 assembly (clang, -O0):
<countLines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <countLines+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <countLines+0xa3>
mov    $0xa000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0xa000,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0xa000,-0x20(%rbp)
jge    11c4 <countLines+0x94>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11b1 <countLines+0x81>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b6 <countLines+0x86>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1186 <countLines+0x56>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countLines>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1217 <countLines+0xe7>
mov    %rdi,%r14
mov    $0xa000,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xa000,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
pxor   %xmm3,%xmm3
mov    $0xc,%eax
movdqa 0xe8d(%rip),%xmm0
movdqa 0xe95(%rip),%xmm2
pxor   %xmm1,%xmm1
nop
movd   -0xc(%rbx,%rax,1),%xmm5
movd   -0x8(%rbx,%rax,1),%xmm4
pcmpeqb %xmm0,%xmm5
punpcklbw %xmm5,%xmm5
punpcklwd %xmm5,%xmm5
pand   %xmm2,%xmm5
paddd  %xmm3,%xmm5
pcmpeqb %xmm0,%xmm4
punpcklbw %xmm4,%xmm4
punpcklwd %xmm4,%xmm4
pand   %xmm2,%xmm4
paddd  %xmm1,%xmm4
movd   -0x4(%rbx,%rax,1),%xmm3
movd   (%rbx,%rax,1),%xmm1
pcmpeqb %xmm0,%xmm3
punpcklbw %xmm3,%xmm3
punpcklwd %xmm3,%xmm3
pand   %xmm2,%xmm3
paddd  %xmm5,%xmm3
pcmpeqb %xmm0,%xmm1
punpcklbw %xmm1,%xmm1
punpcklwd %xmm1,%xmm1
pand   %xmm2,%xmm1
paddd  %xmm4,%xmm1
add    $0x10,%rax
cmp    $0xa00c,%rax
jne    1180 <countLines+0x50>
paddd  %xmm3,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddd  %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    121c <countLines+0xec>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @countLines(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %38
10: ; preds = %1
%11 = call i64 @malloc(i32 40960)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = load i32*, i32** %3
%15 = call i32 @fread(i8* %13, i32 40960, i32 1, i32* %14)
store i32 0, i32* %5
store i32 0, i32* %6
br label %16
16: ; preds = %31, %10
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 40960
br i1 %18, label %19, label %34
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %30
30: ; preds = %27, %19
br label %31
31: ; preds = %30
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %16
34: ; preds = %16
%35 = load i8*, i8** %4
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* %5
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %9
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
define i32 @countLines(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %42, label %3
3: ; preds = %1
%4 = tail call i64 @malloc(i32 40960)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @fread(i8* %5, i32 40960, i32 1, i32* %0)
br label %7
7: ; preds = %7, %3
%8 = phi i64 [ 0, %3 ], [ %36, %7 ]
%9 = phi <4 x i32> [ zeroinitializer, %3 ], [ %34, %7 ]
%10 = phi <4 x i32> [ zeroinitializer, %3 ], [ %35, %7 ]
%11 = getelementptr inbounds i8, i8* %5, i64 %8
%12 = bitcast i8* %11 to <4 x i8>*
%13 = load <4 x i8>, <4 x i8>* %12
%14 = getelementptr inbounds i8, i8* %11, i64 4
%15 = bitcast i8* %14 to <4 x i8>*
%16 = load <4 x i8>, <4 x i8>* %15
%17 = icmp eq <4 x i8> %13, <i8 10, i8 10, i8 10, i8 10>
%18 = icmp eq <4 x i8> %16, <i8 10, i8 10, i8 10, i8 10>
%19 = zext <4 x i1> %17 to <4 x i32>
%20 = zext <4 x i1> %18 to <4 x i32>
%21 = add <4 x i32> %9, %19
%22 = add <4 x i32> %10, %20
%23 = or i64 %8, 8
%24 = getelementptr inbounds i8, i8* %5, i64 %23
%25 = bitcast i8* %24 to <4 x i8>*
%26 = load <4 x i8>, <4 x i8>* %25
%27 = getelementptr inbounds i8, i8* %24, i64 4
%28 = bitcast i8* %27 to <4 x i8>*
%29 = load <4 x i8>, <4 x i8>* %28
%30 = icmp eq <4 x i8> %26, <i8 10, i8 10, i8 10, i8 10>
%31 = icmp eq <4 x i8> %29, <i8 10, i8 10, i8 10, i8 10>
%32 = zext <4 x i1> %30 to <4 x i32>
%33 = zext <4 x i1> %31 to <4 x i32>
%34 = add <4 x i32> %21, %32
%35 = add <4 x i32> %22, %33
%36 = add nuw nsw i64 %8, 16
%37 = icmp eq i64 %36, 40960
br i1 %37, label %38, label %7
38: ; preds = %7
%39 = add <4 x i32> %35, %34
%40 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %39)
%41 = tail call i32 @free(i8* %5)
br label %42
42: ; preds = %1, %38
%43 = phi i32 [ %40, %38 ], [ -1, %1 ]
ret i32 %43
}