#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
unsigned long linecounts(const char *file) {
  FILE *fp;
  int c;
  unsigned long lines = 0;
  if (file == NULL)
    return -1;
  fp = fopen(file, "r");
  if (fp == NULL)
    return -1;
  while ((c = fgetc(fp)) != EOF) {
    if (c == '\n')
      lines++;
  }
  fclose(fp);
  return lines;
}


x86 assembly (clang, -O0):
<linecounts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115c <linecounts+0x2c>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d4 <linecounts+0xa4>
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <linecounts+0x58>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d4 <linecounts+0xa4>
jmp    118d <linecounts+0x5d>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%eax
je     11c3 <linecounts+0x93>
cmpl   $0xa,-0x1c(%rbp)
jne    11be <linecounts+0x8e>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    118d <linecounts+0x5d>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linecounts>:
push   %r15
push   %r14
push   %rbx
mov    $0xffffffffffffffff,%rbx
test   %rdi,%rdi
je     1190 <linecounts+0x60>
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1190 <linecounts+0x60>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    0x2e84(%rip),%r15
cmp    (%r15),%eax
jne    116d <linecounts+0x3d>
xor    %ebx,%ebx
jmp    1188 <linecounts+0x58>
xor    %ebx,%ebx
nop
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %rcx,%rbx
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%eax
jne    1170 <linecounts+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i64 @linecounts(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %22, label %3
3: ; preds = %1
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = tail call i32 @fgetc(i32* %4)
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %19, label %10
10: ; preds = %6, %10
%11 = phi i32 [ %16, %10 ], [ %7, %6 ]
%12 = phi i64 [ %15, %10 ], [ 0, %6 ]
%13 = icmp eq i32 %11, 10
%14 = zext i1 %13 to i64
%15 = add i64 %12, %14
%16 = tail call i32 @fgetc(i32* %4)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %10
19: ; preds = %10, %6
%20 = phi i64 [ 0, %6 ], [ %15, %10 ]
%21 = tail call i32 @fclose(i32* %4)
br label %22
22: ; preds = %3, %1, %19
%23 = phi i64 [ %20, %19 ], [ -1, %1 ], [ -1, %3 ]
ret i64 %23
}