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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ recv(int, int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int recv_all(int fd, uint8_t *buf, size_t size) {
  ssize_t n;
  fd_set rset;
  while (1) {
    errno = 0;
    n = recv(fd, buf, size, 0);
    if (n <= 0) {
      if ((errno != EWOULDBLOCK) || (errno != EAGAIN))
        return -1;
    } else {
      size -= (size_t)n;
      buf += (size_t)n;
      if (size == 0)
        break;
    }
    FD_ZERO(&rset);
    FD_SET(fd, &rset);
    if (select(fd + 1, &rset, NULL, NULL, NULL) <= 0) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<recv_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e86(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     11c7 <recv_all+0x87>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11b6 <recv_all+0x76>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
je     11c2 <recv_all+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <recv_all+0x10c>
jmp    11fb <recv_all+0xbb>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f6 <recv_all+0xb6>
jmp    1245 <recv_all+0x105>
jmp    11fb <recv_all+0xbb>
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1050 <select@plt>
cmp    $0x0,%rax
jg     1240 <recv_all+0x100>
movl   $0xffffffff,-0x4(%rbp)
jmp    124c <recv_all+0x10c>
jmp    1153 <recv_all+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x1(%r15),%r12d
mov    0x2e81(%rip),%r13
lea    0x4(%rsp),%rbp
jmp    119a <recv_all+0x5a>
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
test   %rax,%rax
jle    11f7 <recv_all+0xb7>
movq   $0x0,0x0(%r13)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    11d0 <recv_all+0x90>
sub    %rax,%rbx
je     11fe <recv_all+0xbe>
lea    (%r14,%rax,4),%r14
jmp    1170 <recv_all+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%rcx
mov    $0xffffffff,%eax
mov    0x2df8(%rip),%rdx
cmp    (%rdx),%rcx
jne    1200 <recv_all+0xc0>
mov    0x2ddc(%rip),%rdx
cmp    (%rdx),%rcx
je     1170 <recv_all+0x30>
jmp    1200 <recv_all+0xc0>
mov    $0xffffffff,%eax
jmp    1200 <recv_all+0xc0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
define i32 @recv_all(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
br label %10
10: ; preds = %3, %47
store i64 0, i64* @errno
%11 = load i32, i32* %5
%12 = load i32*, i32** %6
%13 = load i64, i64* %7
%14 = call i64 @recv(i32 %11, i32* %12, i64 %13, i32 0)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = icmp sle i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EWOULDBLOCK
%20 = icmp ne i64 %18, %19
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EAGAIN
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21, %17
store i32 -1, i32* %4
br label %49
26: ; preds = %21
br label %38
27: ; preds = %10
%28 = load i64, i64* %8
%29 = load i64, i64* %7
%30 = sub i64 %29, %28
store i64 %30, i64* %7
%31 = load i64, i64* %8
%32 = load i32*, i32** %6
%33 = getelementptr inbounds i32, i32* %32, i64 %31
store i32* %33, i32** %6
%34 = load i64, i64* %7
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %27
br label %48
37: ; preds = %27
br label %38
38: ; preds = %37, %26
%39 = call i32 @FD_ZERO(i32* %9)
%40 = load i32, i32* %5
%41 = call i32 @FD_SET(i32 %40, i32* %9)
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
%44 = call i64 @select(i32 %43, i32* %9, i32* null, i32* null, i32* null)
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %38
store i32 -1, i32* %4
br label %49
47: ; preds = %38
br label %10
48: ; preds = %36
store i32 0, i32* %4
br label %49
49: ; preds = %48, %46, %25
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
define i32 @recv_all(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = add nsw i32 %0, 1
br label %7
7: ; preds = %23, %3
%8 = phi i32* [ %1, %3 ], [ %24, %23 ]
%9 = phi i64 [ %2, %3 ], [ %25, %23 ]
store i64 0, i64* @errno
%10 = call i64 @recv(i32 %0, i32* %8, i64 %9, i32 0)
%11 = icmp slt i64 %10, 1
br i1 %11, label %12, label %19
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @EWOULDBLOCK
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %23, label %30
19: ; preds = %7
%20 = sub i64 %9, %10
%21 = getelementptr inbounds i32, i32* %8, i64 %10
%22 = icmp eq i64 %20, 0
br i1 %22, label %30, label %23
23: ; preds = %12, %19
%24 = phi i32* [ %21, %19 ], [ %8, %12 ]
%25 = phi i64 [ %20, %19 ], [ %9, %12 ]
%26 = call i32 @FD_ZERO(i32* %4)
%27 = call i32 @FD_SET(i32 %0, i32* %4)
%28 = call i64 @select(i32 %6, i32* %4, i32* null, i32* null, i32* null)
%29 = icmp slt i64 %28, 1
br i1 %29, label %30, label %7
30: ; preds = %19, %23, %12
%31 = phi i32 [ -1, %12 ], [ -1, %23 ], [ 0, %19 ]
ret i32 %31
}