// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x1, -0x44(%rbp)
movq (%rax), %rcx
movl $0x0, 0x28(%rcx)
movq (%rax), %rcx
movq $0x0, 0x30(%rcx)
movq (%rax), %rcx
movq $0x0, 0x70(%rcx)
movq (%rax), %rcx
movq $0x0, 0x80(%rcx)
movq (%rax), %rcx
movq $0x0, 0x1b0(%rcx)
movq (%rax), %rax
movl $0x0, 0x10(%rax)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x4250
movw $0x2, -0x30(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4860
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movl $0x3a, %esi
callq 0x4180
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x16f88
movl $0x15, %edi
callq 0x4080
movw %ax, -0x2e(%rbp)
jmp 0x16fd2
movq -0x58(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x58(%rbp)
movb $0x0, (%rax)
movq -0x58(%rbp), %rax
movsbl (%rax), %edi
callq 0x43e0
cmpl $0x0, %eax
je 0x16fc2
movq -0x58(%rbp), %rdi
callq 0x4710
movzwl %ax, %edi
callq 0x4080
movw %ax, -0x2e(%rbp)
jmp 0x16fd0
movl $0x15, %edi
callq 0x4080
movw %ax, -0x2e(%rbp)
jmp 0x16fd2
movq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rsi
addq $0x4, %rsi
callq 0x4320
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x17040
movq -0x50(%rbp), %rdi
callq 0x4560
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x17020
leaq 0x67d4(%rip), %rdi      # 0x1d7da
callq 0x43a0
movq -0x50(%rbp), %rdi
callq 0x45a0
movl $0x0, -0x4(%rbp)
jmp 0x171b0
leaq -0x30(%rbp), %rdi
addq $0x4, %rdi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x4380
movq -0x50(%rbp), %rdi
callq 0x45a0
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x40f0
movl %eax, -0x20(%rbp)
cmpl $-0x1, -0x20(%rbp)
jne 0x1707e
leaq 0x677b(%rip), %rdi      # 0x1d7e8
callq 0x43a0
movl $0x0, -0x4(%rbp)
jmp 0x171b0
movl -0x20(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x44(%rbp), %rcx
movl $0x4, %r8d
callq 0x4840
cmpl $-0x1, %eax
jne 0x170bf
leaq 0x6749(%rip), %rdi      # 0x1d7ef
callq 0x43a0
movl -0x20(%rbp), %edi
callq 0x48b0
movl $0x0, -0x4(%rbp)
jmp 0x171b0
movl $0x1, -0x5c(%rbp)
movl -0x20(%rbp), %edi
movl $0x6, %esi
movl $0x1, %edx
leaq -0x5c(%rbp), %rcx
movl $0x4, %r8d
callq 0x4840
movl %eax, -0x48(%rbp)
movl -0x20(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0x4390
cmpl $-0x1, %eax
jne 0x1716b
callq 0x4060
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
movq 0xeee4(%rip), %rdi      # 0x25ff0
leaq 0x66e7(%rip), %rsi      # 0x1d7fa
callq 0x4490
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x4490
movq %rax, %rdi
leaq 0x669f(%rip), %rsi      # 0x1d7cd
callq 0x4490
movq %rax, -0x70(%rbp)
movl -0x1c(%rbp), %edi
callq 0x4150
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0x4490
movq %rax, %rdi
movq 0xee43(%rip), %rsi      # 0x25f98
callq 0x44e0
movl -0x20(%rbp), %edi
callq 0x48b0
movl $0x0, -0x4(%rbp)
jmp 0x171b0
movq -0x68(%rbp), %rdi
movl -0x20(%rbp), %ecx
movq (%rdi), %rax
movl %ecx, 0x10(%rax)
movq (%rdi), %rdx
movl $0x32, %esi
callq 0x16cc0
cmpl $0x0, %eax
jne 0x171a9
movl -0x20(%rbp), %edi
callq 0x48b0
movq -0x68(%rbp), %rax
movq (%rax), %rax
movl $0x0, 0x10(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x171b0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)