// File: /workspace/github_cpp_makefile_star2_O3/lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::Connect(char const*)
int ftplib::Connect(const char *host)
{
    int errorCode = 0;
	int sControl;
	struct sockaddr_in sin;
	struct hostent *phe;
	struct servent *pse;
	int on=1;
	int ret;
	char *lhost;
	char *pnum;
	
	mp_ftphandle->dir = FTPLIB_CONTROL;
	mp_ftphandle->ctrl = NULL;
	mp_ftphandle->xfered = 0;
	mp_ftphandle->xfered1 = 0;
#ifndef NOSSL	
	mp_ftphandle->tlsctrl = 0;
	mp_ftphandle->tlsdata = 0;
#endif
	mp_ftphandle->offset = 0;
	mp_ftphandle->handle = 0;
	
	memset(&sin,0,sizeof(sin));
	sin.sin_family = AF_INET;
	lhost = strdup(host);
	pnum = strchr(lhost,':');
	if (pnum == NULL) 	{
		sin.sin_port = htons(21);
	} else 	{
		*pnum++ = '\0';
		if (isdigit(*pnum)) 
            sin.sin_port = htons(atoi(pnum));
		else {
            sin.sin_port = htons(21);
		}
	}
	
#if defined(_WIN32)
	if ((sin.sin_addr.s_addr = inet_addr(lhost)) == -1)
#else
	ret = inet_aton(lhost, &sin.sin_addr);
	if (ret == 0)
#endif
	{
		if ((phe = gethostbyname(lhost)) == NULL) 		{
			perror("gethostbyname");
			free(lhost);
			return 0;
		}
		memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
	}
	
	free(lhost);

	sControl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sControl == -1) 	{
		perror("socket");
		return 0;
	}
	
	if (setsockopt(sControl,SOL_SOCKET,SO_REUSEADDR, SETSOCKOPT_OPTVAL_TYPE &on, sizeof(on)) == -1) 	{
		perror("setsockopt");
		net_close(sControl);
		return 0;
	}
    {
        int flag = 1;
        ret = setsockopt(sControl, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag));
    }   
    
	if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)  {
	    errorCode = errno;
		std::cerr << "connect to " << host << " failed: " << strerror(errorCode) << std::endl;
		net_close(sControl);
		return 0;
	}

	mp_ftphandle->handle = sControl;
 
	if (readresp('2', mp_ftphandle) == 0) 	{
		net_close(sControl);
		mp_ftphandle->handle = 0;
		return 0;
	}

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rsi, %r14
movq %rdi, %r15
movl $0x1, 0xc(%rsp)
movq (%rdi), %rax
xorl %ecx, %ecx
movl %ecx, 0x28(%rax)
movq %rcx, 0x30(%rax)
movq %rcx, 0x70(%rax)
movq %rcx, 0x80(%rax)
movq %rcx, 0x1b0(%rax)
movl %ecx, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movw $0x2, 0x10(%rsp)
movq %rsi, %rdi
callq 0x4720
movq %rax, %rbx
movq %rax, %rdi
movl $0x3a, %esi
callq 0x4160
movq %rax, %rdi
movw $0x1500, %ax            # imm = 0x1500
testq %rdi, %rdi
je 0xe96d
movb $0x0, (%rdi)
movsbl 0x1(%rdi), %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0xe96d
incq %rdi
callq 0x45f0
rolw $0x8, %ax
leaq 0x14(%rsp), %r12
movw %ax, -0x2(%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x42a0
testl %eax, %eax
jne 0xe9ab
movq %rbx, %rdi
callq 0x4450
testq %rax, %rax
je 0xeb79
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
movq %r12, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x44a0
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x40c0
cmpl $-0x1, %eax
je 0xeb5d
movl %eax, %ebx
leaq 0xc(%rsp), %rcx
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4700
cmpl $-0x1, %eax
je 0xeb6b
leaq 0x2c(%rsp), %rcx
movl $0x1, (%rcx)
movl $0x1, %ebp
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x4700
leaq 0x10(%rsp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x4300
cmpl $-0x1, %eax
je 0xea65
movq (%r15), %rdx
movl %ebx, 0x10(%rdx)
movq %r15, %rdi
movl $0x32, %esi
callq 0xe73c
testl %eax, %eax
jne 0xeb4e
movl %ebx, %edi
callq 0x4760
movq (%r15), %rax
movl $0x0, 0x10(%rax)
jmp 0xeb4c
callq 0x4050
movl (%rax), %ebp
movq 0x957d(%rip), %r15      # 0x17ff0
leaq 0x4cec(%rip), %rsi      # 0x13766
movl $0xb, %edx
movq %r15, %rdi
callq 0x4460
testq %r14, %r14
je 0xeaa8
movq %r14, %rdi
callq 0x4180
movq 0x9555(%rip), %rdi      # 0x17ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x4460
jmp 0xeac0
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x4680
movq 0x9529(%rip), %r14      # 0x17ff0
leaq 0x4c6b(%rip), %rsi      # 0x13739
movl $0x9, %edx
movq %r14, %rdi
callq 0x4460
movl %ebp, %edi
callq 0x4130
testq %rax, %rax
je 0xeb06
movq %rax, %r15
movq %rax, %rdi
callq 0x4180
movq 0x94f7(%rip), %rdi      # 0x17ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x4460
jmp 0xeb1e
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x4680
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x43b0
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movl %ebx, %edi
callq 0x4760
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4bf0(%rip), %rdi      # 0x13754
callq 0x4310
jmp 0xeb4c
leaq 0x4be9(%rip), %rdi      # 0x1375b
callq 0x4310
jmp 0xeb45
leaq 0x4bc6(%rip), %rdi      # 0x13746
callq 0x4310
movq %rbx, %rdi
callq 0x44a0
jmp 0xeb4c
nop