#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int count_lines(char *filename) {
  FILE *in;
  int n_lines;
  int c;
  if (!(in = fopen(filename, "r"))) {
    printf("problem opening file %s\n", filename);
    exit(1);
  }
  n_lines = 0;
  do {
    c = fgetc(in);
    if (c == '\n') {
      n_lines++;
    }
  } while (c != EOF);
  rewind(in);
  fclose(in);
  return n_lines;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a4 <count_lines+0x44>
mov    -0x8(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jne    11ca <count_lines+0x6a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cf <count_lines+0x6f>
mov    -0x18(%rbp),%eax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%eax
jne    11ab <count_lines+0x4b>
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11be <count_lines+0x5e>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    0x2e64(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <fgetc@plt>
xor    %ecx,%ecx
cmp    $0xa,%eax
sete   %cl
add    %ecx,%ebp
cmp    (%r14),%eax
jne    1190 <count_lines+0x30>
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3d(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"problem opening file %s\0A\00"
@EOF = global i32 0
define i32 @count_lines(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* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
store i32 0, i32* %4
br label %14
14: ; preds = %23, %13
%15 = load i32*, i32** %3
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
br label %22
22: ; preds = %19, %14
br label %23
23: ; preds = %22
%24 = load i32, i32* %5
%25 = load i32, i32* @EOF
%26 = icmp ne i32 %24, %25
br i1 %26, label %14, label %27
27: ; preds = %23
%28 = load i32*, i32** %3
%29 = call i32 @rewind(i32* %28)
%30 = load i32*, i32** %3
%31 = call i32 @fclose(i32* %30)
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"problem opening file %s\0A\00"
@EOF = global i32 0
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 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1, %7
%8 = phi i32 [ %12, %7 ], [ 0, %1 ]
%9 = tail call i32 @fgetc(i32* %2)
%10 = icmp eq i32 %9, 10
%11 = zext i1 %10 to i32
%12 = add nuw nsw i32 %8, %11
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %9, %13
br i1 %14, label %15, label %7
15: ; preds = %7
%16 = tail call i32 @rewind(i32* %2)
%17 = tail call i32 @fclose(i32* %2)
ret i32 %12
}