// File: /workspace/github_cpp_makefile_star2_O3/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 0xd274(%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 0x695d
leaq 0xd27a(%rip), %r15      # 0x135c1
leaq 0xd279(%rip), %r14      # 0x135c7
movq 0x258(%rbx), %rax
cmpq 0x260(%rbx), %rax
je 0x6950
movl 0x29c(%rbx), %edi
leaq -0x40(%rbp), %rsi
movl $0x14, %edx
leaq -0x60(%rbp), %rcx
callq 0x113a3
movl %eax, %r12d
movl $0x27, %edx
movq %rbx, %rdi
leaq 0xd206(%rip), %rsi      # 0x13592
callq 0x43f0
movq -0x60(%rbp), %r13
testq %r13, %r13
je 0x63b2
movq %r13, %rdi
callq 0x41a0
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x43f0
jmp 0x63c9
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 0xcf4a(%rip), %rsi      # 0x13322
callq 0x43f0
movl $0x6, %edx
movq %rbx, %rdi
leaq 0xd1ce(%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 0x6921
movl -0x40(%rbp), %eax
movl $0x3e, %edx
cmpq $0xb, %rax
ja 0x691a
leaq 0xcf38(%rip), %rcx      # 0x133a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
leaq 0xd18a(%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 0x56fa
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 0x1142c
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x1b0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x1c0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
callq 0x8f5c
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x250(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x260(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x8a74
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x230(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x240(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x8b48
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x270(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x280(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x100(%rbp), %rsi
callq 0x89c8
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x290(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x2a0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0x120(%rbp), %rsi
callq 0x886c
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x2b0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x2c0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x87e8
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
jmp 0x690e
leaq -0x310(%rbp), %rax
movq %rax, -0x320(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0x320(%rbp), %rdi
callq 0x56fa
movq -0x320(%rbp), %rdi
leaq -0x310(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x210(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x220(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8ca4
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x1d0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x1e0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8f0c
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
jmp 0x690e
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 0x56fa
movl -0x1f0(%rbp), %eax
movl %eax, 0x10(%rsp)
movaps -0x200(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x8e28
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0x6950
callq 0x4360
jmp 0x6950
leaq 0xcce7(%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 0x634e
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x696f
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 0x6a3d
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
jmp 0x6a3d
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x6a4e
callq 0x4360
jmp 0x6a4e
jmp 0x6a4b
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x6a60
callq 0x4360
movq %rbx, %rdi
callq 0x4630