// File: /workspace/github_cpp_makefile_star2_O3/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 0xbee2(%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 0x11225
leaq 0x2d8(%rbx), %rdi
movq %r14, %rsi
callq 0x4410
movq (%r14), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x75ce
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 0x56fa
movl 0x2d0(%rbx), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x81aa
movq -0xe8(%rbp), %rdi
cmpq %r15, %rdi
je 0x761b
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 0x56fa
movq %r15, %rdi
movq %r14, %rsi
callq 0x10fc0
movq -0xc8(%rbp), %rdi
cmpq %r12, %rdi
je 0x7674
callq 0x4360
movl 0x2d0(%rbx), %eax
movl %eax, -0x78(%rbp)
movb 0x2cc(%rbx), %al
movb %al, -0x74(%rbp)
movq 0x220(%rbx), %rax
xorl %r13d, %r13d
cmpq 0x218(%rbx), %rax
je 0x7a1d
xorl %r15d, %r15d
movq $0x0, -0x38(%rbp)
movq %rbx, -0x48(%rbp)
movl $0x14, %edx
movq %rbx, %rdi
leaq 0xbd9d(%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 0xc0ea(%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 0x1161a
testl %eax, %eax
je 0x77bb
movl $0x1e, %edx
movq %rbx, %rdi
leaq 0xbd1c(%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)
jne 0x79ec
decl %r13d
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x4490
jmp 0x79ec
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), %r14
movq %r14, %rax
movl $0x14, %ecx
mulq %rcx
movq $-0x1, %rcx
cmovoq %rcx, %rax
movq %rax, %rdi
callq 0x4030
movq %rax, %r15
testq %r14, %r14
jle 0x7948
movq %r13, -0x40(%rbp)
movq %r14, -0x70(%rbp)
movq %r15, %r13
movl -0x2c(%rbp), %edi
movl $0x14, %edx
movq %r13, %rsi
callq 0x44b0
movl $0x14, %edx
movq %rbx, %rdi
leaq 0xbc4e(%rip), %rsi      # 0x1348c
callq 0x43f0
leaq 0x5(%r13), %rsi
leaq -0x68(%rbp), %rdi
callq 0x11124
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0xbab0(%rip), %rsi      # 0x13322
callq 0x43f0
movl 0xc(%r13), %esi
movq %r12, %rdi
callq 0x45f0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r12, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x78bf
callq 0x4360
addq $0x14, %r13
decq %r14
jne 0x781f
movb $0x1, %al
movq %rax, -0x38(%rbp)
movq -0x70(%rbp), %rax
testl %eax, %eax
jle 0x79d0
movq %r15, %rbx
movq %r15, %r14
addq $0x5, %r14
shlq $0x2, %rax
leaq (%rax,%rax,4), %r12
xorl %r13d, %r13d
leaq (%r14,%r13), %rsi
leaq -0xa8(%rbp), %r15
movq %r15, %rdi
callq 0x11124
movl 0xc(%rbx,%r13), %edx
movq -0x48(%rbp), %rdi
movq %r15, %rsi
callq 0x81aa
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x792f
callq 0x4360
addq $0x14, %r13
cmpq %r13, %r12
jne 0x78f3
movq -0x40(%rbp), %r13
movq %rbx, %r15
movq -0x48(%rbp), %rbx
jmp 0x79dc
testl %r14d, %r14d
je 0x79d6
movl $0x23, %edx
movq 0x11663(%rip), %r14     # 0x18fc0
movq %r14, %rdi
leaq 0xbb40(%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 0xbaff(%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 0x79dc
movq -0x40(%rbp), %r13
jmp 0x79dc
movb $0x1, %al
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x4450
movl -0x2c(%rbp), %edi
callq 0x46b0
incl %r13d
movq 0x220(%rbx), %rax
subq 0x218(%rbx), %rax
movslq %r13d, %r15
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
cmpq %r15, %rax
ja 0x76ac
jmp 0x7a25
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
andb $0x1, %al
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 0x7a82
jmp 0x7a87
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
cmpq %r15, %rdi
jne 0x7a82
jmp 0x7a87
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
jmp 0x7a7d
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x7a87
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop