// File: /workspace/github_cpp_makefile_star2_O2/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 $0x308, %rsp            # imm = 0x308
movq %rdi, %rbx
leaq -0x170(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %edi, %edi
callq 0x4330
movl %eax, %edi
callq 0x4360
movq %rbx, %rdi
callq 0x41d0
leaq 0x99d0(%rip), %rsi      # 0xf54b
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
callq 0x41d0
pushq $0x14
popq %r15
leaq -0x180(%rbp), %r12
leaq 0x9a02(%rip), %r14      # 0xf59f
cmpb $0x0, 0x350(%rbx)
jne 0x5fa6
movq 0x258(%rbx), %rax
cmpq 0x260(%rbx), %rax
je 0x5b9d
movl 0x29c(%rbx), %edi
leaq -0x40(%rbp), %rsi
movl %r15d, %edx
movq %r12, %rcx
callq 0xdbbb
movl %eax, %r13d
movq %rbx, %rdi
leaq 0x998e(%rip), %rsi      # 0xf56a
callq 0x4370
movq -0x180(%rbp), %rsi
movq %rax, %rdi
callq 0x4370
movq %rax, %rdi
leaq 0x9728(%rip), %rsi      # 0xf322
callq 0x4370
movq %rbx, %rdi
leaq 0x9989(%rip), %rsi      # 0xf592
callq 0x4370
movl -0x40(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
leaq 0x9976(%rip), %rsi      # 0xf599
callq 0x4370
movsbl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
movq %r14, %rsi
cmpl $0x14, %r13d
jne 0x5ce8
movl -0x40(%rbp), %eax
leaq 0x998d(%rip), %rsi      # 0xf5e0
cmpq $0xb, %rax
ja 0x5ce8
leaq 0x971c(%rip), %rcx      # 0xf380
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
leaq 0x996e(%rip), %rsi      # 0xf5e0
jmpq *%rax
movl -0x30(%rbp), %eax
movl %eax, -0x130(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x140(%rbp)
leaq -0x2f0(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movaps -0x140(%rbp), %xmm0
movaps %xmm0, -0x160(%rbp)
movl -0x130(%rbp), %eax
movl %eax, -0x150(%rbp)
movl $0x1, -0x160(%rbp)
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
leaq -0x2f0(%rbp), %r13
movq %r13, %rsi
leaq -0x160(%rbp), %rcx
movl %r15d, %r8d
callq 0xdc23
movq %r13, %rdi
jmp 0x5f9c
movq %rbx, %rdi
callq 0x4370
movq %rax, %rdi
callq 0x41d0
jmp 0x5b9d
leaq -0x1d0(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0x188(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x198(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x1d0(%rbp), %r13
movq %r13, %rsi
callq 0x799e
movq %r13, %rdi
jmp 0x5f9c
movl -0x30(%rbp), %eax
movl %eax, -0xb0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0xc0(%rbp)
leaq -0x270(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0xb0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0xc0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x270(%rbp), %r13
movq %r13, %rsi
callq 0x7630
movq %r13, %rdi
jmp 0x5f9c
movl -0x30(%rbp), %eax
movl %eax, -0x90(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0xa0(%rbp)
leaq -0x250(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0x90(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0xa0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x250(%rbp), %r13
movq %r13, %rsi
callq 0x76d2
movq %r13, %rdi
jmp 0x5f9c
movl -0x30(%rbp), %eax
movl %eax, -0xd0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0xe0(%rbp)
leaq -0x290(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0xd0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0xe0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x290(%rbp), %r13
movq %r13, %rsi
callq 0x7594
movq %r13, %rdi
jmp 0x5f9c
movl -0x30(%rbp), %eax
movl %eax, -0xf0(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x100(%rbp)
leaq -0x2b0(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0xf0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x100(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x2b0(%rbp), %r13
movq %r13, %rsi
callq 0x74a4
movq %r13, %rdi
jmp 0x5f9c
movl -0x30(%rbp), %eax
movl %eax, -0x110(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x120(%rbp)
leaq -0x2d0(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0x110(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x120(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x7456
leaq -0x2d0(%rbp), %rdi
jmp 0x5f9c
leaq -0x310(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x4450
movq %r13, %rdi
jmp 0x5f9c
movl -0x30(%rbp), %eax
movl %eax, -0x70(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x80(%rbp)
leaq -0x230(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0x70(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x80(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x77c0
leaq -0x230(%rbp), %rdi
jmp 0x5f9c
leaq -0x1f0(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0x1a0(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x1b0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x7972
leaq -0x1f0(%rbp), %rdi
jmp 0x5f9c
movl -0x30(%rbp), %eax
movl %eax, -0x50(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x60(%rbp)
leaq -0x210(%rbp), %rdi
movq %r12, %rsi
callq 0x4450
movl -0x50(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x60(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x210(%rbp), %r13
movq %r13, %rsi
callq 0x78d2
movq %r13, %rdi
callq 0x41b0
jmp 0x5b9d
leaq -0x180(%rbp), %rdi
callq 0x41b0
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x2f0(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x2d0(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x2b0(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x290(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x270(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x250(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x210(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x1f0(%rbp), %rdi
jmp 0x603a
movq %rax, %rbx
leaq -0x1d0(%rbp), %rdi
callq 0x41b0
jmp 0x6046
jmp 0x6043
movq %rax, %rbx
leaq -0x180(%rbp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x45d0