// File: /workspace/github_cpp_makefile_star2_O1/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 0x4730
movq %rax, %rbx
movq %rax, %rdi
movl $0x3a, %esi
callq 0x4160
movq %rax, %rdi
movw $0x1500, %ax            # imm = 0x1500
testq %rdi, %rdi
je 0xe97d
movb $0x0, (%rdi)
movsbl 0x1(%rdi), %ecx
addl $-0x30, %ecx
cmpl $0x9, %ecx
ja 0xe97d
incq %rdi
callq 0x4600
rolw $0x8, %ax
leaq 0x14(%rsp), %r12
movw %ax, -0x2(%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x42a0
testl %eax, %eax
jne 0xe9bb
movq %rbx, %rdi
callq 0x4450
testq %rax, %rax
je 0xeb89
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 0xeb6d
movl %eax, %ebx
leaq 0xc(%rsp), %rcx
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4710
cmpl $-0x1, %eax
je 0xeb7b
leaq 0x2c(%rsp), %rcx
movl $0x1, (%rcx)
movl $0x1, %ebp
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x4710
leaq 0x10(%rsp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x4300
cmpl $-0x1, %eax
je 0xea75
movq (%r15), %rdx
movl %ebx, 0x10(%rdx)
movq %r15, %rdi
movl $0x32, %esi
callq 0xe74c
testl %eax, %eax
jne 0xeb5e
movl %ebx, %edi
callq 0x4770
movq (%r15), %rax
movl $0x0, 0x10(%rax)
jmp 0xeb5c
callq 0x4050
movl (%rax), %ebp
movq 0x956d(%rip), %r15      # 0x17ff0
leaq 0x4ccc(%rip), %rsi      # 0x13756
movl $0xb, %edx
movq %r15, %rdi
callq 0x4460
testq %r14, %r14
je 0xeab8
movq %r14, %rdi
callq 0x4180
movq 0x9545(%rip), %rdi      # 0x17ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x4460
jmp 0xead0
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
orl $0x1, %esi
callq 0x4690
movq 0x9519(%rip), %r14      # 0x17ff0
leaq 0x4c4b(%rip), %rsi      # 0x13729
movl $0x9, %edx
movq %r14, %rdi
callq 0x4460
movl %ebp, %edi
callq 0x4130
testq %rax, %rax
je 0xeb16
movq %rax, %r15
movq %rax, %rdi
callq 0x4180
movq 0x94e7(%rip), %rdi      # 0x17ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x4460
jmp 0xeb2e
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x4690
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 0x4770
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4bd0(%rip), %rdi      # 0x13744
callq 0x4310
jmp 0xeb5c
leaq 0x4bc9(%rip), %rdi      # 0x1374b
callq 0x4310
jmp 0xeb55
leaq 0x4ba6(%rip), %rdi      # 0x13736
callq 0x4310
movq %rbx, %rdi
callq 0x44a0
jmp 0xeb5c
nop