// File: /workspace/github_cpp_makefile_star2_O1/zkl94[P]distributed-key-value-store/MP1Node.cpp
// Function: MP1Node::handleJOINREQ(void*, char*, int)
void MP1Node::handleJOINREQ(void *env, char *data, int size) {
    #ifdef DEBUG_JOINREQ
        cout << "received a join request" << endl;
    #endif

    // when introducer receives JOINREQ, add the node to member list, and return JOINREP with cluster member list
    char addr[6];
    // msg: sizeof(MessageHdr) + 6 + sizeof(long)
    MessageHdr *msg = (MessageHdr *)data;
    memcpy(&addr, (char *)(msg)+4, 6);
    int id = 0;
    short port;
    memcpy(&id, &addr[0], sizeof(int));
    memcpy(&port, &addr[4], sizeof(short));

    #ifdef DEBUG_JOINREQ
        cout << "id = " << id << "; port = " << port << endl;
    #endif

    #ifdef DEBUG_JOINREQ
        cout << "before adding member list entry, there are " << memberNode->memberList.size() << " entries" << endl;
    #endif

    // newly added node have 0 heartbeat
    MemberListEntry *newEntry = new MemberListEntry(id, port, 0, par->getcurrtime());
    memberNode->memberList.push_back(*newEntry);

    logNodeAddWrapper(memberNode, id, port);

    #ifdef DEBUG_JOINREQ
        cout << "after adding member list entry, there are " << memberNode->memberList.size() << " entries" << endl;
    #endif

    // return JOINREP with cluster member list
    int entry_num = memberNode->memberList.size();
    int currentOffset = 0;
    // allocate maximum space possibly needed
    size_t JOINREPMsgSize = sizeof(MessageHdr) + (sizeof(addr) + sizeof(long)) * entry_num;
    MessageHdr *JOINREPMsg;
    JOINREPMsg = (MessageHdr *) malloc(JOINREPMsgSize * sizeof(char));
    JOINREPMsg->msgType = JOINREP;
    // for the message type offset
    currentOffset += sizeof(int);

    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 JOINREPMsg
        memcpy((char *)(JOINREPMsg) + currentOffset, &address.addr, sizeof(address.addr));
        currentOffset += sizeof(address.addr);
        memcpy((char *)(JOINREPMsg) + currentOffset, &memberListEntry.heartbeat, sizeof(long));
        currentOffset += sizeof(long);
        // only send heartbeat, timestamp is set by receivers with par->getcurrtime()
        // also, timestamp is not global and thus no need to sync
    }

    #ifdef DEBUG_JOINREQ
        cout << "currentOffset = " << currentOffset << " JOINREPMsgSize = " << JOINREPMsgSize << endl;
    #endif

    // not necessarily because to be deleted entries are not included
    // assert(currentOffset == (int)JOINREPMsgSize);

    string _address = to_string(id) + ":" + to_string(port);
    Address address = Address(_address);
    emulNet->ENsend(&memberNode->addr, &address, (char *)JOINREPMsg, currentOffset);
    #ifdef DEBUG_JOINREQ
        cout << "JOINREP successfully sent" <<endl;
    #endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %r12
