// File: /workspace/github_cpp_makefile_star2_O2/hunterlxt[P]swim-membership-protocol/MP1Node.cpp
// Function: MP1Node::handleJoinReply(MessageHdr*, int)
bool MP1Node::handleJoinReply(MessageHdr *msg, int size) {
    if (isNullAddress(&msg->srcAddr)) {
        return false;
    }

    Address joinaddr = getJoinAddress();
    bool existed = false;
    for (auto localEntry : memberNode->memberList) {
        if (localEntry.getid() == msg->srcAddr.addr[0]) {
            existed = true;
            break;
        }
    }
    // add the src node to the memberList
    if (!existed) {
        MemberListEntry entry = MemberListEntry((int)msg->srcAddr.addr[0],
                                                (short)msg->srcAddr.addr[4]);
        memberNode->memberList.push_back(entry);
    }

    // join the group when first time
    if ((int)msg->srcAddr.addr[0] == (int)joinaddr.addr[0]) {
        memberNode->inGroup = true;

#ifdef DEBUGLOG
        log->LOG(&memberNode->addr, "Now in the group...");
#endif
    }

#ifdef DEBUGLOG
    log->logNodeAdd(&memberNode->addr, &msg->srcAddr);
#endif

    for (auto remoteEntry : msg->memberList) {
        existed = false;
        if (remoteEntry.getid() == *(int *)(memberNode->addr.addr)) {
            continue;
        }
        // select the non-existed node in local memberlist to send JOINREQ
        for (auto localEntry : memberNode->memberList) {
            if (localEntry.getid() == remoteEntry.getid()) {
                existed = true;
                break;
            }
        }
        if (!existed) {
            // add the non-existed node to the memberList
            memberNode->memberList.push_back(remoteEntry);
            // create the destination address
            Address dstAddr;
            dstAddr.init();
            dstAddr.addr[0] = (char)remoteEntry.getid();
            dstAddr.addr[4] = (char)remoteEntry.getport();
            // create the JOINREQ message
            msg = new MessageHdr;
            msg->msgType = JOINREQ;
            msg->srcAddr = memberNode->addr;
            msg->memberList = memberNode->memberList;
            emulNet->ENsend(&memberNode->addr, &dstAddr, (char *)msg, size);
            free(msg);
        }
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, 0xc(%rsp)
movq %rsi, %r12
leaq 0x20(%rsi), %r15
movq %rdi, (%rsp)
movq %r15, %rsi
callq 0x2e6e
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x29f6
cmpl $0x0, 0x8(%rsp)
sete %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rax), %r13
movq 0x30(%rax), %rbx
leaq 0x30(%rsp), %rbp
cmpq %rbx, %r13
je 0x2a31
movq %rbp, %rdi
movq %r13, %rsi
callq 0x524a
movq %rbp, %rdi
callq 0x5280
movsbl (%r15), %ecx
addq $0x18, %r13
cmpl %ecx, %eax
jne 0x2a0b
jmp 0x2a63
movsbl 0x20(%r12), %esi
movsbl 0x24(%r12), %edx
leaq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x5242
movq (%rsp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
movq %r13, %rsi
callq 0x319a
movb 0x20(%r12), %cl
cmpb $0x1, %cl
jne 0x2a8a
movq (%rsp), %rax
movq 0x20(%rax), %rsi
movb $0x1, 0xf(%rsi)
movq 0x10(%rax), %rdi
addq $0x8, %rsi
leaq 0x45e4(%rip), %rdx      # 0x7067
xorl %eax, %eax
callq 0x4cd2
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq %r15, %rdx
callq 0x4f76
movq 0x8(%r12), %r15
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
leaq 0x30(%rsp), %r12
leaq 0x18(%rsp), %r13
cmpq 0x10(%rsp), %r15
je 0x29df
movq %r12, %rdi
movq %r15, %rsi
callq 0x524a
movq %r12, %rdi
callq 0x5280
movq (%rsp), %rcx
movq 0x20(%rcx), %rcx
cmpl 0x8(%rcx), %eax
je 0x2bbb
movq 0x28(%rcx), %rbp
movq 0x30(%rcx), %r14
cmpq %r14, %rbp
je 0x2b21
movq %r13, %rdi
movq %rbp, %rsi
callq 0x524a
movq %r13, %rdi
callq 0x5280
movl %eax, %ebx
movq %r12, %rdi
callq 0x5280
addq $0x18, %rbp
cmpl %eax, %ebx
jne 0x2af2
jmp 0x2bbb
movq (%rsp), %r14
movq 0x20(%r14), %rdi
addq $0x28, %rdi
movq %r12, %rsi
callq 0x319a
andw $0x0, 0x1c(%rsp)
andl $0x0, 0x18(%rsp)
movq %r12, %rdi
callq 0x5280
movb %al, 0x18(%rsp)
movq %r12, %rdi
callq 0x5284
movb %al, 0x1c(%rsp)
pushq $0x30
popq %rdi
callq 0x2180
movq %rax, %rbp
andq $0x0, 0x18(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
andl $0x0, (%rax)
leaq 0x8(%rax), %rbx
movq 0x20(%r14), %rsi
addq $0x8, %rsi
leaq 0x20(%rax), %rdi
callq 0x5212
movq 0x20(%r14), %rsi
addq $0x28, %rsi
movq %rbx, %rdi
callq 0x31c6
movq 0x8(%r14), %rdi
movq 0x20(%r14), %rsi
addq $0x8, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movl 0xc(%rsp), %r8d
callq 0x3c2a
movq %rbp, %rdi
callq 0x21e0
addq $0x18, %r15
jmp 0x2abb