// File: awbrenn[P]robot-controller/Client.cpp
// Function: recieveAckFromMiddleware()
void recieveAckFromMiddleware() 
{
  uint32_t *id = (uint32_t *) malloc(sizeof(uint32_t));
  uint32_t *total_messages = (uint32_t *) malloc(sizeof(uint32_t));
  uint32_t *sequence_number = (uint32_t *) malloc(sizeof(uint32_t));
  vector<string> fragmented_response;
  int recvMsgSize;

  cout << "\nGOT HERE\n" << endl;

  clntAddrLen = sizeof(clntAddr);

  do {
    string response_chunk;

    if ((recvMsgSize = recvfrom(sock, recvBuffer, UDP_PACKET_MAX_SIZE, 0, 
       (struct sockaddr *) &clntAddr, &clntAddrLen )) < 0 )
         cout << ("error on recvFrom") << endl;

    //Check for timeout
    if (recvMsgSize == -1) 
       if (errno == EINTR)
       {
          cout << "Timeout occured. Program Ending." << endl;
          exit(0);
       }

    recvBuffer[recvMsgSize] = '\0';

    cout << "\nmessageLen: " << recvMsgSize << endl;
    memcpy(id, recvBuffer, 4);
    memcpy(total_messages, recvBuffer + 4, 4);
    memcpy(sequence_number, recvBuffer + 8, 4);

    cout << "\nMessage ID: " << *id << endl;
    cout << "\nTotal Messages: " << *total_messages << endl;
    cout << "\nSequence Number: " << *sequence_number << endl;

    response_chunk.assign(recvBuffer + 12, recvMsgSize - 11);
    fragmented_response.push_back(response_chunk);
    //printf("%s\n\n", recvBuffer + 12);
 } while (*sequence_number < (*total_messages - 1));

  alarm(0);

  PROXY_RESPONSE = "";

  for (int i = 0; i < (int)fragmented_response.size(); i++) {
        PROXY_RESPONSE += fragmented_response.at(i);
  }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x4, %edi
movq %rdi, -0x80(%rbp)
callq 0x22f0
movq -0x80(%rbp), %rdi
movq %rax, -0x8(%rbp)
callq 0x22f0
movq -0x80(%rbp), %rdi
movq %rax, -0x10(%rbp)
callq 0x22f0
movq %rax, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3fa0
movq 0x5789(%rip), %rdi      # 0x8fd0
leaq 0x290f(%rip), %rsi      # 0x615d
callq 0x21f0
movq %rax, -0x78(%rbp)
jmp 0x3859
movq -0x78(%rbp), %rdi
movq 0x5754(%rip), %rsi      # 0x8fb8
callq 0x2230
jmp 0x386b
movl $0x10, 0x5a63(%rip)     # 0x92d8
leaq -0x68(%rbp), %rdi
callq 0x2330
movl 0x5a24(%rip), %edi      # 0x92a8
leaq 0x5a65(%rip), %rsi      # 0x92f0
leaq 0x5a2a(%rip), %r8       # 0x92bc
leaq 0x5a3f(%rip), %r9       # 0x92d8
movl $0x3e8, %edx            # imm = 0x3E8
xorl %ecx, %ecx
callq 0x2180
movq %rax, -0x88(%rbp)
jmp 0x38ae
movq -0x88(%rbp), %rax
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jge 0x391b
movq 0x570c(%rip), %rdi      # 0x8fd0
leaq 0x289d(%rip), %rsi      # 0x6168
callq 0x21f0
movq %rax, -0x90(%rbp)
jmp 0x38d9
movq -0x90(%rbp), %rdi
movq 0x56d1(%rip), %rsi      # 0x8fb8
callq 0x2230
jmp 0x38ee
jmp 0x391b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3b9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x2448
jmp 0x3b9b
cmpl $-0x1, -0x34(%rbp)
jne 0x3965
callq 0x2040
cmpl $0x4, (%rax)
jne 0x3963
movq 0x569e(%rip), %rdi      # 0x8fd0
leaq 0x2841(%rip), %rsi      # 0x617a
callq 0x21f0
movq %rax, -0x98(%rbp)
jmp 0x3947
movq -0x98(%rbp), %rdi
movq 0x5663(%rip), %rsi      # 0x8fb8
callq 0x2230
jmp 0x395c
xorl %edi, %edi
callq 0x22a0
jmp 0x3965
movslq -0x34(%rbp), %rax
leaq 0x5980(%rip), %rcx      # 0x92f0
movb $0x0, (%rax,%rcx)
movq 0x5655(%rip), %rdi      # 0x8fd0
leaq 0x2819(%rip), %rsi      # 0x619b
callq 0x21f0
movq %rax, -0xa0(%rbp)
jmp 0x3990
movq -0xa0(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x23c0
movq %rax, -0xa8(%rbp)
jmp 0x39a8
movq -0xa8(%rbp), %rdi
movq 0x5602(%rip), %rsi      # 0x8fb8
callq 0x2230
jmp 0x39bd
movq -0x8(%rbp), %rax
movl 0x5929(%rip), %ecx      # 0x92f0
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl 0x5921(%rip), %ecx      # 0x92f4
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl 0x5919(%rip), %ecx      # 0x92f8
movl %ecx, (%rax)
movq 0x55e8(%rip), %rdi      # 0x8fd0
leaq 0x27ba(%rip), %rsi      # 0x61a9
callq 0x21f0
movq %rax, -0xb0(%rbp)
jmp 0x39fd
movq -0xb0(%rbp), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %esi
callq 0x2300
movq %rax, -0xb8(%rbp)
jmp 0x3a18
movq -0xb8(%rbp), %rdi
movq 0x5592(%rip), %rsi      # 0x8fb8
callq 0x2230
jmp 0x3a2d
movq 0x559c(%rip), %rdi      # 0x8fd0
leaq 0x277c(%rip), %rsi      # 0x61b7
callq 0x21f0
movq %rax, -0xc0(%rbp)
jmp 0x3a49
movq -0xc0(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
callq 0x2300
movq %rax, -0xc8(%rbp)
jmp 0x3a64
movq -0xc8(%rbp), %rdi
movq 0x5546(%rip), %rsi      # 0x8fb8
callq 0x2230
jmp 0x3a79
movq 0x5550(%rip), %rdi      # 0x8fd0
leaq 0x2742(%rip), %rsi      # 0x61c9
callq 0x21f0
movq %rax, -0xd0(%rbp)
jmp 0x3a95
movq -0xd0(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0x2300
movq %rax, -0xd8(%rbp)
jmp 0x3ab0
movq -0xd8(%rbp), %rdi
movq 0x54fa(%rip), %rsi      # 0x8fb8
callq 0x2230
jmp 0x3ac5
movl -0x34(%rbp), %eax
addl $-0xb, %eax
movslq %eax, %rdx
leaq 0x5827(%rip), %rsi      # 0x92fc
leaq -0x68(%rbp), %rdi
callq 0x2310
jmp 0x3ae0
leaq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x4250
jmp 0x3aef
leaq -0x68(%rbp), %rdi
callq 0x2448
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jb 0x3875
xorl %edi, %edi
callq 0x2400
leaq 0x573b(%rip), %rdi      # 0x9258
leaq 0x27c5(%rip), %rsi      # 0x62e9
callq 0x2320
jmp 0x3b2b
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0xdc(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4190
movq %rax, %rcx
movl -0xdc(%rbp), %eax
cmpl %ecx, %eax
jge 0x3b89
movslq -0x6c(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x40e0
movq %rax, -0xe8(%rbp)
jmp 0x3b67
movq -0xe8(%rbp), %rsi
leaq 0x56e3(%rip), %rdi      # 0x9258
callq 0x22e0
jmp 0x3b7c
jmp 0x3b7e
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3b32
leaq -0x30(%rbp), %rdi
callq 0x3fc0
addq $0xf0, %rsp
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x3fc0
movq -0x40(%rbp), %rdi
callq 0x23d0
nopl (%rax)