// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1260
movl %eax, %ebp
leaq 0x13f2(%rip), %rsi      # 0x3248
movl %eax, %edi
callq 0x1d45
leaq 0x4(%rsp), %rcx
movl $0x1, (%rcx)
movl %ebp, %edi
movl $0x1, %esi
movl $0x9, %edx
movl $0x4, %r8d
callq 0x1080
testl %eax, %eax
js 0x1f12
leaq 0x4(%rsp), %r15
movl $0x5, (%r15)
movl %ebp, %edi
movl $0x6, %esi
movl $0x6, %edx
movq %r15, %rcx
movl $0x4, %r8d
callq 0x1080
movl %eax, %r12d
movl $0x12c, (%r15)          # imm = 0x12C
movl %ebp, %edi
movl $0x6, %esi
movl $0x4, %edx
movq %r15, %rcx
movl $0x4, %r8d
callq 0x1080
movl %eax, %r13d
movl $0x3c, (%r15)
movl %ebp, %edi
movl $0x6, %esi
movl $0x5, %edx
movq %r15, %rcx
movl $0x4, %r8d
callq 0x1080
orl %r12d, %r13d
orl %eax, %r13d
jns 0x1f1f
cmpl $0x0, 0x4244(%rip)      # 0x6148
jle 0x1f1f
leaq 0x13a7(%rip), %rdi      # 0x32b4
jmp 0x1fb6
cmpl $0x0, 0x422f(%rip)      # 0x6148
jg 0x1faf
leaq 0x8(%rsp), %r15
movw $0x2, (%r15)
movq %rbx, %rdi
callq 0x1110
movl %eax, 0x4(%r15)
rolw $0x8, %r14w
movw %r14w, 0x2(%r15)
movl %ebp, %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x1200
movl %eax, %r14d
movl $0x1, %edi
callq 0x1240
cmpl $-0x1, %r14d
je 0x1f40
leaq 0x1395(%rip), %rsi      # 0x32fe
movl %r14d, %edi
callq 0x1d45
cmpl $0x4, 0x41d0(%rip)      # 0x6148
jge 0x1f8b
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4066(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1372(%rip), %rsi      # 0x330e
leaq 0x12bb(%rip), %rdx      # 0x325e
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1130
jmp 0x1f7a
leaq 0x12bb(%rip), %rdi      # 0x3271
callq 0x13ec
jmp 0x1f1f