#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*/ str;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *fp;
  char str[100] = "ferror test string\n";
  fp = fopen("../ferror.txt", "w");
  fputs("ferror test string", fp);
  fclose(fp);
  fp = fopen("../ferror.txt", "r");
  while (!feof(fp)) {
    fgets(str, sizeof(str), fp);
    if (ferror(fp)) {
      printf("read error!");
      break;
    } else {
      printf("%s\n", str);
    }
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe63(%rip),%rsi
mov    $0x64,%edx
call   1090 <memcpy@plt>
lea    0xeb6(%rip),%rdi
lea    0xebd(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xeab(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe86(%rip),%rdi
lea    0xea2(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    120c <main+0x8c>
jmp    125b <main+0xdb>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1244 <main+0xc4>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    125b <main+0xdb>
lea    -0x80(%rbp),%rsi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11ee <main+0x6e>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
movaps 0xec2(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xea6(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe8a(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe6e(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe52(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe36(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
lea    0xe87(%rip),%r14
lea    0xe8e(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xe7e(%rip),%rdi
mov    $0x12,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe70(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    1281 <main+0x101>
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1273 <main+0xf3>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     1240 <main+0xc0>
jmp    1281 <main+0x101>
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.str = [100 x i8] c"ferror test string\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [14 x i8] c"../ferror.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"ferror test string\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [12 x i8] c"read error!\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = bitcast [100 x i8]* %3 to i8*
%5 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%8 = load i32*, i32** %2
%9 = call i32 @fclose(i32* %8)
%10 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %10, i32** %2
br label %11
11: ; preds = %28, %0
%12 = load i32*, i32** %2
%13 = call i32 @feof(i32* %12)
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %2
%19 = call i32 @fgets(i8* %17, i32 100, i32* %18)
%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 = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %29
25: ; preds = %16
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25
br label %11
29: ; preds = %23, %11
%30 = load i32*, i32** %2
%31 = call i32 @fclose(i32* %30)
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@__const.main.str = [100 x i8] c"ferror test string\0A\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [14 x i8] c"../ferror.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"ferror test string\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [12 x i8] c"read error!\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i64 18, i64 1, i32* %3)
%5 = tail call i32 @fclose(i32* %3)
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%7 = call i32 @feof(i32* %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %0, %15
%10 = call i32 @fgets(i8* %2, i32 100, i32* %6)
%11 = call i64 @ferror(i32* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %19
15: ; preds = %9
%16 = call i32 @puts(i8* %2)
%17 = call i32 @feof(i32* %6)
%18 = icmp eq i32 %17, 0
br i1 %18, label %9, label %19
19: ; preds = %15, %0, %13
%20 = call i32 @fclose(i32* %6)
ret i32 0
}