#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 EXIT_FAILURE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ puts(char *);
int main(void) {
  FILE *fp = fopen("test.txt", "r");
  if (!fp) {
    perror("File opening failed");
    return EXIT_FAILURE;
  }
  int c;
  while ((c = fgetc(fp)) != EOF) {
    putchar(c);
  }
  if (ferror(fp))
    puts("I/O error when reading");
  else if (feof(fp))
    puts("End of file reached successfully");
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ce <main+0x4e>
lea    0xe53(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124c <main+0xcc>
jmp    11d3 <main+0x53>
mov    -0x10(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%eax
je     11fb <main+0x7b>
mov    -0x14(%rbp),%edi
call   1030 <putchar@plt>
jmp    11d3 <main+0x53>
mov    -0x10(%rbp),%rdi
call   1050 <ferror@plt>
cmp    $0x0,%rax
je     121f <main+0x9f>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
jmp    1243 <main+0xc3>
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     123e <main+0xbe>
lea    0xdfd(%rip),%rdi
call   1040 <puts@plt>
jmp    1243 <main+0xc3>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe75(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1215 <main+0x95>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fgetc@plt>
mov    0x2e3a(%rip),%r14
cmp    (%r14),%eax
je     11d4 <main+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
cmp    (%r14),%eax
jne    11c0 <main+0x40>
mov    %rbx,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
je     11ea <main+0x6a>
lea    0xe37(%rip),%rdi
jmp    11fe <main+0x7e>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     1203 <main+0x83>
lea    0xe38(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdef(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
jmp    120d <main+0x8d>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"File opening failed\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
@.str.3 = [23 x i8] c"I/O error when reading\00"
@.str.4 = [33 x i8] c"End of file reached successfully\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
store i32 %9, i32* %1
br label %35
10: ; preds = %0
br label %11
11: ; preds = %16, %10
%12 = load i32*, i32** %2
%13 = call i32 @fgetc(i32* %12)
store i32 %13, i32* %3
%14 = load i32, i32* @EOF
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* %3
%18 = call i32 @putchar(i32 %17)
br label %11
19: ; preds = %11
%20 = load i32*, i32** %2
%21 = call i64 @ferror(i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %32
25: ; preds = %19
%26 = load i32*, i32** %2
%27 = call i64 @feof(i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
br label %32
32: ; preds = %31, %23
%33 = load i32*, i32** %2
%34 = call i32 @fclose(i32* %33)
br label %35
35: ; preds = %32, %7
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [20 x i8] c"File opening failed\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
@.str.3 = [23 x i8] c"I/O error when reading\00"
@.str.4 = [33 x i8] c"End of file reached successfully\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %7, label %3
3: ; preds = %0
%4 = tail call i32 @fgetc(i32* %1)
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
br i1 %6, label %16, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
br label %27
10: ; preds = %3, %10
%11 = phi i32 [ %13, %10 ], [ %4, %3 ]
%12 = tail call i32 @putchar(i32 %11)
%13 = tail call i32 @fgetc(i32* %1)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %10
16: ; preds = %10, %3
%17 = tail call i64 @ferror(i32* %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i64 @feof(i32* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19, %16
%23 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), %16 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), %19 ]
%24 = tail call i32 @puts(i8* dereferenceable(1) %23)
br label %25
25: ; preds = %22, %19
%26 = tail call i32 @fclose(i32* %1)
br label %27
27: ; preds = %25, %7
%28 = phi i32 [ 0, %25 ], [ %9, %7 ]
ret i32 %28
}