// File: /workspace/github_cpp_makefile_star2_O1/lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::FtpOpenPasv(ftphandle*, ftphandle**, ftplib::transfermode, int, char*)
int ftplib::FtpOpenPasv(ftphandle *nControl, ftphandle **nData, transfermode mode, int dir, char *cmd)
{
	int sData;
    int errorCode;
    int retry = 0;
	union {
		struct sockaddr sa;
		struct sockaddr_in in;
	} sin;
	struct linger lng = { 0, 0 };
	unsigned int l;
	int on=1;
	ftphandle *ctrl;
	char *cp;
	unsigned char v[6];
	int ret;

	if (nControl->dir != FTPLIB_CONTROL) 
        return -1;
	if ((dir != FTPLIB_READ) && (dir != FTPLIB_WRITE)) 	{
		sprintf(nControl->response, "Invalid direction %d\n", dir);
		return -1;
	}
	if ((mode != ftplib::ascii) && (mode != ftplib::image)) {
		sprintf(nControl->response, "Invalid mode %c\n", mode);
		return -1;
	}
	l = sizeof(sin);

	memset(&sin, 0, l);
	sin.in.sin_family = AF_INET;
	if (!FtpSendCmd("PASV",'2',nControl)) 
        return -1;
	cp = strchr(nControl->response,'(');
	if (cp == NULL) 
        return -1;
	cp++;
#if defined(_WIN32)
	unsigned int v_i[6];
    sscanf(cp,"%u,%u,%u,%u,%u,%u",&v_i[2],&v_i[3],&v_i[4],&v_i[5],&v_i[0],&v_i[1]);
    for (int i = 0; i < 6; i++) 
        v[i] = (unsigned char) v_i[i]; 
#else
	sscanf(cp,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
#endif
	if (nControl->correctpasv) 
        if (!CorrectPasvResponse(v)) 
            return -1;
	sin.sa.sa_data[2] = v[2];
	sin.sa.sa_data[3] = v[3];
	sin.sa.sa_data[4] = v[4];
	sin.sa.sa_data[5] = v[5];
	sin.sa.sa_data[0] = v[0];
	sin.sa.sa_data[1] = v[1];

	if (mp_ftphandle->offset != 0)
	{
		char buf[256];
        sprint_rest(buf, mp_ftphandle->offset);
		if (!FtpSendCmd(buf,'3',nControl)) 
            return 0;
	}

	sData = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
	if (sData == -1) 	{
		perror("socket");
		return -1;
	}
	if (setsockopt(sData,SOL_SOCKET,SO_REUSEADDR, SETSOCKOPT_OPTVAL_TYPE &on,sizeof(on)) == -1) {
		perror("setsockopt");
		net_close(sData);
		return -1;
	}
	if (setsockopt(sData,SOL_SOCKET,SO_LINGER, SETSOCKOPT_OPTVAL_TYPE &lng,sizeof(lng)) == -1) 	{
		perror("setsockopt");
		net_close(sData);
		return -1;
	}

	if (nControl->dir != FTPLIB_CONTROL) 
        return -1;
	memcpy(cmd + strlen(cmd), "\r\n\0", 3);
#ifndef NOSSL
	if (nControl->tlsctrl) 
        ret = SSL_write(nControl->ssl,cmd,strlen(cmd));
	else 
        ret = net_write(nControl->handle,cmd,strlen(cmd));
#else
	ret = net_write(nControl->handle,cmd,strlen(cmd));
#endif

    if(gFtpDebug)
        printf("Thread:%-8ld  sock:%-4d  %s", syscall(SYS_gettid), nControl->handle, cmd);

	if (ret <= 0) 	{
	    errorCode = errno;
        printf("Thread:%-8ld  sock:%-4d  %s  send failed:%s\n", syscall(SYS_gettid), nControl->handle, cmd, strerror(errno));
		return -1;
	}

    while (true) {
        retry++;
        if (connect(sData, &sin.sa, sizeof(sin.sa)) == -1)  {
            if (retry >= 5) {
                errorCode = errno;
                std::cerr << "connect to establish data connnection failed: " << strerror(errorCode) << std::endl;
                net_close(sData);
                return -1;
            }
        } else {
            break;
        }
    }

    // 此处的步骤是： 先建立数据连接，然后再等待控制连接的应答
	if (!readresp('1', nControl)) 	{
		net_close(sData);
		return -1;
	}
    if(gFtpDebug)
        printf("Thread:%-8ld  sock:%-4d  %s\n", syscall(SYS_gettid), nControl->handle,  nControl->response);
    
	ctrl = static_cast<ftphandle*>(calloc(1,sizeof(ftphandle)));
	if (ctrl == NULL) 	{
		perror("calloc");
		net_close(sData);
		return -1;
	}
	if ((mode == 'A') && ((ctrl->buf = static_cast<char*>(malloc(FTPLIB_BUFSIZ))) == NULL)) 	{
		perror("calloc");
		net_close(sData);
		free(ctrl);
		return -1;
	}
	ctrl->handle = sData;
	ctrl->dir = dir;
	ctrl->ctrl = (nControl->cmode == ftplib::pasv) ? nControl : NULL;
	ctrl->idletime = nControl->idletime;
	ctrl->cbarg = nControl->cbarg;
	ctrl->xfered = 0;
	ctrl->xfered1 = 0;
	ctrl->cbbytes = nControl->cbbytes;
	if (ctrl->idletime.tv_sec || ctrl->idletime.tv_usec) ctrl->idlecb = nControl->idlecb;
	else ctrl->idlecb = NULL;
	if (ctrl->cbbytes ) ctrl->xfercb = nControl->xfercb;
	else ctrl->xfercb = NULL;
	*nData = ctrl;

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq $0x0, 0x38(%rsp)
movl $0x1, 0xc(%rsp)
cmpl $0x0, 0x28(%rsi)
jne 0xf49c
movl %r8d, %r15d
movq %rsi, %rbx
leal -0x3(%r15), %eax
cmpl $-0x3, %eax
ja 0xf220
addq $0x88, %rbx
leaq 0x4643(%rip), %rsi      # 0x13858
movq %rbx, %rdi
movl %r15d, %edx
jmp 0xf495
movl %ecx, %ebp
movl %ecx, %eax
andl $-0x9, %eax
cmpl $0x41, %eax
jne 0xf482
movq %r9, %r12
movq %rdx, %r14
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movw $0x2, 0x10(%rsp)
leaq 0x466b(%rip), %rsi      # 0x138ba
movl $0x32, %edx
movq %rbx, %rcx
callq 0xeba0
testl %eax, %eax
je 0xf49c
leaq 0x88(%rbx), %rdi
movq %rdi, 0x30(%rsp)
movl $0x28, %esi
callq 0x4160
testq %rax, %rax
je 0xf49c
incq %rax
leaq 0x8(%rsp), %rdx
leaq 0x9(%rsp), %rcx
leaq 0xa(%rsp), %r8
leaq 0xb(%rsp), %r9
leaq 0x7(%rsp), %r10
leaq 0x6(%rsp), %r11
leaq 0x45f1(%rip), %rsi      # 0x1389c
movq %rax, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x43c0
addq $0x10, %rsp
cmpb $0x1, 0x1b8(%rbx)
jne 0xf2db
leaq 0x6(%rsp), %rsi
movq %r13, %rdi
callq 0xfa86
testl %eax, %eax
je 0xf49c
movq %r14, 0x28(%rsp)
movzwl 0xa(%rsp), %eax
movw %ax, 0x16(%rsp)
movl 0x6(%rsp), %eax
movl %eax, 0x12(%rsp)
movq (%r13), %rax
movq 0x1b0(%rax), %rdx
testq %rdx, %rdx
je 0xf335
leaq 0x4372(%rip), %rsi      # 0x1367b
xorl %r14d, %r14d
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
callq 0x40a0
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
movl $0x33, %edx
movq %rbx, %rcx
callq 0xeba0
testl %eax, %eax
je 0xf4a2
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x40c0
cmpl $-0x1, %eax
je 0xf5fb
movl %eax, %r14d
leaq 0xc(%rsp), %rcx
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4710
cmpl $-0x1, %eax
je 0xf4f4
leaq 0x38(%rsp), %rcx
movl %r14d, %edi
movl $0x1, %esi
movl $0xd, %edx
movl $0x8, %r8d
callq 0x4710
cmpl $-0x1, %eax
je 0xf4f4
cmpl $0x0, 0x28(%rbx)
jne 0xf49c
movl %r14d, (%rsp)
movq %r12, %rdi
callq 0x4180
movw $0xa0d, (%r12,%rax)     # imm = 0xA0D
movb $0x0, 0x2(%r12,%rax)
movl 0x10(%rbx), %r14d
movq %r12, %rdi
callq 0x4180
movl %r14d, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x4140
movq %rax, %r14
leaq 0x90f1(%rip), %rax      # 0x184d8
cmpb $0x1, (%rax)
jne 0xf40f
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movl 0x10(%rbx), %edx
leaq 0x44bd(%rip), %rdi      # 0x138bf
movq %rax, %rsi
movq %r12, %rcx
xorl %eax, %eax
callq 0x4060
testl %r14d, %r14d
jle 0xf4b7
movl $0x5, %r12d
leaq 0x10(%rsp), %r14
movl (%rsp), %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x4300
cmpl $-0x1, %eax
jne 0xf508
decl %r12d
jne 0xf423
callq 0x4050
movl (%rax), %ebx
movq 0x8ba1(%rip), %r14      # 0x17ff0
leaq 0x44b2(%rip), %rsi      # 0x13908
movl $0x2e, %edx
movq %r14, %rdi
callq 0x4460
movl %ebx, %edi
callq 0x4130
movq %r14, %rdi
movq %rax, %rsi
callq 0x43d0
movq %rax, %rdi
callq 0x41c0
jmp 0xf631
addq $0x88, %rbx
leaq 0x43de(%rip), %rsi      # 0x1386e
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x40a0
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4050
movq %rax, %r14
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movq %rax, %r15
movl 0x10(%rbx), %ebx
movl (%r14), %edi
callq 0x4130
leaq 0x43fb(%rip), %rdi      # 0x138db
movq %r15, %rsi
movl %ebx, %edx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x4060
jmp 0xf49c
leaq 0x4250(%rip), %rdi      # 0x1374b
callq 0x4310
movl %r14d, %edi
jmp 0xf634
movq %r13, %rdi
movl $0x31, %esi
movq %rbx, %rdx
callq 0xe74c
testl %eax, %eax
je 0xf631
leaq 0x8fb1(%rip), %rax      # 0x184d8
cmpb $0x1, (%rax)
jne 0xf551
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movl 0x10(%rbx), %edx
leaq 0x4264(%rip), %rdi      # 0x137a6
movq %rax, %rsi
movq 0x30(%rsp), %rcx
xorl %eax, %eax
callq 0x4060
movl $0x1, %edi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4240
testq %rax, %rax
movq 0x28(%rsp), %r14
je 0xf625
movq %rax, %r12
cmpl $0x41, %ebp
jne 0xf58e
movl $0x400, %edi            # imm = 0x400
callq 0x4500
movq %rax, 0x20(%r12)
testq %rax, %rax
je 0xf63e
movl (%rsp), %eax
movl %eax, 0x10(%r12)
movl %r15d, 0x28(%r12)
xorl %ecx, %ecx
cmpl $0x1, 0x38(%rbx)
movl $0x0, %eax
cmoveq %rbx, %rax
movq %rax, 0x30(%r12)
movups 0x40(%rbx), %xmm0
movups %xmm0, 0x40(%r12)
movq 0x68(%rbx), %rax
movq %rax, 0x68(%r12)
movq %rcx, 0x70(%r12)
movq %rcx, 0x80(%r12)
movq 0x78(%rbx), %rax
movq %rax, 0x78(%r12)
cmpq %rcx, 0x40(%r12)
jne 0xf5e7
cmpq $0x0, 0x48(%r12)
je 0xf621
movq 0x58(%rbx), %rcx
movq %rcx, 0x58(%r12)
testq %rax, %rax
je 0xf60c
movq 0x50(%rbx), %rax
jmp 0xf60e
leaq 0x4142(%rip), %rdi      # 0x13744
callq 0x4310
jmp 0xf49c
xorl %eax, %eax
movq %rax, 0x50(%r12)
movq %r12, (%r14)
movl $0x1, %r14d
jmp 0xf4a2
xorl %ecx, %ecx
jmp 0xf5eb
leaq 0x4048(%rip), %rdi      # 0x13674
callq 0x4310
movl (%rsp), %edi
callq 0x4770
jmp 0xf49c
leaq 0x402f(%rip), %rdi      # 0x13674
callq 0x4310
movl (%rsp), %edi
callq 0x4770
movq %r12, %rdi
callq 0x44a0
jmp 0xf49c
nop