// File: 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
movq %rsp, %rbp
subq $0x380, %rsp            # imm = 0x380
movq %rdi, -0x8(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x3320
leaq -0x248(%rbp), %rdi
callq 0x2330
leaq -0x268(%rbp), %rdi
callq 0x2330
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x26c(%rbp)
jmp 0x2da1
leaq -0x228(%rbp), %rdi
callq 0x33a0
leaq -0x248(%rbp), %rdi
callq 0x2310
leaq -0x268(%rbp), %rdi
callq 0x2310
movl -0x26c(%rbp), %edi
leaq -0x210(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
xorl %ecx, %ecx
callq 0x20b0
movq %rax, -0x298(%rbp)
jmp 0x2de7
movq -0x298(%rbp), %rax
movl %eax, -0x270(%rbp)
cmpl $-0x1, -0x270(%rbp)
jne 0x2e96
movq 0x61e8(%rip), %rdi      # 0x8ff0
leaq 0x32fa(%rip), %rsi      # 0x6109
callq 0x2220
movq %rax, -0x2a0(%rbp)
jmp 0x2e1d
callq 0x2030
movl (%rax), %edi
callq 0x20c0
movq -0x2a0(%rbp), %rdi
movq %rax, %rsi
callq 0x2220
movq %rax, -0x2a8(%rbp)
jmp 0x2e41
movq -0x2a8(%rbp), %rdi
movq 0x6169(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x2e56
jmp 0x32bf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x2150
leaq -0x248(%rbp), %rdi
callq 0x2150
leaq -0x228(%rbp), %rdi
callq 0x37f0
jmp 0x3308
cmpl $0x0, -0x270(%rbp)
jne 0x2ea4
jmp 0x32bf
jmp 0x2ea6
leaq -0x210(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
leaq -0x228(%rbp), %rdx
leaq -0x248(%rbp), %rcx
leaq -0x268(%rbp), %r8
callq 0x47a0
jmp 0x2ece
leaq -0x248(%rbp), %rdi
leaq 0x3249(%rip), %rsi      # 0x6125
callq 0x2090
cmpl $0x0, %eax
jne 0x2ff6
movq 0x60d7(%rip), %rdi      # 0x8fc8
movl $0x1, %esi
callq 0x3340
movq %rax, -0x2b0(%rbp)
jmp 0x2f04
movq -0x2b0(%rbp), %rdi
movl $0x1f, %esi
callq 0x3340
movq %rax, -0x2b8(%rbp)
jmp 0x2f1e
leaq -0x228(%rbp), %rdi
callq 0x3850
movq -0x2b8(%rbp), %rdi
movq %rax, %rsi
callq 0x21f0
movq %rax, -0x2c0(%rbp)
jmp 0x2f42
movq -0x2c0(%rbp), %rdi
xorl %esi, %esi
callq 0x3340
movq %rax, -0x2c8(%rbp)
jmp 0x2f59
movq -0x2c8(%rbp), %rdi
leaq 0x31c6(%rip), %rsi      # 0x612d
callq 0x2220
movq %rax, -0x2d0(%rbp)
jmp 0x2f75
movq -0x2d0(%rbp), %rdi
movl $0x1, %esi
callq 0x3340
movq %rax, -0x2d8(%rbp)
jmp 0x2f8f
movq -0x2d8(%rbp), %rdi
movl $0x20, %esi
callq 0x3340
movq %rax, -0x2e0(%rbp)
jmp 0x2fa9
movq -0x2e0(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x21f0
movq %rax, -0x2e8(%rbp)
jmp 0x2fc5
movq -0x2e8(%rbp), %rdi
xorl %esi, %esi
callq 0x3340
movq %rax, -0x2f0(%rbp)
jmp 0x2fdc
movq -0x2f0(%rbp), %rdi
movq 0x5fce(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x2ff1
jmp 0x32ba
leaq -0x248(%rbp), %rdi
leaq 0x312d(%rip), %rsi      # 0x6131
callq 0x2090
cmpl $0x0, %eax
jne 0x311e
movq 0x5faf(%rip), %rdi      # 0x8fc8
movl $0x1, %esi
callq 0x3340
movq %rax, -0x2f8(%rbp)
jmp 0x302c
movq -0x2f8(%rbp), %rdi
movl $0x25, %esi
callq 0x3340
movq %rax, -0x300(%rbp)
jmp 0x3046
leaq -0x228(%rbp), %rdi
callq 0x3850
movq -0x300(%rbp), %rdi
movq %rax, %rsi
callq 0x21f0
movq %rax, -0x308(%rbp)
jmp 0x306a
movq -0x308(%rbp), %rdi
xorl %esi, %esi
callq 0x3340
movq %rax, -0x310(%rbp)
jmp 0x3081
movq -0x310(%rbp), %rdi
leaq 0x309e(%rip), %rsi      # 0x612d
callq 0x2220
movq %rax, -0x318(%rbp)
jmp 0x309d
movq -0x318(%rbp), %rdi
movl $0x1, %esi
callq 0x3340
movq %rax, -0x320(%rbp)
jmp 0x30b7
movq -0x320(%rbp), %rdi
movl $0x20, %esi
callq 0x3340
movq %rax, -0x328(%rbp)
jmp 0x30d1
movq -0x328(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x21f0
movq %rax, -0x330(%rbp)
jmp 0x30ed
movq -0x330(%rbp), %rdi
xorl %esi, %esi
callq 0x3340
movq %rax, -0x338(%rbp)
jmp 0x3104
movq -0x338(%rbp), %rdi
movq 0x5ea6(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x3119
jmp 0x32b8
leaq -0x248(%rbp), %rdi
leaq 0x300c(%rip), %rsi      # 0x6138
callq 0x2090
cmpl $0x0, %eax
jne 0x32b6
movq 0x5e87(%rip), %rdi      # 0x8fc8
movl $0x1, %esi
callq 0x3340
movq %rax, -0x340(%rbp)
jmp 0x3154
movq -0x340(%rbp), %rdi
movl $0x24, %esi
callq 0x3340
jmp 0x3167
leaq -0x228(%rbp), %rdi
callq 0x36a0
movq %rax, -0x288(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x3710
movq %rax, -0x290(%rbp)
leaq -0x288(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x36d0
testb $0x1, %al
jne 0x31a6
jmp 0x3221
leaq -0x288(%rbp), %rdi
callq 0x3740
movq %rax, %rsi
movq 0x5e0c(%rip), %rdi      # 0x8fc8
callq 0x21f0
jmp 0x31c3
leaq -0x288(%rbp), %rdi
callq 0x3740
movq %rax, -0x348(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x3790
movq -0x348(%rbp), %rdi
movq %rax, %rsi
callq 0x3760
testb $0x1, %al
jne 0x31f7
jmp 0x320e
movq 0x5dca(%rip), %rdi      # 0x8fc8
leaq 0x2efd(%rip), %rsi      # 0x6102
callq 0x2220
jmp 0x320c
jmp 0x320e
jmp 0x3210
leaq -0x288(%rbp), %rdi
callq 0x37d0
jmp 0x317a
movq 0x5da0(%rip), %rdi      # 0x8fc8
xorl %esi, %esi
callq 0x3340
movq %rax, -0x350(%rbp)
jmp 0x3238
movq -0x350(%rbp), %rdi
movl $0x1, %esi
callq 0x3340
movq %rax, -0x358(%rbp)
jmp 0x3252
movq -0x358(%rbp), %rdi
movl $0x25, %esi
callq 0x3340
movq %rax, -0x360(%rbp)
jmp 0x326c
movq -0x360(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x21f0
movq %rax, -0x368(%rbp)
jmp 0x3288
movq -0x368(%rbp), %rdi
xorl %esi, %esi
callq 0x3340
movq %rax, -0x370(%rbp)
jmp 0x329f
movq -0x370(%rbp), %rdi
movq 0x5d0b(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x32b4
jmp 0x32b6
jmp 0x32b8
jmp 0x32ba
jmp 0x2d9f
movq 0x5d02(%rip), %rdi      # 0x8fc8
leaq 0x2e79(%rip), %rsi      # 0x6146
callq 0x2220
movq %rax, -0x378(%rbp)
jmp 0x32db
movq -0x378(%rbp), %rdi
movq 0x5ccf(%rip), %rsi      # 0x8fb8
callq 0x2270
jmp 0x32f0
movl -0x26c(%rbp), %edi
callq 0x23f0
jmp 0x32fd
xorl %eax, %eax
movl %eax, %edi
callq 0x23a0
jmp 0x3308
movq -0x278(%rbp), %rdi
callq 0x2390
nopw %cs:(%rax,%rax)
nop