// File: tonymuu[P]distributed-kv-store/MP1Node.cpp
// Function: MP1Node::introduceSelfToGroup(Address*)
int MP1Node::introduceSelfToGroup(Address *joinaddr) {
	MessageHdr *msg;
#ifdef DEBUGLOG
    static char s[1024];
#endif

    if ( 0 == strcmp((char *)&(memberNode->addr.addr), (char *)&(joinaddr->addr))) {
        // I am the group booter (first process to join the group). Boot up the group
#ifdef DEBUGLOG
        log->LOG(&memberNode->addr, "Starting up group...");
#endif
        memberNode->inGroup = true;
        // add myself to my memberlist
        int id = 0;
        short port;
        setIdAndPortFromAddress(memberNode->addr, &id, &port);
        MemberListEntry entry = MemberListEntry(id, port, 0, par->getcurrtime());
        memberNode->memberList.push_back(entry);

        // log to debug log about new node join
        log->logNodeAdd(&memberNode->addr, &memberNode->addr);
    }
    else {
        size_t msgsize = sizeof(MessageHdr) + sizeof(joinaddr->addr) + sizeof(long) + 1;
        msg = (MessageHdr *) malloc(msgsize * sizeof(char));

        // create JOINREQ message: format of data is {struct Address myaddr}
        msg->msgType = JOINREQ;
        memcpy((char *)(msg+1), &memberNode->addr.addr, sizeof(memberNode->addr.addr));
        memcpy((char *)(msg+1) + 1 + sizeof(memberNode->addr.addr), &memberNode->heartbeat, sizeof(long));

#ifdef DEBUGLOG
        sprintf(s, "Trying to join...");
        log->LOG(&memberNode->addr, s);
#endif

        // send JOINREQ message to introducer member
        emulNet->ENsend(&memberNode->addr, joinaddr, (char *)msg, msgsize);

        free(msg);
    }

    return 1;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x20(%rax), %rdi
addq $0x8, %rdi
movq -0x10(%rbp), %rsi
callq 0x44c0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x5eb8
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
leaq 0x26259(%rip), %rdx     # 0x2c065
movb $0x0, %al
callq 0x17ae0
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rcx
movb $0x1, 0xf(%rcx)
movl $0x0, -0x1c(%rbp)
movq 0x20(%rax), %rsi
addq $0x8, %rsi
leaq -0x24(%rbp), %rdi
callq 0x18e40
movq -0x50(%rbp), %rdi
leaq -0x24(%rbp), %rsi
leaq -0x1c(%rbp), %rdx
leaq -0x1e(%rbp), %rcx
callq 0x5ff0
movq -0x50(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, -0x58(%rbp)
movw -0x1e(%rbp), %cx
movw %cx, -0x52(%rbp)
movq 0x18(%rax), %rdi
callq 0x18c60
movl -0x58(%rbp), %esi
movl %eax, %ecx
movw -0x52(%rbp), %ax
movslq %ecx, %r8
leaq -0x40(%rbp), %rdi
xorl %ecx, %ecx
movswl %ax, %edx
callq 0x18f20
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
leaq -0x40(%rbp), %rsi
callq 0x7210
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq 0x20(%rax), %rdx
addq $0x8, %rdx
callq 0x17ea0
jmp 0x5f65
movq $0x13, -0x48(%rbp)
movq -0x48(%rbp), %rdi
shlq $0x0, %rdi
callq 0x4490
movq -0x50(%rbp), %rcx
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rcx), %rdx
movl 0x8(%rdx), %esi
movl %esi, 0x4(%rax)
movw 0xc(%rdx), %dx
movw %dx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x20(%rcx), %rcx
movq 0x18(%rcx), %rcx
movq %rcx, 0xb(%rax)
leaq 0x384d4(%rip), %rdi     # 0x3e3e0
leaq 0x26167(%rip), %rsi     # 0x2c07a
movb $0x0, %al
callq 0x40a0
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
leaq 0x384af(%rip), %rdx     # 0x3e3e0
movb $0x0, %al
callq 0x17ae0
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %rax
movl %eax, %r8d
callq 0xef60
movq -0x18(%rbp), %rdi
callq 0x4420
movl $0x1, %eax
addq $0x60, %rsp
popq %rbp
retq