#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 */
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int readn_block(int fd, void *buf, size_t len) {
  int cnt;
  int rc;
  char *bp;
  fd_set rfds;
  cnt = len;
  bp = buf;
  while (cnt > 0) {
    rc = recv(fd, bp, cnt, 0);
    if (rc < 0) {
      if (errno == EINTR)
        continue;
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);
        if (select(fd + 1, &rfds, NULL, NULL, NULL) < 0) {
          fprintf(stderr, "readn_nonblock select error:%m\n");
          return -1;
        }
        continue;
      }
      return -1;
    } else if (rc == 0)
      return len - cnt;
    cnt -= rc;
    bp += rc;
  }
  return len;
}


x86 assembly (clang, -O0):
<readn_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    129b <readn_block+0x14b>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1258 <readn_block+0x108>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <readn_block+0x69>
jmp    1172 <readn_block+0x22>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
je     11ed <readn_block+0x9d>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <readn_block+0xfc>
lea    -0x2c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1060 <select@plt>
cmp    $0x0,%rax
jge    1247 <readn_block+0xf7>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <readn_block+0x152>
jmp    1172 <readn_block+0x22>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a2 <readn_block+0x152>
cmpl   $0x0,-0x20(%rbp)
jne    1275 <readn_block+0x125>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    12a2 <readn_block+0x152>
jmp    127a <readn_block+0x12a>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1172 <readn_block+0x22>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readn_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    120c <readn_block+0xbc>
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x1(%r15),%eax
mov    %eax,0x4(%rsp)
mov    0x2e4d(%rip),%r12
lea    0x14(%rsp),%r13
mov    %rdx,0x8(%rsp)
mov    %edx,%ebp
mov    $0xffffffff,%r14d
jmp    11bb <readn_block+0x6b>
nop
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   1070 <FD_SET@plt>
mov    0x4(%rsp),%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
test   %rax,%rax
js     1211 <readn_block+0xc1>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    11f8 <readn_block+0xa8>
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     11bb <readn_block+0x6b>
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     1190 <readn_block+0x40>
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
je     1190 <readn_block+0x40>
jmp    1232 <readn_block+0xe2>
test   %eax,%eax
je     1228 <readn_block+0xd8>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     11bb <readn_block+0x6b>
mov    0x8(%rsp),%rax
jmp    122f <readn_block+0xdf>
mov    %edx,%r14d
jmp    1232 <readn_block+0xe2>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1232 <readn_block+0xe2>
mov    0x8(%rsp),%rax
sub    %ebp,%eax
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x18,%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
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [32 x i8] c"readn_nonblock select error:%m\0A\00"
define i32 @readn_block(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i64, i64* %7
%13 = trunc i64 %12 to i32
store i32 %13, i32* %8
%14 = load i8*, i8** %6
store i8* %14, i8** %10
br label %15
15: ; preds = %61, %49, %29, %3
%16 = load i32, i32* %8
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %69
18: ; preds = %15
%19 = load i32, i32* %5
%20 = load i8*, i8** %10
%21 = load i32, i32* %8
%22 = call i32 @recv(i32 %19, i8* %20, i32 %21, i32 0)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %51
25: ; preds = %18
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
br label %15
30: ; preds = %25
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EWOULDBLOCK
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %50
38: ; preds = %34, %30
%39 = call i32 @FD_ZERO(i32* %11)
%40 = load i32, i32* %5
%41 = call i32 @FD_SET(i32 %40, i32* %11)
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
%44 = call i64 @select(i32 %43, i32* %11, i32* null, i32* null, i32* null)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %72
49: ; preds = %38
br label %15
50: ; preds = %34
store i32 -1, i32* %4
br label %72
51: ; preds = %18
%52 = load i32, i32* %9
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i64, i64* %7
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = sub i64 %55, %57
%59 = trunc i64 %58 to i32
store i32 %59, i32* %4
br label %72
60: ; preds = %51
br label %61
61: ; preds = %60
%62 = load i32, i32* %9
%63 = load i32, i32* %8
%64 = sub nsw i32 %63, %62
store i32 %64, i32* %8
%65 = load i32, i32* %9
%66 = load i8*, i8** %10
%67 = sext i32 %65 to i64
%68 = getelementptr inbounds i8, i8* %66, i64 %67
store i8* %68, i8** %10
br label %15
69: ; preds = %15
%70 = load i64, i64* %7
%71 = trunc i64 %70 to i32
store i32 %71, i32* %4
br label %72
72: ; preds = %69, %54, %50, %46
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [32 x i8] c"readn_nonblock select error:%m\0A\00"
define i32 @readn_block(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = trunc i64 %2 to i32
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %46
8: ; preds = %3
%9 = add nsw i32 %0, 1
br label %10
10: ; preds = %8, %41
%11 = phi i8* [ %1, %8 ], [ %44, %41 ]
%12 = phi i32 [ %6, %8 ], [ %42, %41 ]
%13 = call i32 @recv(i32 %0, i8* %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %36
15: ; preds = %10, %19
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15, %28
%20 = call i32 @recv(i32 %0, i8* %11, i32 %12, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %15, label %36
22: ; preds = %15
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %16, %23
%25 = load i64, i64* @EWOULDBLOCK
%26 = icmp eq i64 %16, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %46
28: ; preds = %22
%29 = call i32 @FD_ZERO(i32* %4)
%30 = call i32 @FD_SET(i32 %0, i32* %4)
%31 = call i64 @select(i32 %9, i32* %4, i32* null, i32* null, i32* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %19
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %46
36: ; preds = %19, %10
%37 = phi i32 [ %13, %10 ], [ %20, %19 ]
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = sub i32 %6, %12
br label %46
41: ; preds = %36
%42 = sub nsw i32 %12, %37
%43 = zext i32 %37 to i64
%44 = getelementptr inbounds i8, i8* %11, i64 %43
%45 = icmp sgt i32 %42, 0
br i1 %45, label %10, label %46
46: ; preds = %41, %22, %3, %39, %33
%47 = phi i32 [ -1, %33 ], [ %40, %39 ], [ %6, %3 ], [ -1, %22 ], [ %6, %41 ]
ret i32 %47
}