#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;

/* Variables and functions */
int /*<<< orphan*/ MSG_WAITALL;
int recv(int, void *, int, int /*<<< orphan*/);
int recv_all(int fmfd, uint8_t *b, size_t len) {
  int nread;
  int nleft;
  int ret;
  nread = 0;
  nleft = len;
  ret = -1;
  while (0 < nleft) {
    if (-1 == (ret = recv(fmfd, (void *)(b + nread), nleft, MSG_WAITALL))) {
      return -16;
    } else if (0 == ret) {
      return -23;
    }
    nleft -= ret;
    nread += ret;
  }
  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)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%eax
jge    11b9 <recv_all+0xa9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1184 <recv_all+0x74>
movl   $0xfffffff0,-0x4(%rbp)
jmp    11c0 <recv_all+0xb0>
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jne    119b <recv_all+0x8b>
movl   $0xffffffe9,-0x4(%rbp)
jmp    11c0 <recv_all+0xb0>
jmp    11a0 <recv_all+0x90>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1138 <recv_all+0x28>
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   %r12
push   %rbx
mov    %rdx,%rbx
test   %ebx,%ebx
jle    1154 <recv_all+0x44>
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
movslq %ebp,%rax
lea    (%r14,%rax,4),%rsi
mov    (%r12),%ecx
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1158 <recv_all+0x48>
test   %eax,%eax
je     115f <recv_all+0x4f>
add    %eax,%ebp
sub    %eax,%ebx
jg     1130 <recv_all+0x20>
xor    %eax,%eax
jmp    1164 <recv_all+0x54>
mov    $0xfffffff0,%eax
jmp    1164 <recv_all+0x54>
mov    $0xffffffe9,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_WAITALL = global i32 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 i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
store i32 0, i32* %8
%11 = load i64, i64* %7
%12 = trunc i64 %11 to i32
store i32 %12, i32* %9
store i32 -1, i32* %10
br label %13
13: ; preds = %33, %3
%14 = load i32, i32* %9
%15 = icmp slt i32 0, %14
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i32*, i32** %6
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %18, i64 %20
%22 = bitcast i32* %21 to i8*
%23 = load i32, i32* %9
%24 = load i32, i32* @MSG_WAITALL
%25 = call i32 @recv(i32 %17, i8* %22, i32 %23, i32 %24)
store i32 %25, i32* %10
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %28
27: ; preds = %16
store i32 -16, i32* %4
br label %41
28: ; preds = %16
%29 = load i32, i32* %10
%30 = icmp eq i32 0, %29
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -23, i32* %4
br label %41
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = load i32, i32* %10
%35 = load i32, i32* %9
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %9
%37 = load i32, i32* %10
%38 = load i32, i32* %8
%39 = add nsw i32 %38, %37
store i32 %39, i32* %8
br label %13
40: ; preds = %13
store i32 0, i32* %4
br label %41
41: ; preds = %40, %31, %27
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@MSG_WAITALL = global i32 0
define i32 @recv_all(i32 %0, i32* %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %20
6: ; preds = %3, %14
%7 = phi i32 [ %15, %14 ], [ %4, %3 ]
%8 = phi i32 [ %16, %14 ], [ 0, %3 ]
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i32, i32* %1, i64 %9
%11 = bitcast i32* %10 to i8*
%12 = load i32, i32* @MSG_WAITALL
%13 = tail call i32 @recv(i32 %0, i8* %11, i32 %7, i32 %12)
switch i32 %13, label %14 [
i32 -1, label %18
i32 0, label %20
]
14: ; preds = %6
%15 = sub nsw i32 %7, %13
%16 = add nsw i32 %13, %8
%17 = icmp sgt i32 %15, 0
br i1 %17, label %6, label %18
18: ; preds = %14, %6
%19 = phi i32 [ 0, %14 ], [ -16, %6 ]
br label %20
20: ; preds = %6, %18, %3
%21 = phi i32 [ 0, %3 ], [ %19, %18 ], [ -23, %6 ]
ret i32 %21
}