#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 recv(int, char *, int, int /*<<< orphan*/);
int htxd_receive_bytes(int new_fd, char *receive_buffer, int receive_length) {
  int received_bytes;
  int remaining_bytes;
  remaining_bytes = receive_length;
  while (remaining_bytes > 0) {
    received_bytes = recv(new_fd, receive_buffer, remaining_bytes, 0);
    if (received_bytes == -1) {
      return -1;
    }
    if (received_bytes == 0) {
      break;
    }
    remaining_bytes -= received_bytes;
    receive_buffer += received_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    118c <htxd_receive_bytes+0x7c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    115c <htxd_receive_bytes+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <htxd_receive_bytes+0x85>
cmpl   $0x0,-0x18(%rbp)
jne    116b <htxd_receive_bytes+0x5b>
jmp    118c <htxd_receive_bytes+0x7c>
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   %rbx
push   %rax
mov    %edx,%r14d
mov    %edx,%ebp
test   %edx,%edx
jle    1152 <htxd_receive_bytes+0x42>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %r14d,%ebp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1157 <htxd_receive_bytes+0x47>
test   %eax,%eax
je     1152 <htxd_receive_bytes+0x42>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1130 <htxd_receive_bytes+0x20>
sub    %ebp,%r14d
jmp    115a <htxd_receive_bytes+0x4a>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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