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

    // check if the requested node be in memberlist
    bool existed = false;
    for (auto entry : memberNode->memberList) {
        if (entry.getid() == msg->srcAddr.addr[0]) {
            existed = true;
            break;
        }
    }

    // add the node into the memberlist
    if (!existed) {
        MemberListEntry entry = MemberListEntry((int)msg->srcAddr.addr[0],
                                                (short)msg->srcAddr.addr[4]);
        memberNode->memberList.push_back(entry);
    }

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

    // send a JOINREP message to the requested node
    size_t msgSize = sizeof(MessageHdr);
    Address dstAddr = msg->srcAddr;
    msg = new MessageHdr;
    msg->msgType = JOINREP;
    msg->srcAddr = memberNode->addr;
    msg->memberList = memberNode->memberList;
    emulNet->ENsend(&memberNode->addr, &dstAddr, (char *)msg, msgSize);

    free(msg);
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x90(%rbp)
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
callq 0x4910
cmpl $0x0, %eax
je 0x3f2c
movb $0x0, -0x1(%rbp)
jmp 0x40e3
movq -0x90(%rbp), %rax
movb $0x0, -0x1d(%rbp)
movq 0x20(%rax), %rax
addq $0x28, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4fc0
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4ff0
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x5020
testb $0x1, %al
jne 0x3f70
jmp 0x3fad
leaq -0x30(%rbp), %rdi
callq 0x5060
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0xaf80
leaq -0x50(%rbp), %rdi
callq 0xb040
movq -0x18(%rbp), %rcx
movsbl 0x20(%rcx), %ecx
cmpl %ecx, %eax
jne 0x3fa0
movb $0x1, -0x1d(%rbp)
jmp 0x3fad
jmp 0x3fa2
leaq -0x30(%rbp), %rdi
callq 0x5080
jmp 0x3f5d
testb $0x1, -0x1d(%rbp)
jne 0x3fe7
movq -0x18(%rbp), %rax
movsbl 0x20(%rax), %esi
movq -0x18(%rbp), %rax
movsbl 0x24(%rax), %eax
leaq -0x68(%rbp), %rdi
movswl %ax, %edx
callq 0xaf50
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
leaq -0x68(%rbp), %rsi
callq 0x50a0
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rdx
addq $0x20, %rdx
callq 0xa880
movq $0x30, -0x70(%rbp)
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
leaq -0x76(%rbp), %rdi
callq 0xae70
movl $0x30, %edi
callq 0x3210
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
callq 0x4ef0
jmp 0x403e
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
movq %rcx, -0x18(%rbp)
movq -0x18(%rbp), %rcx
movl $0x1, (%rcx)
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rdi
addq $0x20, %rdi
callq 0xaea0
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rsi
addq $0x28, %rsi
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x5110
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x18(%rbp), %rcx
movq -0x70(%rbp), %rax
movl %eax, %r8d
leaq -0x76(%rbp), %rdx
callq 0x8440
movq -0x18(%rbp), %rdi
callq 0x32b0
movb $0x1, -0x1(%rbp)
jmp 0x40e3
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x30, %esi
callq 0x3220
jmp 0x40f1
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x33a0
nopw (%rax,%rax)