// File: awbrenn[P]robot-controller/Middleware.cpp
// Function: sendRobotResponseToClient()
void sendRobotResponseToClient() {
    //cout << HTTP_RESPONSE << endl; // test - remove later
    vector<string> fragmented_response;

    /* first remove the header from the HTTP response */
    removeHeaderFromHTTPResponse();

    /* then fragement the response into 1000 Byte chunks */
    if (HTTP_RESPONSE.length() > UDP_PACKET_MAX_SIZE - 12) // subtract 12 for header
        fragmented_response = fragmentHTTPResponse();
    else // only one UDP packet needed
        fragmented_response.push_back(HTTP_RESPONSE);

    /* add the header to the fragments of the response */
    fragmented_response = addHeaderToResponseToClient(fragmented_response);

    // cout << HTTP_RESPONSE.length() << endl;

    // cout << "\n\nNEW MESSASGE:" << endl;

    // for (int i = 0; i < (int)fragmented_response.size(); i++) {
    //     cout << fragmented_response.at(i) << "\n" << endl;
    // }

    for (int i = 0; i < fragmented_response.size(); ++i) {
        cout << fragmented_response.at(i) << "\n" << endl;
        sendto(PROXY_SOCKET, fragmented_response.at(i).data(), fragmented_response.at(i).length(),
               0, (struct sockaddr *) &CLIENT_ADDR, sizeof(CLIENT_ADDR));
    }

    // char * hello_world = "hello world";
    // sendto(PROXY_SOCKET, hello_world, strlen(hello_world), 0, (struct sockaddr *) &CLIENT_ADDR, sizeof(CLIENT_ADDR));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
leaq -0x18(%rbp), %rdi
callq 0x4f30
callq 0x4a50
jmp 0x413b
leaq 0x61d6(%rip), %rdi      # 0xa318
callq 0x3420
cmpq $0x3dc, %rax            # imm = 0x3DC
jbe 0x4183
leaq -0x40(%rbp), %rdi
callq 0x4ac0
jmp 0x415a
leaq -0x18(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x4f50
leaq -0x40(%rbp), %rdi
callq 0x4f80
jmp 0x4197
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x4315
leaq 0x618e(%rip), %rsi      # 0xa318
leaq -0x18(%rbp), %rdi
callq 0x4fe0
jmp 0x4195
jmp 0x4197
leaq -0x70(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x5050
jmp 0x41a6
leaq -0x58(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4be0
jmp 0x41b5
leaq -0x18(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x4f50
leaq -0x58(%rbp), %rdi
callq 0x4f80
leaq -0x70(%rbp), %rdi
callq 0x4f80
movl $0x0, -0x74(%rbp)
movslq -0x74(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x5140
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
jae 0x4303
movslq -0x74(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x5160
movq %rax, -0x88(%rbp)
jmp 0x4212
movq -0x88(%rbp), %rsi
movq 0x5db0(%rip), %rdi      # 0x9fd0
callq 0x3290
movq %rax, -0x90(%rbp)
jmp 0x422e
movq -0x90(%rbp), %rdi
leaq 0x2ef8(%rip), %rsi      # 0x7134
callq 0x32b0
movq %rax, -0x98(%rbp)
jmp 0x424a
movq -0x98(%rbp), %rdi
movq 0x5d60(%rip), %rsi      # 0x9fb8
callq 0x3300
jmp 0x425f
movl 0x6157(%rip), %eax      # 0xa3bc
movl %eax, -0xa4(%rbp)
movslq -0x74(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x5160
movq %rax, -0xa0(%rbp)
jmp 0x4281
movq -0xa0(%rbp), %rdi
callq 0x3190
movq %rax, -0xb8(%rbp)
movslq -0x74(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x5160
movq %rax, -0xb0(%rbp)
jmp 0x42aa
movq -0xb0(%rbp), %rdi
callq 0x3420
movl -0xa4(%rbp), %edi
movq -0xb8(%rbp), %rsi
movq %rax, %rdx
leaq 0x6113(%rip), %r8       # 0xa3e0
xorl %ecx, %ecx
movl $0x10, %r9d
callq 0x3050
jmp 0x42dc
jmp 0x42de
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x41db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x4f80
jmp 0x4315
leaq -0x18(%rbp), %rdi
callq 0x4f80
addq $0xc0, %rsp
popq %rbp
retq
leaq -0x18(%rbp), %rdi
callq 0x4f80
movq -0x20(%rbp), %rdi
callq 0x3490
nopw (%rax,%rax)