// File: vianna274[P]dropbox/src/WrapperSocket.cpp
// Function: Dropbox::WrapperSocket::receive(int)
MessageData* WrapperSocket::receive(int timeout) {
  if(timeout == TIMEOUT_ON) {
    struct pollfd fd;
    fd.fd = this->localSocketHandler;
    fd.events = POLLIN;
    int ret = poll(&fd, 1, 2000);
    switch(ret) {
      case -1:
        return NULL;
      case 0: printf("Timeout\n");
        return NULL;
      default: break;
    }
  }

  char* msg = new char[PACKET_LEN];
  memset(msg, 0, PACKET_LEN);
  bool receivedCorrectly = false;
  MessageData * data;
  while(!receivedCorrectly) {

    int msgSize = recvfrom(this->localSocketHandler, (void *) msg, 
      PACKET_LEN, 0, (struct sockaddr *) &this->remoteSocketAddr, &this->remoteSocketLen);
    if (msgSize < 0) {
      fprintf(stderr, "Error on receiving\n");
      exit(1);
    }

    data = (MessageData *) msg;

    if (data->type != TYPE_ACK && this->lastData != NULL && this->isEqual(this->lastData, data))
      continue;
    if (data->socketSeq == -1)
      break;
    if (data->socketSeq == 0)
      this->socketSeq = 0;
    if (data->socketSeq == this->socketSeq && data->type != TYPE_ACK) {
      MessageData message = make_packet(TYPE_ACK, data->seq, 1, -1, "");
      this->sendAck(&message);
      this->socketSeq = data->socketSeq + 1;
      break;
    }
  }

  this->lastData = data;
  // TODO delete msg
  return data;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1020, %rsp           # imm = 0x1020
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1018(%rbp)
cmpl $0x1, -0x14(%rbp)
jne 0x18123
movq -0x1018(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
movw $0x1, -0x18(%rbp)
leaq -0x1c(%rbp), %rdi
movl $0x1, %esi
movl $0x7d0, %edx            # imm = 0x7D0
callq 0x3530
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x101c(%rbp)
subl $-0x1, %eax
je 0x180f7
jmp 0x180eb
movl -0x101c(%rbp), %eax
testl %eax, %eax
je 0x18104
jmp 0x1811f
movq $0x0, -0x8(%rbp)
jmp 0x18290
leaq 0x152b(%rip), %rdi      # 0x19636
movb $0x0, %al
callq 0x3080
movq $0x0, -0x8(%rbp)
jmp 0x18290
jmp 0x18121
jmp 0x18123
movl $0x1000, %edi           # imm = 0x1000
callq 0x3030
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3240
movb $0x0, -0x29(%rbp)
movb -0x29(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x18153
jmp 0x18279
movq -0x1018(%rbp), %r9
movl (%r9), %edi
movq -0x28(%rbp), %rsi
movq %r9, %r8
addq $0x20, %r8
addq $0x40, %r9
movl $0x1000, %edx           # imm = 0x1000
xorl %ecx, %ecx
callq 0x32d0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x181a3
movq 0xae48(%rip), %rax      # 0x22fd0
movq (%rax), %rdi
leaq 0x14ad(%rip), %rsi      # 0x1963f
movb $0x0, %al
callq 0x34b0
movl $0x1, %edi
callq 0x3420
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpl $0x1, (%rax)
je 0x181e1
movq -0x1018(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x181e1
movq -0x1018(%rbp), %rdi
movq 0x10(%rdi), %rsi
movq -0x38(%rbp), %rdx
callq 0x182a0
testb $0x1, %al
jne 0x181dc
jmp 0x181e1
jmp 0x18145
movq -0x38(%rbp), %rax
cmpl $-0x1, 0x10(%rax)
jne 0x181f0
jmp 0x18279
movq -0x38(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x18208
movq -0x1018(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x1018(%rbp), %rcx
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
cmpl 0x8(%rcx), %eax
jne 0x18274
movq -0x38(%rbp), %rax
cmpl $0x1, (%rax)
je 0x18274
movq -0x38(%rbp), %rax
movl 0x4(%rax), %edx
leaq -0x1010(%rbp), %rdi
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
leaq 0x140e(%rip), %r9       # 0x19652
movl %ecx, %esi
callq 0x97a0
movq -0x1018(%rbp), %rdi
leaq -0x1010(%rbp), %rsi
callq 0x18580
movq -0x1018(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
addl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x18279
jmp 0x18145
movq -0x1018(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1020, %rsp           # imm = 0x1020
popq %rbp
retq
nopl (%rax)