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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe57(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe4f(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0xf(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1200 <main+0x70>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    -0xf(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1205 <main+0x75>
mov    -0x20(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%eax
jne    1279 <main+0xe9>
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     123b <main+0xab>
jmp    1286 <main+0xf6>
mov    -0x20(%rbp),%rdi
call   1040 <ferror@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1256 <main+0xc6>
jmp    1286 <main+0xf6>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    -0xf(%rbp),%rdx
lea    0xdb4(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1030 <putchar@plt>
jmp    1205 <main+0x75>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%eax
jne    12b0 <main+0x120>
lea    0xd86(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movabs $0x742e656c706d6173,%rax
65 2e 74
mov    %rax,0x8(%rsp)
movl   $0x747874,0xf(%rsp)
lea    0xe8d(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1232 <main+0xa2>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    0x2e12(%rip),%r14
cmp    (%r14),%eax
je     11f4 <main+0x64>
nopl   0x0(%rax,%rax,1)
mov    %eax,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r14),%eax
jne    11e0 <main+0x50>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    120d <main+0x7d>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %eax,%eax
jne    1244 <main+0xb4>
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    (%r14),%eax
je     1224 <main+0x94>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe06(%rip),%rdi
call   10a0 <perror@plt>
jmp    121a <main+0x8a>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
jmp    1254 <main+0xc4>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <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"
@EOF = global i32 0
@.str.2 = [19 x i8] c"\0AFile error in %s\0A\00"
@.str.3 = [19 x i8] c"File closing error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [11 x i8]
%3 = alloca i32
%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, %39
%18 = load i32*, i32** %5
%19 = call i32 @fgetc(i32* %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %39
23: ; preds = %17
%24 = load i32*, i32** %5
%25 = call i64 @feof(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %42
28: ; preds = %23
%29 = load i32*, i32** %5
%30 = call i32 @ferror(i32* %29)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
br label %42
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %17
%40 = load i32, i32* %3
%41 = call i32 @putchar(i32 %40)
br label %17
42: ; preds = %33, %27
%43 = load i32*, i32** %5
%44 = call i32 @fclose(i32* %43)
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = load i32, i32* @EOF
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
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"
@EOF = global i32 0
@.str.2 = [19 x i8] c"\0AFile error in %s\0A\00"
@.str.3 = [19 x i8] c"File closing error\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%3 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %9, label %5
5: ; preds = %0
%6 = tail call i32 @fgetc(i32* %3)
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %6, %7
br i1 %8, label %13, label %23
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %23, %5
%14 = tail call i64 @feof(i32* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = tail call i32 @ferror(i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %5, %23
%24 = phi i32 [ %26, %23 ], [ %6, %5 ]
%25 = tail call i32 @putchar(i32 %24)
%26 = tail call i32 @fgetc(i32* %3)
%27 = load i32, i32* @EOF
%28 = icmp eq i32 %26, %27
br i1 %28, label %13, label %23
29: ; preds = %16, %13
%30 = tail call i32 @fclose(i32* %3)
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
ret i32 0
}