// File: /workspace/github_c_makefile_star2_O2/hasindu2008[P]f5p/socket.c
// Function: TCP_client_connect
int TCP_client_connect(char* ip, int PORT) {
    //socket for connecting
    int socketfd = socket(AF_INET, SOCK_STREAM, 0);
    errorCheck(socketfd, "Cannot create socket.");

    //tcpkeepalive activation (https://www.tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/)
    /* Set the option active */   
    int optval = 1;
    socklen_t optlen = sizeof(optval);
    if(setsockopt(socketfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0 ) {
        if(verbose_socklibc>=1) {
			WARNING("%s","Could not enable tcp keepalive. Dead host detection will not work.");    
		}
    }
    else{
        //TCP_KEEPCNT: overrides tcp_keepalive_probes : the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
        //TCP_KEEPIDLE: overrides tcp_keepalive_time : the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further
        //TCP_KEEPINTVL: overrides  tcp_keepalive_intvl : the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
        optval=5;
        int ret1=setsockopt(socketfd, IPPROTO_TCP, TCP_KEEPCNT, &optval, optlen);
        optval=300;
        int ret2=setsockopt(socketfd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, optlen);
        optval=60;
        int ret3=setsockopt(socketfd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, optlen);
        if(ret1<0 || ret2<0 || ret3<0){
            if(verbose_socklibc>=1){
				WARNING("%s","Could not set tcp keepalive parameters. Dead host detection may not work.");
			}
        }
                  
    }

    //initializing the server address and assigning port numbers
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ip);
    server.sin_port = htons(PORT);

    //connecting. The following (comment) tries ones and giveup
    //int ret=connect(socketfd,(struct sockaddr *)&server, sizeof(server));

    //try connecting until successful. this is better than trying once and giving up
    int ret = -1;
    while (ret == -1) {
        ret = connect(socketfd, (struct sockaddr*)&server, sizeof(server));
        sleep(1);
    }

    errorCheck(ret, "Cannot connect.");
    if(verbose_socklibc>=4){
		INFO("Connected to server %s.", ip);
	}

    return socketfd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r14d
movq %rdi, %rbx
pushq $0x2
popq %rdi
pushq $0x1
popq %r15
movl %r15d, %esi
xorl %edx, %edx
callq 0x1270
movl %eax, %ebp
leaq 0x18a1(%rip), %rsi      # 0x3248
movl %eax, %edi
callq 0x1879
leaq 0x14(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x9
popq %rdx
pushq $0x4
popq %r8
movl %ebp, %edi
movl %r15d, %esi
callq 0x1080
testl %eax, %eax
js 0x1a60
leaq 0x14(%rsp), %r15
movl $0x5, (%r15)
pushq $0x6
popq %r12
pushq $0x4
popq %r13
movl %ebp, %edi
movl %r12d, %esi
movl %r12d, %edx
movq %r15, %rcx
movl %r13d, %r8d
callq 0x1080
movl %eax, 0xc(%rsp)
movl $0x12c, (%r15)          # imm = 0x12C
movl %ebp, %edi
movl %r12d, %esi
movl %r13d, %edx
movq %r15, %rcx
movl %r13d, %r8d
callq 0x1080
movl %r14d, 0x10(%rsp)
movq %rbx, %r14
movl %eax, %ebx
movl $0x3c, (%r15)
pushq $0x5
popq %rdx
movl %ebp, %edi
movl %r12d, %esi
movq %r15, %rcx
movl %r13d, %r8d
callq 0x1080
orl 0xc(%rsp), %ebx
orl %eax, %ebx
movq %r14, %rbx
movl 0x10(%rsp), %r14d
jns 0x1a6d
cmpl $0x0, 0x46fe(%rip)      # 0x6150
jle 0x1a6d
leaq 0x1859(%rip), %rcx      # 0x32b4
jmp 0x1b0e
cmpl $0x0, 0x46e9(%rip)      # 0x6150
jg 0x1b07
leaq 0x18(%rsp), %r15
movw $0x2, (%r15)
movq %rbx, %rdi
callq 0x1110
movl %eax, 0x4(%r15)
rolw $0x8, %r14w
movw %r14w, 0x2(%r15)
pushq $-0x1
popq %r13
pushq $0x10
popq %r14
pushq $0x1
popq %r12
cmpl $-0x1, %r13d
jne 0x1aba
movl %ebp, %edi
movq %r15, %rsi
movl %r14d, %edx
callq 0x1210
movl %eax, %r13d
movl %r12d, %edi
callq 0x1250
jmp 0x1a9a
leaq 0x183d(%rip), %rsi      # 0x32fe
movl %r13d, %edi
callq 0x1879
cmpl $0x4, 0x4680(%rip)      # 0x6150
jge 0x1ae3
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x450e(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x181a(%rip), %rsi      # 0x330e
leaq 0x1763(%rip), %rdx      # 0x325e
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1130
jmp 0x1ad2
leaq 0x1763(%rip), %rcx      # 0x3271
movq 0x44e3(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1597(%rip), %rsi      # 0x30b6
leaq 0x1738(%rip), %rdx      # 0x325e
xorl %eax, %eax
callq 0x1130
jmp 0x1a6d