// File: /workspace/github_cpp_makefile_star2_O1/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 -0x108(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x0, 0x351(%rdi)
jne 0x732d
movq %rdi, %rbx
leaq -0x40(%rbp), %r15
leaq 0x310(%rdi), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x1, %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 $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
addl $0x19999999, %eax       # imm = 0x19999999
cmpl $0x33333332, %eax       # imm = 0x33333332
ja 0x6b3e
movq %rbx, %rdi
callq 0x9276
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 0xcad0(%rip), %rsi      # 0x13647
callq 0x43f0
movl 0x2c8(%rbx), %esi
movq %r13, %rdi
callq 0x45f0
movq %rax, %r13
movl $0xd, %edx
movq %rax, %rdi
leaq 0xcac6(%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 0x93de
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rbx, %rdi
callq 0x43f0
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x6bf3
callq 0x4360
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
imulq %r14, %rax
cmpq $0x1, %rax
ja 0x6c30
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x4490
movq $0x0, -0x58(%rbp)
jmp 0x7320
callq 0x4100
cltq
movq 0x258(%rbx), %r13
movq 0x260(%rbx), %rcx
subq %r13, %rcx
sarq $0x3, %rcx
imulq %r14, %rcx
decq %rcx
xorl %edx, %edx
divq %rcx
incl %edx
movslq %edx, %rax
movq %r15, %rcx
leaq (%rax,%rax,4), %r15
movq %rcx, %r14
movq %rcx, -0x50(%rbp)
movq (%r13,%r15,8), %rsi
movq 0x8(%r13,%r15,8), %rdx
addq %rsi, %rdx
leaq -0x50(%rbp), %rdi
callq 0x570a
leaq (,%r15,8), %rax
addq %r13, %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
testb $0x1, -0x58(%rbp)
movq %r14, %r15
je 0x6cb8
leaq -0x50(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x4210
movq -0xf8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x78(%rbp)
movb $0x0, -0x68(%rbp)
movb 0x2c8(%rbx), %al
movb %al, -0x74(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x570a
leaq -0xf0(%rbp), %rdi
leaq -0x73(%rbp), %rsi
callq 0x1114c
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x6d19
callq 0x4360
movl $0x0, -0x6c(%rbp)
movl $0x28, %edx
movq %rbx, %rdi
leaq 0xc941(%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 0xc5cc(%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 -0x78(%rbp), %rcx
movl $0x14, %r8d
callq 0x115b6
movq 0x300(%rbx), %rax
subq 0x2f8(%rbx), %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
cmpl $0xb, %eax
jl 0x6e32
movl $0x17, %edx
movq 0x121e8(%rip), %rdi     # 0x18fc0
leaq 0xc8ba(%rip), %rsi      # 0x13699
callq 0x43f0
movq 0x300(%rbx), %rax
subq 0x2f8(%rbx), %rax
shrq $0x2, %rax
imull $0xcccccccd, %eax, %esi # imm = 0xCCCCCCCD
movq 0x121bd(%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 -0x60(%rbp), %rdi
callq 0x44d0
testl %eax, %eax
jne 0x7357
movq 0x2f8(%rbx), %rax
movq 0x300(%rbx), %rcx
cmpq %rax, %rcx
je 0x6f24
movl 0x2c8(%rbx), %edx
addq $-0x14, %rcx
xorl %r13d, %r13d
movzbl 0x4(%rcx), %esi
cmpl %esi, %edx
movzbl %r13b, %r13d
cmovel %r12d, %r13d
movq %rcx, 0x300(%rbx)
leaq -0x14(%rcx), %rsi
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x6e71
movq -0x60(%rbp), %rdi
callq 0x42a0
testb $0x1, %r13b
je 0x6f2d
movl $0x1a, %edx
movq %rbx, %rdi
leaq 0xc7fd(%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 0xc447(%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 0x709a
movq -0x60(%rbp), %rdi
callq 0x42a0
movl $0xc, %edx
movq %rbx, %rdi
leaq 0xc790(%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 0xc776(%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, -0x78(%rbp)
movb $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x78(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x3, %esi
callq 0x7d06
movl $0xc3500, %edi          # imm = 0xC3500
callq 0x4490
movq -0x60(%rbp), %rdi
callq 0x44d0
testl %eax, %eax
jne 0x7357
movq 0x2f8(%rbx), %rax
movq 0x300(%rbx), %rcx
cmpq %rax, %rcx
je 0x70a7
movl 0x2c8(%rbx), %edx
addq $-0x14, %rcx
xorl %r13d, %r13d
movzbl 0x4(%rcx), %esi
cmpl %esi, %edx
movzbl %r13b, %r13d
cmovel %r12d, %r13d
movq %rcx, 0x300(%rbx)
leaq -0x14(%rcx), %rsi
cmpq %rax, %rcx
movq %rsi, %rcx
jne 0x6ff6
movq -0x60(%rbp), %rdi
callq 0x42a0
testb $0x1, %r13b
je 0x70b0
movl $0x2d, %edx
movq %rbx, %rdi
leaq 0xc6c1(%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 0xc2c2(%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
movq $0x0, -0x58(%rbp)
jmp 0x7312
movq -0x60(%rbp), %rdi
callq 0x42a0
movl $0x2c, %edx
movq %rbx, %rdi
leaq 0xc669(%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 0xc23c(%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, -0x58(%rbp)
je 0x727d
movl $0x2c, %edx
movq %rbx, %rdi
leaq 0xc5ef(%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 0xc1c2(%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 -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %rdi
callq 0x570a
movl -0x30(%rbp), %edx
movq %rbx, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x8040
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x71e9
callq 0x4360
movl $0x5, -0x90(%rbp)
movb 0x2c8(%rbx), %al
movb %al, -0x8c(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xb0(%rbp), %rdi
callq 0x570a
leaq -0xb0(%rbp), %rdi
leaq -0x8b(%rbp), %rsi
callq 0x1114c
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x724f
callq 0x4360
movl -0x30(%rbp), %eax
movl %eax, -0x84(%rbp)
movb $0x3, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x90(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7d06
jmp 0x7308
movl $0x25, %edx
movq %rbx, %rdi
leaq 0xc4c9(%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 0xc06f(%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 -0x118(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4210
movq -0x30(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x58(%rbp), %rax
xorb $0x1, %al
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x7320
callq 0x4360
cmpb $0x0, 0x351(%rbx)
je 0x6aed
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x7345
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 -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
jmp 0x739b
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
jmp 0x739b
jmp 0x73a7
jmp 0x73ba
jmp 0x73b5
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x73aa
callq 0x4360
jmp 0x73aa
movq %rax, %rbx
movq %r14, %r15
jmp 0x73bd
jmp 0x73b5
jmp 0x73ba
jmp 0x73ba
movq %rax, %rbx
jmp 0x73cb
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x73cb
callq 0x4360
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x73e3
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop