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

/* Variables and functions */
scalar_t__ recv(int, char *, size_t, int /*<<< orphan*/);
size_t pullFromSocket(int socket, char *buffer, size_t length) {
  ssize_t bytesReceived;
  size_t receivedLength = 0;
  while (receivedLength < length) {
    bytesReceived =
        recv(socket, buffer + receivedLength, length - receivedLength, 0);
    if (bytesReceived <= 0)
      break;
    else
      receivedLength += bytesReceived;
  }
  return receivedLength;
}


x86 assembly (clang, -O0):
<pullFromSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1178 <pullFromSocket+0x68>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     1167 <pullFromSocket+0x57>
jmp    1178 <pullFromSocket+0x68>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    112b <pullFromSocket+0x1b>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pullFromSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     1152 <pullFromSocket+0x42>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    1154 <pullFromSocket+0x44>
add    %rax,%rbx
cmp    %r14,%rbx
jb     1130 <pullFromSocket+0x20>
jmp    1154 <pullFromSocket+0x44>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @pullFromSocket(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %8
br label %9
9: ; preds = %29, %3
%10 = load i64, i64* %8
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %30
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i64, i64* %8
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i64, i64* %6
%19 = load i64, i64* %8
%20 = sub i64 %18, %19
%21 = call i64 @recv(i32 %14, i8* %17, i64 %20, i32 0)
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = icmp sle i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %13
br label %30
25: ; preds = %13
%26 = load i64, i64* %7
%27 = load i64, i64* %8
%28 = add i64 %27, %26
store i64 %28, i64* %8
br label %29
29: ; preds = %25
br label %9
30: ; preds = %24, %9
%31 = load i64, i64* %8
ret i64 %31
}

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