// File: luisremis[P]SDFS/connections.cpp
// Function: connect_to_server(char const*, int, int*)
int connect_to_server(const char* add, int port, int* connectionFd)
{
    struct sockaddr_in svrAdd;
    struct hostent *server;
    
    if((port > 65535) || (port < 2000))
    {
        std::cout<<"Please enter port number between 2000 - 65535"<<std::endl;
        exit(1);
    }       
    
    //create client skt
    *connectionFd = socket(AF_INET, SOCK_STREAM, 0);
        int iSetOption = 1;
    setsockopt(*connectionFd, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption,
        sizeof(iSetOption));
    
    if(*connectionFd < 0)
    {
        std::cout << "Cannot open socket" << std::endl;
        exit(1);
    }
    else{
        //std::cout << "Socket opened" << std::endl;
    }
    
    server = gethostbyname(add);
    
    if(server == NULL)
    {
        std::cout << "Host does not exist" << std::endl;
        exit(1);
    }
    
    memset((char *) &svrAdd,0, sizeof(svrAdd));
    svrAdd.sin_family = AF_INET;
    
    memcpy((char *) &svrAdd.sin_addr.s_addr,(char *) server -> h_addr, server -> h_length);
    
    svrAdd.sin_port = htons(port);
    
    int checker = connect(*connectionFd,(struct sockaddr *) &svrAdd, sizeof(svrAdd));
    
    if (checker < 0)
    {
        //printf("Cannot connect to: %s \n",add);
        return -1;
        //exit(1);
    }
    else{
        return 0;
        //std::cout << "Client Connection Successful" << std::endl;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpl $0xffff, -0x14(%rbp)    # imm = 0xFFFF
jg 0x1ab15
cmpl $0x7d0, -0x14(%rbp)     # imm = 0x7D0
jge 0x1ab41
movq 0xc4a4(%rip), %rdi      # 0x26fc0
leaq 0x22f8(%rip), %rsi      # 0x1ce1b
callq 0x43f0
movq %rax, %rdi
movq 0xc476(%rip), %rsi      # 0x26fa8
callq 0x4410
movl $0x1, %edi
callq 0x4460
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x4110
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x3c(%rbp), %rcx
movl $0x4, %r8d
callq 0x46d0
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x1abb5
movq 0xc430(%rip), %rdi      # 0x26fc0
leaq 0x21ec(%rip), %rsi      # 0x1cd83
callq 0x43f0
movq %rax, %rdi
movq 0xc402(%rip), %rsi      # 0x26fa8
callq 0x4410
movl $0x1, %edi
callq 0x4460
jmp 0x1abb7
movq -0x10(%rbp), %rdi
callq 0x4450
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x1abf7
movq 0xc3ee(%rip), %rdi      # 0x26fc0
leaq 0x21ea(%rip), %rsi      # 0x1cdc3
callq 0x43f0
movq %rax, %rdi
movq 0xc3c0(%rip), %rsi      # 0x26fa8
callq 0x4410
movl $0x1, %edi
callq 0x4460
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4240
movw $0x2, -0x30(%rbp)
leaq -0x30(%rbp), %rdi
addq $0x4, %rdi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x4330
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0x4080
movw %ax, -0x2e(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %edi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0x4340
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jge 0x1ac62
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1ac69
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)