#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 */
scalar_t__ errno;
int recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int recv_all(int sock, uint8_t *buf, int len) {
  int recvd = 0;
  while (recvd < len) {
    errno = 0;
    int ret = recv(sock, buf + recvd, len - recvd, 0);
    if (ret < 0)
      return ret;
    if (ret == 0)
      return -1;
    recvd += ret;
  }
  return recvd;
}


x86 assembly (clang, -O0):
<recv_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119e <recv_all+0x8e>
mov    0x2ea4(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117a <recv_all+0x6a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a4 <recv_all+0x94>
cmpl   $0x0,-0x1c(%rbp)
jne    1190 <recv_all+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <recv_all+0x94>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <recv_all+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    115b <recv_all+0x4b>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
mov    0x2eb3(%rip),%r12
nopl   (%rax)
movq   $0x0,(%r12)
movslq %ebx,%rax
lea    (%r14,%rax,4),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     115f <recv_all+0x4f>
je     1163 <recv_all+0x53>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1130 <recv_all+0x20>
jmp    1168 <recv_all+0x58>
xor    %ebx,%ebx
jmp    1168 <recv_all+0x58>
mov    %eax,%ebx
jmp    1168 <recv_all+0x58>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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