// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1280
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %edi
leaq 0x274b(%rip), %rsi      # 0x4248
callq 0x1970
movl $0x1, -0x14(%rbp)
movl $0x4, -0x18(%rbp)
movl -0x10(%rbp), %edi
movl -0x18(%rbp), %r8d
movl $0x1, %esi
movl $0x9, %edx
leaq -0x14(%rbp), %rcx
callq 0x1080
cmpl $0x0, %eax
jge 0x1b63
cmpl $0x1, 0x5622(%rip)      # 0x7158
jl 0x1b5e
movq 0x54b9(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x256d(%rip), %rsi      # 0x40b6
leaq 0x270e(%rip), %rdx      # 0x425e
leaq 0x271a(%rip), %rcx      # 0x4271
movb $0x0, %al
callq 0x1160
jmp 0x1c11
movl $0x5, -0x14(%rbp)
movl -0x10(%rbp), %edi
movl -0x18(%rbp), %r8d
movl $0x6, %edx
leaq -0x14(%rbp), %rcx
movl %edx, %esi
callq 0x1080
movl %eax, -0x1c(%rbp)
movl $0x12c, -0x14(%rbp)     # imm = 0x12C
movl -0x10(%rbp), %edi
movl -0x18(%rbp), %r8d
movl $0x6, %esi
movl $0x4, %edx
leaq -0x14(%rbp), %rcx
callq 0x1080
movl %eax, -0x20(%rbp)
movl $0x3c, -0x14(%rbp)
movl -0x10(%rbp), %edi
movl -0x18(%rbp), %r8d
movl $0x6, %esi
movl $0x5, %edx
leaq -0x14(%rbp), %rcx
callq 0x1080
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x1bde
cmpl $0x0, -0x20(%rbp)
jl 0x1bde
cmpl $0x0, -0x24(%rbp)
jge 0x1c0f
cmpl $0x1, 0x5573(%rip)      # 0x7158
jl 0x1c0d
movq 0x540a(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x24be(%rip), %rsi      # 0x40b6
leaq 0x265f(%rip), %rdx      # 0x425e
leaq 0x26ae(%rip), %rcx      # 0x42b4
movb $0x0, %al
callq 0x1160
jmp 0x1c0f
jmp 0x1c11
movw $0x2, -0x34(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1130
movl %eax, -0x30(%rbp)
movl -0xc(%rbp), %eax
movzwl %ax, %edi
callq 0x10d0
movw %ax, -0x32(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
cmpl $-0x1, -0x38(%rbp)
jne 0x1c5f
movl -0x10(%rbp), %edi
leaq -0x34(%rbp), %rsi
movl $0x10, %edx
callq 0x1230
movl %eax, -0x38(%rbp)
movl $0x1, %edi
callq 0x1260
jmp 0x1c39
movl -0x38(%rbp), %edi
leaq 0x2695(%rip), %rsi      # 0x42fe
callq 0x1970
cmpl $0x4, 0x54e3(%rip)      # 0x7158
jl 0x1c9a
movq 0x537a(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rcx
leaq 0x2682(%rip), %rsi      # 0x430e
leaq 0x25cb(%rip), %rdx      # 0x425e
movb $0x0, %al
callq 0x1160
movl -0x10(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)