// File: /workspace/github_cpp_makefile_star2_O2/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 0x50(%rsp), %rsi
movaps %xmm0, 0x20(%rsi)
andw $0x0, 0x30(%rsi)
andq $0x0, 0x40(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, 0x10(%rsi)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0xa0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, (%rsi)
movaps %xmm0, 0x30(%rsp)
movb %cl, (%rax)
movl (%rdi), %edi
pushq $0x32
popq %rdx
movl %edi, 0xc(%rsp)
xorl %ecx, %ecx
callq 0x20b0
leaq 0xe0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x3a59(%rip), %rbx      # 0x61e0
xorl %r15d, %r15d
leaq 0xd0(%rsp), %r14
leaq 0xf0(%rsp), %r13
leaq 0x50(%rsp), %r12
leaq 0x2f0(%rsp), %rbp
leaq 0x3a22(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
je 0x2832
leaq 0x20(%rbx), %rsi
movq %r14, %rdi
callq 0x2320
incq %r15
cmpq 0x3a27(%rip), %r15      # 0x61f0
jae 0x27da
movq %r14, %rdi
leaq 0x18f7(%rip), %rsi      # 0x40cc
callq 0x2310
movq %r13, %rdi
leaq 0x18f8(%rip), %rsi      # 0x40dc
leaq 0x10(%rsp), %rdx
callq 0x2e3c
movl $0x200, %ecx            # imm = 0x200
movq %r12, %rdi
leaq 0x18d1(%rip), %rsi      # 0x40ce
movq %r13, %rdx
movq %rbp, %r8
callq 0x3621
movq %r13, %rdi
callq 0x2110
movl 0x40(%rbx), %edi
movl $0x200, %edx            # imm = 0x200
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x2130
movq %rbx, %rdi
callq 0x2180
movq %rax, %rbx
jmp 0x27a7
cmpq $0x0, 0x39b6(%rip)      # 0x61f0
jne 0x2879
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0xb(%rsp), %rdx
callq 0x2e3c
leaq 0x100(%rsp), %rcx
movq %rcx, -0x10(%rcx)
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdx
cmpq %rax, %rdx
jne 0x2931
movups (%rax), %xmm0
movups %xmm0, (%rcx)
jmp 0x2946
leaq 0x100(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpq $0x2, %r15
jb 0x28ab
leaq 0x184d(%rip), %rsi      # 0x40e7
leaq 0x10(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x2e3c
jmp 0x28c1
leaq 0x182a(%rip), %rsi      # 0x40dc
leaq 0x10(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x2e3c
leaq 0xf0(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x2260
movq %rbx, %rdi
callq 0x2110
movq 0xd0(%rsp), %rdi
leaq 0x17e1(%rip), %rsi      # 0x40ce
leaq 0xf0(%rsp), %rdx
leaq 0x2f0(%rsp), %r8
movl $0x200, %ecx            # imm = 0x200
callq 0x3621
leaq 0x2f0(%rsp), %rsi
movl $0x200, %edx            # imm = 0x200
movl 0xc(%rsp), %edi
xorl %ecx, %ecx
callq 0x2130
leaq 0xf0(%rsp), %rdi
callq 0x2110
jmp 0x283c
movq %rdx, 0xf0(%rsp)
movq 0x20(%rsp), %rcx
movq %rcx, 0x100(%rsp)
movq 0x18(%rsp), %rcx
movq %rax, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
leaq 0xf0(%rsp), %rsi
movq %rcx, 0x8(%rsi)
movb $0x0, 0x20(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x20(%rsi)
leaq 0x3853(%rip), %rdi      # 0x61c8
callq 0x3000
leaq 0xf0(%rsp), %rdi
callq 0x2110
leaq 0x10(%rsp), %rdi
callq 0x2110
movq 0x3638(%rip), %rdi      # 0x5fd0
leaq 0x1754(%rip), %rsi      # 0x40f3
callq 0x21f0
movq %rax, %rbx
callq 0x21e0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2170
leaq 0x17c0(%rip), %rsi      # 0x417e
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x22b0
leaq 0x17b4(%rip), %rsi      # 0x418d
movq %rax, %rdi
callq 0x21f0
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x2120
leaq 0x2f0(%rsp), %r12
leaq 0x90(%rsp), %rbp
leaq 0x50(%rsp), %r13
movl $0x200, %edx            # imm = 0x200
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2150
leaq 0x30(%rsp), %rdi
callq 0x2e78
andq $0x0, 0xb8(%rsp)
movq 0xb0(%rsp), %rax
xorl %ecx, %ecx
movb %cl, (%rax)
andq $0x0, 0x98(%rsp)
movq 0x90(%rsp), %rax
movb %cl, (%rax)
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x2150
movl $0x200, %edx            # imm = 0x200
movl 0xc(%rsp), %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x20b0
testl %eax, %eax
je 0x2c9b
cmpl $-0x1, %eax
je 0x2c66
movl $0x200, %esi            # imm = 0x200
leaq 0xf0(%rsp), %rdi
leaq 0x30(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
movq %rbp, %r8
callq 0x36a9
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %r15
cmpq %r15, %rbx
je 0x2bb5
cmpq %r15, %rbx
je 0x2a0b
leaq 0x36fc(%rip), %rdi      # 0x61c8
movq %rbx, %rsi
callq 0x3230
leaq 0x36f5(%rip), %rcx      # 0x61d0
cmpq %rcx, %rax
je 0x2b5d
movl 0x40(%rax), %r14d
movq 0x34e5(%rip), %rdi      # 0x5fd0
movq %r13, %rsi
callq 0x21f0
movq %rax, %rdi
leaq 0x1647(%rip), %rsi      # 0x4144
callq 0x21f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21d0
movq %rax, %rdi
leaq 0x15e5(%rip), %rsi      # 0x40fc
callq 0x21f0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2120
movl $0x200, %ecx            # imm = 0x200
movq %r13, %rdi
leaq 0x160b(%rip), %rsi      # 0x4149
movq %rbp, %rdx
movq %r12, %r8
callq 0x3621
movl $0x200, %edx            # imm = 0x200
movl %r14d, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x2130
jmp 0x2bac
movq 0x346c(%rip), %rdi      # 0x5fd0
leaq 0x15b0(%rip), %rsi      # 0x411b
callq 0x21f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x21f0
movq %rax, %rdi
leaq 0x159d(%rip), %rsi      # 0x4122
callq 0x21f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21d0
movq %rax, %rdi
leaq 0x1599(%rip), %rsi      # 0x4138
callq 0x21f0
movq %rax, %rdi
callq 0x2120
addq $0x20, %rbx
jmp 0x2abc
movq 0x3624(%rip), %rbx      # 0x61e0
leaq 0x360d(%rip), %rax      # 0x61d0
cmpq %rax, %rbx
je 0x2a0b
leaq 0x20(%rbx), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x2090
testl %eax, %eax
je 0x2c56
movq 0x33ea(%rip), %rdi      # 0x5fd0
movq %r13, %rsi
callq 0x21f0
movq %rax, %rdi
leaq 0x154c(%rip), %rsi      # 0x4144
callq 0x21f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
leaq 0x14ea(%rip), %rsi      # 0x40fc
callq 0x21f0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2120
movl $0x200, %ecx            # imm = 0x200
movq %r13, %rdi
leaq 0x1518(%rip), %rsi      # 0x4151
movq %rbp, %rdx
movq %r12, %r8
callq 0x3621
movl 0x40(%rbx), %edi
movl $0x200, %edx            # imm = 0x200
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x2130
movq %rbx, %rdi
callq 0x2180
movq %rax, %rbx
jmp 0x2bbc
movq 0x3383(%rip), %rdi      # 0x5ff0
leaq 0x148b(%rip), %rsi      # 0x40ff
callq 0x21f0
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20c0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x21f0
movq %rax, %rdi
callq 0x2120
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0xb(%rsp), %rdx
callq 0x2e3c
leaq 0x3512(%rip), %rdi      # 0x61c8
leaq 0x10(%rsp), %rsi
callq 0x32a6
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2110
movq 0x350c(%rip), %r13      # 0x61e0
leaq 0xb(%rsp), %r15
leaq 0x13ee(%rip), %r12      # 0x40ce
leaq 0x50(%rsp), %rbp
leaq 0x2f0(%rsp), %r14
leaq 0x34dc(%rip), %rax      # 0x61d0
cmpq %rax, %r13
je 0x2d49
movq %rbx, %rdi
leaq 0x1455(%rip), %rsi      # 0x4158
movq %r15, %rdx
callq 0x2e3c
movl $0x200, %ecx            # imm = 0x200
movq %rbp, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %r8
callq 0x3621
movq %rbx, %rdi
callq 0x2110
movl 0x40(%r13), %edi
movl $0x200, %edx            # imm = 0x200
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2130
movq %r13, %rdi
callq 0x2180
movq %rax, %r13
jmp 0x2ced
movq 0x3280(%rip), %rdi      # 0x5fd0
leaq 0x1410(%rip), %rsi      # 0x4167
callq 0x21f0
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x22b0
leaq 0x141c(%rip), %rsi      # 0x418b
movq %rax, %rdi
callq 0x21f0
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
callq 0x21f0
leaq 0x140a(%rip), %rsi      # 0x4195
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x2120
movl 0xc(%rsp), %edi
callq 0x2330
xorl %edi, %edi
callq 0x22d0
jmp 0x2df5
jmp 0x2db1
jmp 0x2df5
movq %rax, %rbx
jmp 0x2dd6
jmp 0x2df5
jmp 0x2df5
jmp 0x2df5
jmp 0x2df5
jmp 0x2df5
jmp 0x2de1
jmp 0x2de1
jmp 0x2df5
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x2110
leaq 0x10(%rsp), %rdi
jmp 0x2dec
jmp 0x2de1
jmp 0x2df5
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x2110
jmp 0x2df8
jmp 0x2df5
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x2110
jmp 0x2e0a
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x2110
leaq 0xb0(%rsp), %rdi
callq 0x2110
leaq 0x30(%rsp), %rdi
callq 0x2e80
movq %rbx, %rdi
callq 0x22c0