// File: luisremis[P]SDFS/Membership.cpp
// Function: Membership::spreadMessage(Message, int)
void Membership::spreadMessage(Message msg, int forwardNumber)
{
    //choose k or size-1 members
    membersLock.lock();
    std::vector<Node> selectedNode = members; 
    membersLock.unlock();

    if (selectedNode.size() < 3) return;   // Not enough nodes to do something
    // We need to remeve myself and the carrier from the possible list

    selectedNode.erase(selectedNode.begin()); // remove myself

    for (int i = 0; i < selectedNode.size(); ++i)
    {
        logFile << "comparing: " << selectedNode.at(i).ip_str << " " << char42String(msg.carrierAdd) << std::endl;
        if ( selectedNode.at(i).ip_str.compare(char42String(msg.carrierAdd)) == 0 )
        {
            selectedNode.erase(selectedNode.begin() + i); // remove carrier
            logFile << "equal" << std::endl;
            break;
        }
    }

    int k = forwardNumber == 0 ? K_FORWARD : forwardNumber;

    while (selectedNode.size() > 0 && k > 0)
    {
        int random = rand() % selectedNode.size();
        sendUDP( sockfd, selectedNode[random].ip_str, port, (char*)&msg, sizeof(Message) );
        selectedNode.erase(selectedNode.begin()+random);
        k--;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
leaq 0x10(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
addq $0x230, %rdi            # imm = 0x230
movq %rdi, -0xe8(%rbp)
callq 0xb5f0
movq -0xf0(%rbp), %rsi
addq $0x258, %rsi            # imm = 0x258
leaq -0x28(%rbp), %rdi
callq 0xbc10
movq -0xe8(%rbp), %rdi
callq 0xb620
jmp 0x8af0
leaq -0x28(%rbp), %rdi
callq 0xb990
cmpq $0x3, %rax
jae 0x8b1c
movl $0x1, -0x38(%rbp)
jmp 0x8f0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x8f1f
leaq -0x28(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0xbd70
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0xbde0
movq -0x108(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xbd00
movq %rax, -0x100(%rbp)
jmp 0x8b56
movq -0x100(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movslq -0x54(%rbp), %rax
movq %rax, -0x110(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xb990
movq %rax, %rcx
movq -0x110(%rbp), %rax
cmpq %rcx, %rax
jae 0x8d9e
movq -0xf0(%rbp), %rdi
leaq 0x13d9e(%rip), %rsi     # 0x1c93b
callq 0x43f0
movq %rax, -0x118(%rbp)
jmp 0x8bab
movslq -0x54(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xb950
movq %rax, -0x120(%rbp)
jmp 0x8bc1
movq -0x120(%rbp), %rsi
movq -0x118(%rbp), %rdi
callq 0x4370
movq %rax, -0x128(%rbp)
jmp 0x8bdd
movq -0x128(%rbp), %rdi
leaq 0x13737(%rip), %rsi     # 0x1c322
callq 0x43f0
movq %rax, -0x130(%rbp)
jmp 0x8bf9
movq -0xf8(%rbp), %rsi
addq $0x5, %rsi
leaq -0x78(%rbp), %rdi
callq 0x1a480
jmp 0x8c0f
movq -0x130(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4370
movq %rax, -0x138(%rbp)
jmp 0x8c28
movq -0x138(%rbp), %rdi
movq 0x1e372(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x8c3d
leaq -0x78(%rbp), %rdi
callq 0x42a0
movslq -0x54(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xb950
movq %rax, -0x140(%rbp)
jmp 0x8c5c
movq -0xf8(%rbp), %rsi
movq -0x140(%rbp), %rax
movq %rax, -0x148(%rbp)
addq $0x5, %rsi
leaq -0x98(%rbp), %rdi
callq 0x1a480
jmp 0x8c83
movq -0x148(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x4040
movl %eax, -0x14c(%rbp)
jmp 0x8c9e
leaq -0x98(%rbp), %rdi
callq 0x42a0
movl -0x14c(%rbp), %eax
cmpl $0x0, %eax
jne 0x8d8e
leaq -0x28(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0xbd70
movq %rax, -0xb0(%rbp)
movslq -0x54(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0xbda0
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xbde0
movq -0x160(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0xbd00
movq %rax, -0x158(%rbp)
jmp 0x8d16
movq -0xf0(%rbp), %rdi
movq -0x158(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq 0x13c15(%rip), %rsi     # 0x1c947
callq 0x43f0
movq %rax, -0x168(%rbp)
jmp 0x8d40
movq -0x168(%rbp), %rdi
movq 0x1e25a(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x8d55
jmp 0x8d9e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x42a0
jmp 0x8f1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x42a0
jmp 0x8f1f
jmp 0x8d90
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x8b68
cmpl $0x0, -0xc(%rbp)
jne 0x8db1
movl $0x4, %eax
movl %eax, -0x16c(%rbp)
jmp 0x8dba
movl -0xc(%rbp), %eax
movl %eax, -0x16c(%rbp)
movl -0x16c(%rbp), %eax
movl %eax, -0xbc(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xb990
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0x16d(%rbp)
jbe 0x8df0
cmpl $0x0, -0xbc(%rbp)
setg %al
movb %al, -0x16d(%rbp)
movb -0x16d(%rbp), %al
testb $0x1, %al
jne 0x8dff
jmp 0x8f06
callq 0x4130
cltq
movq %rax, -0x180(%rbp)
leaq -0x28(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0xb990
movq -0x188(%rbp), %rdi
movq %rax, %rcx
movq -0x180(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq -0xf0(%rbp), %rax
movl %edx, %ecx
movl %ecx, -0xc0(%rbp)
movl 0x29c(%rax), %eax
movl %eax, -0x174(%rbp)
movslq -0xc0(%rbp), %rsi
callq 0xb8d0
movl -0x174(%rbp), %edi
movq -0xf8(%rbp), %rcx
movq %rax, %rsi
movq -0xf0(%rbp), %rax
movl 0x298(%rax), %edx
movl $0x14, %r8d
callq 0x1a870
jmp 0x8e84
leaq -0x28(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0xbd70
movq %rax, -0xd8(%rbp)
movslq -0xc0(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0xbda0
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0xbde0
movq -0x198(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0xbd00
movq %rax, -0x190(%rbp)
jmp 0x8ee4
movq -0x190(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl -0xbc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x8dc6
movl $0x0, -0x38(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xb810
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0xb810
movq -0x30(%rbp), %rdi
callq 0x46a0
nopw %cs:(%rax,%rax)