// File: /workspace/github_cpp_makefile_star2_O2/zkl94[P]distributed-key-value-store/MP2Node.cpp
// Function: MP2Node::checkMessages()
void MP2Node::checkMessages() {
    char *data;
    int size;

    /*
     * Declare your local variables here
     */

    // dequeue all messages and handle them
    while ( !memberNode->mp2q.empty() ) {
        /*
         * Pop a message from the queue
         */
        data = (char *)memberNode->mp2q.front().elt;
        size = memberNode->mp2q.front().size;
        memberNode->mp2q.pop();

        string message(data, data + size);

        /*
         * Handle the message types here
         */

        Message *msg = new Message(message);
        int transID = msg->transID;
        ReplicaType replica = msg->replica;
        string key = msg->key;
        string value = msg->value;
        Address fromAddr = msg->fromAddr;
        MessageType t = msg->type;
        bool success = msg->success;

        // MessageType {CREATE, READ, UPDATE, DELETE, REPLY, READREPLY};
        switch(msg->type) {
        case CREATE:
            // cout << "received a CREATE message" << endl;
            createKeyValue(fromAddr, key, value, replica, transID, t);
            break;
        case READ:
            readKey(fromAddr, key, transID, t);
            break;
        case UPDATE:
            updateKeyValue(fromAddr, key, value, replica, transID, t);
            break;
        case DELETE:
            cout << "received a DELETE message" << endl;
            deletekey(fromAddr, key, transID, t);
            break;
        case REPLY:
            handleReply(transID, success);
            break;
        case READREPLY:
            handleReadReply(value, transID);
            break;
        }
    }

    /*
     * This function should also ensure all READ and UPDATE operation
     * get QUORUM replies
     */

#define PROPOGATE_ITERATION_LIMIT 5

    std::map<int, Bundle *>::iterator it = this->transID2Bundle.begin();

    while(it != this->transID2Bundle.end()) {
        if (((it->second->success_responses + it->second->failure_responses) == 3) || (it->second->iteration >= PROPOGATE_ITERATION_LIMIT)) {
            int transID = it->first;
            MessageType t = it->second->t;
            string key = it->second->key;
            string value = it->second->value;

            // static char s[1024];
            // sprintf(s, "successful response %d\n", it->second->success_responses);
            // log->LOG(&memberNode->addr, s);
            // sprintf(s, "failure response %d\n", it->second->failure_responses);
            // log->LOG(&memberNode->addr, s);

            // 2 (out of 3) is quorum
            if (it->second->success_responses >= 2) {
                if (t == READ) {
                    // read
                    // @TODO: need to aggregate read return values, can't just use handleReply
                    log->logReadSuccess(&(this->memberNode->addr), true, transID, key, value);
                } else if (t == UPDATE) {
                    log->logUpdateSuccess(&(this->memberNode->addr), true, transID, key, value);
                } else if (t == CREATE) {
                    log->logCreateSuccess(&(this->memberNode->addr), true, transID, key, value);
                } else if (t == DELETE) {
                    log->logDeleteSuccess(&(this->memberNode->addr), true, transID, key);
                }
            } else {
                if (t == READ) {
                    // read
                    log->logReadFail(&(this->memberNode->addr), true, transID, key);
                } else if (t == UPDATE) {
                    log->logUpdateFail(&(this->memberNode->addr), true, transID, key, value);
                } else if (t == CREATE) {
                    log->logCreateFail(&(this->memberNode->addr), true, transID, key, value);
                } else if (t == DELETE) {
                    log->logDeleteFail(&(this->memberNode->addr), true, transID, key);
                }
            }

            delete it->second;
            it = this->transID2Bundle.erase(it);
        } else {
            it->second->iteration++;
            ++it;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x368, %rsp            # imm = 0x368
movq %rdi, %rbx
leaq 0x308(%rsp), %r14
leaq 0x2e8(%rsp), %r12
movq 0x50(%rbx), %rdi
movq 0xa8(%rdi), %rax
cmpq %rax, 0xc8(%rdi)
je 0xddb9
addq $0x98, %rdi
movq (%rax), %r15
movslq 0x8(%rax), %rbp
callq 0x5230
addq %r15, %rbp
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xe8c4
pushq $0x78
popq %rdi
callq 0x3260
movq %rax, %rbp
leaq 0x328(%rsp), %r15
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
movq %rbp, %rdi
movq %r15, %rsi
callq 0x10824
movq %r15, %rdi
callq 0x3110
movl 0x50(%rbp), %r15d
leaq 0x8(%rbp), %rsi
leaq 0x40(%rsp), %rdi
callq 0x3340
leaq 0x28(%rbp), %rsi
leaq 0x88(%rsp), %rdi
callq 0x3340
movq %rbp, %rsi
addq $0x48, %rsi
leaq 0x1a(%rsp), %rdi
callq 0x9f14
movl (%rbp), %eax
cmpq $0x5, %rax
ja 0xdd93
leaq 0x5035(%rip), %rcx      # 0x12c14
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x72(%rsp), %rdi
leaq 0x1a(%rsp), %rsi
callq 0x9f14
leaq 0x228(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
movq %r14, %r13
movq %r14, %rdi
leaq 0x88(%rsp), %rsi
callq 0x3340
movq %rbx, %rdi
leaq 0x72(%rsp), %rsi
leaq 0x228(%rsp), %rbp
movq %rbp, %rdx
movq %r13, %rcx
movl %r15d, %r9d
callq 0xd2ae
movq %r13, %rdi
jmp 0xdd59
movzbl 0x54(%rbp), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0xd21e
jmp 0xdd93
leaq 0x66(%rsp), %rdi
leaq 0x1a(%rsp), %rsi
callq 0x9f14
leaq 0x1e8(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
movq %r12, %rbp
movq %r12, %rdi
leaq 0x88(%rsp), %rsi
callq 0x3340
movq %rbx, %rdi
leaq 0x66(%rsp), %rsi
leaq 0x1e8(%rsp), %r13
movq %r13, %rdx
movq %rbp, %rcx
movl %r15d, %r9d
callq 0xd6a6
movq %rbp, %rdi
movq %r13, %rbp
jmp 0xdd59
movq 0xd30e(%rip), %rdi      # 0x1afc8
leaq 0x4f8b(%rip), %rsi      # 0x12c4c
callq 0x3240
movq %rax, %rdi
callq 0x3130
leaq 0x60(%rsp), %rdi
leaq 0x1a(%rsp), %rsi
callq 0x9f14
leaq 0x1c8(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
leaq 0x1c8(%rsp), %rbp
movq %rbp, %rdx
movl %r15d, %ecx
callq 0xd936
jmp 0xdd8b
leaq 0x6c(%rsp), %rdi
leaq 0x1a(%rsp), %rsi
callq 0x9f14
leaq 0x208(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
leaq 0x348(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x6c(%rsp), %rdx
leaq 0x208(%rsp), %r13
movq %r13, %rcx
movl %r15d, %r8d
callq 0xd458
movq %rbp, %rdi
movq %r13, %rbp
callq 0x3110
jmp 0xdd8b
leaq 0x1a8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3340
movq %rbx, %rdi
leaq 0x1a8(%rsp), %rbp
movq %rbp, %rsi
movl %r15d, %edx
callq 0xd250
movq %rbp, %rdi
callq 0x3110
leaq 0x88(%rsp), %rdi
callq 0x3110
leaq 0x40(%rsp), %rdi
callq 0x3110
leaq 0x20(%rsp), %rdi
callq 0x3110
jmp 0xdb1c
leaq 0x90(%rbx), %rax
movq %rax, 0x78(%rsp)
movq 0xa8(%rbx), %rbp
movq %rbx, 0x10(%rsp)
leaq 0x98(%rbx), %rax
movq %rax, 0x80(%rsp)
leaq 0x288(%rsp), %rbx
cmpq 0x80(%rsp), %rbp
je 0xe173
movq 0x28(%rbp), %rsi
movl 0x4(%rsi), %eax
addl (%rsi), %eax
cmpl $0x3, %eax
je 0xde10
movl 0x50(%rsi), %eax
cmpl $0x5, %eax
jl 0xdf2c
movl 0x20(%rbp), %r13d
movl 0x8(%rsi), %r12d
addq $0x10, %rsi
leaq 0x20(%rsp), %rdi
callq 0x3340
movq 0x28(%rbp), %rsi
addq $0x30, %rsi
leaq 0x40(%rsp), %rdi
callq 0x3340
movq 0x28(%rbp), %r14
cmpl $0x2, (%r14)
movl %r12d, %eax
jl 0xdeb6
cmpl $0x3, %r12d
ja 0xe0ee
leaq 0x4de6(%rip), %rcx      # 0x12c3c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0x148(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
movq %rbx, %r15
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0x148(%rsp), %r14
movq %r14, %r8
movq %r15, %r9
callq 0x97ee
jmp 0xe0d5
cmpl $0x3, %r12d
ja 0xe0ee
leaq 0x4d65(%rip), %rcx      # 0x12c2c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0xc8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
leaq 0x248(%rsp), %r15
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0xc8(%rsp), %r14
movq %r14, %r8
movq %r15, %r9
callq 0x9ae0
jmp 0xe0d5
incl %eax
movl %eax, 0x50(%rsi)
movq %rbp, %rdi
callq 0x31a0
movq %rax, %rbp
jmp 0xdde8
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0x168(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
leaq 0x2a8(%rsp), %r15
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0x168(%rsp), %r14
movq %r14, %r8
movq %r15, %r9
callq 0x9972
jmp 0xe0d5
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0xe8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
leaq 0x268(%rsp), %r15
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0xe8(%rsp), %r14
movq %r14, %r8
movq %r15, %r9
callq 0x9c4e
jmp 0xe0d5
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0x128(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0x128(%rsp), %r14
movq %r14, %r8
callq 0x9a34
jmp 0xe0dd
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0xa8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0xa8(%rsp), %r14
movq %r14, %r8
callq 0x9d10
jmp 0xe0dd
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0x188(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
leaq 0x2c8(%rsp), %r15
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0x188(%rsp), %r14
movq %r14, %r8
movq %r15, %r9
callq 0x98b0
movq %r15, %rdi
callq 0x3110
movq %r14, %rdi
callq 0x3110
movq 0x28(%rbp), %r14
testq %r14, %r14
je 0xe0f8
movq %r14, %rdi
callq 0xe776
jmp 0xe0fb
xorl %r14d, %r14d
movq %r14, %rdi
pushq $0x58
popq %rsi
callq 0x3270
movq 0x78(%rsp), %rdi
movq %rbp, %rsi
callq 0xf7ce
movq %rax, %rbp
leaq 0x40(%rsp), %rdi
callq 0x3110
leaq 0x20(%rsp), %rdi
callq 0x3110
jmp 0xdde8
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r14
movq 0x68(%rax), %r12
leaq 0x108(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3340
addq $0x8, %r14
movq %r12, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movl %r13d, %ecx
leaq 0x108(%rsp), %r14
movq %r14, %r8
callq 0x9ba2
jmp 0xe0dd
addq $0x368, %rsp            # imm = 0x368
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x288(%rsp), %rdi
callq 0x3110
jmp 0xe19a
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0x2c8(%rsp), %rdi
callq 0x3110
jmp 0xe1bc
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0x2a8(%rsp), %rdi
callq 0x3110
jmp 0xe1de
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0x248(%rsp), %rdi
callq 0x3110
jmp 0xe210
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0x268(%rsp), %rdi
callq 0x3110
jmp 0xe242
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0xe30f
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
jmp 0xe2c2
movq %rax, %rbx
leaq 0x2e8(%rsp), %rdi
callq 0x3110
jmp 0xe281
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
jmp 0xe2c2
movq %rax, %rbx
leaq 0x1c8(%rsp), %rdi
jmp 0xe2c2
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
jmp 0xe2c2
movq %rax, %rbx
leaq 0x308(%rsp), %rdi
callq 0x3110
jmp 0xe2ba
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x3110
jmp 0xe307
jmp 0xe2e3
jmp 0xe2e3
jmp 0xe2ef
movq %rax, %rbx
leaq 0x328(%rsp), %rdi
callq 0x3110
jmp 0xe2f7
jmp 0xe2ef
movq %rax, %rbx
jmp 0xe314
jmp 0xe304
movq %rax, %rbx
jmp 0xe328
movq %rax, %rbx
jmp 0xe31e
movq %rax, %rbx
pushq $0x78
popq %rsi
movq %rbp, %rdi
callq 0x3270
jmp 0xe31e
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3110
leaq 0x40(%rsp), %rdi
callq 0x3110
leaq 0x20(%rsp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x3420