// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
pushq %rbx
subq $0x38, %rsp
cmpl $0x1, %edi
jg 0x246f
movq 0x4bc1(%rip), %rbx      # 0x6ff0
leaq 0x2bce(%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 0x2594
movq 0x8(%rsi), %rbx
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2050
movl %eax, 0xc(%rsp)
cmpl $-0x1, %eax
je 0x2505
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r14
movaps %xmm0, (%r14)
movq %rbx, %rdi
callq 0x21b0
movl %eax, 0x4(%r14)
movl $0xe9070002, (%r14)     # imm = 0xE9070002
movl 0xc(%rsp), %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x2160
cmpl $-0x1, %eax
jne 0x259e
movq 0x4b20(%rip), %r14      # 0x6ff0
leaq 0x2b78(%rip), %rsi      # 0x504f
movl $0x25, %edx
movq %r14, %rdi
callq 0x21e0
testq %rbx, %rbx
je 0x254c
movq %rbx, %rdi
callq 0x20b0
movq 0x4af8(%rip), %rdi      # 0x6ff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x21e0
jmp 0x2564
movq 0x4ae4(%rip), %rbx      # 0x6ff0
leaq 0x2b17(%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
jmp 0x257e
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2240
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2190
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movl 0xc(%rsp), %edi
callq 0x22b0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
retq
leaq 0x49(%rip), %rdx        # 0x25ee
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 0x7ec(%rip), %rdx       # 0x2db7
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