// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, (%rsp)
xorw 0x3c(%rdi), %cx
movzwl %cx, %ecx
orl %eax, %ecx
movl %ecx, 0x8(%rsp)
je 0x2d8f
movq %rsi, %r12
leaq 0x20(%rsi), %r15
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rax), %r13
movq 0x30(%rax), %rbx
cmpq %rbx, %r13
je 0x2bee
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x558c
movq %rbp, %rdi
callq 0x55c2
movsbl (%r15), %ecx
cmpl %ecx, %eax
je 0x2c20
addq $0x18, %r13
cmpq %rbx, %r13
jne 0x2bc8
jmp 0x2bf1
movb (%r15), %cl
movsbl %cl, %esi
movsbl 0x24(%r12), %edx
leaq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x5584
movq (%rsp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
movq %r13, %rsi
callq 0x33d2
movb 0x20(%r12), %cl
cmpb $0x1, %cl
jne 0x2c47
movq (%rsp), %rax
movq 0x20(%rax), %rsi
movb $0x1, 0xf(%rsi)
movq 0x10(%rax), %rdi
addq $0x8, %rsi
leaq 0x4427(%rip), %rdx      # 0x7067
xorl %eax, %eax
callq 0x502c
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq %r15, %rdx
callq 0x52e0
movq 0x8(%r12), %r15
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r15
je 0x2d8f
leaq 0x30(%rsp), %r12
leaq 0x18(%rsp), %r13
movq %r12, %rdi
movq %r15, %rsi
callq 0x558c
movq %r12, %rdi
callq 0x55c2
movq (%rsp), %rcx
movq 0x20(%rcx), %rcx
cmpl 0x8(%rcx), %eax
je 0x2d80
movq 0x28(%rcx), %rbp
movq 0x30(%rcx), %r14
cmpq %r14, %rbp
je 0x2cdd
movq %r13, %rdi
movq %rbp, %rsi
callq 0x558c
movq %r13, %rdi
callq 0x55c2
movl %eax, %ebx
movq %r12, %rdi
callq 0x55c2
cmpl %eax, %ebx
je 0x2d80
addq $0x18, %rbp
jmp 0x2cad
movq (%rsp), %r14
movq 0x20(%r14), %rdi
addq $0x28, %rdi
movq %r12, %rsi
callq 0x33d2
xorl %eax, %eax
movl %eax, 0x18(%rsp)
movw $0x0, 0x1c(%rsp)
movq %r12, %rdi
callq 0x55c2
movb %al, 0x18(%rsp)
movq %r12, %rdi
callq 0x55c6
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)
xorl %eax, %eax
movl %eax, (%rbp)
movq 0x20(%r14), %rsi
addq $0x8, %rsi
leaq 0x20(%rbp), %rdi
callq 0x554e
movq 0x20(%r14), %rsi
addq $0x28, %rsi
movq %rbx, %rdi
callq 0x33fe
movq 0x8(%r14), %rdi
movq 0x20(%r14), %rsi
addq $0x8, %rsi
movq %r13, %rdx
movq %rbp, %rcx
movl 0xc(%rsp), %r8d
callq 0x3da0
movq %rbp, %rdi
callq 0x2200
addq $0x18, %r15
cmpq 0x10(%rsp), %r15
jne 0x2c81
cmpl $0x0, 0x8(%rsp)
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq