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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%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    -0x60(%rbp),%rdx
mov    $0x40,%esi
call   1050 <fgets@plt>
lea    -0x50(%rbp),%rsi
lea    0xe0c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x60(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     122f <main+0xaf>
jmp    1234 <main+0xb4>
jmp    11f5 <main+0x75>
mov    -0x60(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%eax
jne    125e <main+0xde>
lea    0xdc8(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
movabs $0x742e656c706d6173,%rax
65 2e 74
mov    %rax,(%rsp)
movl   $0x747874,0x7(%rsp)
lea    0xe8c(%rip),%rsi
mov    %rsp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1225 <main+0xa5>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
lea    0xe5c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     11d0 <main+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%eax
je     1217 <main+0x97>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe03(%rip),%rdi
call   1090 <perror@plt>
jmp    120b <main+0x8b>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%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 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.3 = [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 i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = bitcast [11 x i8]* %2 to i8*
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
br label %17
17: ; preds = %16, %27
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%19 = load i32*, i32** %5
%20 = call i32 @fgets(i8* %18, i32 64, i32* %19)
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%22 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %5
%24 = call i64 @feof(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %17
br label %28
27: ; preds = %17
br label %17
28: ; preds = %26
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = load i32, i32* @EOF
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
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 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.3 = [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, %11
%12 = call i32 @fgets(i8* %4, i32 64, i32* %5)
%13 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%14 = call i64 @feof(i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %11, label %16
16: ; preds = %11
%17 = call i32 @fclose(i32* %5)
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
ret i32 0
}