// File: /workspace/github_cpp_makefile_star2_O1/AraKhachatryan[P]messenger/client.cpp
// Function: send_thread(void*)
void * send_thread( void * arg )
{
    int socket_desc = *(int*)arg;

    char client_name[50];
    std::vector<std::string> dest_names;
    std::string message_info;
    std::string message;

    char input[392];
    char input_encoded[512];

    bzero(client_name, sizeof(client_name));

    std::cout << "Write your name: " << terminal::TEXT_BOLD << terminal::TEXTCOLOR_CYAN;
    std::cin.getline(client_name, 50);
    std::cout << terminal::RESET_ALL;

	send(socket_desc, (char*)&client_name, sizeof(client_name), 0);

	std::cout << "Private message pattern: @<Destination name>: <message>" << std::endl;
	std::cout << "Public message typed as is: <message>" << std::endl;
	std::cout << "Write your first message !!!" << std::endl << std::endl;

    while(1)
    {
        // clear all data buffers
        bzero(input, sizeof(input));
        bzero(input_encoded, sizeof(input_encoded));
        dest_names.clear();
        message_info.clear();
        message.clear();

        std::cin.getline(input, sizeof(input));


        // Erase input from terminal   ////////////////////////////////////////
        int input_size = std::cin.gcount();
        struct winsize w;
        ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
        int erase_line_count = 0;
        if( (input_size % w.ws_col) != 0 )
        {
            erase_line_count = (int)(input_size / w.ws_col) + 1;
        } else {
            erase_line_count = (int)(input_size / w.ws_col);
        }
        while( erase_line_count-- != 0 )
        {
            std::cout << terminal::Cursor_Previous_Line() << terminal::Erase_Line();
        }
        ///////////////////////////////////////////////////////////////////////


        // If input is empty (Enter key is pressed) do nothing
        if ( input_size == 1 )
        {
            continue;
        }


        encode_message(input, input_encoded, sizeof(input_encoded));
        //print_encoded_message(input_encoded, sizeof(input_encoded));

        send(socket_desc,(char*)&input_encoded, sizeof(input_encoded), 0);

        // decoding for input formatting in terminal
        decode_message(input_encoded, sizeof(input_encoded), dest_names, message_info, message);


        // Formatting input (encoded -> decoded input message)   ////////////////////
        // Private message is send
        if ( !dest_names.empty() )
        {
            std::cout << terminal::TEXT_BOLD << terminal::TEXTCOLOR_BLUE;

            for( auto it = dest_names.begin(); it != dest_names.end(); ++it )
            {
                std::cout << *it;
                if ( *it != dest_names.back() )
                {
                    std::cout << ", ";
                }
            }

            std::cout << terminal::RESET_ALL << "<- " << terminal::TEXT_BOLD << terminal::TEXTCOLOR_YELLOW
                        << message << terminal::RESET_ALL << std::endl;

        // Public message is send (if no dest_names)
        } else {
            std::cout << terminal::TEXT_BOLD << terminal::TEXTCOLOR_YELLOW
                        << message << terminal::RESET_ALL << std::endl;

        }
        ///////////////////////////////////////////////////////////////////////



    } // End while()

    pthread_exit(NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x478, %rsp            # imm = 0x478
movl (%rdi), %eax
movl %eax, 0xc(%rsp)
xorl %eax, %eax
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x98(%rsp), %r14
movq %r14, -0x10(%r14)
movq %rax, -0x8(%r14)
movb %al, (%r14)
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
movq %rax, -0x8(%r12)
movb %al, (%r12)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movw $0x0, 0xe0(%rsp)
movq 0x4965(%rip), %rbx      # 0x6fc8
leaq 0x2a0b(%rip), %rsi      # 0x5075
movl $0x11, %edx
movq %rbx, %rdi
callq 0x21e0
cmpb $0x1, 0x4af2(%rip)      # 0x7170
jne 0x26c0
movq 0x4941(%rip), %rdi      # 0x6fc8
leaq 0x2aec(%rip), %rsi      # 0x517a
movl $0x2, %edx
callq 0x21e0
movq 0x4929(%rip), %rdi      # 0x6fc8
movl $0x1, %esi
callq 0x2250
movq %rax, %rbx
leaq 0x2aca(%rip), %rsi      # 0x517d
movl $0x1, %edx
movq %rax, %rdi
callq 0x21e0
cmpb $0x1, 0x4aa9(%rip)      # 0x7170
jne 0x26fe
leaq 0x2aaa(%rip), %rsi      # 0x517a
movl $0x2, %edx
movq %rbx, %rdi
callq 0x21e0
movq %rbx, %rdi
movl $0x24, %esi
callq 0x2250
leaq 0x2a8c(%rip), %rsi      # 0x517d
movl $0x1, %edx
movq %rax, %rdi
callq 0x21e0
movq 0x48d3(%rip), %rdi      # 0x6fd8
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %ecx
movq 0x48b8(%rip), %rdi      # 0x6fd8
leaq 0xb0(%rsp), %rsi
movl $0x32, %edx
callq 0x22a0
cmpb $0x1, 0x4a37(%rip)      # 0x7170
jne 0x2775
movq 0x4886(%rip), %rdi      # 0x6fc8
leaq 0x2a31(%rip), %rsi      # 0x517a
movl $0x2, %edx
callq 0x21e0
movq 0x486e(%rip), %rdi      # 0x6fc8
xorl %esi, %esi
callq 0x2250
leaq 0x2a15(%rip), %rsi      # 0x517d
movl $0x1, %edx
movq %rax, %rdi
callq 0x21e0
leaq 0xb0(%rsp), %rsi
movl $0x32, %edx
movl 0xc(%rsp), %edi
xorl %ecx, %ecx
callq 0x20d0
movq 0x4834(%rip), %rdi      # 0x6fc8
leaq 0x28ec(%rip), %rsi      # 0x5087
movl $0x37, %edx
callq 0x21e0
movq 0x481c(%rip), %rdi      # 0x6fc8
callq 0x20c0
movq 0x4810(%rip), %rdi      # 0x6fc8
leaq 0x2900(%rip), %rsi      # 0x50bf
movl $0x25, %edx
callq 0x21e0
movq 0x47f8(%rip), %rdi      # 0x6fc8
callq 0x20c0
movq 0x47ec(%rip), %rdi      # 0x6fc8
leaq 0x2902(%rip), %rsi      # 0x50e5
movl $0x1c, %edx
callq 0x21e0
movq 0x47d4(%rip), %r15      # 0x6fc8
movq %r15, %rdi
callq 0x20c0
movq %rax, %rdi
callq 0x20c0
leaq 0x68(%rsp), %rbx
leaq 0x48(%rsp), %r12
movl $0x188, %edx            # imm = 0x188
leaq 0x2f0(%rsp), %rdi
xorl %esi, %esi
callq 0x2100
movl $0x200, %edx            # imm = 0x200
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
callq 0x2100
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r13
cmpq %r14, %r13
je 0x2873
leaq 0x10(%r14), %rbp
movq -0x10(%rbp), %rdi
cmpq %rdi, %rbp
je 0x285e
movq (%rbp), %rsi
incq %rsi
callq 0x21c0
leaq 0x20(%rbp), %rax
addq $0x10, %rbp
cmpq %r13, %rbp
movq %rax, %rbp
jne 0x2849
movq %r14, 0x18(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x90(%rsp)
movq 0x88(%rsp), %rax
movb %cl, (%rax)
movq %rcx, 0x30(%rsp)
movq 0x28(%rsp), %rax
movb %cl, (%rax)
movq 0x473e(%rip), %r14      # 0x6fd8
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %ecx
movl $0x188, %edx            # imm = 0x188
movq %r14, %rdi
leaq 0x2f0(%rsp), %rsi
callq 0x22a0
movl 0x8(%r14), %r13d
movl $0x5413, %esi           # imm = 0x5413
movl $0x1, %edi
leaq 0xa8(%rsp), %rdx
xorl %eax, %eax
callq 0x2210
movzwl 0xaa(%rsp), %ecx
movl %r13d, %eax
cltd
idivl %ecx
xorl %ebp, %ebp
testl %edx, %edx
setne %bpl
addl %eax, %ebp
je 0x297a
movq %rbx, %rdi
movl $0x1, %esi
callq 0x35d4
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq %r15, %rdi
callq 0x21e0
movq %rax, %r14
movq %r12, %rdi
movl $0x2, %esi
callq 0x36a6
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %r14, %rdi
callq 0x21e0
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x295a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2976
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21c0
decl %ebp
jne 0x28fd
cmpl $0x1, %r13d
je 0x280e
movl $0x200, %edx            # imm = 0x200
leaq 0x2f0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x3864
movl $0x200, %edx            # imm = 0x200
movl 0xc(%rsp), %edi
leaq 0xf0(%rsp), %rsi
xorl %ecx, %ecx
callq 0x20d0
movl $0x200, %esi            # imm = 0x200
leaq 0xf0(%rsp), %rdi
leaq 0x10(%rsp), %rdx
leaq 0x88(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x3afb
movq 0x10(%rsp), %rcx
movb 0x478b(%rip), %al       # 0x7170
cmpq 0x18(%rsp), %rcx
je 0x2c12
movq %r15, %r13
testb %al, %al
je 0x2a2f
movl $0x2, %edx
movq %r15, %rdi
leaq 0x2774(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2753(%rip), %rsi      # 0x517d
callq 0x21e0
cmpb $0x1, 0x473a(%rip)      # 0x7170
jne 0x2a6d
movl $0x2, %edx
movq %r13, %rdi
leaq 0x2733(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r13, %rdi
movl $0x22, %esi
callq 0x2250
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2715(%rip), %rsi      # 0x517d
callq 0x21e0
movq 0x10(%rsp), %r14
cmpq 0x18(%rsp), %r14
leaq 0x2684(%rip), %r13      # 0x5102
je 0x2ace
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x21e0
movq 0x18(%rsp), %rax
movq 0x8(%r14), %rdx
cmpq -0x18(%rax), %rdx
jne 0x2ab3
testq %rdx, %rdx
je 0x2ac3
movq -0x20(%rax), %rsi
movq (%r14), %rdi
callq 0x21d0
testl %eax, %eax
je 0x2ac3
movl $0x2, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x21e0
addq $0x20, %r14
cmpq 0x18(%rsp), %r14
jne 0x2a80
cmpb $0x1, 0x469b(%rip)      # 0x7170
movq %r15, %r13
jne 0x2b0f
movl $0x2, %edx
movq %r15, %rdi
leaq 0x2691(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2673(%rip), %rsi      # 0x517d
callq 0x21e0
movl $0x3, %edx
movq %r13, %rdi
leaq 0x25e7(%rip), %rsi      # 0x5105
callq 0x21e0
cmpb $0x1, 0x4646(%rip)      # 0x7170
jne 0x2b64
movl $0x2, %edx
movq %r13, %rdi
leaq 0x263f(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r13, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x261e(%rip), %rsi      # 0x517d
callq 0x21e0
cmpb $0x1, 0x4605(%rip)      # 0x7170
jne 0x2ba5
movl $0x2, %edx
movq %r13, %rdi
leaq 0x25fe(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r13, %rdi
movl $0x21, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x25dd(%rip), %rsi      # 0x517d
callq 0x21e0
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r13, %rdi
callq 0x21e0
movq %rax, %r13
cmpb $0x1, 0x45af(%rip)      # 0x7170
jne 0x2bf8
movl $0x2, %edx
movq %r13, %rdi
leaq 0x25a8(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r13, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x258a(%rip), %rsi      # 0x517d
callq 0x21e0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2190
jmp 0x2cfa
movq %r15, %r13
testb %al, %al
je 0x2c51
movl $0x2, %edx
movq %r15, %rdi
leaq 0x2552(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2531(%rip), %rsi      # 0x517d
callq 0x21e0
cmpb $0x1, 0x4518(%rip)      # 0x7170
jne 0x2c92
movl $0x2, %edx
movq %r13, %rdi
leaq 0x2511(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r13, %rdi
movl $0x21, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x24f0(%rip), %rsi      # 0x517d
callq 0x21e0
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r13, %rdi
callq 0x21e0
movq %rax, %r13
cmpb $0x1, 0x44c2(%rip)      # 0x7170
jne 0x2ce5
movl $0x2, %edx
movq %r13, %rdi
leaq 0x24bb(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r13, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x249d(%rip), %rsi      # 0x517d
callq 0x21e0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
jmp 0x280e
jmp 0x2d18
jmp 0x2d18
jmp 0x2d18
movq %rax, %rbx
jmp 0x2d5f
jmp 0x2d1f
movq %rax, %rbx
jmp 0x2d43
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x2d43
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x2d5f
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x98(%rsp), %r14
leaq 0x38(%rsp), %r12
jmp 0x2d71
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x2d88
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x2da5
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x10(%rsp), %rdi
callq 0x3778
movq %rbx, %rdi
callq 0x2260