#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 fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int main() {
  const char filename[] = "sample.txt";
  char buffer[64];
  char *b;
  int r;
  FILE *fp;
  fp = fopen(filename, "r");
  if (fp == NULL) {
    fprintf(stderr, "Unable to open %s\n", filename);
    exit(1);
  }
  while (1) {
    b = fgets(buffer, 64, fp);
    if (b == NULL) {
      if (feof(fp) != 0)
        break;
      else
        perror("File read error");
    }
    printf("%s", buffer);
  }
  r = fclose(fp);
  if (r == EOF)
    perror("File closing error");
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe67(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe5f(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0xf(%rbp),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11f0 <main+0x70>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    -0xf(%rbp),%rdx
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11f5 <main+0x75>
lea    -0x50(%rbp),%rdi
mov    -0x68(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    123f <main+0xbf>
mov    -0x68(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     122e <main+0xae>
jmp    1254 <main+0xd4>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
jmp    123f <main+0xbf>
lea    -0x50(%rbp),%rsi
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
jmp    11f5 <main+0x75>
mov    -0x68(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%eax
jne    127e <main+0xfe>
lea    0xdb8(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
movabs $0x742e656c706d6173,%rax
65 2e 74
mov    %rax,(%rsp)
movl   $0x747874,0x7(%rsp)
lea    0xe84(%rip),%rsi
mov    %rsp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1246 <main+0xc6>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
lea    0xe56(%rip),%r15
lea    0xe5f(%rip),%r12
jmp    11eb <main+0x6b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11e0 <main+0x60>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    1217 <main+0x97>
mov    %r15,%rdi
call   1090 <perror@plt>
jmp    11e0 <main+0x60>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%eax
je     1238 <main+0xb8>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf2(%rip),%rdi
call   1090 <perror@plt>
jmp    122a <main+0xaa>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.filename = [11 x i8] c"sample.txt\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [16 x i8] c"File read error\00"
@.str.3 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.4 = [19 x i8] c"File closing error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [11 x i8]
%3 = alloca [64 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = bitcast [11 x i8]* %2 to i8*
%8 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
br label %18
18: ; preds = %17, %32
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%20 = load i32*, i32** %6
%21 = call i8* @fgets(i8* %19, i32 64, i32* %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %32
24: ; preds = %18
%25 = load i32*, i32** %6
%26 = call i64 @feof(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
br label %35
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29
br label %32
32: ; preds = %31, %18
%33 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%34 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %18
35: ; preds = %28
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %35
ret i32 0
}

LLVM IR (-O2):
@__const.main.filename = [11 x i8] c"sample.txt\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [16 x i8] c"File read error\00"
@.str.3 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.4 = [19 x i8] c"File closing error\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0, %19
%12 = call i8* @fgets(i8* %4, i32 64, i32* %5)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = call i64 @feof(i32* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %11
21: ; preds = %14
%22 = call i32 @fclose(i32* %5)
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
ret i32 0
}