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

/* Variables and functions */
scalar_t__ recv(int, char *, scalar_t__, int /*<<< orphan*/);
ssize_t recv_full(int fd, void *buf, ssize_t len) {
  char *p = buf;
  ssize_t recvd;
  for (recvd = 1; len > 0 && recvd > 0; len -= recvd, p += recvd)
    recvd = recv(fd, p, len, 0);
  return p - (char *)buf;
}


x86 assembly (clang, -O0):
<recv_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x1,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x29(%rbp)
jle    114e <recv_full+0x3e>
cmpq   $0x0,-0x28(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    115e <recv_full+0x4e>
jmp    1194 <recv_full+0x84>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1133 <recv_full+0x23>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_full>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rsi,%rbx
test   %rdx,%rdx
jle    114c <recv_full+0x3c>
mov    %rdx,%r15
mov    %edi,%ebp
mov    %r14,%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
add    %rax,%rbx
sub    %rax,%r15
jle    114c <recv_full+0x3c>
test   %rax,%rax
jg     1130 <recv_full+0x20>
sub    %r14,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @recv_full(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
store i8* %9, i8** %7
store i64 1, i64* %8
br label %10
10: ; preds = %23, %3
%11 = load i64, i64* %6
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i64, i64* %8
%15 = icmp sgt i64 %14, 0
br label %16
16: ; preds = %13, %10
%17 = phi i1 [ false, %10 ], [ %15, %13 ]
br i1 %17, label %18, label %30
18: ; preds = %16
%19 = load i32, i32* %4
%20 = load i8*, i8** %7
%21 = load i64, i64* %6
%22 = call i64 @recv(i32 %19, i8* %20, i64 %21, i32 0)
store i64 %22, i64* %8
br label %23
23: ; preds = %18
%24 = load i64, i64* %8
%25 = load i64, i64* %6
%26 = sub nsw i64 %25, %24
store i64 %26, i64* %6
%27 = load i64, i64* %8
%28 = load i8*, i8** %7
%29 = getelementptr inbounds i8, i8* %28, i64 %27
store i8* %29, i8** %7
br label %10
30: ; preds = %16
%31 = load i8*, i8** %7
%32 = load i8*, i8** %5
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
ret i64 %35
}

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