// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
movl 0x4(%rdx), %r12d
movswl 0x8(%rdx), %r15d
movl $0x18, %edi
callq 0x3270
movq %rax, %r14
movq %rbx, 0x88(%rsp)
movq 0x18(%rbx), %rdi
callq 0xd00e
movslq %eax, %r8
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0xd050
movq 0x88(%rsp), %rbx
movq 0x20(%rbx), %rdi
addq $0x28, %rdi
movq %r14, %rsi
callq 0x629a
movq 0x20(%rbx), %rsi
movq %rbx, %rdi
movq %r12, 0xa0(%rsp)
movl %r12d, %edx
movq %r15, 0xb0(%rsp)
movl %r15d, %ecx
callq 0x4f38
movq 0x20(%rbx), %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, 0xa8(%rsp)
movl $0x1, (%rax)
movq %rbx, 0xb8(%rsp)
cmpq %r13, %rbx
je 0x5770
leaq 0x18(%rsp), %r12
movl $0x4, %ebp
leaq 0x100(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0xd068
movq 0x88(%rsp), %rax
movq 0x38(%rax), %rbx
movq %r14, %rdi
callq 0xd09e
movq 0x10(%rbx), %rcx
testq %rcx, %rcx
je 0x5434
addq $0x8, %rbx
movq %rbx, %rdx
xorl %esi, %esi
cmpl %eax, 0x20(%rcx)
setl %sil
cmovgeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x540f
cmpq %rbx, %rdx
je 0x5434
cmpl 0x20(%rdx), %eax
jge 0x575c
movq %r12, %r15
movl %ebp, 0x94(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xd09e
movl %eax, %ebx
movl %eax, %r12d
negl %r12d
cmovsl %eax, %r12d
movl $0x1, %ebp
cmpl $0xa, %r12d
jb 0x54a4
movl $0x4, %ebp
movl %r12d, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0x549d
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x54a2
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x54a4
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %ebp
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x546f
addl $-0x3, %ebp
jmp 0x54a4
addl $-0x2, %ebp
jmp 0x54a4
decl %ebp
shrl $0x1f, %ebx
leal (%rbx,%rbp), %esi
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movl $0x2d, %edx
callq 0x32f0
addq 0x68(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x6723
movq %r14, %rdi
leaq 0x127cf(%rip), %rsi     # 0x17cb1
callq 0x3480
movq %r15, 0x8(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x550a
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0x5511
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)
leaq 0x100(%rsp), %rdi
callq 0xd0a2
movswl %ax, %ebp
movl %ebp, %ebx
negl %ebx
cmovsl %ebp, %ebx
movl $0x1, %r12d
cmpl $0xa, %ebx
jb 0x5574
movl $0x2, %r12d
cmpl $0x64, %ebx
jb 0x5574
movl $0x3, %r12d
cmpl $0x3e8, %ebx            # imm = 0x3E8
jb 0x5574
cmpl $0x2710, %ebx           # imm = 0x2710
movl $0x5, %r12d
sbbl $0x0, %r12d
shrl $0x1f, %ebp
leal (%r12,%rbp), %esi
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x48(%rsp), %rdi
movl $0x2d, %edx
callq 0x32f0
movl %ebp, %edi
addq 0x48(%rsp), %rdi
movl %r12d, %esi
movl %ebx, %edx
callq 0x6723
movq 0x8(%rsp), %rcx
movl $0xf, %edi
movq %r15, %r12
cmpq %r15, %rcx
je 0x55bc
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
leaq 0x38(%rsp), %rbx
leaq 0xf0(%rsp), %r14
jbe 0x55fa
movl $0xf, %edi
leaq 0x58(%rsp), %r9
cmpq %r9, %rsi
je 0x55f5
movq 0x58(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x5606
leaq 0x8(%rsp), %rdi
callq 0x30d0
jmp 0x5614
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 0x5634
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0x563a
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
leaq 0x8(%rsp), %r15
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 0x5678
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x568f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x56ab
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
movq %r14, 0xe0(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0xe0(%rsp), %rbp
movq %rbp, %rdi
callq 0x6790
movq %r15, %rdi
movq %rbp, %rsi
callq 0x62c6
leaq 0x9a(%rsp), %rdi
movq %r15, %rsi
callq 0xd02a
movq 0xe0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5708
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3280
movl 0x94(%rsp), %ebp
movslq %ebp, %rax
movzwl 0x9e(%rsp), %ecx
movq 0xa8(%rsp), %rdx
movw %cx, 0x4(%rdx,%rax)
movl 0x9a(%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 0x5759
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3280
addl $0xe, %ebp
addq $0x18, %r13
cmpq 0xb8(%rsp), %r13
jne 0x53d8
jmp 0x5775
movl $0x4, %ebp
movq 0xa0(%rsp), %rax
movl %eax, %ebx
negl %ebx
cmovsl %eax, %ebx
movl $0x1, %r15d
cmpl $0xa, %ebx
movq 0xb0(%rsp), %r13
jb 0x57e0
movl $0x4, %r15d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebx, %ecx
cmpl $0x63, %ecx
jbe 0x57d7
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x57dd
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x57e0
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r15d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x57a4
addl $-0x3, %r15d
jmp 0x57e0
addl $-0x2, %r15d
jmp 0x57e0
decl %r15d
movq 0xa0(%rsp), %r14
shrl $0x1f, %r14d
movl %r14d, %esi
addl %r15d, %esi
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movl $0x2d, %edx
callq 0x32f0
movq (%r12), %rdi
addq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x6723
leaq 0x1248c(%rip), %rsi     # 0x17cb1
movq %r12, %rdi
callq 0x3480
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x5855
movq %rdx, 0x8(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x18(%rsp)
jmp 0x585d
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl %r13d, %ebx
negl %ebx
cmovsl %r13d, %ebx
movl $0x1, %r15d
cmpl $0xa, %ebx
jb 0x58b2
movl $0x2, %r15d
cmpl $0x64, %ebx
jb 0x58b2
movl $0x3, %r15d
cmpl $0x3e8, %ebx            # imm = 0x3E8
jb 0x58b2
cmpl $0x2710, %ebx           # imm = 0x2710
movl $0x5, %r15d
sbbl $0x0, %r15d
shrl $0x1f, %r13d
leal (%r15,%r13), %esi
leaq 0x58(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x48(%rsp), %rdi
movl $0x2d, %edx
callq 0x32f0
movl %r13d, %edi
addq 0x48(%rsp), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x6723
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %r8
movq 0x50(%rsp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %edi
cmpq %r12, %rcx
je 0x5906
movq 0x18(%rsp), %rdi
movq 0x48(%rsp), %rsi
cmpq %rdi, %rax
jbe 0x5924
movl $0xf, %edi
cmpq %r14, %rsi
je 0x591f
movq 0x58(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x5930
leaq 0x8(%rsp), %rdi
callq 0x30d0
jmp 0x593e
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 0x5962
movq %rsi, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0x5968
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 %r14, %rdi
je 0x599c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x59b3
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x59cf
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
leaq 0xd0(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0xc0(%rsp), %rdi
callq 0x6790
leaq 0x8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x62c6
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5a24
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x88(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
leaq 0x8(%rsp), %rdx
movq 0xa8(%rsp), %rcx
movl %ebp, %r8d
callq 0x6e4c
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5a68
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 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5ad3
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5ad3
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r14, %rdi
je 0x5abb
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5abb
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
jne 0x5b5c
jmp 0x5b69
jmp 0x5af3
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x5b43
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
jmp 0x5b80
movq %rax, %rbx
jmp 0x5b39
movq %rax, %rbx
jmp 0x5b69
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x5b52
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3280
jmp 0x5b52
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %r14, %rdi
je 0x5b39
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x5b85
movq 0x38(%rsp), %rsi
jmp 0x5b7d
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x5b69
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3280
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x5b85
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3280
movq %rbx, %rdi
callq 0x3450
nop