#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ *read_line(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int count_lines(const char *fname) {
  FILE *f = NULL;
  int count = -1;
  if ((f = fopen(fname, "r")) == NULL) {
    fprintf(stderr, "Cannot find file \"%s\"\n", fname);
    return -1;
  }
  while (read_line(f) != NULL)
    count++;
  fclose(f);
  return count;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    119e <count_lines+0x5e>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe75(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <count_lines+0x93>
jmp    11a3 <count_lines+0x63>
mov    -0x18(%rbp),%rdi
call   1050 <read_line@plt>
cmp    $0x0,%rax
je     11c4 <count_lines+0x84>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a3 <count_lines+0x63>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     117a <count_lines+0x3a>
mov    %rax,%r14
mov    $0xfffffffe,%ebx
mov    %r14,%rdi
call   1050 <read_line@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    1160 <count_lines+0x20>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1197 <count_lines+0x57>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Cannot find file \22%s\22\0A\00"
define i32 @count_lines(i8* %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 %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %14
7: ; preds = %1, %7
%8 = phi i32 [ %11, %7 ], [ -1, %1 ]
%9 = tail call i32* @read_line(i32* %2)
%10 = icmp eq i32* %9, null
%11 = add nsw i32 %8, 1
br i1 %10, label %12, label %7
12: ; preds = %7
%13 = tail call i32 @fclose(i32* %2)
br label %14
14: ; preds = %12, %4
%15 = phi i32 [ -1, %4 ], [ %8, %12 ]
ret i32 %15
}