#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 */
struct pollfd {
  int fd;
  int /*<<< orphan*/ events;
  int /*<<< orphan*/ member_0;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ POLLIN;
scalar_t__ errno;
int /*<<< orphan*/ poll(struct pollfd *, int, int);
scalar_t__ read(int, void *, size_t);
ssize_t xread(int fd, void *buf, size_t len) {
  if (len > (8 * 1024 * 1024)) {
    len = (8 * 1024 * 1024);
  }
  while (1) {
    ssize_t nr = read(fd, buf, len);
    if (nr < 0) {
      if (errno == EINTR) {
        continue;
      } else if (errno == EAGAIN) {
        struct pollfd pfd = {0};
        pfd.fd = fd;
        pfd.events = POLLIN;
        poll(&pfd, 1, -1);
        continue;
      }
    }
    return nr;
  }
}


x86 assembly (clang, -O0):
<xread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x800000,-0x18(%rbp)
jbe    1159 <xread+0x29>
movq   $0x800000,-0x18(%rbp)
jmp    115e <xread+0x2e>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11fa <xread+0xca>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    119c <xread+0x6c>
jmp    115e <xread+0x2e>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    11f0 <xread+0xc0>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1050 <poll@plt>
jmp    115e <xread+0x2e>
jmp    11f5 <xread+0xc5>
jmp    11fa <xread+0xca>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
cmp    $0x800000,%rdx
mov    $0x800000,%ebx
cmovb  %rdx,%rbx
mov    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
js     115f <xread+0x3f>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e72(%rip),%r12
mov    0x2e63(%rip),%r13
lea    0x8(%rsp),%r15
jmp    1192 <xread+0x72>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
jns    1150 <xread+0x30>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1180 <xread+0x60>
mov    0x2e1d(%rip),%rdx
cmp    (%rdx),%rcx
jne    1150 <xread+0x30>
movl   $0x0,0x10(%rsp)
mov    %ebp,0x8(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1040 <poll@plt>
jmp    1180 <xread+0x60>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@POLLIN = global i32 0
define i64 @xread(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca %struct.pollfd
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = icmp ugt i64 %9, 8388608
br i1 %10, label %11, label %12
11: ; preds = %3
store i64 8388608, i64* %6
br label %12
12: ; preds = %11, %3
br label %13
13: ; preds = %12, %24, %29
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = call i64 @read(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %38
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %13
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %25
%30 = bitcast %struct.pollfd* %8 to i8*
%31 = load i32, i32* %4
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @POLLIN
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 1
store i32 %33, i32* %34
%35 = call i32 @poll(%struct.pollfd* %8, i32 1, i32 -1)
br label %13
36: ; preds = %25
br label %37
37: ; preds = %36
br label %38
38: ; preds = %37, %13
%39 = load i64, i64* %7
ret i64 %39
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@POLLIN = global i32 0
define i64 @xread(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca %struct.pollfd
%5 = icmp ult i64 %2, 8388608
%6 = select i1 %5, i64 %2, i64 8388608
%7 = call i64 @read(i32 %0, i8* %1, i64 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %3
%10 = bitcast %struct.pollfd* %4 to i8*
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
br label %14
14: ; preds = %9, %25
%15 = phi i64 [ %7, %9 ], [ %26, %25 ]
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %25, label %19
19: ; preds = %14
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %16, %20
br i1 %21, label %22, label %28
22: ; preds = %19
store i32 0, i32* %13
store i32 %0, i32* %11
%23 = load i32, i32* @POLLIN
store i32 %23, i32* %12
%24 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 -1)
br label %25
25: ; preds = %14, %22
%26 = call i64 @read(i32 %0, i8* %1, i64 %6)
%27 = icmp slt i64 %26, 0
br i1 %27, label %14, label %28
28: ; preds = %25, %19, %3
%29 = phi i64 [ %7, %3 ], [ %15, %19 ], [ %26, %25 ]
ret i64 %29
}