#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, unsigned char *, int, int /*<<< orphan*/);
int TCP_Receive(int sock, unsigned char *buffer, int length) {
  int rcvd = 0;
  while (rcvd < length) {
    int ret = recv(sock, buffer + rcvd, length - rcvd, 0);
    if (ret <= 0)
      return -1;
    rcvd += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<TCP_Receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1177 <TCP_Receive+0x67>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1169 <TCP_Receive+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    117e <TCP_Receive+0x6e>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <TCP_Receive+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TCP_Receive>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r12d
xor    %r14d,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
cmp    %ebp,%ebx
jge    1154 <TCP_Receive+0x44>
movslq %ebx,%rsi
add    %r15,%rsi
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r12d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
add    %eax,%ebx
test   %eax,%eax
jg     1130 <TCP_Receive+0x20>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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