// File: /workspace/github_cpp_makefile_star2_O1/AraKhachatryan[P]messenger/client.cpp
// Function: recv_thread(void*)
void * recv_thread( void * arg )
{
    char input[512];

	std::vector<std::string> names_from;
	std::string message_info;
	std::string message_from;

	int socket_desc = *(int*)arg;

	while(1)
    {
        names_from.clear();
        message_info.clear();
        message_from.clear();

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

        decode_message(input, sizeof(input), names_from, message_info, message_from);

        // Private incoming messages
        if( !message_info.compare("Private") )
        {
            std::cout << terminal::TEXT_BOLD << terminal::TEXTCOLOR_RED << names_from.front()
                        << terminal::RESET_ALL <<  "-> " << terminal::TEXT_BOLD << terminal::TEXTCOLOR_GREEN
                        << message_from << terminal::RESET_ALL << std::endl;
        // Public incoming messages
        } else if ( !message_info.compare("Public") ) {
            std::cout << terminal::TEXT_BOLD << terminal::TEXTCOLOR_WHITE << names_from.front()
                        << terminal::RESET_ALL <<  "-> " << terminal::TEXT_BOLD << terminal::TEXTCOLOR_GREEN
                        << message_from << terminal::RESET_ALL << std::endl;
        // New active clients notifications from server
        } else if ( !message_info.compare("Server:online") ) {
            std::cout << terminal::TEXT_BOLD << terminal::TEXTCOLOR_CYAN;
            for( auto it = names_from.begin(); it != names_from.end(); ++it )
            {
                std::cout << *it;
                if( *it != names_from.back() )
                {
                    std:: cout << ", ";
                }
            }
            std::cout << terminal::RESET_ALL << terminal::TEXT_BOLD << terminal::TEXTCOLOR_WHITE
                        << message_from << terminal::RESET_ALL << std::endl;
        }


	} // End while()

	std::cout << "Server is down!!! Closing recv_thread and socket..." << std::endl;
	close(socket_desc);
	pthread_exit(NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
xorl %ecx, %ecx
leaq 0x10(%rsp), %rax
movq %rcx, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rcx, -0x8(%rax)
movb %cl, (%rax)
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rcx, -0x8(%rax)
movb %cl, (%rax)
movl (%rdi), %eax
movl %eax, 0xc(%rsp)
leaq 0x70(%rsp), %rbp
leaq 0x50(%rsp), %r12
movq 0x41ba(%rip), %r14      # 0x6fc8
leaq 0x2368(%rip), %r13      # 0x517d
movq %r13, %rbx
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r13
cmpq %r15, %r13
movq %rbp, %rsi
je 0x2e6a
movq %rsi, %r14
leaq 0x10(%r15), %rbp
movq -0x10(%rbp), %rdi
cmpq %rdi, %rbp
je 0x2e46
movq (%rbp), %rsi
incq %rsi
callq 0x21c0
leaq 0x20(%rbp), %rax
addq $0x10, %rbp
cmpq %r13, %rbp
movq %rax, %rbp
jne 0x2e31
movq %r15, 0x18(%rsp)
movq %r14, %rsi
leaq 0x50(%rsp), %r12
movq 0x415e(%rip), %r14      # 0x6fc8
xorl %ecx, %ecx
movq %rcx, 0x58(%rsp)
movq 0x50(%rsp), %rax
movb %cl, (%rax)
movq %rcx, 0x38(%rsp)
movq 0x30(%rsp), %rax
movb %cl, (%rax)
movl $0x200, %edx            # imm = 0x200
movl 0xc(%rsp), %edi
movq %rsi, %rbp
xorl %ecx, %ecx
callq 0x2070
movq %rbx, %r13
leaq 0x2261(%rip), %rbx      # 0x5102
testl %eax, %eax
je 0x352b
cmpl $-0x1, %eax
je 0x349e
movl $0x200, %esi            # imm = 0x200
movq %rbp, %rdi
leaq 0x10(%rsp), %rdx
movq %r12, %rcx
leaq 0x30(%rsp), %r8
callq 0x3afb
movq %r12, %rdi
leaq 0x224f(%rip), %rsi      # 0x5125
callq 0x2060
testl %eax, %eax
je 0x2fe4
movq %r12, %rdi
leaq 0x2244(%rip), %rsi      # 0x5131
callq 0x2060
testl %eax, %eax
je 0x31a8
movq %r12, %rdi
leaq 0x2234(%rip), %rsi      # 0x5138
callq 0x2060
testl %eax, %eax
jne 0x2e15
cmpb $0x1, 0x4258(%rip)      # 0x7170
movq %r14, %r15
jne 0x2f51
movl $0x2, %edx
movq %r14, %rdi
leaq 0x224e(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r14, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0x4218(%rip)      # 0x7170
jne 0x2f8b
movl $0x2, %edx
movq %r15, %rdi
leaq 0x2211(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x24, %esi
callq 0x2250
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq 0x10(%rsp), %r15
cmpq 0x18(%rsp), %r15
je 0x336c
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %r14, %rdi
callq 0x21e0
movq 0x18(%rsp), %rax
movq 0x8(%r15), %rdx
cmpq -0x18(%rax), %rdx
jne 0x2fce
testq %rdx, %rdx
je 0x2fde
movq -0x20(%rax), %rsi
movq (%r15), %rdi
callq 0x21d0
testl %eax, %eax
je 0x2fde
movl $0x2, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21e0
addq $0x20, %r15
jmp 0x2f90
cmpb $0x1, 0x4185(%rip)      # 0x7170
movq %r14, %r15
jne 0x3024
movl $0x2, %edx
movq %r14, %rdi
leaq 0x217b(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r14, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0x4145(%rip)      # 0x7170
jne 0x3061
movl $0x2, %edx
movq %r15, %rdi
leaq 0x213e(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x1f, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r15, %rdi
callq 0x21e0
movq %rax, %r15
cmpb $0x1, 0x40f1(%rip)      # 0x7170
jne 0x30b2
movl $0x2, %edx
movq %r15, %rdi
leaq 0x20ea(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movl $0x3, %edx
movq %r15, %rdi
leaq 0x206c(%rip), %rsi      # 0x512d
callq 0x21e0
cmpb $0x1, 0x40a3(%rip)      # 0x7170
jne 0x3103
movl $0x2, %edx
movq %r15, %rdi
leaq 0x209c(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0x4066(%rip)      # 0x7170
jne 0x3140
movl $0x2, %edx
movq %r15, %rdi
leaq 0x205f(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x20, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r15, %rdi
callq 0x21e0
movq %rax, %r15
cmpb $0x1, 0x4014(%rip)      # 0x7170
jne 0x318f
movl $0x2, %edx
movq %r15, %rdi
leaq 0x200d(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2190
jmp 0x3486
cmpb $0x1, 0x3fc1(%rip)      # 0x7170
movq %r14, %r15
jne 0x31e8
movl $0x2, %edx
movq %r14, %rdi
leaq 0x1fb7(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r14, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0x3f81(%rip)      # 0x7170
jne 0x3225
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1f7a(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x25, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r15, %rdi
callq 0x21e0
movq %rax, %r15
cmpb $0x1, 0x3f2d(%rip)      # 0x7170
jne 0x3276
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1f26(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movl $0x3, %edx
movq %r15, %rdi
leaq 0x1ea8(%rip), %rsi      # 0x512d
callq 0x21e0
cmpb $0x1, 0x3edf(%rip)      # 0x7170
jne 0x32c7
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1ed8(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0x3ea2(%rip)      # 0x7170
jne 0x3304
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1e9b(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x20, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r15, %rdi
callq 0x21e0
movq %rax, %r15
cmpb $0x1, 0x3e50(%rip)      # 0x7170
jne 0x3353
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1e49(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2190
jmp 0x3486
cmpb $0x1, 0x3dfd(%rip)      # 0x7170
movq %r14, %r15
jne 0x33a9
movl $0x2, %edx
movq %r14, %rdi
leaq 0x1df3(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r14, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0x3dc0(%rip)      # 0x7170
jne 0x33e6
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1db9(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x1, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
cmpb $0x1, 0x3d83(%rip)      # 0x7170
jne 0x3423
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1d7c(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
movl $0x25, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r15, %rdi
callq 0x21e0
movq %rax, %r15
cmpb $0x1, 0x3d31(%rip)      # 0x7170
jne 0x3472
movl $0x2, %edx
movq %r15, %rdi
leaq 0x1d2a(%rip), %rsi      # 0x517a
callq 0x21e0
movq %r15, %rdi
xorl %esi, %esi
callq 0x2250
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
jmp 0x2e15
movq 0x3b4b(%rip), %rbx      # 0x6ff0
leaq 0x1c5d(%rip), %rsi      # 0x5109
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x21e0
callq 0x2040
movl (%rax), %edi
callq 0x2090
testq %rax, %rax
jne 0x34e3
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x2240
jmp 0x3500
movq %rax, %r14
movq %rax, %rdi
callq 0x20b0
movq 0x3afb(%rip), %rdi      # 0x6ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x21e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x3ad2(%rip), %rdi      # 0x6ff0
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq 0x3a96(%rip), %rbx      # 0x6fc8
leaq 0x1c0d(%rip), %rsi      # 0x5146
movl $0x33, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq 0x3a64(%rip), %rdi      # 0x6fc8
callq 0x2030
movq %rax, %rdi
callq 0x2110
movl 0xc(%rsp), %edi
callq 0x22b0
xorl %edi, %edi
callq 0x2270
jmp 0x3587
jmp 0x3587
jmp 0x3587
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x35a6
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x35c2
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x10(%rsp), %rdi
callq 0x3778
movq %rbx, %rdi
callq 0x2260