#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*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  FILE *out = fopen("feof.temp", "w");
  char bytes[] = {1, 2, 3, 4, 5, 6, 7, 8};
  fwrite(bytes, sizeof(char), 8, out);
  fclose(out);
  for (int i = 0; i < 8; i++)
    bytes[i] = 0;
  FILE *in = fopen("feof.temp", "r");
  fread(bytes, sizeof(char), 8, in);
  if (feof(in) == 0)
    printf("First feof() says that we are NOT at the end of the file.\n");
  else
    printf("First feof() says that we ARE at the end of the file.\n");
  char b;
  if (fread(&b, 1, 1, in) == 0) {
    if (feof(in) == 0)
      printf("Second feof() says that we are NOT at the end of the file.\n");
    else
      printf("Second feof() says that we ARE at the end of the file.\n");
    if (ferror(in) == 0)
      printf("ferror() says there are NO errors.\n");
    else
      printf("ferror() says there ARE errors.\n");
  } else {
    printf("This should never happen. The file should only contain 8 bytes and "
           "the fread() above should always fail.\n");
  }
  fclose(in);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    0x1006(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    11e9 <main+0x79>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x18(%rbp,%rax,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c8 <main+0x58>
lea    0xe10(%rip),%rdi
lea    0xe15(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    123b <main+0xcb>
lea    0xddd(%rip),%rdi
call   1060 <printf@plt>
jmp    1247 <main+0xd7>
lea    0xe07(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rcx
lea    -0x29(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
jne    12ca <main+0x15a>
mov    -0x28(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    1289 <main+0x119>
lea    0xe01(%rip),%rdi
call   1060 <printf@plt>
jmp    1295 <main+0x125>
lea    0xe2c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    12b9 <main+0x149>
lea    0xe45(%rip),%rdi
call   1060 <printf@plt>
jmp    12c5 <main+0x155>
lea    0xe58(%rip),%rdi
call   1060 <printf@plt>
jmp    12d6 <main+0x166>
lea    0xe68(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe80(%rip),%r14
lea    0xe83(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
movabs $0x807060504030201,%rax
06 07 08
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
movq   $0x0,0x8(%rsp)
lea    0xe39(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
lea    0xf5d(%rip),%rax
lea    0xe02(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     123a <main+0xca>
lea    0xe0c(%rip),%rdi
jmp    1279 <main+0x109>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
lea    0xedb(%rip),%rax
lea    0xe5a(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
lea    0xe96(%rip),%rax
lea    0xe6f(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"feof.temp\00"
@.str.1 = [2 x i8] c"w\00"
@__const.main.bytes = [8 x i8] c"\01\02\03\04\05\06\07\08"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [59 x i8] c"First feof() says that we are NOT at the end of the file.\0A\00"
@.str.4 = [55 x i8] c"First feof() says that we ARE at the end of the file.\0A\00"
@.str.5 = [60 x i8] c"Second feof() says that we are NOT at the end of the file.\0A\00"
@.str.6 = [56 x i8] c"Second feof() says that we ARE at the end of the file.\0A\00"
@.str.7 = [36 x i8] c"ferror() says there are NO errors.\0A\00"
@.str.8 = [33 x i8] c"ferror() says there ARE errors.\0A\00"
@.str.9 = [106 x i8] c"This should never happen. The file should only contain 8 bytes and the fread() above should always fail.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [8 x i8]
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8
store i32 0, i32* %1
%7 = 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* %7, i32** %2
%8 = bitcast [8 x i8]* %3 to i8*
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i32 @fwrite(i8* %9, i32 1, i32 8, i32* %10)
%12 = load i32*, i32** %2
%13 = call i32 @fclose(i32* %12)
store i32 0, i32* %4
br label %14
14: ; preds = %21, %0
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 8
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %17
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %14
24: ; preds = %14
%25 = 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.2, i64 0, i64 0))
store i32* %25, i32** %5
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%27 = load i32*, i32** %5
%28 = call i64 @fread(i8* %26, i32 1, i32 8, i32* %27)
%29 = load i32*, i32** %5
%30 = call i64 @feof(i32* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0))
br label %36
34: ; preds = %24
%35 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %32
%37 = load i32*, i32** %5
%38 = call i64 @fread(i8* %6, i32 1, i32 1, i32* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %57
40: ; preds = %36
%41 = load i32*, i32** %5
%42 = call i64 @feof(i32* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0))
br label %48
46: ; preds = %40
%47 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %46, %44
%49 = load i32*, i32** %5
%50 = call i64 @ferror(i32* %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
br label %56
54: ; preds = %48
%55 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0))
br label %56
56: ; preds = %54, %52
br label %59
57: ; preds = %36
%58 = call i32 @printf(i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str.9, i64 0, i64 0))
br label %59
59: ; preds = %57, %56
%60 = load i32*, i32** %5
%61 = call i32 @fclose(i32* %60)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"feof.temp\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
@str = [54 x i8] c"First feof() says that we ARE at the end of the file.\00"
@str.10 = [105 x i8] c"This should never happen. The file should only contain 8 bytes and the fread() above should always fail.\00"
@str.11 = [55 x i8] c"Second feof() says that we ARE at the end of the file.\00"
@str.12 = [32 x i8] c"ferror() says there ARE errors.\00"
@str.13 = [35 x i8] c"ferror() says there are NO errors.\00"
@str.14 = [59 x i8] c"Second feof() says that we are NOT at the end of the file.\00"
@str.15 = [58 x i8] c"First feof() says that we are NOT at the end of the file.\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = alloca i8
%4 = 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))
store i64 578437695752307201, i64* %1
%5 = call i32 @fwrite(i8* %2, i32 1, i32 8, i32* %4)
%6 = tail call i32 @fclose(i32* %4)
store i64 0, i64* %1
%7 = 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.2, i64 0, i64 0))
%8 = call i64 @fread(i8* %2, i32 1, i32 8, i32* %7)
%9 = tail call i64 @feof(i32* %7)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @str.15, i64 0, i64 0), i8* getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0)
%12 = tail call i32 @puts(i8* dereferenceable(1) %11)
%13 = call i64 @fread(i8* %3, i32 1, i32 1, i32* %7)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %0
%16 = tail call i64 @feof(i32* %7)
%17 = icmp eq i64 %16, 0
%18 = select i1 %17, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @str.14, i64 0, i64 0), i8* getelementptr inbounds ([55 x i8], [55 x i8]* @str.11, i64 0, i64 0)
%19 = tail call i32 @puts(i8* dereferenceable(1) %18)
%20 = tail call i64 @ferror(i32* %7)
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @str.13, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @str.12, i64 0, i64 0)
br label %23
23: ; preds = %15, %0
%24 = phi i8* [ getelementptr inbounds ([105 x i8], [105 x i8]* @str.10, i64 0, i64 0), %0 ], [ %22, %15 ]
%25 = tail call i32 @puts(i8* dereferenceable(1) %24)
%26 = tail call i32 @fclose(i32* %7)
ret i32 0
}