// File: /workspace/github_cpp_makefile_star2_O1/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)
movl 0x20(%rsi), %eax
xorl 0x38(%rdi), %eax
movzwl 0x24(%rsi), %ecx
xorw 0x3c(%rdi), %cx
movzwl %cx, %ecx
orl %eax, %ecx
movl %ecx, 0x8(%rsp)
je 0x2de1
movq %rsi, %r14
movq %rdi, %r12
leaq 0x20(%rsi), %r15
movq 0x20(%rdi), %rax
movq 0x28(%rax), %r13
movq 0x30(%rax), %rbx
cmpq %rbx, %r13
movq %rdi, 0x10(%rsp)
je 0x2c2e
movq %r14, (%rsp)
xorl %r14d, %r14d
leaq 0x30(%rsp), %rbp
movl $0x1, %r12d
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5638
movq %rbp, %rdi
callq 0x566e
movsbl (%r15), %ecx
movzbl %r14b, %r14d
cmpl %ecx, %eax
cmovel %r12d, %r14d
je 0x2c1f
addq $0x18, %r13
cmpq %rbx, %r13
jne 0x2bf3
testb $0x1, %r14b
movq 0x10(%rsp), %r12
movq (%rsp), %r14
jne 0x2c56
movsbl 0x20(%r14), %esi
movsbl 0x24(%r14), %edx
leaq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x5630
movq 0x20(%r12), %rdi
addq $0x28, %rdi
movq %r13, %rsi
callq 0x3424
cmpb $0x1, (%r15)
jne 0x2c7c
movq 0x20(%r12), %rsi
movb $0x1, 0xf(%rsi)
movq 0x10(%r12), %rdi
addq $0x8, %rsi
leaq 0x53f2(%rip), %rdx      # 0x8067
xorl %eax, %eax
callq 0x5090
movq 0x10(%r12), %rdi
movq 0x20(%r12), %rsi
addq $0x8, %rsi
movq %r15, %rdx
callq 0x5394
movq 0x8(%r14), %r15
movq 0x10(%r14), %rax
movq %rax, (%rsp)
cmpq %rax, %r15
je 0x2de1
leaq 0x18(%rsp), %r13
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5638
movq %rbx, %rdi
callq 0x566e
movq 0x20(%r12), %rcx
cmpl 0x8(%rcx), %eax
je 0x2dd3
movq 0x28(%rcx), %rbp
movq 0x30(%rcx), %r14
cmpq %r14, %rbp
je 0x2d2a
xorl %r12d, %r12d
movq %r13, %rdi
movq %rbp, %rsi
callq 0x5638
movq %r13, %rdi
callq 0x566e
movl %eax, %ebx
leaq 0x30(%rsp), %rdi
callq 0x566e
movzbl %r12b, %r12d
cmpl %eax, %ebx
movl $0x1, %eax
cmovel %eax, %r12d
je 0x2d1b
addq $0x18, %rbp
cmpq %r14, %rbp
jne 0x2ce2
testb $0x1, %r12b
movq 0x10(%rsp), %r12
jne 0x2dd3
movq 0x20(%r12), %rdi
addq $0x28, %rdi
leaq 0x30(%rsp), %rbx
movq %rbx, %rsi
callq 0x3424
xorl %r14d, %r14d
movl %r14d, 0x18(%rsp)
movw $0x0, 0x1c(%rsp)
movq %rbx, %rdi
callq 0x566e
movb %al, 0x18(%rsp)
movq %rbx, %rdi
callq 0x5672
movb %al, 0x1c(%rsp)
movl $0x30, %edi
callq 0x2190
movq %rax, %rbp
leaq 0x8(%rax), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movl %r14d, (%rax)
movq 0x20(%r12), %rsi
addq $0x8, %rsi
leaq 0x20(%rax), %rdi
callq 0x55fa
movq 0x20(%r12), %rsi
addq $0x28, %rsi
movq %rbx, %rdi
callq 0x3450
movq 0x8(%r12), %rdi
movq 0x20(%r12), %rsi
addq $0x8, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movl 0xc(%rsp), %r8d
callq 0x3de4
movq %rbp, %rdi
callq 0x2200
addq $0x18, %r15
cmpq (%rsp), %r15
jne 0x2cac
cmpl $0x0, 0x8(%rsp)
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq