// File: plasma-umass[P]simplesocket/simplesocket.cpp
// Function: simplesocket::recvNBytes(void*, int, bool)
ssize_t simplesocket::recvNBytes(void* data, int size, bool lessOk){
  if (size<=0) return 0;
  int ret, nRecv = 0;
  int toRecv = size;
  do{
    if (_timeout > 0){
      ret = descriptorReady (_socketfd, _timeout);
      if (ret < 0){
	assert(errno > 0 && errno!=EINTR);
	close();
	return -errno;
      } else if (ret == 0) return (ssize_t)TIMEOUT_ERR;
    }
    if (_debug) cerr << "Descriptor ready, reading .. " << endl;
    ret = recv(_socketfd, data, toRecv, 0);
    if (_debug) cerr << "Recv returned error of .. " << ret << endl;
    if (ret<0 && errno == EINTR) continue;
    else if (ret<0) {
      assert(errno > 0);
      close();
      return -errno;
    } else if (ret == 0)  {
      _seenEof = true; 
      close();
      /* Close your side, abort connection */
      break; /* Other side send a fin, tell caller */
      /* Break with whatever data we could receive */
    } else {
      data = (void*) ((unsigned long) data + ret);
      toRecv -= ret;
      nRecv += ret;
    }
  } while(toRecv > 0 && !lessOk);
  return nRecv;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %cl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
andb $0x1, %al
movb %al, -0x1d(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x1c(%rbp)
jg 0x4995
movq $0x0, -0x8(%rbp)
jmp 0x4b9b
movl $0x0, -0x28(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x0, 0x1d8(%rax)
jle 0x4a4f
movq -0x38(%rbp), %rdi
movl 0x10(%rdi), %esi
movl 0x1d8(%rdi), %edx
callq 0x4810
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x4a38
callq 0x3040
movq %rax, %rcx
xorl %eax, %eax
cmpl $0x0, (%rcx)
movb %al, -0x39(%rbp)
jle 0x49ee
callq 0x3040
cmpl $0x4, (%rax)
setne %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
testb $0x1, %al
jne 0x49f7
jmp 0x49f9
jmp 0x4a18
leaq 0x1745(%rip), %rdi      # 0x6145
leaq 0x1701(%rip), %rsi      # 0x6108
movl $0x42, %edx
leaq 0x174c(%rip), %rcx      # 0x615f
callq 0x3160
movq -0x38(%rbp), %rdi
callq 0x47d0
callq 0x3040
movq %rax, %rcx
xorl %eax, %eax
subl (%rcx), %eax
cltq
movq %rax, -0x8(%rbp)
jmp 0x4b9b
cmpl $0x0, -0x24(%rbp)
jne 0x4a4b
movq $-0x2, -0x8(%rbp)
jmp 0x4b9b
jmp 0x4a4d
jmp 0x4a4f
movq -0x38(%rbp), %rax
testb $0x1, 0x1dc(%rax)
je 0x4a7e
movq 0x358d(%rip), %rdi      # 0x7ff0
leaq 0x1729(%rip), %rsi      # 0x6193
callq 0x3260
movq %rax, %rdi
movq 0x353f(%rip), %rsi      # 0x7fb8
callq 0x3290
movq -0x38(%rbp), %rax
movl 0x10(%rax), %edi
movq -0x18(%rbp), %rsi
movslq -0x2c(%rbp), %rdx
xorl %ecx, %ecx
callq 0x30b0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movl %ecx, -0x24(%rbp)
testb $0x1, 0x1dc(%rax)
je 0x4ad4
movq 0x3542(%rip), %rdi      # 0x7ff0
leaq 0x16fc(%rip), %rsi      # 0x61b1
callq 0x3260
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x33e0
movq %rax, %rdi
movq 0x34e9(%rip), %rsi      # 0x7fb8
callq 0x3290
cmpl $0x0, -0x24(%rbp)
jge 0x4ae9
callq 0x3040
cmpl $0x4, (%rax)
jne 0x4ae9
jmp 0x4b75
cmpl $0x0, -0x24(%rbp)
jge 0x4b37
callq 0x3040
cmpl $0x0, (%rax)
jle 0x4afb
jmp 0x4b1a
leaq 0x16ca(%rip), %rdi      # 0x61cc
leaq 0x15ff(%rip), %rsi      # 0x6108
movl $0x4c, %edx
leaq 0x164a(%rip), %rcx      # 0x615f
callq 0x3160
movq -0x38(%rbp), %rdi
callq 0x47d0
callq 0x3040
movq %rax, %rcx
xorl %eax, %eax
subl (%rcx), %eax
cltq
movq %rax, -0x8(%rbp)
jmp 0x4b9b
cmpl $0x0, -0x24(%rbp)
jne 0x4b4c
movq -0x38(%rbp), %rdi
movb $0x1, 0x15(%rdi)
callq 0x47d0
jmp 0x4b93
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movl -0x24(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x24(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x4b71
jmp 0x4b73
jmp 0x4b75
xorl %eax, %eax
cmpl $0x0, -0x2c(%rbp)
movb %al, -0x3a(%rbp)
jle 0x4b88
movb -0x1d(%rbp), %al
xorb $-0x1, %al
movb %al, -0x3a(%rbp)
movb -0x3a(%rbp), %al
testb $0x1, %al
jne 0x49a2
movslq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)