// File: tonymuu[P]distributed-kv-store/MP1Node.cpp
// Function: MP1Node::deserializeAndUpdateMemberList(char*, int)
void MP1Node::deserializeAndUpdateMemberList(char *data, int size) {
    // map of port, heartbeat, timestamp
    auto map = createMemberlistMap();

    auto currTime = par->getcurrtime();

    int mleSize = sizeof(int) + sizeof(short) + sizeof(long) + sizeof(long);
    int numEntries = (size - sizeof(MessageHdr)) / mleSize;
    int offset = sizeof(MessageHdr);

    for (int i = 0; i < numEntries; i++) {
        // get the member list
        int id = 0;
        short port = 0;
        long heartbeat = 0;
        long timestamp = 0;

        memcpy(&id, data + offset, sizeof(int));
        offset += sizeof(int);

        memcpy(&port, data + offset, sizeof(short));
        offset += sizeof(short);

        memcpy(&heartbeat, data + offset, sizeof(long));
        offset += sizeof(long);

        memcpy(&timestamp, data + offset, sizeof(long));
        offset += sizeof(long);

        // if member doesn't exist in current member list, we add it there
//        MemberListEntry* old = getNodeFromMemberListTable(id);
        if (map.count(id) == 0) {
//        if (old == nullptr) {
            MemberListEntry entry = MemberListEntry(id, port, heartbeat, currTime);
            memberNode->memberList.push_back(entry);

            // log the new mle join
            Address addr = createAddressFromIdAndPort(id, port);
            log->logNodeAdd(&memberNode->addr, &addr);
        } else {
            // update the existing member entry
            auto old = map[id];
            if (old->heartbeat < heartbeat) {
                if (failed.count(id) > 0) {
                    failed.erase(id);
                }
                old->setheartbeat(heartbeat);
                old->settimestamp(currTime);
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rsi
movq %rsi, -0xc0(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x6c70
movq -0xc0(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x18c60
movl %eax, -0xb4(%rbp)
jmp 0x66f2
movl -0xb4(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x16, -0x60(%rbp)
movslq -0x14(%rbp), %rax
subq $0x4, %rax
movslq -0x60(%rbp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, -0x64(%rbp)
movl $0x4, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0x691c
movl $0x0, -0x70(%rbp)
movw $0x0, -0x72(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx), %eax
movl %eax, -0x70(%rbp)
movl -0x68(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
movw (%rax,%rcx), %ax
movw %ax, -0x72(%rbp)
movl -0x68(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx), %rax
movq %rax, -0x80(%rbp)
movl -0x68(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx), %rax
movq %rax, -0x88(%rbp)
movl -0x68(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x68(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x75e0
movq %rax, -0xc8(%rbp)
jmp 0x67cb
movq -0xc8(%rbp), %rax
cmpq $0x0, %rax
jne 0x6872
movl -0x70(%rbp), %esi
movswl -0x72(%rbp), %edx
movq -0x80(%rbp), %rcx
movslq -0x4c(%rbp), %r8
leaq -0xa0(%rbp), %rdi
callq 0x18f20
jmp 0x67f9
movq -0xc0(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x7210
jmp 0x6816
movq -0xc0(%rbp), %rsi
movl -0x70(%rbp), %edx
movswl -0x72(%rbp), %ecx
leaq -0xa6(%rbp), %rdi
callq 0x6b30
jmp 0x6832
movq -0xc0(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
leaq -0xa6(%rbp), %rdx
callq 0x17ea0
jmp 0x6853
jmp 0x690c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x7720
jmp 0x692e
leaq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x7640
movq %rax, -0xd0(%rbp)
jmp 0x6888
movq -0xd0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x80(%rbp), %rax
jge 0x690a
leaq 0x37aef(%rip), %rdi     # 0x3e3a0
leaq -0x70(%rbp), %rsi
callq 0x7480
movq %rax, -0xd8(%rbp)
jmp 0x68c3
movq -0xd8(%rbp), %rax
cmpq $0x0, %rax
jbe 0x68e4
leaq 0x37ac9(%rip), %rdi     # 0x3e3a0
leaq -0x70(%rbp), %rsi
callq 0x7590
jmp 0x68e2
jmp 0x68e4
movq -0xb0(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x19100
jmp 0x68f6
movq -0xb0(%rbp), %rdi
movslq -0x4c(%rbp), %rsi
callq 0x19120
jmp 0x6908
jmp 0x690a
jmp 0x690c
jmp 0x690e
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x6724
leaq -0x48(%rbp), %rdi
callq 0x7720
addq $0xe0, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x45b0
nopw (%rax,%rax)