// File: /workspace/github_cpp_makefile_star2_O2/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 %rdi, %rbx
movl $0x1, 0xc(%rsp)
movq (%rdi), %rax
andl $0x0, 0x28(%rax)
andq $0x0, 0x30(%rax)
andq $0x0, 0x70(%rax)
andq $0x0, 0x80(%rax)
andq $0x0, 0x1b0(%rax)
andl $0x0, 0x10(%rax)
movq %rsi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movw $0x2, 0x10(%rsp)
movq %rsi, %rdi
callq 0x46e0
movq %rax, %r15
pushq $0x3a
popq %rsi
movq %rax, %rdi
callq 0x4140
movq %rax, %rdi
movw $0x1500, %ax            # imm = 0x1500
testq %rdi, %rdi
je 0xc839
movb $0x0, (%rdi)
movsbl 0x1(%rdi), %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0xc839
incq %rdi
callq 0x45c0
rolw $0x8, %ax
leaq 0x14(%rsp), %r12
movw %ax, -0x2(%r12)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4290
testl %eax, %eax
jne 0xc877
movq %r15, %rdi
callq 0x4440
testq %rax, %rax
je 0xc997
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
movq %r12, %rdi
callq 0x42e0
movq %r15, %rdi
callq 0x4480
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x40b0
cmpl $-0x1, %eax
je 0xc97b
movl %eax, %ebp
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
leaq 0xc(%rsp), %rcx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x46c0
cmpl $-0x1, %eax
je 0xc989
leaq 0x2c(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x6
popq %rsi
pushq $0x1
popq %r15
pushq $0x4
popq %r8
movl %ebp, %edi
movl %r15d, %edx
callq 0x46c0
leaq 0x10(%rsp), %rsi
pushq $0x10
popq %rdx
movl %ebp, %edi
callq 0x42f0
cmpl $-0x1, %eax
je 0xc910
movq (%rbx), %rdx
movl %ebp, 0x10(%rdx)
pushq $0x32
popq %rsi
movq %rbx, %rdi
callq 0xc65c
testl %eax, %eax
jne 0xc96b
movl %ebp, %edi
callq 0x4740
movq (%rbx), %rax
andl $0x0, 0x10(%rax)
jmp 0xc968
callq 0x4040
movl (%rax), %ebx
movq 0xa6d2(%rip), %rdi      # 0x16ff0
leaq 0x4de1(%rip), %rsi      # 0x11706
callq 0x43c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x43c0
leaq 0x4d9d(%rip), %rsi      # 0x116d9
movq %rax, %rdi
callq 0x43c0
movq %rax, %r14
movl %ebx, %edi
callq 0x4110
movq %r14, %rdi
movq %rax, %rsi
callq 0x43c0
movq %rax, %rdi
callq 0x41b0
movl %ebp, %edi
callq 0x4740
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4d72(%rip), %rdi      # 0x116f4
callq 0x4300
jmp 0xc968
leaq 0x4d6b(%rip), %rdi      # 0x116fb
callq 0x4300
jmp 0xc961
leaq 0x4d48(%rip), %rdi      # 0x116e6
callq 0x4300
movq %r15, %rdi
callq 0x4480
jmp 0xc968
nop