#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *test = fopen("readme.md", "r");
  if (test != NULL) {
    char buf[15] = {0};
    int counter = 0;
    while (fgets(buf, 15, test) != NULL) {
      printf("%s", buf);
      ++counter;
    }
    printf("Counted %d strings\n", counter);
    fclose(test);
  } else
    perror("fopen");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1203 <main+0xa3>
lea    -0x1f(%rbp),%rdi
xor    %esi,%esi
mov    $0xf,%edx
call   1050 <memset@plt>
movl   $0x0,-0x24(%rbp)
lea    -0x1f(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xf,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11e4 <main+0x84>
lea    -0x1f(%rbp),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a8 <main+0x48>
mov    -0x24(%rbp),%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
jmp    120f <main+0xaf>
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe9f(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11fe <main+0xae>
mov    %rax,%r15
movq   $0x0,0xf(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0xf,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11d7 <main+0x87>
lea    0xe63(%rip),%r14
lea    0x8(%rsp),%rbx
xor    %ebp,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0xf,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
jmp    11d9 <main+0x89>
xor    %ebp,%ebp
lea    0xe2f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
jmp    11f1 <main+0xa1>

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

LLVM IR (-O2):
@.str = [10 x i8] c"readme.md\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [20 x i8] c"Counted %d strings\0A\00"
@.str.4 = [6 x i8] c"fopen\00"
define i32 @main() {
%1 = alloca [15 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %18, label %4
4: ; preds = %0
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%6 = call i32* @fgets(i8* %5, i32 15, i32* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %4, %8
%9 = phi i32 [ %11, %8 ], [ 0, %4 ]
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%11 = add nuw nsw i32 %9, 1
%12 = call i32* @fgets(i8* %5, i32 15, i32* %2)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %8
14: ; preds = %8, %4
%15 = phi i32 [ 0, %4 ], [ %11, %8 ]
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 @fclose(i32* %2)
br label %20
18: ; preds = %0
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
ret i32 0
}