// File: /workspace/github_cpp_makefile_star2_O1/luisremis[P]SDFS/Membership.cpp
// Function: Membership::listeningThread()
void Membership::listeningThread()
{
    //char buffer[BUFFER_MAX];
    struct Message msg;
    std::string sender;
    srand (time(NULL));
    logFile<< std::endl <<"listeningThread: thread begins"<<std::endl; 
    while (!killListeningThread)
    {
        if (members.empty())
        {
            //In case the node decided to leave
            continue;
        }

        int byte_count = receiveUDP(sockfd, (char*)&msg, sizeof(msg), sender);

        logFile << "listeningThread: Receive message from: " << sender.c_str() << " ";
        logFile << "type: " << msg.type << "ttl: " << (int)msg.TTL << std::endl;

        if (byte_count != sizeof(msg))
        {
            logFile << "ERROR listeningThread: size receiving incorrect: Message dropped" << std::endl;
            continue;
        }
        
        switch (msg.type)
        {
            case MSG_PING:  
                pingMsg(msg, sender);
                break;
            case MSG_ACK:
                ackMsg(msg, sender);
                break;
            case MSG_PIGGY:         
                piggyMsg(msg, sender);
                break;
            case MSG_PIGGY_PING:    
                piggyPingMsg(msg, sender);
                break;
            case MSG_PIGGY_ACK:     
                piggyAckMsg(msg, sender);
                break;
            case MSG_FAIL:          
                failMsg(msg, sender);
                break;
            case MSG_JOIN:          
                joinMsg(msg, sender);
                break;
            case MSG_LEAVE:         
                leaveMsg(msg, sender);
                break;
            case MSG_ELECTION:         
                electionMsg(msg, sender);
                break;
            case MSG_BULLY:         
                bullyMsg(msg, sender);
                break;
            case MSG_NEWLEADER:         
                newLeaderMsg(msg, sender);
                break;
            
            default:
        	   logFile<<"ERROR: listeningThread: received msg does not belong to a type"<<std::endl;
        }

    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rdi, %rbx
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %edi, %edi
callq 0x4340
movl %eax, %edi
callq 0x4370
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, %r14
leaq 0xd246(%rip), %rsi      # 0x13573
movl $0x1e, %edx
movq %rax, %rdi
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
cmpb $0x0, 0x350(%rbx)
jne 0x698b
leaq 0xd24c(%rip), %r15      # 0x135c1
leaq 0xd24b(%rip), %r14      # 0x135c7
movq 0x258(%rbx), %rax
cmpq 0x260(%rbx), %rax
je 0x697e
movl 0x29c(%rbx), %edi
leaq -0x40(%rbp), %rsi
movl $0x14, %edx
leaq -0x60(%rbp), %rcx
callq 0x1152d
movl %eax, %r12d
movl $0x27, %edx
movq %rbx, %rdi
leaq 0xd1d8(%rip), %rsi      # 0x13592
callq 0x43f0
movq -0x60(%rbp), %r13
testq %r13, %r13
je 0x63e0
movq %r13, %rdi
callq 0x41a0
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x43f0
jmp 0x63f7
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x45b0
movl $0x1, %edx
movq %rbx, %rdi
leaq 0xcf1c(%rip), %rsi      # 0x13322
callq 0x43f0
movl $0x6, %edx
movq %rbx, %rdi
leaq 0xd1a0(%rip), %rsi      # 0x135ba
callq 0x43f0
movl -0x40(%rbp), %esi
movq %rbx, %rdi
callq 0x45f0
movq %rax, %r13
movl $0x5, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x43f0
movsbl -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 $0x40, %edx
movq %r14, %rsi
cmpl $0x14, %r12d
jne 0x694f
movl -0x40(%rbp), %eax
movl $0x3e, %edx
cmpq $0xb, %rax
ja 0x6948
leaq 0xcf0a(%rip), %rcx      # 0x133a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
leaq 0xd15c(%rip), %rsi      # 0x13608
jmpq *%rax
movl -0x30(%rbp), %eax
movl %eax, -0x2d0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x2e0(%rbp)
leaq -0x130(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x140(%rbp), %rdi
callq 0x570a
movaps -0x2e0(%rbp), %xmm0
movaps %xmm0, -0x300(%rbp)
movl -0x2d0(%rbp), %eax
movl %eax, -0x2f0(%rbp)
movl $0x1, -0x300(%rbp)
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
leaq -0x140(%rbp), %rsi
leaq -0x300(%rbp), %rcx
movl $0x14, %r8d
callq 0x115b6
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x1b0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x1c0(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %rdi
callq 0x570a
movl -0x1b0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x1c0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
callq 0x8fc6
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x250(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x260(%rbp)
leaq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0xe0(%rbp), %rdi
callq 0x570a
movl -0x250(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x260(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x8ade
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x230(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x240(%rbp)
leaq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %rdi
callq 0x570a
movl -0x230(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x240(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x8bb2
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x270(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x280(%rbp)
leaq -0xf0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x100(%rbp), %rdi
callq 0x570a
movl -0x270(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x280(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x100(%rbp), %rsi
callq 0x8a32
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x290(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x2a0(%rbp)
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x120(%rbp), %rdi
callq 0x570a
movl -0x290(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x2a0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x120(%rbp), %rsi
callq 0x88d6
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x2b0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x2c0(%rbp)
leaq -0x190(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x1a0(%rbp), %rdi
callq 0x570a
movl -0x2b0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x2c0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8852
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
jmp 0x693c
leaq -0x310(%rbp), %rax
movq %rax, -0x320(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x320(%rbp), %rdi
callq 0x570a
movq -0x320(%rbp), %rdi
leaq -0x310(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x210(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x220(%rbp)
leaq -0x170(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x180(%rbp), %rdi
callq 0x570a
movl -0x210(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x220(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8d0e
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x1d0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x1e0(%rbp)
leaq -0x150(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x160(%rbp), %rdi
callq 0x570a
movl -0x1d0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x1e0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8f76
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
jmp 0x693c
movl -0x30(%rbp), %eax
movl %eax, -0x1f0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x200(%rbp)
leaq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %rdi
callq 0x570a
movl -0x1f0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x200(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x8e92
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0x697e
callq 0x4360
jmp 0x697e
leaq 0xccb9(%rip), %rsi      # 0x13608
movq %rbx, %rdi
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
cmpb $0x0, 0x350(%rbx)
je 0x637c
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x699d
callq 0x4360
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
jmp 0x6a6b
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x6a7c
callq 0x4360
jmp 0x6a7c
jmp 0x6a79
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x6a8e
callq 0x4360
movq %rbx, %rdi
callq 0x4630