// File: /workspace/github_cpp_makefile_star2_O1/luisremis[P]SDFS/Membership.cpp
// Function: Membership::firstJoin()
bool Membership::firstJoin()
{
    logFile<<"calling firstJoin"<< std::endl;

    //set my own addr, ip, timeStamp, roundID
    myTimeStamp = time(NULL);
    my_ip_str = getOwnIPAddr();
    
    //now I have my self as member
    addMember( my_ip_str, myTimeStamp );

    bool joined = false;

    Message msg;
    msg.type = MSG_JOIN;
    msg.roundId = -1;

    ipString2Char4(my_ip_str, msg.carrierAdd);

    msg.timeStamp = myTimeStamp;
    msg.TTL = isIntroducer;	//used to distinguish joining node is Introducer or not

    for(int i=0; (i < nodes.size()) /*&& !joined*/ ; i++){
        int connectionToServer;
        //TCP connect to introducer/other nodes
        logFile <<"Join: Connecting to "<< nodes[i].ip_str << "..." << std::endl;

        int ret = connect_to_server(nodes[i].ip_str.c_str(), port + 1, &connectionToServer);
        if(ret!=0)
        {

            logFile <<"ERROR Join: Cannot connect to "<<nodes[i].ip_str<< std::endl;
            if(!isIntroducer){
            	i--;
            	usleep(200*1000);
            }
            continue;
        }
        else{
            ret = write(connectionToServer, &msg, sizeof(Message) );
            
            Message newMsg;
            read(connectionToServer, &newMsg, sizeof(Message));

            int size = newMsg.timeStamp;
            
            Message * msgs = new Message[size];

            int j=0;
            for(j=0; j < size; j++){
                read(connectionToServer, msgs + j, sizeof(Message));
                logFile<<"FirstJoin: received "<<char42String(msgs[j].carrierAdd)<<" "<<msgs[j].timeStamp<< std::endl;
            }

            if(j == size){
                joined = true;
                for(j=0; j < size; j++)
                	addMember(char42String(msgs[j].carrierAdd), msgs[j].timeStamp);
            }
            else{
                std::cout << "Join: Failed downloading nodes list"<< std::endl;
            	logFile   <<"ERROR Join: Failed downloading nodes list"<< std::endl;
            }

            delete [] msgs;
            close(connectionToServer);
        }    
    }

    //printMember();

    return joined;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
leaq 0xbe96(%rip), %rsi      # 0x13446
movl $0x11, %edx
callq 0x43f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
xorl %edi, %edi
callq 0x4340
movl %eax, 0x2d0(%rbx)
leaq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x113b1
leaq 0x2d8(%rbx), %rdi
movq %r14, %rsi
callq 0x4410
movq (%r14), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x761a
callq 0x4360
leaq -0xd8(%rbp), %r15
movq %r15, -0x10(%r15)
movq 0x2d8(%rbx), %rsi
movq 0x2e0(%rbx), %rdx
addq %rsi, %rdx
leaq -0xe8(%rbp), %r14
movq %r14, %rdi
callq 0x570a
movl 0x2d0(%rbx), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8232
movq -0xe8(%rbp), %rdi
cmpq %r15, %rdi
je 0x7667
callq 0x4360
leaq -0x7f(%rbp), %r14
movl $0x6, -0x5(%r14)
movb $-0x1, -0x1(%r14)
leaq -0xb8(%rbp), %r12
movq %r12, -0x10(%r12)
movq 0x2d8(%rbx), %rsi
movq 0x2e0(%rbx), %rdx
addq %rsi, %rdx
leaq -0xc8(%rbp), %r15
movq %r15, %rdi
callq 0x570a
movq %r15, %rdi
movq %r14, %rsi
callq 0x1114c
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x76c0
callq 0x4360
movl 0x2d0(%rbx), %eax
movl %eax, -0x78(%rbp)
movb 0x2cc(%rbx), %al
movb %al, -0x74(%rbp)
movq 0x220(%rbx), %rax
xorl %r14d, %r14d
cmpq 0x218(%rbx), %rax
je 0x7a82
xorl %r15d, %r15d
movq $0x0, -0x38(%rbp)
movq %rbx, -0x48(%rbp)
movq %r14, %r13
movl $0x14, %edx
movq %rbx, %rdi
leaq 0xbd4e(%rip), %rsi      # 0x13458
callq 0x43f0
movq 0x218(%rbx), %rax
leaq (,%r15,8), %rcx
leaq (%rcx,%rcx,4), %r12
movq (%rax,%r12), %rsi
movq 0x8(%rax,%r12), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r14
movl $0x3, %edx
movq %rax, %rdi
leaq 0xc09b(%rip), %rsi      # 0x137e0
callq 0x43f0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movq 0x218(%rbx), %rax
movq (%rax,%r12), %rdi
movl 0x298(%rbx), %esi
incl %esi
leaq -0x2c(%rbp), %rdx
callq 0x1179a
testl %eax, %eax
je 0x7800
movl $0x1e, %edx
movq %rbx, %rdi
leaq 0xbccd(%rip), %rsi      # 0x1346d
callq 0x43f0
movq 0x218(%rbx), %rax
leaq (%r15,%r15,4), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x8(%rax,%rcx,8), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
cmpb $0x0, 0x2cc(%rbx)
je 0x791c
movq %r13, %r14
jmp 0x7a4c
movq %r13, -0x40(%rbp)
movl -0x2c(%rbp), %edi
movl $0x14, %edx
leaq -0x84(%rbp), %rsi
callq 0x4170
movl -0x2c(%rbp), %edi
movl $0x14, %edx
leaq -0xfc(%rbp), %rsi
callq 0x44b0
movslq -0xf0(%rbp), %r13
movq %r13, %rax
movl $0x14, %ecx
mulq %rcx
movq $-0x1, %rcx
cmovoq %rcx, %rax
movq %rax, %rdi
callq 0x4030
movq %rax, %r12
testq %r13, %r13
jle 0x7931
movq %r12, -0x70(%rbp)
xorl %r15d, %r15d
movl -0x2c(%rbp), %edi
movl $0x14, %edx
movq %r12, %rsi
callq 0x44b0
movl $0x14, %edx
movq %rbx, %rdi
leaq 0xbc09(%rip), %rsi      # 0x1348c
callq 0x43f0
leaq 0x5(%r12), %rsi
leaq -0x68(%rbp), %rdi
callq 0x112b0
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0xba6a(%rip), %rsi      # 0x13322
callq 0x43f0
movl 0xc(%r12), %esi
movq %r14, %rdi
callq 0x45f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x7906
callq 0x4360
incq %r15
addq $0x14, %r12
cmpq %r15, %r13
jne 0x7864
movq -0x70(%rbp), %r12
jmp 0x7934
movq %r13, %r14
decl %r14d
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x4490
jmp 0x7a4c
xorl %r15d, %r15d
cmpl %r13d, %r15d
jne 0x79b5
movb $0x1, %al
testl %r13d, %r13d
jle 0x7a34
movq %rax, -0x38(%rbp)
movq %r12, %rbx
movq %r12, %r14
addq $0x5, %r14
shlq $0x2, %r13
leaq (,%r13,4), %r15
addq %r13, %r15
xorl %r12d, %r12d
leaq (%r14,%r12), %rsi
leaq -0xa8(%rbp), %r13
movq %r13, %rdi
callq 0x112b0
movl 0xc(%rbx,%r12), %edx
movq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x8232
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x79a0
callq 0x4360
addq $0x14, %r12
cmpq %r12, %r15
jne 0x7964
movq %rbx, %r12
movq -0x48(%rbp), %rbx
jmp 0x7a38
movl $0x23, %edx
movq 0x115ff(%rip), %r14     # 0x18fc0
movq %r14, %rdi
leaq 0xbadc(%rip), %rsi      # 0x134a7
callq 0x43f0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movl $0x29, %edx
movq %rbx, %rdi
leaq 0xba9b(%rip), %rsi      # 0x134a1
callq 0x43f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
jmp 0x7a38
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %r14
movq %r12, %rdi
callq 0x4450
movl -0x2c(%rbp), %edi
callq 0x46b0
movabsq $-0x3333333333333333, %r13 # imm = 0xCCCCCCCCCCCCCCCD
incl %r14d
movq 0x220(%rbx), %rax
subq 0x218(%rbx), %rax
movslq %r14d, %r15
sarq $0x3, %rax
imulq %r13, %rax
cmpq %r15, %rax
ja 0x76f8
movq -0x38(%rbp), %rax
movl %eax, %r14d
andb $0x1, %r14b
movl %r14d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
jne 0x7ae0
jmp 0x7ae5
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
cmpq %r15, %rdi
jne 0x7ae0
jmp 0x7ae5
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
jmp 0x7adb
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x7ae5
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop