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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int recv(int, void *, int, int /*<<< orphan*/);
ssize_t readallfd(int sock, void *ptr, size_t len) {
  ssize_t ret, rd;
  rd = 0;
  while ((size_t)rd < len) {
    ret = recv(sock, ptr + rd, len - rd, 0);
    if (ret == 0) {
      ;
      return -1;
    } else if (ret == -1) {
      ;
      if (errno == EINTR) {
        ;
        continue;
      } else {
        return -1;
      }
    }
    rd += ret;
  }
  return rd;
}


x86 assembly (clang, -O0):
<readallfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11bb <readallfd+0xab>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1173 <readallfd+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <readallfd+0xb1>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a8 <readallfd+0x98>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
jne    119c <readallfd+0x8c>
jmp    112a <readallfd+0x1a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <readallfd+0xb1>
jmp    11ad <readallfd+0x9d>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    112a <readallfd+0x1a>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readallfd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     119b <readallfd+0x8b>
mov    %edi,%r12d
xor    %r13d,%r13d
mov    0x2eb0(%rip),%r14
mov    0x2ea1(%rip),%r15
mov    %rdx,0x10(%rsp)
mov    %edx,%ebp
mov    %rsi,0x8(%rsp)
mov    %rsi,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
jne    1175 <readallfd+0x65>
mov    (%r14),%rcx
cmp    (%r15),%rcx
je     1150 <readallfd+0x40>
jmp    119d <readallfd+0x8d>
test   %ecx,%ecx
je     119d <readallfd+0x8d>
add    %ecx,%r13d
movslq %r13d,%rcx
mov    0x8(%rsp),%rax
lea    (%rax,%rcx,1),%rbx
mov    0x10(%rsp),%rdx
mov    %edx,%ebp
sub    %ecx,%ebp
mov    %r13d,%eax
cmp    %rdx,%rcx
jb     1150 <readallfd+0x40>
jmp    119d <readallfd+0x8d>
xor    %eax,%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
define i32 @readallfd(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %9
br label %10
10: ; preds = %40, %37, %3
%11 = load i32, i32* %9
%12 = sext i32 %11 to i64
%13 = load i64, i64* %7
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %44
15: ; preds = %10
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr i8, i8* %17, i64 %19
%21 = load i64, i64* %7
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = sub i64 %21, %23
%25 = trunc i64 %24 to i32
%26 = call i32 @recv(i32 %16, i8* %20, i32 %25, i32 0)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %15
store i32 -1, i32* %4
br label %46
30: ; preds = %15
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %10
38: ; preds = %33
store i32 -1, i32* %4
br label %46
39: ; preds = %30
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = load i32, i32* %9
%43 = add nsw i32 %42, %41
store i32 %43, i32* %9
br label %10
44: ; preds = %10
%45 = load i32, i32* %9
store i32 %45, i32* %4
br label %46
46: ; preds = %44, %38, %29
%47 = load i32, i32* %4
ret i32 %47
}

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