#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 */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int count_lines(char *filename) {
  FILE *infile = fopen(filename, "r");
  if (!infile) {
    fprintf(stderr, "Error opening input file \"%s\"\n", filename);
    return -1;
  }
  char *lineptr = NULL;
  size_t n = 0;
  int result = 0;
  while (getline(&lineptr, &n, infile) > 0)
    result++;
  if (fclose(infile)) {
    free(lineptr);
    fprintf(stderr, "Error closing input file \"%s\"\n", filename);
    return -1;
  }
  free(lineptr);
  return result;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <count_lines+0x50>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe73(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <count_lines+0xe0>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <getline@plt>
cmp    $0x0,%rax
jle    11e0 <count_lines+0x90>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b7 <count_lines+0x67>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1221 <count_lines+0xd1>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <count_lines+0xe0>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e1 <count_lines+0x91>
mov    %rax,%rbx
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    $0xffffffff,%ebp
mov    %rsp,%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
add    $0x1,%ebp
test   %rax,%rax
jg     11a0 <count_lines+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11fe <count_lines+0xae>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
jmp    11f1 <count_lines+0xa1>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error opening input file \22%s\22\0A\00"
@.str.2 = [31 x i8] c"Error closing input file \22%s\22\0A\00"
define i32 @count_lines(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %2
br label %38
16: ; preds = %1
store i8* null, i8** %5
store i64 0, i64* %6
store i32 0, i32* %7
br label %17
17: ; preds = %21, %16
%18 = load i32*, i32** %4
%19 = call i64 @getline(i8** %5, i64* %6, i32* %18)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
br label %17
24: ; preds = %17
%25 = load i32*, i32** %4
%26 = call i64 @fclose(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %24
%29 = load i8*, i8** %5
%30 = call i32 @free(i8* %29)
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %3
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 -1, i32* %2
br label %38
34: ; preds = %24
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* %7
store i32 %37, i32* %2
br label %38
38: ; preds = %34, %28, %12
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Error opening input file \22%s\22\0A\00"
@.str.2 = [31 x i8] c"Error closing input file \22%s\22\0A\00"
define i32 @count_lines(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%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 %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
9: ; preds = %1
%10 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%11 = bitcast i64* %3 to i8*
store i64 0, i64* %3
br label %12
12: ; preds = %12, %9
%13 = phi i32 [ 0, %9 ], [ %16, %12 ]
%14 = call i64 @getline(i8** %2, i64* %3, i32* %4)
%15 = icmp sgt i64 %14, 0
%16 = add nuw nsw i32 %13, 1
br i1 %15, label %12, label %17
17: ; preds = %12
%18 = call i64 @fclose(i32* %4)
%19 = icmp eq i64 %18, 0
%20 = load i8*, i8** %2
%21 = call i32 @free(i8* %20)
br i1 %19, label %25, label %22
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %17, %22
%26 = phi i32 [ -1, %22 ], [ %13, %17 ]
br label %27
27: ; preds = %25, %6
%28 = phi i32 [ %26, %25 ], [ -1, %6 ]
ret i32 %28
}