#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 */

/* Variables and functions */
int /*<<< orphan*/ MSG_WAITALL;
int recv(int, char *, int, int /*<<< orphan*/);
int htxd_receive_bytes(int new_fd, char *receive_buffer, int receive_length) {
  int read_bytes;
  int remaining_bytes;
  remaining_bytes = receive_length;
  while (remaining_bytes > 0) {
    read_bytes = recv(new_fd, receive_buffer, remaining_bytes, MSG_WAITALL);
    if (read_bytes == -1) {
      return -1;
    }
    if (read_bytes == 0) {
      break;
    }
    remaining_bytes -= read_bytes;
    receive_buffer += read_bytes;
  }
  return (receive_length - remaining_bytes);
}


x86 assembly (clang, -O0):
<htxd_receive_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1193 <htxd_receive_bytes+0x83>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    0x2e95(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1163 <htxd_receive_bytes+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    119c <htxd_receive_bytes+0x8c>
cmpl   $0x0,-0x18(%rbp)
jne    1172 <htxd_receive_bytes+0x62>
jmp    1193 <htxd_receive_bytes+0x83>
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1128 <htxd_receive_bytes+0x18>
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htxd_receive_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %edx,%ebp
test   %edx,%edx
jle    1164 <htxd_receive_bytes+0x54>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eaa(%rip),%r12
mov    %r14d,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%ecx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1169 <htxd_receive_bytes+0x59>
test   %eax,%eax
je     1164 <htxd_receive_bytes+0x54>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1140 <htxd_receive_bytes+0x30>
sub    %ebp,%r14d
jmp    116c <htxd_receive_bytes+0x5c>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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