// File: /workspace/github_cpp_makefile_star2_O3/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, 0x14(%rsp)
cmpl $0x0, 0x28(%rsi)
jne 0xf493
movl %r8d, %r15d
movq %rsi, %rbx
leal -0x3(%r15), %eax
cmpl $-0x3, %eax
ja 0xf214
addq $0x88, %rbx
leaq 0x465f(%rip), %rsi      # 0x13868
movq %rbx, %rdi
movl %r15d, %edx
jmp 0xf48c
movl %ecx, %ebp
movl %ecx, %eax
andl $-0x9, %eax
cmpl $0x41, %eax
jne 0xf479
movq %r9, %r12
movq %rdx, %r14
movq %rdi, %r13
movq $0x0, 0x20(%rsp)
movw $0x2, 0x18(%rsp)
leaq 0x4686(%rip), %rsi      # 0x138ca
movl $0x32, %edx
movq %rbx, %rcx
callq 0xeb90
testl %eax, %eax
je 0xf493
leaq 0x88(%rbx), %rdi
movq %rdi, 0x30(%rsp)
movl $0x28, %esi
callq 0x4160
testq %rax, %rax
je 0xf493
incq %rax
leaq 0xe(%rsp), %rdx
leaq 0xf(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x11(%rsp), %r9
leaq 0xd(%rsp), %r10
leaq 0xc(%rsp), %r11
leaq 0x460c(%rip), %rsi      # 0x138ac
movq %rax, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x43c0
addq $0x10, %rsp
cmpb $0x1, 0x1b8(%rbx)
jne 0xf2d0
leaq 0xc(%rsp), %rsi
movq %r13, %rdi
callq 0xfa3a
testl %eax, %eax
je 0xf493
movq %r14, 0x28(%rsp)
movzwl 0x10(%rsp), %eax
movw %ax, 0x1e(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x1a(%rsp)
movq (%r13), %rax
movq 0x1b0(%rax), %rdx
testq %rdx, %rdx
je 0xf32a
leaq 0x438d(%rip), %rsi      # 0x1368b
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 0xeb90
testl %eax, %eax
je 0xf499
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x40c0
cmpl $-0x1, %eax
je 0xf5e2
movl %eax, %r14d
leaq 0x14(%rsp), %rcx
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x4700
cmpl $-0x1, %eax
je 0xf4eb
leaq 0x38(%rsp), %rcx
movl %r14d, %edi
movl $0x1, %esi
movl $0xd, %edx
movl $0x8, %r8d
callq 0x4700
cmpl $-0x1, %eax
je 0xf4eb
cmpl $0x0, 0x28(%rbx)
jne 0xf493
movl %r14d, 0x8(%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 0x90f3(%rip), %rax      # 0x184d0
cmpb $0x1, (%rax)
jne 0xf405
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movl 0x10(%rbx), %edx
leaq 0x44d7(%rip), %rdi      # 0x138cf
movq %rax, %rsi
movq %r12, %rcx
xorl %eax, %eax
callq 0x4060
testl %r14d, %r14d
jle 0xf4ae
movl $0x5, %r12d
leaq 0x18(%rsp), %r14
movl 0x8(%rsp), %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x4300
cmpl $-0x1, %eax
jne 0xf4ff
decl %r12d
jne 0xf419
callq 0x4050
movl (%rax), %ebx
movq 0x8baa(%rip), %r14      # 0x17ff0
leaq 0x44cb(%rip), %rsi      # 0x13918
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 0xf614
addq $0x88, %rbx
leaq 0x43f7(%rip), %rsi      # 0x1387e
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 0x4414(%rip), %rdi      # 0x138eb
movq %r15, %rsi
movl %ebx, %edx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x4060
jmp 0xf493
leaq 0x4269(%rip), %rdi      # 0x1375b
callq 0x4310
movl %r14d, %edi
jmp 0xf618
movq %r13, %rdi
movl $0x31, %esi
movq %rbx, %rdx
callq 0xe73c
testl %eax, %eax
je 0xf614
leaq 0x8fb2(%rip), %rax      # 0x184d0
cmpb $0x1, (%rax)
jne 0xf548
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movl 0x10(%rbx), %edx
leaq 0x427d(%rip), %rdi      # 0x137b6
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 0xf608
movq %rax, %r12
cmpl $0x41, %ebp
jne 0xf585
movl $0x400, %edi            # imm = 0x400
callq 0x4500
movq %rax, 0x20(%r12)
testq %rax, %rax
je 0xf622
movl 0x8(%rsp), %eax
movl %eax, 0x10(%r12)
movl %r15d, 0x28(%r12)
xorl %eax, %eax
cmpl $0x1, 0x38(%rbx)
cmoveq %rbx, %rax
movq %rax, 0x30(%r12)
movups 0x40(%rbx), %xmm0
movups %xmm0, 0x40(%r12)
movq 0x68(%rbx), %rax
movq %rax, 0x68(%r12)
movq 0x78(%rbx), %rax
movq %rax, 0x78(%r12)
cmpq $0x0, 0x40(%r12)
jne 0xf5ce
cmpq $0x0, 0x48(%r12)
je 0xf5d7
movq 0x58(%rbx), %rcx
movq %rcx, 0x58(%r12)
testq %rax, %rax
je 0xf5f3
movq 0x50(%rbx), %rax
jmp 0xf5f5
leaq 0x416b(%rip), %rdi      # 0x13754
callq 0x4310
jmp 0xf493
xorl %eax, %eax
movq %rax, 0x50(%r12)
movq %r12, (%r14)
movl $0x1, %r14d
jmp 0xf499
leaq 0x4075(%rip), %rdi      # 0x13684
callq 0x4310
movl 0x8(%rsp), %edi
callq 0x4760
jmp 0xf493
leaq 0x405b(%rip), %rdi      # 0x13684
callq 0x4310
movl 0x8(%rsp), %edi
callq 0x4760
movq %r12, %rdi
callq 0x44a0
jmp 0xf493