#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;
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int count_lines(FILE *fp) {
  printf("Counting lines...\n");
  char ch;
  int lines = 0;
  while ((ch = fgetc(fp)) != EOF) {
    if (ch == '\n')
      ++lines;
  }
  printf("done!\tLines: %d\n", lines);
  return lines;
}


x86 assembly (clang, -O0):
<count_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xecd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
mov    0x2e91(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     117d <count_lines+0x5d>
movsbl -0x9(%rbp),%eax
cmp    $0xa,%eax
jne    1178 <count_lines+0x58>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1141 <count_lines+0x21>
mov    -0x10(%rbp),%esi
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_lines>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    0x2e96(%rip),%r14
xor    %ebp,%ebp
cmp    (%r14),%al
je     1176 <count_lines+0x46>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebp
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    (%r14),%al
jne    1160 <count_lines+0x30>
lea    0xe83(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Counting lines...\0A\00"
@EOF = global i8 0
@.str.1 = [17 x i8] c"done!\09Lines: %d\0A\00"
define i32 @count_lines(i32* %0) {
%2 = alloca i32*
%3 = alloca i8
%4 = alloca i32
store i32* %0, i32** %2
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %6
6: ; preds = %20, %1
%7 = load i32*, i32** %2
%8 = call signext i8 @fgetc(i32* %7)
store i8 %8, i8* %3
%9 = sext i8 %8 to i32
%10 = load i8, i8* @EOF
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %9, %11
br i1 %12, label %13, label %21
13: ; preds = %6
%14 = load i8, i8* %3
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
br label %20
20: ; preds = %17, %13
br label %6
21: ; preds = %6
%22 = load i32, i32* %4
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@EOF = global i8 0
@.str.1 = [17 x i8] c"done!\09Lines: %d\0A\00"
@str = [18 x i8] c"Counting lines...\00"
define i32 @count_lines(i32* nocapture %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%3 = tail call signext i8 @fgetc(i32* %0)
%4 = load i8, i8* @EOF
%5 = icmp eq i8 %3, %4
br i1 %5, label %15, label %6
6: ; preds = %1, %6
%7 = phi i8 [ %12, %6 ], [ %3, %1 ]
%8 = phi i32 [ %11, %6 ], [ 0, %1 ]
%9 = icmp eq i8 %7, 10
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %8, %10
%12 = tail call signext i8 @fgetc(i32* %0)
%13 = load i8, i8* @EOF
%14 = icmp eq i8 %12, %13
br i1 %14, label %15, label %6
15: ; preds = %6, %1
%16 = phi i32 [ 0, %1 ], [ %11, %6 ]
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %16)
ret i32 %16
}