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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int recv(int, int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
ssize_t read_block(int fd, uint8_t *dst, size_t len) {
  uint8_t *p = dst;
  ssize_t ret;
  while (len > 0) {
    if ((ret = recv(fd, p, len, 0)) <= 0) {
      fprintf(stderr, "recv() returned %zd\n", ret);
      return -1;
    }
    p += ret;
    len -= ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11b1 <read_block+0x91>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     1188 <read_block+0x68>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe89(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <read_block+0x98>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
movslq -0x24(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    113b <read_block+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_block>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     115e <read_block+0x3e>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1162 <read_block+0x42>
mov    %eax,%eax
lea    (%rbx,%rax,4),%rbx
sub    %rax,%r14
jne    1140 <read_block+0x20>
xor    %eax,%eax
jmp    117e <read_block+0x5e>
mov    0x2e77(%rip),%rcx
mov    (%rcx),%edi
lea    0xe8e(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"recv() returned %zd\0A\00"
define i32 @read_block(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
%10 = load i32*, i32** %6
store i32* %10, i32** %8
br label %11
11: ; preds = %24, %3
%12 = load i64, i64* %7
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %33
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i32*, i32** %8
%17 = load i64, i64* %7
%18 = call i32 @recv(i32 %15, i32* %16, i64 %17, i32 0)
store i32 %18, i32* %9
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = load i32, i32* %9
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 -1, i32* %4
br label %34
24: ; preds = %14
%25 = load i32, i32* %9
%26 = load i32*, i32** %8
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32* %28, i32** %8
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = load i64, i64* %7
%32 = sub i64 %31, %30
store i64 %32, i64* %7
br label %11
33: ; preds = %11
store i32 0, i32* %4
br label %34
34: ; preds = %33, %20
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"recv() returned %zd\0A\00"
define i32 @read_block(i32 %0, i32* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %18, label %5
5: ; preds = %3, %13
%6 = phi i32* [ %15, %13 ], [ %1, %3 ]
%7 = phi i64 [ %16, %13 ], [ %2, %3 ]
%8 = tail call i32 @recv(i32 %0, i32* %6, i64 %7, i32 0)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %18
13: ; preds = %5
%14 = zext i32 %8 to i64
%15 = getelementptr inbounds i32, i32* %6, i64 %14
%16 = sub i64 %7, %14
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %5
18: ; preds = %13, %3, %10
%19 = phi i32 [ -1, %10 ], [ 0, %3 ], [ 0, %13 ]
ret i32 %19
}