#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__ EAGAIN;
scalar_t__ EINTR;
int do_read(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int copy_from_stdin(void) {
  int c, bad_read = 0;
  while (1) {
    c = do_read(0);
    if ((c == 0) || ((c < 0) && ((errno == EAGAIN) || (errno == EINTR)))) {
      if (bad_read++ > 3)
        break;
      continue;
    }
    if (c < 0) {
      perror("read");
      exit(1);
    }
    bad_read = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_from_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
xor    %edi,%edi
call   1030 <do_read@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1191 <copy_from_stdin+0x61>
cmpl   $0x0,-0x4(%rbp)
jge    11af <copy_from_stdin+0x7f>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
je     1191 <copy_from_stdin+0x61>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
jne    11af <copy_from_stdin+0x7f>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x3,%eax
jle    11aa <copy_from_stdin+0x7a>
jmp    11db <copy_from_stdin+0xab>
jmp    113f <copy_from_stdin+0xf>
cmpl   $0x0,-0x4(%rbp)
jge    11cf <copy_from_stdin+0x9f>
lea    0xe40(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
movl   $0x0,-0x8(%rbp)
jmp    113f <copy_from_stdin+0xf>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_from_stdin>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
mov    0x2e9f(%rip),%r14
mov    0x2e80(%rip),%r12
mov    0x2e89(%rip),%r15
jmp    1168 <copy_from_stdin+0x38>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbx),%eax
cmp    $0x3,%ebx
jg     11a0 <copy_from_stdin+0x70>
mov    %eax,%ebx
xor    %edi,%edi
call   1030 <do_read@plt>
test   %eax,%eax
je     1160 <copy_from_stdin+0x30>
mov    $0x0,%eax
jns    1168 <copy_from_stdin+0x38>
mov    (%r14),%rax
cmp    (%r12),%rax
je     1160 <copy_from_stdin+0x30>
cmp    (%r15),%rax
je     1160 <copy_from_stdin+0x30>
lea    0xe6f(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
define i32 @copy_from_stdin() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %2
br label %3
3: ; preds = %0, %23, %30
%4 = call i32 @do_read(i32 0)
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %3
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %24
18: ; preds = %14, %10, %3
%19 = load i32, i32* %2
%20 = add nsw i32 %19, 1
store i32 %20, i32* %2
%21 = icmp sgt i32 %19, 3
br i1 %21, label %22, label %23
22: ; preds = %18
br label %31
23: ; preds = %18
br label %3
24: ; preds = %14, %7
%25 = load i32, i32* %1
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %24
store i32 0, i32* %2
br label %3
31: ; preds = %22
ret i32 0
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
define i32 @copy_from_stdin() {
br label %1
1: ; preds = %17, %0
%2 = phi i32 [ 0, %0 ], [ %18, %17 ]
%3 = tail call i32 @do_read(i32 0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %14, label %5
5: ; preds = %1
%6 = icmp slt i32 %3, 0
br i1 %6, label %7, label %17
7: ; preds = %5
%8 = load i64, i64* @errno
%9 = load i64, i64* @EAGAIN
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @EINTR
%12 = icmp eq i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %19
14: ; preds = %7, %1
%15 = add nsw i32 %2, 1
%16 = icmp sgt i32 %2, 3
br i1 %16, label %22, label %17
17: ; preds = %14, %5
%18 = phi i32 [ %15, %14 ], [ 0, %5 ]
br label %1
19: ; preds = %7
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
ret i32 0
}