#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 */

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ clearerr(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ fputc(char, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char buf[100];
  int result = 0;
  if (ferror(stdin) != 0) {
    fputs("error bit set for stdin at startup\n", stdout);
    result = 1;
  }
  if (fgets(buf, sizeof buf, stdin) != buf) {
    fputs("fgets with existing input has problem\n", stdout);
    result = 1;
  }
  if (ferror(stdin) != 0) {
    fputs("error bit set for stdin after setup\n", stdout);
    result = 1;
  }
  if (fputc('a', stdin) != EOF) {
    fputs("fputc to stdin does not terminate with an error\n", stdout);
    result = 1;
  }
  if (ferror(stdin) == 0) {
    fputs("error bit not set for stdin after fputc\n", stdout);
    result = 1;
  }
  clearerr(stdin);
  if (ferror(stdin) != 0) {
    fputs("error bit set for stdin after clearerr\n", stdout);
    result = 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x84(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11aa <main+0x5a>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x1,-0x84(%rbp)
lea    -0x80(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    -0x80(%rbp),%rcx
cmp    %rcx,%rax
je     11ed <main+0x9d>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x1,-0x84(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1224 <main+0xd4>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x1,-0x84(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    $0x61,%edi
call   1050 <fputc@plt>
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
je     1266 <main+0x116>
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    0xe19(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x1,-0x84(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    129d <main+0x14d>
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
lea    0xe13(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x1,-0x84(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1070 <clearerr@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12e2 <main+0x192>
mov    0x2d06(%rip),%rax
mov    (%rax),%esi
lea    0xdf7(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x1,-0x84(%rbp)
mov    -0x84(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e89(%rip),%r14
mov    (%r14),%edi
call   1030 <ferror@plt>
test   %rax,%rax
je     1188 <main+0x38>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%ebp
jmp    118a <main+0x3a>
xor    %ebp,%ebp
mov    (%r14),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
cmp    %rbx,%rax
je     11bc <main+0x6c>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%ebp
mov    (%r14),%edi
call   1030 <ferror@plt>
test   %rax,%rax
je     11e3 <main+0x93>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%ebp
mov    (%r14),%esi
mov    $0x61,%edi
call   1050 <fputc@plt>
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
je     1216 <main+0xc6>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xe64(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%ebp
mov    (%r14),%edi
call   1030 <ferror@plt>
test   %rax,%rax
jne    123d <main+0xed>
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%ebp
mov    (%r14),%edi
call   1070 <clearerr@plt>
mov    (%r14),%edi
call   1030 <ferror@plt>
test   %rax,%rax
je     126c <main+0x11c>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [36 x i8] c"error bit set for stdin at startup\0A\00"
@stdout = global i32 0
@.str.1 = [39 x i8] c"fgets with existing input has problem\0A\00"
@.str.2 = [37 x i8] c"error bit set for stdin after setup\0A\00"
@EOF = global i64 0
@.str.3 = [49 x i8] c"fputc to stdin does not terminate with an error\0A\00"
@.str.4 = [41 x i8] c"error bit not set for stdin after fputc\0A\00"
@.str.5 = [40 x i8] c"error bit set for stdin after clearerr\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [100 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%8 = load i32, i32* @stdin
%9 = call i64 @ferror(i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stdout
%13 = call i32 @fputs(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 1, i32* %7
br label %14
14: ; preds = %11, %2
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @stdin
%17 = call i8* @fgets(i8* %15, i32 100, i32 %16)
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%19 = icmp ne i8* %17, %18
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @stdout
%22 = call i32 @fputs(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 1, i32* %7
br label %23
23: ; preds = %20, %14
%24 = load i32, i32* @stdin
%25 = call i64 @ferror(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stdout
%29 = call i32 @fputs(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 1, i32* %7
br label %30
30: ; preds = %27, %23
%31 = load i32, i32* @stdin
%32 = call i64 @fputc(i8 signext 97, i32 %31)
%33 = load i64, i64* @EOF
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @stdout
%37 = call i32 @fputs(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i32 %36)
store i32 1, i32* %7
br label %38
38: ; preds = %35, %30
%39 = load i32, i32* @stdin
%40 = call i64 @ferror(i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @stdout
%44 = call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %43)
store i32 1, i32* %7
br label %45
45: ; preds = %42, %38
%46 = load i32, i32* @stdin
%47 = call i32 @clearerr(i32 %46)
%48 = load i32, i32* @stdin
%49 = call i64 @ferror(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = load i32, i32* @stdout
%53 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %52)
store i32 1, i32* %7
br label %54
54: ; preds = %51, %45
%55 = load i32, i32* %7
ret i32 %55
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [36 x i8] c"error bit set for stdin at startup\0A\00"
@stdout = global i32 0
@.str.1 = [39 x i8] c"fgets with existing input has problem\0A\00"
@.str.2 = [37 x i8] c"error bit set for stdin after setup\0A\00"
@EOF = global i64 0
@.str.3 = [49 x i8] c"fputc to stdin does not terminate with an error\0A\00"
@.str.4 = [41 x i8] c"error bit not set for stdin after fputc\0A\00"
@.str.5 = [40 x i8] c"error bit set for stdin after clearerr\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = tail call i64 @ferror(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stdout
%10 = tail call i32 @fputs(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %8, %2
%12 = phi i32 [ 1, %8 ], [ 0, %2 ]
%13 = load i32, i32* @stdin
%14 = call i8* @fgets(i8* %4, i32 100, i32 %13)
%15 = icmp eq i8* %14, %4
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = load i32, i32* @stdout
%18 = call i32 @fputs(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %11
%20 = phi i32 [ 1, %16 ], [ %12, %11 ]
%21 = load i32, i32* @stdin
%22 = call i64 @ferror(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @stdout
%26 = call i32 @fputs(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %19
%28 = phi i32 [ 1, %24 ], [ %20, %19 ]
%29 = load i32, i32* @stdin
%30 = call i64 @fputc(i8 signext 97, i32 %29)
%31 = load i64, i64* @EOF
%32 = icmp eq i64 %30, %31
br i1 %32, label %36, label %33
33: ; preds = %27
%34 = load i32, i32* @stdout
%35 = call i32 @fputs(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %33, %27
%37 = phi i32 [ 1, %33 ], [ %28, %27 ]
%38 = load i32, i32* @stdin
%39 = call i64 @ferror(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @stdout
%43 = call i32 @fputs(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %36
%45 = phi i32 [ 1, %41 ], [ %37, %36 ]
%46 = load i32, i32* @stdin
%47 = call i32 @clearerr(i32 %46)
%48 = load i32, i32* @stdin
%49 = call i64 @ferror(i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %44
%52 = load i32, i32* @stdout
%53 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %51, %44
%55 = phi i32 [ 1, %51 ], [ %45, %44 ]
ret i32 %55
}