#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int countlines(char *filename) {
  char c;
  int numof_lines = 0;
  FILE *csv = fopen(filename, "r");
  if (!csv) {
    printf("File could not be opened.\n");
    return -1;
  } else
    printf("File opened succesfully.\n");
  printf("Counting lines.\n");
  do {
    c = fgetc(csv);
    if (c == '\n') {
      ++numof_lines;
    }
  } while (c != EOF);
  printf("Done counting %d lines.\n", numof_lines);
  fclose(csv);
  return numof_lines;
}


x86 assembly (clang, -O0):
<countlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118c <countlines+0x4c>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <countlines+0xc5>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    11ca <countlines+0x8a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11cf <countlines+0x8f>
movsbl -0x11(%rbp),%eax
mov    0x2e0e(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11a8 <countlines+0x68>
mov    -0x18(%rbp),%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%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
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c0 <countlines+0x70>
mov    %rax,%rbx
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    0x2e5f(%rip),%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebp
cmp    (%r14),%al
jne    1190 <countlines+0x40>
lea    0xe55(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11d1 <countlines+0x81>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"File could not be opened.\0A\00"
@.str.2 = [26 x i8] c"File opened succesfully.\0A\00"
@.str.3 = [17 x i8] c"Counting lines.\0A\00"
@EOF = global i8 0
@.str.4 = [25 x i8] c"Done counting %d lines.\0A\00"
define i32 @countlines(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %5
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
13: ; preds = %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %27, %15
%18 = load i32*, i32** %6
%19 = call signext i8 @fgetc(i32* %18)
store i8 %19, i8* %4
%20 = load i8, i8* %4
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %26
26: ; preds = %23, %17
br label %27
27: ; preds = %26
%28 = load i8, i8* %4
%29 = sext i8 %28 to i32
%30 = load i8, i8* @EOF
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %29, %31
br i1 %32, label %17, label %33
33: ; preds = %27
%34 = load i32, i32* %5
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
%38 = load i32, i32* %5
store i32 %38, i32* %2
br label %39
39: ; preds = %33, %11
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.4 = [25 x i8] c"Done counting %d lines.\0A\00"
@str = [26 x i8] c"File could not be opened.\00"
@str.5 = [25 x i8] c"File opened succesfully.\00"
@str.6 = [16 x i8] c"Counting lines.\00"
define i32 @countlines(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 %4, label %6
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %20
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
br label %9
9: ; preds = %9, %6
%10 = phi i32 [ 0, %6 ], [ %14, %9 ]
%11 = tail call signext i8 @fgetc(i32* %2)
%12 = icmp eq i8 %11, 10
%13 = zext i1 %12 to i32
%14 = add nuw nsw i32 %10, %13
%15 = load i8, i8* @EOF
%16 = icmp eq i8 %11, %15
br i1 %16, label %17, label %9
17: ; preds = %9
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%19 = tail call i32 @fclose(i32* %2)
br label %20
20: ; preds = %17, %4
%21 = phi i32 [ %14, %17 ], [ -1, %4 ]
ret i32 %21
}