// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
callq 0x4910
cmpl $0x0, %eax
je 0x413c
movb $0x0, -0x1(%rbp)
jmp 0x44d4
movq -0xf0(%rbp), %rsi
leaq -0x22(%rbp), %rdi
callq 0x3960
movq -0xf0(%rbp), %rax
movb $0x0, -0x23(%rbp)
movq 0x20(%rax), %rax
addq $0x28, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4fc0
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4ff0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x5020
testb $0x1, %al
jne 0x4190
jmp 0x41cd
leaq -0x38(%rbp), %rdi
callq 0x5060
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0xaf80
leaq -0x58(%rbp), %rdi
callq 0xb040
movq -0x18(%rbp), %rcx
movsbl 0x20(%rcx), %ecx
cmpl %ecx, %eax
jne 0x41c0
movb $0x1, -0x23(%rbp)
jmp 0x41cd
jmp 0x41c2
leaq -0x38(%rbp), %rdi
callq 0x5080
jmp 0x417d
testb $0x1, -0x23(%rbp)
jne 0x4207
movq -0x18(%rbp), %rax
movsbl 0x20(%rax), %esi
movq -0x18(%rbp), %rax
movsbl 0x24(%rax), %eax
leaq -0x70(%rbp), %rdi
movswl %ax, %edx
callq 0xaf50
movq -0xf0(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
leaq -0x70(%rbp), %rsi
callq 0x50a0
movq -0x18(%rbp), %rax
movsbl 0x20(%rax), %eax
movsbl -0x22(%rbp), %ecx
cmpl %ecx, %eax
jne 0x4240
movq -0xf0(%rbp), %rax
movq 0x20(%rax), %rcx
movb $0x1, 0xf(%rcx)
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
leaq 0xae56(%rip), %rdx      # 0xf08f
movb $0x0, %al
callq 0xa4c0
movq -0xf0(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rdx
addq $0x20, %rdx
callq 0xa880
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x4fc0
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
callq 0x4ff0
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x5020
testb $0x1, %al
jne 0x42a2
jmp 0x44d0
leaq -0x80(%rbp), %rdi
callq 0x5060
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0xaf80
movb $0x0, -0x23(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xb040
movq -0xf0(%rbp), %rcx
movq 0x20(%rcx), %rcx
cmpl 0x8(%rcx), %eax
jne 0x42df
jmp 0x44c2
movq -0xf0(%rbp), %rax
movq 0x20(%rax), %rax
addq $0x28, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x4fc0
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x4ff0
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x5020
testb $0x1, %al
jne 0x4334
jmp 0x438f
leaq -0xb0(%rbp), %rdi
callq 0x5060
movq %rax, %rsi
leaq -0xd0(%rbp), %rdi
callq 0xaf80
leaq -0xd0(%rbp), %rdi
callq 0xb040
movl %eax, -0xf4(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xb040
movl %eax, %ecx
movl -0xf4(%rbp), %eax
cmpl %ecx, %eax
jne 0x437f
movb $0x1, -0x23(%rbp)
jmp 0x438f
jmp 0x4381
leaq -0xb0(%rbp), %rdi
callq 0x5080
jmp 0x431b
testb $0x1, -0x23(%rbp)
jne 0x44c0
movq -0xf0(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
leaq -0xa0(%rbp), %rsi
movq %rsi, -0x108(%rbp)
callq 0x50a0
leaq -0xd6(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x4e70
movq -0x110(%rbp), %rdi
callq 0x5360
movq -0x108(%rbp), %rdi
callq 0xb040
movq -0x108(%rbp), %rdi
movb %al, -0xd6(%rbp)
callq 0xb050
movb %al, -0xd2(%rbp)
movl $0x30, %edi
callq 0x3210
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x100(%rbp)
callq 0x4ef0
jmp 0x441c
movq -0xf0(%rbp), %rax
movq -0x100(%rbp), %rcx
movq %rcx, -0x18(%rbp)
movq -0x18(%rbp), %rcx
movl $0x0, (%rcx)
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0xaea0
movq -0xf0(%rbp), %rax
movq 0x20(%rax), %rsi
addq $0x28, %rsi
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x5110
movq -0xf0(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
leaq -0xd6(%rbp), %rdx
callq 0x8440
movq -0x18(%rbp), %rdi
callq 0x32b0
jmp 0x44c0
movq -0x100(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
movl $0x30, %esi
callq 0x3220
jmp 0x44e2
jmp 0x44c2
leaq -0x80(%rbp), %rdi
callq 0x5080
jmp 0x4289
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0xe0(%rbp), %rdi
callq 0x33a0
nop