// File: /workspace/github_cpp_makefile_star2_O3/AraKhachatryan[P]messenger/client.cpp
// Function: main
int main (int argc, char* argv[])
{
    if(argc < 2)
    {
        std::cerr << "Syntax : ./client <server ip address>" << std::endl;
        return 0;
    }

    const char * ip_addres = argv[1];
    const int port_number = 2025;

    int socket_desc;
    struct sockaddr_in server_addr;


    // create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(socket_desc == -1)
    {
        std::cerr << "Cannot open socket with port number " << port_number << std::endl;
        close(socket_desc);
        return 0;
    }


    bzero((char *) &server_addr, sizeof(server_addr));

    // Set remote server information
	server_addr.sin_addr.s_addr = inet_addr( ip_addres );
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons( port_number );


    if ( connect(socket_desc, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1 )
    {
        std::cerr << "Cannot connect to the server with ip " << ip_addres << std::endl;
        close(socket_desc);
        return 0;
    }


    pthread_t send_thread_id;
    pthread_t recv_thread_id;

    pthread_create(&send_thread_id, NULL, send_thread, (void*)&socket_desc);
    pthread_detach(send_thread_id);

    pthread_create(&recv_thread_id, NULL, recv_thread, (void*)&socket_desc);
    pthread_join(recv_thread_id, NULL);

    pthread_exit(NULL);

    return 0;

}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x30, %rsp
cmpl $0x1, %edi
jg 0x2471
movq 0x4bbf(%rip), %rbx      # 0x6ff0
leaq 0x2bcc(%rip), %rsi      # 0x5004
movl $0x25, %edx
movq %rbx, %rdi
callq 0x21e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
jmp 0x25a7
movq 0x8(%rsi), %r14
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2050
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
je 0x2508
movl %eax, %ebx
leaq 0x20(%rsp), %r15
movq $0x0, 0x8(%r15)
movq %r14, %rdi
callq 0x21b0
movl %eax, 0x4(%r15)
movl $0xe9070002, (%r15)     # imm = 0xE9070002
movl %ebx, %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x2160
cmpl $-0x1, %eax
jne 0x25b3
movq 0x4b1d(%rip), %r15      # 0x6ff0
leaq 0x2b75(%rip), %rsi      # 0x504f
movl $0x25, %edx
movq %r15, %rdi
callq 0x21e0
testq %r14, %r14
je 0x2561
movq %r14, %rdi
callq 0x20b0
movq 0x4af5(%rip), %rdi      # 0x6ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x2579
movq 0x4ae1(%rip), %rbx      # 0x6ff0
leaq 0x2b14(%rip), %rsi      # 0x502a
movl $0x24, %edx
movq %rbx, %rdi
callq 0x21e0
movq %rbx, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x2250
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
jmp 0x25a2
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x2240
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
movl %ebx, %edi
callq 0x22b0
xorl %eax, %eax
addq $0x30, %rsp
popq %rbx
popq %r14
popq %r15
retq
leaq 0x49(%rip), %rdx        # 0x2603
leaq 0x18(%rsp), %rbx
leaq 0xc(%rsp), %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rcx
callq 0x2290
movq (%rbx), %rdi
callq 0x2130
leaq 0x7f2(%rip), %rdx       # 0x2dd2
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rcx
callq 0x2290
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x2180
xorl %edi, %edi
callq 0x2270