// File: 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 %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x25ea
movq 0x6a2d(%rip), %rdi      # 0x8ff0
leaq 0x3a3a(%rip), %rsi      # 0x6004
callq 0x2220
movq %rax, %rdi
movq 0x69df(%rip), %rsi      # 0x8fb8
callq 0x2270
movl $0x0, -0x4(%rbp)
jmp 0x2731
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x7e9, -0x1c(%rbp)     # imm = 0x7E9
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2070
movl %eax, -0x20(%rbp)
cmpl $-0x1, -0x20(%rbp)
jne 0x265d
movq 0x69cf(%rip), %rdi      # 0x8ff0
leaq 0x3a02(%rip), %rsi      # 0x602a
callq 0x2220
movq %rax, %rdi
movl $0x7e9, %esi            # imm = 0x7E9
callq 0x2380
movq %rax, %rdi
movq 0x6974(%rip), %rsi      # 0x8fb8
callq 0x2270
movl -0x20(%rbp), %edi
callq 0x23f0
movl $0x0, -0x4(%rbp)
jmp 0x2731
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2130
movq -0x18(%rbp), %rdi
callq 0x2240
movl %eax, -0x2c(%rbp)
movw $0x2, -0x30(%rbp)
movl $0x7e9, %edi            # imm = 0x7E9
callq 0x2040
movw %ax, -0x2e(%rbp)
movl -0x20(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0x21c0
cmpl $-0x1, %eax
jne 0x26e2
movq 0x6946(%rip), %rdi      # 0x8ff0
leaq 0x399e(%rip), %rsi      # 0x604f
callq 0x2220
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x2220
movq %rax, %rdi
movq 0x68ec(%rip), %rsi      # 0x8fb8
callq 0x2270
movl -0x20(%rbp), %edi
callq 0x23f0
movl $0x0, -0x4(%rbp)
jmp 0x2731
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x4f(%rip), %rdx        # 0x2740
leaq -0x20(%rbp), %rcx
callq 0x23d0
movq -0x38(%rbp), %rdi
callq 0x2190
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x64e(%rip), %rdx       # 0x2d60
leaq -0x20(%rbp), %rcx
callq 0x23d0
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2200
xorl %eax, %eax
movl %eax, %edi
callq 0x23a0
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)