#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;
scalar_t__ errno;
int /*<<< orphan*/ flush();
int read(int, unsigned char *, unsigned int);
int iread(int fd, unsigned char *buf, unsigned int len) {
  int n;
start:
  flush();
  n = read(fd, buf, len);
  if (n < 0) {
    if (errno == EINTR)
      return ((-2));
    if (errno == EAGAIN)
      goto start;
    return (-1);
  }
  return (n);
}


x86 assembly (clang, -O0):
<iread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1030 <flush@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11a6 <iread+0x86>
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    0x2e72(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <iread+0x5b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11ac <iread+0x8c>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <iread+0x7a>
jmp    1132 <iread+0x12>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ac <iread+0x8c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ea6(%rip),%r15
mov    0x2e97(%rip),%r12
mov    0x2e80(%rip),%r13
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <flush@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    1183 <iread+0x63>
mov    (%r15),%rax
cmp    (%r12),%rax
je     117e <iread+0x5e>
cmp    0x0(%r13),%rax
je     1150 <iread+0x30>
mov    $0xffffffff,%eax
jmp    1183 <iread+0x63>
mov    $0xfffffffe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @iread(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %9
9: ; preds = %26, %3
%10 = call i32 (...) @flush()
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i32 @read(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %9
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -2, i32* %4
br label %30
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %9
27: ; preds = %22
store i32 -1, i32* %4
br label %30
28: ; preds = %9
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %30
30: ; preds = %28, %27, %21
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @iread(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %12, %3
%5 = tail call i32 (...) @flush()
%6 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %9, %13
br i1 %14, label %4, label %15
15: ; preds = %4, %12, %8
%16 = phi i32 [ -2, %8 ], [ -1, %12 ], [ %6, %4 ]
ret i32 %16
}