movl 0x4(%rdx), %r15d
movswl 0x8(%rdx), %ebx
movl $0x18, %edi
callq 0x3270
movq %rax, %r14
movq 0x18(%r12), %rdi
callq 0xd162
movslq %eax, %r8
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
xorl %ecx, %ecx
callq 0xd1a4
movq 0x20(%r12), %rdi
addq $0x28, %rdi
movq %r14, %rsi
callq 0x6320
movq 0x20(%r12), %rsi
movq %r12, %rdi
movq %r15, 0x98(%rsp)
movl %r15d, %edx
movq %rbx, 0xd0(%rsp)
movl %ebx, %ecx
callq 0x4f3e
movq 0x20(%r12), %rax
movq 0x28(%rax), %r13
movq 0x30(%rax), %rbx
movq %rbx, %rax
subq %r13, %rax
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
movslq %eax, %rdi
leaq (%rdi,%rdi), %rax
shlq $0x4, %rdi
subq %rax, %rdi
addq $0x4, %rdi
callq 0x3350
movq %rax, 0xa0(%rsp)
movl $0x1, (%rax)
movq %rbx, 0xd8(%rsp)
cmpq %r13, %rbx
movq %r12, 0xa8(%rsp)
movl $0x4, %ebp
je 0x57a2
leaq 0x8(%rsp), %r14
movq %r14, %r15
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xd1bc
movq 0x38(%r12), %r14
movq %rbx, %rdi
callq 0xd1f2
movq 0x10(%r14), %rdx
addq $0x8, %r14
movq %r14, %rcx
testq %rdx, %rdx
je 0x5426
movq %r14, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x540f
movq %r14, %rdx
cmpq %r14, %rcx
je 0x5438
cmpl 0x20(%rcx), %eax
cmovlq %r14, %rcx
movq %rcx, %rdx
cmpq %r14, %rdx
je 0x5445
movq %r15, %r14
jmp 0x5790
movl %ebp, 0x8c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xd1f2
movl %eax, %ebp
movl %eax, %r12d
negl %r12d
cmovsl %eax, %r12d
movl $0x1, %r14d
cmpl $0xa, %r12d
jb 0x54b8
movl $0x4, %r14d
movl %r12d, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0x54af
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x54b5
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x54b8
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r14d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x547f
addl $-0x3, %r14d
jmp 0x54b8
addl $-0x2, %r14d
jmp 0x54b8
decl %r14d
shrl $0x1f, %ebp
leal (%r14,%rbp), %esi
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movl $0x2d, %edx
callq 0x32f0
addq 0x68(%rsp), %rbp
movq %rbp, %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0x67a3
movq %rbx, %rdi
leaq 0x12815(%rip), %rsi     # 0x17d0d
callq 0x3480
leaq 0x18(%rsp), %rsi
movq %rsi, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x5525
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0x552b
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x100(%rsp), %rdi
callq 0xd1f6
movswl %ax, %r12d
movl %r12d, %ebp
negl %ebp
cmovsl %r12d, %ebp
movl $0x1, %r14d
cmpl $0xa, %ebp
jb 0x5591
movl $0x2, %r14d
cmpl $0x64, %ebp
jb 0x5591
movl $0x3, %r14d
cmpl $0x3e8, %ebp            # imm = 0x3E8
jb 0x5591
cmpl $0x2710, %ebp           # imm = 0x2710
movl $0x5, %r14d
sbbl $0x0, %r14d
shrl $0x1f, %r12d
leal (%r14,%r12), %esi
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
movl $0x2d, %edx
callq 0x32f0
movl %r12d, %edi
addq 0x48(%rsp), %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x67a3
movq 0x8(%rsp), %rcx
movl $0xf, %esi
leaq 0x18(%rsp), %rax
cmpq %rax, %rcx
je 0x55dd
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r8
movq 0x50(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
movq 0xa8(%rsp), %r12
movl 0x8c(%rsp), %ebp
leaq 0x38(%rsp), %rbx
movq %r15, %r14
leaq 0xb0(%rsp), %r15
jbe 0x562a
movl $0xf, %esi
leaq 0x58(%rsp), %rdi
cmpq %rdi, 0x48(%rsp)
je 0x5625
movq 0x58(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x5639
movq 0x48(%rsp), %rsi
movq %r14, %rdi
callq 0x30d0
jmp 0x5647
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32c0
movq %rbx, 0x28(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x5667
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0x566d
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x30(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x56a6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x56c2
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x56de
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xc0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x6810
movq %r14, %rdi
movq %r15, %rsi
callq 0x634c
leaq 0x92(%rsp), %rdi
movq %r14, %rsi
callq 0xd17e
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x5743
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3280
movslq %ebp, %rax
movzwl 0x96(%rsp), %ecx
movq 0xa0(%rsp), %rdx
movw %cx, 0x4(%rdx,%rax)
movl 0x92(%rsp), %ecx
movl %ecx, (%rdx,%rax)
addl $0x6, %eax
cltq
movq 0x108(%rsp), %rcx
movq %rcx, (%rdx,%rax)
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
je 0x578d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3280
addl $0xe, %ebp
addq $0x18, %r13
cmpq 0xd8(%rsp), %r13
jne 0x53d9
movl %ebp, %ebx
movq 0x98(%rsp), %rax
movl %eax, %r15d
negl %r15d
cmovsl %eax, %r15d
movl $0x1, %ebp
cmpl $0xa, %r15d
movq 0xd0(%rsp), %r13
jb 0x580e
movl $0x4, %ebp
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %r15d, %ecx
cmpl $0x63, %ecx
jbe 0x5807
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x580c
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x580e
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %ebp
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x57d6
addl $-0x3, %ebp
jmp 0x580e
addl $-0x2, %ebp
jmp 0x580e
decl %ebp
movq 0x98(%rsp), %r12
shrl $0x1f, %r12d
movl %r12d, %esi
addl %ebp, %esi
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movl $0x2d, %edx
callq 0x32f0
movq (%r14), %rdi
addq %r12, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x67a3
leaq 0x124bc(%rip), %rsi     # 0x17d0d
movq %r14, %rdi
callq 0x3480
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x5880
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0x5887
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl %r13d, %ebp
negl %ebp
cmovsl %r13d, %ebp
movl $0x1, %r14d
cmpl $0xa, %ebp
jb 0x58dc
movl $0x2, %r14d
cmpl $0x64, %ebp
jb 0x58dc
movl $0x3, %r14d
cmpl $0x3e8, %ebp            # imm = 0x3E8
jb 0x58dc
cmpl $0x2710, %ebp           # imm = 0x2710
movl $0x5, %r14d
sbbl $0x0, %r14d
shrl $0x1f, %r13d
leal (%r14,%r13), %esi
leaq 0x58(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x48(%rsp), %rdi
movl $0x2d, %edx
callq 0x32f0
movl %r13d, %edi
addq 0x48(%rsp), %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x67a3
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %r8
movq 0x50(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %esi
cmpq %r15, %rcx
je 0x5931
movq 0x18(%rsp), %rsi
cmpq %rsi, %rax
movl %ebx, %ebp
jbe 0x594e
movl $0xf, %esi
cmpq %r12, 0x48(%rsp)
je 0x5949
movq 0x58(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x595f
movq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x30d0
jmp 0x596d
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32c0
leaq 0x38(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x5991
movq %rsi, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0x5997
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x30(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x59cb
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x59e2
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x59fe
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xf0(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0xe0(%rsp), %rdi
callq 0x6810
movq 0xa8(%rsp), %rbx
leaq 0x8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x634c
movq 0xe0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5a5b
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x8(%rbx), %rdi
movq 0x20(%rbx), %rsi
addq $0x8, %rsi
leaq 0x8(%rsp), %rdx
movq 0xa0(%rsp), %rcx
movl %ebp, %r8d
callq 0x6ed0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5a97
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3280
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5b02
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5b02
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x5aea
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5aea
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
jne 0x5b98
jmp 0x5ba5
jmp 0x5b22
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x5b7a
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
jmp 0x5bbc
movq %rax, %rbx
jmp 0x5b70
movq %rax, %rbx
jmp 0x5ba5
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x5b89
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5b89
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x5b70
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x5bc1
movq 0x38(%rsp), %rsi
jmp 0x5bb9
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x5ba5
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x5bc1
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3450
nop