// File: davidcairuz[P]network-internet-relay-chat/server.cpp
// Function: spread_message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
void spread_message(string message, int speaker) {
    pthread_mutex_lock(&lock);
    
    int pos = active->find(speaker);
    string name = active->clients[pos].name + "#" + to_string(active->clients[pos].id) + ": ";
    
    if (active->clients[pos].channel_name == "$") {
        send_message("Please, join a channel", speaker);
        pthread_mutex_unlock(&lock);
        return;
    }

    if (active->clients[pos].is_muted) {
        send_message("You have the right to remain silent!", speaker);
        pthread_mutex_unlock(&lock);
        return;
    }

    int max_len = Socket::buffer_size - name.size();
    vector<int> to_be_removed;

    for (Client client : active->clients) {
        int conn = client.conn;

        int receiver = active->find(conn);

        // checa canal do destinatário
        if (active->clients[receiver].channel_name != active->clients[pos].channel_name) continue;

        // envia batches da mensagem
        for (int i = 0; (unsigned int)i < message.size(); i += max_len) {
            string actual_message = name + message.substr(i, max_len);
            
            int status = -1, retries = Socket::max_retries;
            while (status == -1 && retries--)
                status = socket_server->Write(actual_message, conn);

            if (status == -1) to_be_removed.push_back(conn);
        }
    }

    for (int client : to_be_removed) active->remove(client);
    pthread_mutex_unlock(&lock);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x238(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
leaq 0x107d8(%rip), %rdi     # 0x152c8
callq 0x3400
movq 0x10854(%rip), %rdi     # 0x15350
movl -0xc(%rbp), %esi
callq 0x7a00
movl %eax, -0x10(%rbp)
movq 0x10842(%rip), %rdi     # 0x15350
movslq -0x10(%rbp), %rsi
callq 0x7c20
movq %rax, %rsi
addq $0x28, %rsi
leaq 0x9616(%rip), %rdx      # 0xe13b
leaq -0x70(%rbp), %rdi
callq 0x7ba0
movq 0x1081b(%rip), %rdi     # 0x15350
movslq -0x10(%rbp), %rsi
callq 0x7c20
movl (%rax), %esi
leaq -0x90(%rbp), %rdi
callq 0x7c40
jmp 0x4b4e
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x7ad0
jmp 0x4b64
leaq 0x9845(%rip), %rdx      # 0xe3b0
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x7a80
jmp 0x4b7a
leaq -0x50(%rbp), %rdi
callq 0x31c0
leaq -0x90(%rbp), %rdi
callq 0x31c0
leaq -0x70(%rbp), %rdi
callq 0x31c0
movq 0x107b1(%rip), %rdi     # 0x15350
movslq -0x10(%rbp), %rsi
callq 0x7c20
movq %rax, %rdi
addq $0x50, %rdi
leaq 0x9587(%rip), %rsi      # 0xe13d
callq 0x7d60
movb %al, -0x239(%rbp)
jmp 0x4bc3
movb -0x239(%rbp), %al
testb $0x1, %al
jne 0x4bd2
jmp 0x4cfc
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x34b0
movq -0x248(%rbp), %rdx
leaq 0x954c(%rip), %rsi      # 0xe13f
leaq -0xc0(%rbp), %rdi
callq 0x3a20
jmp 0x4c01
movl -0xc(%rbp), %esi
leaq -0xc0(%rbp), %rdi
callq 0x4990
jmp 0x4c12
leaq -0xc0(%rbp), %rdi
callq 0x31c0
leaq -0xc1(%rbp), %rdi
callq 0x3330
leaq 0x10697(%rip), %rdi     # 0x152c8
callq 0x3220
movl $0x1, -0xc8(%rbp)
jmp 0x51dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x4c94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x4c88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x31c0
leaq -0x90(%rbp), %rdi
callq 0x31c0
leaq -0x70(%rbp), %rdi
callq 0x31c0
jmp 0x5203
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x51fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x4ceb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x31c0
leaq -0xc1(%rbp), %rdi
callq 0x3330
jmp 0x51fa
movq 0x1064d(%rip), %rdi     # 0x15350
movslq -0x10(%rbp), %rsi
callq 0x7c20
testb $0x1, 0x71(%rax)
je 0x4dcc
leaq -0xe9(%rbp), %rdi
movq %rdi, -0x250(%rbp)
callq 0x34b0
movq -0x250(%rbp), %rdx
leaq 0x941f(%rip), %rsi      # 0xe156
leaq -0xe8(%rbp), %rdi
callq 0x3a20
jmp 0x4d45
movl -0xc(%rbp), %esi
leaq -0xe8(%rbp), %rdi
callq 0x4990
jmp 0x4d56
leaq -0xe8(%rbp), %rdi
callq 0x31c0
leaq -0xe9(%rbp), %rdi
callq 0x3330
leaq 0x10553(%rip), %rdi     # 0x152c8
callq 0x3220
movl $0x1, -0xc8(%rbp)
jmp 0x51dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x4dbb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x31c0
leaq -0xe9(%rbp), %rdi
callq 0x3330
jmp 0x51fa
leaq -0x30(%rbp), %rdi
callq 0x31f0
movq %rax, %rcx
movl $0x1000, %eax           # imm = 0x1000
subq %rcx, %rax
movl %eax, -0xf0(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x7d90
movq 0x10557(%rip), %rax     # 0x15350
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rdi
callq 0x7db0
movq %rax, -0x118(%rbp)
movq -0x110(%rbp), %rdi
callq 0x7de0
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x7e10
testb $0x1, %al
jne 0x4e42
jmp 0x5135
leaq -0x118(%rbp), %rdi
callq 0x7e50
movq %rax, %rsi
leaq -0x198(%rbp), %rdi
callq 0x7e70
jmp 0x4e5f
movl -0x194(%rbp), %eax
movl %eax, -0x19c(%rbp)
movq 0x104de(%rip), %rdi     # 0x15350
movl -0x19c(%rbp), %esi
callq 0x7a00
movl %eax, -0x254(%rbp)
jmp 0x4e85
movl -0x254(%rbp), %eax
movl %eax, -0x1a0(%rbp)
movq 0x104b8(%rip), %rdi     # 0x15350
movslq -0x1a0(%rbp), %rsi
callq 0x7c20
addq $0x50, %rax
movq %rax, -0x260(%rbp)
movq 0x1049a(%rip), %rdi     # 0x15350
movslq -0x10(%rbp), %rsi
callq 0x7c20
movq -0x260(%rbp), %rdi
movq %rax, %rsi
addq $0x50, %rsi
callq 0x7f30
testb $0x1, %al
jne 0x4ed8
jmp 0x4f15
movl $0x3, -0xc8(%rbp)
jmp 0x50f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x51ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x5124
movl $0x0, -0x1a4(%rbp)
movq -0x238(%rbp), %rdi
movl -0x1a4(%rbp), %eax
movq %rax, -0x268(%rbp)
callq 0x31f0
movq %rax, %rcx
movq -0x268(%rbp), %rax
cmpq %rcx, %rax
jae 0x50ed
movq -0x238(%rbp), %rsi
movslq -0x1a4(%rbp), %rdx
movslq -0xf0(%rbp), %rcx
leaq -0x1e8(%rbp), %rdi
callq 0x32f0
jmp 0x4f6e
leaq -0x1c8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x1e8(%rbp), %rdx
callq 0x7f60
jmp 0x4f87
leaq -0x1e8(%rbp), %rdi
callq 0x31c0
movl $0xffffffff, -0x1ec(%rbp) # imm = 0xFFFFFFFF
movl $0x5, -0x1f0(%rbp)
xorl %eax, %eax
cmpl $-0x1, -0x1ec(%rbp)
movb %al, -0x269(%rbp)
jne 0x4fd5
movl -0x1f0(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x1f0(%rbp)
cmpl $0x0, %eax
setne %al
movb %al, -0x269(%rbp)
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x4fe4
jmp 0x509c
movq 0x102d5(%rip), %rax     # 0x152c0
movq %rax, -0x278(%rbp)
leaq -0x210(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x3140
jmp 0x5007
movq -0x278(%rbp), %rdi
movl -0x19c(%rbp), %edx
leaq -0x210(%rbp), %rsi
callq 0x45b0
movl %eax, -0x27c(%rbp)
jmp 0x5028
movl -0x27c(%rbp), %eax
movl %eax, -0x1ec(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x31c0
jmp 0x4fa7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x31c0
jmp 0x5124
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x50df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x31c0
jmp 0x50df
cmpl $-0x1, -0x1ec(%rbp)
jne 0x50bc
leaq -0x108(%rbp), %rdi
leaq -0x19c(%rbp), %rsi
callq 0x7fb0
jmp 0x50ba
jmp 0x50bc
leaq -0x1c8(%rbp), %rdi
callq 0x31c0
movl -0xf0(%rbp), %eax
addl -0x1a4(%rbp), %eax
movl %eax, -0x1a4(%rbp)
jmp 0x4f1f
leaq -0x1c8(%rbp), %rdi
callq 0x31c0
jmp 0x5124
movl $0x0, -0xc8(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x8020
movl -0xc8(%rbp), %eax
testl %eax, %eax
je 0x5111
jmp 0x510f
jmp 0x5113
jmp 0x5113
leaq -0x118(%rbp), %rdi
callq 0x8060
jmp 0x4e26
leaq -0x198(%rbp), %rdi
callq 0x8020
jmp 0x51ee
leaq -0x108(%rbp), %rax
movq %rax, -0x218(%rbp)
movq -0x218(%rbp), %rdi
callq 0x8080
movq %rax, -0x220(%rbp)
movq -0x218(%rbp), %rdi
callq 0x80b0
movq %rax, -0x228(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x80e0
testb $0x1, %al
jne 0x5182
jmp 0x51ba
leaq -0x220(%rbp), %rdi
callq 0x8120
movl (%rax), %eax
movl %eax, -0x22c(%rbp)
movq 0x101b3(%rip), %rdi     # 0x15350
movl -0x22c(%rbp), %esi
callq 0x7980
jmp 0x51aa
jmp 0x51ac
leaq -0x220(%rbp), %rdi
callq 0x8140
jmp 0x5169
leaq 0x10107(%rip), %rdi     # 0x152c8
callq 0x3220
leaq -0x108(%rbp), %rdi
callq 0x8160
movl $0x0, -0xc8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x31c0
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
leaq -0x108(%rbp), %rdi
callq 0x8160
leaq -0x30(%rbp), %rdi
callq 0x31c0
movq -0x98(%rbp), %rdi
callq 0x3490
nop