// File: /workspace/github_cpp_makefile_star2_O1/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 0x396a(%rip), %rbx      # 0x61e0
leaq 0x3953(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x28ea
xorl %eax, %eax
cmpq $0x0, 0x3964(%rip)      # 0x61f0
jne 0x29cb
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 0x3380
leaq 0x100(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x10(%rsp), %rax
cmpq %r15, %rax
jne 0x2ab0
movups (%r15), %xmm0
movups %xmm0, (%r14)
jmp 0x2ac5
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 0x38c9(%rip), %r12      # 0x61f0
jae 0x2938
movq %r14, %rdi
leaq 0x1799(%rip), %rsi      # 0x40cc
callq 0x2320
movq %r15, 0xf0(%rsp)
movq %rbp, %rdi
leaq 0x1792(%rip), %rsi      # 0x40dc
leaq 0x1795(%rip), %rdx      # 0x40e6
callq 0x3380
movl $0x200, %ecx            # imm = 0x200
leaq 0x90(%rsp), %rdi
leaq 0x1764(%rip), %rsi      # 0x40ce
movq %rbp, %rdx
movq %r13, %r8
callq 0x39bc
movq 0xf0(%rsp), %rdi
cmpq %r15, %rdi
je 0x2992
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 0x381a(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x290d
cmpq $0x2, %r12
setae %al
jmp 0x2884
leaq 0x100(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq 0x20(%rsp), %r15
leaq 0x10(%rsp), %rdi
testb %al, %al
je 0x2a09
movq %r15, (%rdi)
leaq 0x16ec(%rip), %rsi      # 0x40e7
leaq 0x16f0(%rip), %rdx      # 0x40f2
callq 0x3380
jmp 0x2a1f
movq %r15, (%rdi)
leaq 0x16c9(%rip), %rsi      # 0x40dc
leaq 0x16cc(%rip), %rdx      # 0x40e6
callq 0x3380
leaq 0xf0(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x2270
movq (%rbx), %rdi
cmpq %r15, %rdi
je 0x2a49
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0xd0(%rsp), %rdi
leaq 0x1676(%rip), %rsi      # 0x40ce
leaq 0xf0(%rsp), %rdx
leaq 0x2f0(%rsp), %r8
movl $0x200, %ecx            # imm = 0x200
callq 0x39bc
leaq 0x2f0(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movl 0xc(%rsp), %edi
xorl %ecx, %ecx
callq 0x2140
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x2892
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x2892
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 0x36d1(%rip), %rdi      # 0x61c8
callq 0x3410
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x2b19
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x2b30
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x3499(%rip), %rdi      # 0x5fd0
leaq 0x15b5(%rip), %rsi      # 0x40f3
movl $0xb, %edx
callq 0x2250
callq 0x21f0
movq 0x347c(%rip), %rdi      # 0x5fd0
movq %rax, %rsi
callq 0x2180
movq %rax, %rbx
leaq 0x1618(%rip), %rsi      # 0x417e
movl $0xc, %edx
movq %rax, %rdi
callq 0x2250
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x22c0
movq %rax, %rbx
leaq 0x1604(%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 0x3405(%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 0x2c1b
leaq 0x10(%rbx), %r15
movq -0x10(%r15), %rdi
cmpq %rdi, %r15
je 0x2c06
movq (%r15), %rsi
incq %rsi
callq 0x2220
leaq 0x20(%r15), %rax
addq $0x10, %r15
cmpq %r14, %r15
movq %rax, %r15
jne 0x2bf2
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 0x2fbc
cmpl $-0x1, %eax
je 0x2f2f
movl $0x200, %esi            # imm = 0x200
leaq 0xf0(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x3a8f
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %r15
cmpq %r15, %r14
je 0x2e2e
leaq 0x351b(%rip), %rdi      # 0x61c8
movq %r14, %rsi
callq 0x35da
leaq 0x3514(%rip), %rcx      # 0x61d0
cmpq %rcx, %rax
je 0x2d90
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 0x1457(%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 0x13e9(%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 0x13e6(%rip), %rsi      # 0x4149
leaq 0x30(%rsp), %rdx
leaq 0x2f0(%rsp), %r8
callq 0x39bc
movl $0x200, %edx            # imm = 0x200
movl %ebp, %edi
leaq 0x2f0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x2140
jmp 0x2e1c
movl $0x6, %edx
movq %r13, %rdi
leaq 0x137c(%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 0x1359(%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 0x1349(%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 0x2ca6
jmp 0x2bcb
movq 0x33ab(%rip), %r14      # 0x61e0
leaq 0x3394(%rip), %rax      # 0x61d0
cmpq %rax, %r14
je 0x2bcb
leaq 0x20(%r14), %rdi
movq %r12, %rsi
callq 0x20a0
testl %eax, %eax
je 0x2f1f
movq %r12, %rdi
callq 0x2110
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2250
movl $0x4, %edx
movq %r13, %rdi
leaq 0x12c6(%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 0x1257(%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 0x125c(%rip), %rsi      # 0x4151
leaq 0x30(%rsp), %rdx
leaq 0x2f0(%rsp), %r8
callq 0x39bc
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 0x2e35
movq 0x30ba(%rip), %rbx      # 0x5ff0
leaq 0x11c2(%rip), %rsi      # 0x40ff
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x2250
callq 0x2050
movl (%rax), %edi
callq 0x20e0
testq %rax, %rax
jne 0x2f74
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22b0
jmp 0x2f91
movq %rax, %r14
movq %rax, %rdi
callq 0x2110
movq 0x306a(%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 0x3041(%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 0x3380
leaq 0x31d2(%rip), %rdi      # 0x61c8
leaq 0x10(%rsp), %rsi
callq 0x3278
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x301c
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x31bd(%rip), %rbx      # 0x61e0
leaq 0x31a6(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x30bc
movq 0x2f96(%rip), %rdi      # 0x5fd0
leaq 0x1126(%rip), %rsi      # 0x4167
movl $0x23, %edx
callq 0x2250
movq 0x2f7e(%rip), %rdi      # 0x5fd0
movl 0xc(%rsp), %esi
callq 0x22c0
movq %rax, %rbx
leaq 0x1126(%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 0x10fe(%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 0x10a3(%rip), %r15      # 0x4166
leaq 0x10(%rsp), %r12
leaq 0xfff(%rip), %r13       # 0x40ce
leaq 0x90(%rsp), %rbp
leaq 0x2f0(%rsp), %r14
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
leaq 0x1065(%rip), %rsi      # 0x4158
movq %r15, %rdx
callq 0x3380
movl $0x200, %ecx            # imm = 0x200
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %r8
callq 0x39bc
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x312d
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 0x307f(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
jne 0x30df
jmp 0x3033
jmp 0x31f4
jmp 0x3167
jmp 0x31f4
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
jne 0x31b5
jmp 0x31f7
jmp 0x31f4
jmp 0x31f4
jmp 0x31f4
jmp 0x31f4
jmp 0x31f4
jmp 0x31d0
jmp 0x31d0
jmp 0x31f4
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x31ab
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x31f7
movq 0x20(%rsp), %rsi
jmp 0x31e8
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r15, %rdi
jne 0x31e0
jmp 0x31f7
jmp 0x31f4
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r14, %rdi
je 0x31f7
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x31f7
jmp 0x31f4
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x3221
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x2220
jmp 0x3221
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x323d
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2220
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x325f
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2220
leaq 0x50(%rsp), %rdi
callq 0x32a2
movq %rbx, %rdi
callq 0x22d0
nop