#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int countlines(char *filename) {
  printf("getted filename = %s;\n", filename);
  FILE *fp = fopen(filename, "r");
  int ch = 0;
  int lines = 0;
  while (!feof(fp)) {
    ch = fgetc(fp);
    if (ch == '\n')
      lines++;
  };
  fclose(fp);
  return lines;
}


x86 assembly (clang, -O0):
<countlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe99(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xea0(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ac <countlines+0x5c>
jmp    11d0 <countlines+0x80>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    11cb <countlines+0x7b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118e <countlines+0x3e>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countlines>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xea3(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xeab(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    11ae <countlines+0x5e>
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     1190 <countlines+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"getted filename = %s;\0A\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @countlines(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %3
store i32 0, i32* %4
store i32 0, i32* %5
br label %10
10: ; preds = %23, %1
%11 = load i32*, i32** %3
%12 = call i32 @feof(i32* %11)
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %24
15: ; preds = %10
%16 = load i32*, i32** %3
%17 = call i32 @fgetc(i32* %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
store i32 %22, i32* %5
br label %23
23: ; preds = %20, %15
br label %10
24: ; preds = %10
%25 = load i32*, i32** %3
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
@.str = [23 x i8] c"getted filename = %s;\0A\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @countlines(i8* %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @feof(i32* %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %1, %6
%7 = phi i32 [ %11, %6 ], [ 0, %1 ]
%8 = tail call i32 @fgetc(i32* %3)
%9 = icmp eq i32 %8, 10
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %7, %10
%12 = tail call i32 @feof(i32* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %6, label %14
14: ; preds = %6, %1
%15 = phi i32 [ 0, %1 ], [ %11, %6 ]
%16 = tail call i32 @fclose(i32* %3)
ret i32 %15
}