// File: /workspace/github_cpp_makefile_star2_O2/zkl94[P]distributed-key-value-store/MP1Node.cpp
// Function: MP1Node::nodeLoopOps()
void MP1Node::nodeLoopOps() {

    // 0. update self heartbeat and tiemstamp
    // 1. Check if any node hasn't responded within a timeout period and then delete the nodes
    // 2. Propagate your membership list to every other member (MEMBERLIST)

    // update self heartbeat
    memberNode->heartbeat++;

    // update the entry of self in self memberlist
    memberNode->memberList[0].setheartbeat(memberNode->heartbeat);
    memberNode->memberList[0].settimestamp(par->getcurrtime());

    memberNode->myPos = memberNode->memberList.begin();
    while (memberNode->myPos != memberNode->memberList.end()) {
        if(isEntryInvalid(memberNode, *memberNode->myPos)) {
            int id = memberNode->myPos->getid();
            short port = memberNode->myPos->getport();
            Address address;
            string _address = to_string(id) + ":" + to_string(port);
            address = Address(_address);

            // cout << "found invalid entry with id " << id <<endl;

            if (this->tobedeleted->count(id) == 0) {
                // may have already added to tobedeleted map
                this->tobedeleted->insert(std::make_pair(id, 0));
            }

            // can't remove it at once although we are using tobedeleted map
            // because we still have to send broadcast to it
            // memberNode->myPos = memberNode->memberList.erase(memberNode->myPos);
        }
        ++memberNode->myPos;
    }

    this->updateToBeDeletedMap();

    // construct MEMBERLIST message
    int entry_num = memberNode->memberList.size();
    int currentOffset = 0; 
    // allocate maximum space possibly needed
    size_t memberListMsgSize = sizeof(MessageHdr) + (6 + sizeof(long)) * entry_num;
    MessageHdr *memberListMsg;
    memberListMsg = (MessageHdr *) malloc(memberListMsgSize * sizeof(char));
    memberListMsg->msgType = MEMBERLIST;
    currentOffset += sizeof(int);

    // send / construct two kinds of messages
    for (MemberListEntry memberListEntry: memberNode->memberList) {
        // if it is to be deleted, don't add it to member list messages
        if (this->tobedeleted->count(memberListEntry.getid()) != 0) {
            continue;
        }

        Address address;
        string _address = to_string(memberListEntry.getid()) + ":" + to_string(memberListEntry.getport());
        address = Address(_address);

        // add to MEMBERLIST
        memcpy((char *)(memberListMsg) + currentOffset, &address.addr, sizeof(address.addr));
        currentOffset += sizeof(address.addr);
        memcpy((char *)(memberListMsg) + currentOffset, &memberListEntry.heartbeat, sizeof(long));
        currentOffset += sizeof(long);
    }


    // Propagate your membership list; it is sent to even nodes (entries) to be deleted
    broadcast++;
    // decide whether broadcast or unicast
    if (broadcast >= BROADCAST_INTERVAL) {
        // broadcast
        for (MemberListEntry memberListEntry: memberNode->memberList) {
            Address address;
            string _address = to_string(memberListEntry.getid()) + ":" + to_string(memberListEntry.getport());
            address = Address(_address);
            emulNet->ENsend(&memberNode->addr, &address, (char *)memberListMsg, currentOffset);
        }
        broadcast = 0;
    } else {
        // unicast
        int member_num = memberNode->memberList.size();
        int index = rand() % member_num;
        Address address;
        string _address = to_string(memberNode->memberList.at(index).getid()) + ":" + to_string(memberNode->memberList.at(index).getport());
        address = Address(_address);
        emulNet->ENsend(&memberNode->addr, &address, (char *)memberListMsg, currentOffset);
    }

    #ifdef DEBUGLOG
        log->LOG(&memberNode->addr, "sent member list message");
    #endif

    free(memberListMsg);

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdi, %rbp
movq 0x20(%rdi), %rax
movq 0x18(%rax), %rsi
movq 0x28(%rax), %rdi
incq %rsi
movq %rsi, 0x18(%rax)
callq 0x9fb0
movq 0x18(%rbp), %rdi
movq 0x20(%rbp), %rax
movq 0x28(%rax), %r14
callq 0x9f08
movslq %eax, %rsi
movq %r14, %rdi
callq 0x9fb6
movq 0x20(%rbp), %rax
movq 0x28(%rax), %rcx
movq %rcx, 0x40(%rax)
movq 0x20(%rbp), %rdi
movq 0x40(%rdi), %rsi
leaq 0x30(%rsp), %r12
leaq 0x90(%rsp), %r14
leaq 0x70(%rsp), %r13
leaq 0x128(%rsp), %rbx
movq %rbp, 0x8(%rsp)
cmpq 0x30(%rdi), %rsi
je 0x3bc4
callq 0x4741
testb %al, %al
je 0x3baf
movq %r13, %r15
movq %rbp, %r13
movq %r14, %rbp
movq 0x20(%r13), %rax
movq 0x40(%rax), %rdi
callq 0x9f90
movl %eax, (%rsp)
movq 0x20(%r13), %rax
movq 0x40(%rax), %rdi
callq 0x9f94
movl %eax, %r14d
movl (%rsp), %esi
leaq 0x50(%rsp), %r13
movq %r13, %rdi
callq 0x4a9a
movq %r12, %rdi
movq %r13, %rsi
leaq 0xf1a1(%rip), %rdx      # 0x12cb1
callq 0x4a7b
movswl %r14w, %esi
movq %rbp, %r14
movq %rbp, %rdi
callq 0x4a9a
movq %r15, %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4a14
movq 0x8(%rsp), %rbp
movq %r14, %rdi
callq 0x3110
movq %r12, %rdi
callq 0x3110
leaq 0x50(%rsp), %rdi
callq 0x3110
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3340
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4af4
leaq 0xb0(%rsp), %rdi
movq %r12, %rsi
callq 0x9f22
movq %rbx, %rdi
callq 0x3110
movq 0x38(%rbp), %rdi
movq %rsp, %rsi
callq 0x4ba2
testq %rax, %rax
jne 0x3ba7
movq 0x38(%rbp), %rdi
movl (%rsp), %eax
movq %rax, 0x30(%rsp)
movq %r12, %rsi
callq 0x53a6
movq %r13, %rdi
callq 0x3110
movq 0x20(%rbp), %rdi
movq 0x40(%rdi), %rsi
addq $0x18, %rsi
movq %rsi, 0x40(%rdi)
jmp 0x3ab3
movq %rbp, %rdi
callq 0x476e
movq 0x20(%rbp), %rax
movq 0x28(%rax), %rbx
movq 0x30(%rax), %rax
movq %rax, 0x20(%rsp)
subq %rbx, %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
cltq
imulq $0xe, %rax, %rdi
addq $0x4, %rdi
callq 0x3330
movq %rbx, %rsi
movq %rax, 0x10(%rsp)
movl $0x2, (%rax)
pushq $0x4
popq %rax
movq %rax, 0x18(%rsp)
leaq 0xb0(%rsp), %r15
leaq 0x70(%rsp), %rbp
leaq 0x30(%rsp), %r14
leaq 0x90(%rsp), %r13
cmpq 0x20(%rsp), %rsi
je 0x3d66
movq %r14, %rbx
movq %r15, %rdi
movq %rsi, 0x28(%rsp)
callq 0x9f5a
movq 0x8(%rsp), %rax
movq 0x38(%rax), %r14
movq %r15, %rdi
callq 0x9f90
movl %eax, 0x70(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4ba2
testq %rax, %rax
je 0x3c6f
movq %rbx, %r14
jmp 0x3d58
movq %rbp, %r12
movq %r15, %rdi
callq 0x9f90
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movl %eax, %esi
callq 0x4a9a
movq %rbx, %r14
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0xf018(%rip), %rdx      # 0x12cb1
callq 0x4a7b
movq %r15, %rdi
callq 0x9f94
movq %r12, %rbp
movswl %ax, %esi
movq %r13, %rdi
callq 0x4a9a
movq %rbp, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x4a14
movq %r13, %rdi
callq 0x3110
movq %r14, %rdi
callq 0x3110
leaq 0x50(%rsp), %rdi
callq 0x3110
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3340
leaq 0xb0(%rsp), %r15
movq %r14, %rdi
movq %r12, %rsi
callq 0x4af4
movq %rsp, %rdi
movq %r14, %rsi
callq 0x9f22
leaq 0xe8(%rsp), %rdi
callq 0x3110
movq 0x18(%rsp), %rsi
movslq %esi, %rsi
movzwl 0x4(%rsp), %eax
movq 0x10(%rsp), %rdx
movw %ax, 0x4(%rdx,%rsi)
movl (%rsp), %eax
movl %eax, (%rdx,%rsi)
leal 0x6(%rsi), %eax
cltq
movq 0xb8(%rsp), %rcx
movq %rcx, (%rdx,%rax)
addl $0xe, %esi
movq %rsi, 0x18(%rsp)
movq %rbp, %rdi
callq 0x3110
movq 0x28(%rsp), %rsi
addq $0x18, %rsi
jmp 0x3c27
movq 0x8(%rsp), %rbx
movl 0x30(%rbx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x30(%rbx)
movq 0x20(%rbx), %rcx
cmpl $0x2, %eax
jle 0x3ea5
movq 0x28(%rcx), %rsi
movq 0x30(%rcx), %rax
movq %rax, 0x20(%rsp)
leaq 0xb0(%rsp), %r13
leaq 0x30(%rsp), %rbp
leaq 0x90(%rsp), %rbx
leaq 0x108(%rsp), %r15
cmpq 0x20(%rsp), %rsi
je 0x3fd1
movq %r15, %r14
movq %rbx, %r15
movq %r13, %rdi
movq %rsi, 0x28(%rsp)
callq 0x9f5a
movq %r13, %rdi
callq 0x9f90
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movl %eax, %esi
callq 0x4a9a
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0xeec4(%rip), %rdx      # 0x12cb1
callq 0x4a7b
movq %r13, %r12
movq %r13, %rdi
callq 0x9f94
movq %r15, %rbx
movswl %ax, %esi
movq %r15, %rdi
callq 0x4a9a
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4a14
movq %rbx, %rdi
callq 0x3110
movq %rbp, %rdi
callq 0x3110
leaq 0x50(%rsp), %rdi
callq 0x3110
movq %r14, %rdi
movq %r15, %rsi
callq 0x3340
movq %rbp, %rdi
movq %r14, %rsi
callq 0x4af4
movq %r14, %r15
movq %rsp, %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x9f22
movq %r15, %rdi
callq 0x3110
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x56ee
leaq 0x70(%rsp), %rdi
callq 0x3110
movq 0x28(%rsp), %rsi
addq $0x18, %rsi
movq %r12, %r13
jmp 0x3dab
movq 0x30(%rcx), %rax
subq 0x28(%rcx), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %r14
callq 0x30a0
cltd
idivl %r14d
movq 0x20(%rbx), %rdi
addq $0x28, %rdi
movslq %edx, %r14
movq %r14, %rsi
callq 0x4bc2
movq %rax, %rdi
callq 0x9f90
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movl %eax, %esi
callq 0x4a9a
leaq 0xedbf(%rip), %rdx      # 0x12cb1
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x4a7b
movq 0x20(%rbx), %rdi
addq $0x28, %rdi
movq %r14, %rsi
callq 0x4bc2
movq %rax, %rdi
callq 0x9f94
movswl %ax, %esi
leaq 0x90(%rsp), %rdi
callq 0x4a9a
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x4a14
leaq 0x90(%rsp), %rdi
callq 0x3110
leaq 0x30(%rsp), %rdi
callq 0x3110
leaq 0x50(%rsp), %rdi
callq 0x3110
leaq 0xc8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3340
leaq 0x30(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x4af4
leaq 0xb0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x9f22
leaq 0xc8(%rsp), %rdi
callq 0x3110
movq 0x8(%rbx), %rdi
movq 0x20(%rbx), %rsi
addq $0x8, %rsi
leaq 0xb0(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x56ee
leaq 0x70(%rsp), %rdi
callq 0x3110
jmp 0x3fda
movq 0x8(%rsp), %rax
andl $0x0, 0x30(%rax)
movq 0x10(%rsp), %rdi
callq 0x32f0
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4033
jmp 0x4033
jmp 0x401d
jmp 0x4038
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x4078
jmp 0x4059
jmp 0x4033
jmp 0x4033
jmp 0x4038
jmp 0x401d
jmp 0x4033
jmp 0x4033
jmp 0x4038
jmp 0x401d
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x3110
jmp 0x405c
jmp 0x4033
jmp 0x4059
movq %rax, %rbx
jmp 0x407d
movq %rax, %rbx
jmp 0x4066
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0x4078
jmp 0x4059
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x4078
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3110
leaq 0x50(%rsp), %rdi
jmp 0x4082
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x3110
leaq 0x70(%rsp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x3420
nop