#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 */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ EIO;
#define EOF 128
int /*<<< orphan*/ clearerr(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ ferror(int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ stdin;
int readc() {
  unsigned tries = 1000;
  int c;
  while (tries) {
    c = getchar();
    switch (c) {
    case '\n':
      continue;
    case '\r':
      continue;
    case EOF:
      if (ferror(stdin)) {
        if ((errno == EINTR) || (errno == EIO)) {
          clearerr(stdin);
          tries--;
          continue;
        }
      }
    default:
      return c;
    }
  }
  return EOF;
}


x86 assembly (clang, -O0):
<readc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x3e8,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     120d <readc+0xdd>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0xa,%eax
je     118b <readc+0x5b>
jmp    1167 <readc+0x37>
mov    -0x10(%rbp),%eax
sub    $0xd,%eax
je     1190 <readc+0x60>
jmp    1178 <readc+0x48>
mov    -0x10(%rbp),%eax
sub    $0x80,%eax
je     1195 <readc+0x65>
jmp    1202 <readc+0xd2>
jmp    113f <readc+0xf>
jmp    113f <readc+0xf>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%eax
je     11fd <readc+0xcd>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%eax
je     11dc <readc+0xac>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%eax
jne    11f8 <readc+0xc8>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1050 <clearerr@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    113f <readc+0xf>
jmp    11fd <readc+0xcd>
jmp    1202 <readc+0xd2>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <readc+0xe4>
movl   $0x80,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x3e8,%ebp
mov    0x2ea1(%rip),%r14
mov    0x2e82(%rip),%r15
mov    0x2e73(%rip),%r12
mov    0x2e7c(%rip),%r13
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
je     1160 <readc+0x30>
mov    %eax,%ebx
cmp    $0xd,%eax
je     1160 <readc+0x30>
cmp    $0x80,%ebx
jne    11a3 <readc+0x73>
mov    (%r14),%edi
call   1030 <ferror@plt>
test   %eax,%eax
je     11a3 <readc+0x73>
mov    (%r15),%eax
cmp    (%r12),%eax
je     1196 <readc+0x66>
cmp    0x0(%r13),%eax
jne    11a3 <readc+0x73>
mov    (%r14),%edi
call   1050 <clearerr@plt>
add    $0xffffffff,%ebp
jne    1160 <readc+0x30>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EIO = global i32 0
define i32 @readc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 1000, i32* %2
br label %4
4: ; preds = %24, %11, %10, %0
%5 = load i32, i32* %2
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %33
7: ; preds = %4
%8 = call i32 (...) @getchar()
store i32 %8, i32* %3
%9 = load i32, i32* %3
switch i32 %9, label %31 [
i32 10, label %10
i32 13, label %11
i32 128, label %12
]
10: ; preds = %7
br label %4
11: ; preds = %7
br label %4
12: ; preds = %7
%13 = load i32, i32* @stdin
%14 = call i32 @ferror(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = load i32, i32* @EINTR
%19 = icmp eq i32 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = load i32, i32* @EIO
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20, %16
%25 = load i32, i32* @stdin
%26 = call i32 @clearerr(i32 %25)
%27 = load i32, i32* %2
%28 = add i32 %27, -1
store i32 %28, i32* %2
br label %4
29: ; preds = %20
br label %30
30: ; preds = %29, %12
br label %31
31: ; preds = %7, %30
%32 = load i32, i32* %3
store i32 %32, i32* %1
br label %34
33: ; preds = %4
store i32 128, i32* %1
br label %34
34: ; preds = %33, %31
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@stdin = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EIO = global i32 0
define i32 @readc() {
br label %1
1: ; preds = %0, %17
%2 = phi i32 [ 1000, %0 ], [ %20, %17 ]
br label %3
3: ; preds = %5, %1
%4 = tail call i32 (...) @getchar()
switch i32 %4, label %22 [
i32 10, label %5
i32 13, label %5
i32 128, label %6
]
5: ; preds = %3, %3
br label %3
6: ; preds = %3
%7 = load i32, i32* @stdin
%8 = tail call i32 @ferror(i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = load i32, i32* @errno
%12 = load i32, i32* @EINTR
%13 = icmp eq i32 %11, %12
%14 = load i32, i32* @EIO
%15 = icmp eq i32 %11, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = load i32, i32* @stdin
%19 = tail call i32 @clearerr(i32 %18)
%20 = add nsw i32 %2, -1
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %1
22: ; preds = %6, %10, %17, %3
ret i32 %4
}