// File: /workspace/github_cpp_makefile_star2_O3/luisremis[P]SDFS/Membership.cpp
// Function: Membership::detectThread()
void Membership::detectThread()
{
    /*
    WHILE LOOP
        roundId++

        randomly select one node
        send ping message
        sleep(1)
        see msgQueue, search ack
        if(ack)
            cout<<alive
            sleep(4)
            continue
        
        send ping message to random K nodes (call spreadMessage())
        sleep(4)
        see msgQueue, search ack
        if(ack)
            cout<<alive
            continue
        
        if(!ack)
            cout<<fail
            delete node
            send fail message to other nodes
            continue
    */
    bool flagFail = false;
    Node failedNode;
    while(!killDetectingThread)
    {
  
        roundId = (roundId+1)%255;

        if (roundId % 5 == 0)
        {
        	checkLeader();
        }

        logFile<< std::endl << "Detection Thread - Round: "<< roundId << " ------------" << std::endl;
        logFile<<printMember();
        
        if(members.size() < 2)
        {
            usleep(5 * MAX_LATENCY);
            flagFail = false;
            continue;
        }

        int select = rand()%(members.size()-1) + 1;
        Node theNode = members[select];

        if (flagFail == true)
        {
            theNode = failedNode;
        }

        Message msg;
        msg.type = MSG_PING;
        msg.TTL = 0;
        msg.roundId = roundId;
        ipString2Char4(theNode.ip_str, msg.carrierAdd);
        msg.timeStamp = 0;

        logFile<<"detectThread: checking alive or not for "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
        sendUDP(sockfd, theNode.ip_str, port, (char*)&msg, sizeof(Message));

        bool acked = false;

        if (queueSize() > 10){
            std::cout << "Warning!!! queueSize = " << queueSize() << std::endl;
        }

        usleep(MAX_LATENCY);

        msgQueueLock.lock();
        acked = ackMsgQueue();
        msgQueueLock.unlock();

        static int count = 0;
        if(acked){
            logFile<<"detectThread: node alive: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
            flagFail = false;
            usleep(4 * MAX_LATENCY);     
            continue;       
        }

        logFile<<"Hey!!! Node "<<theNode.ip_str<<" did not respond the first time!" << std::endl;

        msg.type = MSG_PIGGY;
        msg.TTL = 0;
        spreadMessage(msg,3);
        usleep(4 * MAX_LATENCY);

        msgQueueLock.lock();
        acked = ackMsgQueue();
        msgQueueLock.unlock();

        if(acked){
            logFile<<"detectThread: second round found node alive: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
            flagFail = false;
            continue;
        }
        else{
            logFile<< "detectThread: No ack received: node failed: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
            
            if (flagFail == true)
            {
                logFile<< "detectThread: No ack received: node failed: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
                failMember(theNode.ip_str, theNode.timeStamp);

                Message failMsg;
                failMsg.type = MSG_FAIL;
                failMsg.roundId = roundId;
                ipString2Char4(theNode.ip_str, failMsg.carrierAdd);
                failMsg.timeStamp = theNode.timeStamp;
                failMsg.TTL = 3;

                spreadMessage(failMsg);
                flagFail = false;
            }
            else
            {
                logFile<< "detectThread: Robust 2: It is alive: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
                failedNode = theNode;
                flagFail = true;

            }


            continue;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
leaq -0x100(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x0, 0x351(%rdi)
jne 0x72fa
movq %rdi, %rbx
leaq -0x40(%rbp), %r14
leaq 0x310(%rdi), %rax
movq %rax, -0x58(%rbp)
xorl %r12d, %r12d
movl 0x2c8(%rbx), %eax
movl %eax, %ecx
incl %ecx
movslq %ecx, %rcx
imulq $-0x7f7f7f7f, %rcx, %rcx # imm = 0x80808081
shrq $0x20, %rcx
addl %eax, %ecx
incl %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x7, %ecx
addl %edx, %ecx
movl %ecx, %edx
shll $0x8, %edx
subl %edx, %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x2c8(%rbx)
imull $0xffffcccd, %eax, %eax # imm = 0xFFFFCCCD
addl $0x1999, %eax           # imm = 0x1999
movzwl %ax, %eax
cmpl $0x3332, %eax           # imm = 0x3332
ja 0x6b08
movq %rbx, %rdi
callq 0x920c
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
movq %rax, %r13
movl $0x1a, %edx
movq %rax, %rdi
leaq 0xcb06(%rip), %rsi      # 0x13647
callq 0x43f0
movl 0x2c8(%rbx), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movl $0xd, %edx
movq %rax, %rdi
leaq 0xcafc(%rip), %rsi      # 0x13662
callq 0x43f0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x936e
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x6bbd
callq 0x4360
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %r15 # imm = 0xCCCCCCCCCCCCCCCD
imulq %r15, %rax
cmpq $0x1, %rax
ja 0x6bf5
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x4490
xorl %r12d, %r12d
jmp 0x72d5
callq 0x4100
cltq
movq 0x258(%rbx), %r13
movq 0x260(%rbx), %rcx
subq %r13, %rcx
sarq $0x3, %rcx
imulq %r15, %rcx
decq %rcx
xorl %edx, %edx
divq %rcx
incl %edx
movslq %edx, %rax
movq %r14, %rcx
leaq (%rax,%rax,4), %r14
movq %rcx, %r15
movq %rcx, -0x50(%rbp)
movq (%r13,%r14,8), %rsi
movq 0x8(%r13,%r14,8), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rdi
callq 0x56fa
leaq (,%r14,8), %rax
addq %r13, %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
testb $0x1, %r12b
movq %r15, %r14
je 0x6c7d
leaq -0x50(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x4210
movq -0xf0(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x70(%rbp)
movb $0x0, -0x60(%rbp)
movb 0x2c8(%rbx), %al
movb %al, -0x6c(%rbp)
leaq -0xd8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x56fa
leaq -0xe8(%rbp), %rdi
leaq -0x6b(%rbp), %rsi
callq 0x10fc0
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0x6cde
callq 0x4360
movl $0x0, -0x64(%rbp)
movl $0x28, %edx
movq %rbx, %rdi
leaq 0xc97c(%rip), %rsi      # 0x13670
callq 0x43f0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc607(%rip), %rsi      # 0x13322
callq 0x43f0
movl -0x30(%rbp), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
leaq -0x50(%rbp), %rsi
leaq -0x70(%rbp), %rcx
movl $0x14, %r8d
callq 0x1142c
movq 0x300(%rbx), %rax
subq 0x2f8(%rbx), %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
cmpl $0xb, %eax
jl 0x6df7
movl $0x17, %edx
movq 0x12223(%rip), %rdi     # 0x18fc0
leaq 0xc8f5(%rip), %rsi      # 0x13699
callq 0x43f0
movq 0x300(%rbx), %rax
subq 0x2f8(%rbx), %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %esi # imm = 0xCCCCCCCD
movq 0x121f8(%rip), %rdi     # 0x18fc0
callq 0x45f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x4490
movq -0x58(%rbp), %rdi
callq 0x44d0
testl %eax, %eax
jne 0x730c
movq 0x2f8(%rbx), %rax
movq 0x300(%rbx), %rcx
cmpq %rax, %rcx
je 0x6ee8
movl 0x2c8(%rbx), %edx
addq $-0x14, %rcx
xorl %r13d, %r13d
movzbl 0x4(%rcx), %esi
movq %rcx, 0x300(%rbx)
cmpl %esi, %edx
sete %sil
orb %sil, %r13b
leaq -0x14(%rcx), %rsi
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x6e36
movq -0x58(%rbp), %rdi
callq 0x42a0
testb $0x1, %r13b
je 0x6ef1
movl $0x1a, %edx
movq %rbx, %rdi
leaq 0xc839(%rip), %rsi      # 0x136b1
callq 0x43f0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc483(%rip), %rsi      # 0x13322
callq 0x43f0
movl -0x30(%rbp), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movl $0xc3500, %edi          # imm = 0xC3500
callq 0x4490
jmp 0x705d
movq -0x58(%rbp), %rdi
callq 0x42a0
movl $0xc, %edx
movq %rbx, %rdi
leaq 0xc7cc(%rip), %rsi      # 0x136cc
callq 0x43f0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
movl $0x20, %edx
movq %rax, %rdi
leaq 0xc7b2(%rip), %rsi      # 0x136d9
callq 0x43f0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movl $0x2, -0x70(%rbp)
movb $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x70(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x3, %esi
callq 0x7c7e
movl $0xc3500, %edi          # imm = 0xC3500
callq 0x4490
movq -0x58(%rbp), %rdi
callq 0x44d0
testl %eax, %eax
jne 0x730c
movq 0x2f8(%rbx), %rax
movq 0x300(%rbx), %rcx
cmpq %rax, %rcx
je 0x7065
movl 0x2c8(%rbx), %edx
addq $-0x14, %rcx
xorl %r13d, %r13d
movzbl 0x4(%rcx), %esi
movq %rcx, 0x300(%rbx)
cmpl %esi, %edx
sete %sil
orb %sil, %r13b
leaq -0x14(%rcx), %rsi
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x6fba
movq -0x58(%rbp), %rdi
callq 0x42a0
testb $0x1, %r13b
je 0x706e
movl $0x2d, %edx
movq %rbx, %rdi
leaq 0xc6fe(%rip), %rsi      # 0x136fa
callq 0x43f0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc2ff(%rip), %rsi      # 0x13322
callq 0x43f0
movl -0x30(%rbp), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
xorl %r12d, %r12d
jmp 0x72c7
movq -0x58(%rbp), %rdi
callq 0x42a0
movl $0x2c, %edx
movq %rbx, %rdi
leaq 0xc6ab(%rip), %rsi      # 0x13728
callq 0x43f0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc27e(%rip), %rsi      # 0x13322
callq 0x43f0
movl -0x30(%rbp), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
testb $0x1, %r12b
je 0x7238
movl $0x2c, %edx
movq %rbx, %rdi
leaq 0xc631(%rip), %rsi      # 0x13728
callq 0x43f0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc204(%rip), %rsi      # 0x13322
callq 0x43f0
movl -0x30(%rbp), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
leaq -0xb8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc8(%rbp), %rdi
callq 0x56fa
movl -0x30(%rbp), %edx
movq %rbx, %rdi
leaq -0xc8(%rbp), %rsi
callq 0x7fc0
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x71a7
callq 0x4360
movl $0x5, -0x88(%rbp)
movb 0x2c8(%rbx), %al
movb %al, -0x84(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xa8(%rbp), %rdi
callq 0x56fa
leaq -0xa8(%rbp), %rdi
leaq -0x83(%rbp), %rsi
callq 0x10fc0
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x720d
callq 0x4360
movl -0x30(%rbp), %eax
movl %eax, -0x7c(%rbp)
movb $0x3, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x88(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7c7e
jmp 0x72c3
movl $0x25, %edx
movq %rbx, %rdi
leaq 0xc50e(%rip), %rsi      # 0x13755
callq 0x43f0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc0b4(%rip), %rsi      # 0x13322
callq 0x43f0
movl -0x30(%rbp), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
leaq -0x110(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4210
movq -0x30(%rbp), %rax
movq %rax, -0xf0(%rbp)
xorb $0x1, %r12b
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x72d5
callq 0x4360
cmpb $0x0, 0x351(%rbx)
je 0x6ab4
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x72fa
callq 0x4360
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x4240
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
jmp 0x7350
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
jmp 0x7350
jmp 0x735c
jmp 0x736f
jmp 0x736a
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0x735f
callq 0x4360
jmp 0x735f
movq %rax, %rbx
movq %r15, %r14
jmp 0x7372
jmp 0x736a
jmp 0x736f
jmp 0x736f
movq %rax, %rbx
jmp 0x7380
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x7380
callq 0x4360
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x7398
callq 0x4360
movq %rbx, %rdi
callq 0x4630