// File: /workspace/github_cpp_makefile_star2_O3/AraKhachatryan[P]messenger/server.cpp
// Function: client_thread(void*)
void * client_thread ( void * client_socket_fd )
{
    char client_name[50];
    bzero(client_name, sizeof(client_name));

    char encoded_message[512];
    std::vector<std::string> dest_names;
    std::string message_info;
    std::string message;
    char output[512];


    int fd = *(int *)client_socket_fd;

    recv(fd, client_name, sizeof(client_name), 0);


    // Notify to all clients that this connection is online
    std::string active_clients;
    size_t clinets_count = 0;
    for( const auto& client: clients_list )
    {
        active_clients += client.first;
        ++clinets_count;
        if ( clinets_count < clients_list.size() ) {
            active_clients += ",";
        }

        make_encoded_message(client_name, "Server:online", std::string(" is online"), sizeof(output), output);
        //print_encoded_message(output, sizeof(output));

        send(client.second, output, sizeof(output), 0);
    }

    // Notify to this connection for available online clients
    if ( !clients_list.empty() )
    {
        std::string is_are;
        if( clinets_count > 1 )
        {
            is_are = std::string(" are online");
        } else {
            is_are = std::string(" is online");
        }
        make_encoded_message(active_clients.c_str(), "Server:online", is_are, sizeof(output), output);
        //print_encoded_message(output, sizeof(output));

        send(fd, output, sizeof(output), 0);
    }


    clients_list.insert(std::pair<std::string, int>(std::string(client_name), fd));


    std::cout << "Thread ID: " << pthread_self() << " socket_fd: " << fd << " Name: " << client_name << std::endl;

    while(1)
    {

        bzero(encoded_message, sizeof(encoded_message));
        dest_names.clear();
        message_info.clear();
        message.clear();
        bzero(output, sizeof(output));


        int rv = recv(fd, encoded_message, sizeof(encoded_message), 0);
        if ( rv  == -1 )
        {
            std::cerr << "Error reading from client: " << std::strerror(errno) << std::endl;
            break;
        } else if ( rv  == 0 ) {
            break;
        }

        //print_encoded_message(encoded_message, sizeof(encoded_message));

        decode_message(encoded_message, sizeof(encoded_message), dest_names, message_info, message);


        // Private messages
        if ( !dest_names.empty() )
        {
            // for each destination names in encoded_message do
            for( const auto& d_name: dest_names )
            {
                int dest_socket_desc;

                // find dest_socket_desc
                std::map <std::string, int>::iterator it = clients_list.find(d_name);
                if (it != clients_list.end()) {
                    dest_socket_desc = it->second;
                } else {
                    std::cout << "From \"" << client_name << "\": destination name \"" << d_name << "\" not found" << std::endl;
                    continue;
                }

                std::cout << client_name << " -> " << d_name << ": " << message << std::endl;

                make_encoded_message(client_name, "Private", message, sizeof(output), output);

                // and send message to destination
                send(dest_socket_desc, output, sizeof(output), 0);

            }
        // Public messages
        } else {
            for( const auto& client: clients_list )
            {
                // send message to all except sender
                if ( client.first.compare(client_name) == 0 )
                {
                    continue;
                }
                std::cout << client_name << " -> " << client.first << ": " << message << std::endl;

                make_encoded_message(client_name, "Public", message, sizeof(output), output);

                send(client.second, output, sizeof(output), 0);
            }
        }

    }

    // removing connection information from clients_list
    clients_list.erase(client_name);

    // Notify to all clients that this connection is leaving
    for( const auto& client: clients_list )
    {
        make_encoded_message(client_name, "Server:online", std::string(" left the chat"), sizeof(output), output);
        //print_encoded_message(output, sizeof(output));

        send(client.second, output, sizeof(output), 0);
    }

    std::cout << "Closing connection with socket_fd: " << fd << ".  Name: " << client_name
                << ".  Exiting the thread." << std::endl;

    close(fd);
    pthread_exit(NULL);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4f8, %rsp            # imm = 0x4F8
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %rsi
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movw $0x0, 0x30(%rsi)
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
leaq 0x80(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x40(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movl (%rdi), %edi
movl $0x32, %edx
movl %edi, 0xc(%rsp)
xorl %ecx, %ecx
callq 0x20d0
leaq 0xe0(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x3965(%rip), %rbx      # 0x61e0
leaq 0x394e(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x28ed
cmpq $0x0, 0x3961(%rip)      # 0x61f0
jne 0x2a15
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0x2110
leaq (%rsp,%rax), %rdx
addq $0x90, %rdx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x33a0
leaq 0x100(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x10(%rsp), %rax
cmpq %r15, %rax
jne 0x2adf
movups (%r15), %xmm0
movups %xmm0, (%r14)
jmp 0x2af4
leaq 0x100(%rsp), %r15
xorl %r12d, %r12d
leaq 0xd0(%rsp), %r14
leaq 0xf0(%rsp), %rbp
leaq 0x2f0(%rsp), %r13
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq %r14, %rdi
callq 0x20b0
incq %r12
cmpq 0x38c6(%rip), %r12      # 0x61f0
jae 0x293b
movq %r14, %rdi
leaq 0x1796(%rip), %rsi      # 0x40cc
callq 0x2320
movq %r15, 0xf0(%rsp)
movq %rbp, %rdi
leaq 0x178f(%rip), %rsi      # 0x40dc
leaq 0x1792(%rip), %rdx      # 0x40e6
callq 0x33a0
movl $0x200, %ecx            # imm = 0x200
leaq 0x90(%rsp), %rdi
leaq 0x1761(%rip), %rsi      # 0x40ce
movq %rbp, %rdx
movq %r13, %r8
callq 0x39d9
movq 0xf0(%rsp), %rdi
cmpq %r15, %rdi
je 0x2995
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
movl 0x40(%rbx), %edi
movl $0x200, %edx            # imm = 0x200
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x2140
movq %rbx, %rdi
callq 0x21a0
movq %rax, %rbx
leaq 0x3817(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x2910
cmpq $0x0, 0x3826(%rip)      # 0x61f0
je 0x2895
movq %r15, 0xf0(%rsp)
movq $0x0, 0xf8(%rsp)
movb $0x0, 0x100(%rsp)
cmpq $0x2, %r12
jb 0x2a2d
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x16e5(%rip), %rsi      # 0x40e7
leaq 0x16e9(%rip), %rdx      # 0x40f2
leaq 0x10(%rsp), %rdi
callq 0x33a0
jmp 0x2a4e
leaq 0x100(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x169f(%rip), %rsi      # 0x40dc
leaq 0x16a2(%rip), %rdx      # 0x40e6
leaq 0x10(%rsp), %rdi
callq 0x33a0
leaq 0xf0(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x2270
movq (%rbx), %rdi
cmpq %r14, %rdi
je 0x2a78
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0xd0(%rsp), %rdi
leaq 0x1647(%rip), %rsi      # 0x40ce
leaq 0xf0(%rsp), %rdx
leaq 0x2f0(%rsp), %r8
movl $0x200, %ecx            # imm = 0x200
callq 0x39d9
leaq 0x2f0(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movl 0xc(%rsp), %edi
xorl %ecx, %ecx
callq 0x2140
movq 0xf0(%rsp), %rdi
cmpq %r15, %rdi
je 0x2895
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x2895
movq %rax, 0xf0(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x18(%rsp), %rax
leaq 0xf0(%rsp), %rsi
movq %rax, 0x8(%rsi)
movq %r15, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x20(%rsi)
leaq 0x36a2(%rip), %rdi      # 0x61c8
callq 0x3436
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x2b48
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x2b5f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x346a(%rip), %rdi      # 0x5fd0
leaq 0x1586(%rip), %rsi      # 0x40f3
movl $0xb, %edx
callq 0x2250
callq 0x21f0
movq 0x344d(%rip), %rdi      # 0x5fd0
movq %rax, %rsi
callq 0x2180
movq %rax, %rbx
leaq 0x15e9(%rip), %rsi      # 0x417e
movl $0xc, %edx
movq %rax, %rdi
callq 0x2250
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x22c0
movq %rax, %rbx
leaq 0x15d5(%rip), %rsi      # 0x418d
movl $0x7, %edx
movq %rax, %rdi
callq 0x2250
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x2110
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2250
movq %rbx, %rdi
callq 0x2130
leaq 0x90(%rsp), %r12
movq 0x33d6(%rip), %r13      # 0x5fd0
movl $0x200, %edx            # imm = 0x200
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x2160
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r14
cmpq %rbx, %r14
je 0x2c4a
leaq 0x10(%rbx), %r15
movq -0x10(%r15), %rdi
cmpq %rdi, %r15
je 0x2c35
movq (%r15), %rsi
incq %rsi
callq 0x2220
leaq 0x20(%r15), %rax
addq $0x10, %r15
cmpq %r14, %r15
movq %rax, %r15
jne 0x2c21
movq %rbx, 0x58(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x78(%rsp)
movq 0x70(%rsp), %rax
movb %cl, (%rax)
movq %rcx, 0x38(%rsp)
movq 0x30(%rsp), %rax
movb %cl, (%rax)
movl $0x200, %edx            # imm = 0x200
leaq 0x2f0(%rsp), %rdi
xorl %esi, %esi
callq 0x2160
movl $0x200, %edx            # imm = 0x200
movl 0xc(%rsp), %edi
leaq 0xf0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x20d0
testl %eax, %eax
je 0x2feb
cmpl $-0x1, %eax
je 0x2f5e
movl $0x200, %esi            # imm = 0x200
leaq 0xf0(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x3aae
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %r15
cmpq %r15, %r14
je 0x2e5d
leaq 0x34ec(%rip), %rdi      # 0x61c8
movq %r14, %rsi
callq 0x3600
leaq 0x34e5(%rip), %rcx      # 0x61d0
cmpq %rcx, %rax
je 0x2dbf
movl 0x40(%rax), %ebp
movq %r12, %rdi
callq 0x2110
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2250
movl $0x4, %edx
movq %r13, %rdi
leaq 0x1428(%rip), %rsi      # 0x4144
callq 0x2250
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r13, %rdi
callq 0x2250
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x13ba(%rip), %rsi      # 0x40fc
callq 0x2250
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x2250
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl $0x200, %ecx            # imm = 0x200
movq %r12, %rdi
leaq 0x13b7(%rip), %rsi      # 0x4149
leaq 0x30(%rsp), %rdx
leaq 0x2f0(%rsp), %r8
callq 0x39d9
movl $0x200, %edx            # imm = 0x200
movl %ebp, %edi
leaq 0x2f0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x2140
jmp 0x2e4b
movl $0x6, %edx
movq %r13, %rdi
leaq 0x134d(%rip), %rsi      # 0x411b
callq 0x2250
movq %r12, %rdi
callq 0x2110
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2250
movl $0x15, %edx
movq %r13, %rdi
leaq 0x132a(%rip), %rsi      # 0x4122
callq 0x2250
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r13, %rdi
callq 0x2250
movq %rax, %rbp
movl $0xb, %edx
movq %rax, %rdi
leaq 0x131a(%rip), %rsi      # 0x4138
callq 0x2250
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbp, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
addq $0x20, %r14
cmpq %r15, %r14
jne 0x2cd5
jmp 0x2bfa
movq 0x337c(%rip), %r14      # 0x61e0
leaq 0x3365(%rip), %rax      # 0x61d0
cmpq %rax, %r14
je 0x2bfa
leaq 0x20(%r14), %rdi
movq %r12, %rsi
callq 0x20a0
testl %eax, %eax
je 0x2f4e
movq %r12, %rdi
callq 0x2110
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2250
movl $0x4, %edx
movq %r13, %rdi
leaq 0x1297(%rip), %rsi      # 0x4144
callq 0x2250
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %r13, %rdi
callq 0x2250
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x1228(%rip), %rsi      # 0x40fc
callq 0x2250
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x2250
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl $0x200, %ecx            # imm = 0x200
movq %r12, %rdi
leaq 0x122d(%rip), %rsi      # 0x4151
leaq 0x30(%rsp), %rdx
leaq 0x2f0(%rsp), %r8
callq 0x39d9
movl 0x40(%r14), %edi
movl $0x200, %edx            # imm = 0x200
leaq 0x2f0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x2140
movq %r14, %rdi
callq 0x21a0
movq %rax, %r14
jmp 0x2e64
movq 0x308b(%rip), %rbx      # 0x5ff0
leaq 0x1193(%rip), %rsi      # 0x40ff
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x2250
callq 0x2050
movl (%rax), %edi
callq 0x20e0
testq %rax, %rax
jne 0x2fa3
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22b0
jmp 0x2fc0
movq %rax, %r14
movq %rax, %rdi
callq 0x2110
movq 0x303b(%rip), %rdi      # 0x5ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x2250
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq 0x3012(%rip), %rdi      # 0x5ff0
callq 0x2030
movq %rax, %rdi
callq 0x2190
leaq 0x10(%rsp), %rbx
leaq 0x20(%rsp), %rax
movq %rax, (%rbx)
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x2110
leaq (%rsp,%rax), %rdx
addq $0x90, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33a0
leaq 0x31a3(%rip), %rdi      # 0x61c8
leaq 0x10(%rsp), %rsi
callq 0x3294
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x304b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x318e(%rip), %rbx      # 0x61e0
leaq 0x3177(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x30eb
movq 0x2f67(%rip), %rdi      # 0x5fd0
leaq 0x10f7(%rip), %rsi      # 0x4167
movl $0x23, %edx
callq 0x2250
movq 0x2f4f(%rip), %rdi      # 0x5fd0
movl 0xc(%rsp), %esi
callq 0x22c0
movq %rax, %rbx
leaq 0x10f7(%rip), %rsi      # 0x418b
movl $0x9, %edx
movq %rax, %rdi
callq 0x2250
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x2110
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2250
leaq 0x10cf(%rip), %rsi      # 0x4195
movl $0x16, %edx
movq %rbx, %rdi
callq 0x2250
movq %rbx, %rdi
callq 0x2130
movl 0xc(%rsp), %edi
callq 0x2330
xorl %edi, %edi
callq 0x22e0
leaq 0x1074(%rip), %r15      # 0x4166
leaq 0x10(%rsp), %r12
leaq 0xfd0(%rip), %r13       # 0x40ce
leaq 0x90(%rsp), %rbp
leaq 0x2f0(%rsp), %r14
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x1036(%rip), %rsi      # 0x4158
movq %r15, %rdx
callq 0x33a0
movl $0x200, %ecx            # imm = 0x200
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %r8
callq 0x39d9
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x315c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2220
movl 0x40(%rbx), %edi
movl $0x200, %edx            # imm = 0x200
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2140
movq %rbx, %rdi
callq 0x21a0
movq %rax, %rbx
leaq 0x3050(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x310e
jmp 0x3062
jmp 0x3210
jmp 0x3193
jmp 0x3210
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
jne 0x31df
jmp 0x3213
jmp 0x3210
jmp 0x3210
jmp 0x3210
jmp 0x3210
jmp 0x3210
jmp 0x31ec
jmp 0x3210
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x31d5
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x3213
movq 0x20(%rsp), %rsi
jmp 0x3204
jmp 0x31ec
jmp 0x3210
jmp 0x31ec
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r15, %rdi
je 0x3213
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x3213
jmp 0x3210
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x323d
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x323d
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3259
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x327b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2220
leaq 0x50(%rsp), %rdi
callq 0x32be
movq %rbx, %rdi
callq 0x22d0
nop