// File: /workspace/github_cpp_makefile_star2_O2/lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::Fxp(ftplib*, ftplib*, char const*, char const*, ftplib::transfermode, ftplib::fxpmethod)
int ftplib::Fxp(ftplib* src, ftplib* dst, const char *pathSrc, const char *pathDst, transfermode mode, fxpmethod method)
{
	char *cp;
	unsigned char v[6];
	char buf[256];
	int retval = 0;
	
	sprintf(buf, "TYPE %c", mode);
	if (!dst->FtpSendCmd(buf,'2',dst->mp_ftphandle)) 
        return -1;
	if (!src->FtpSendCmd(buf,'2',src->mp_ftphandle)) 
        return -1;

	if (method == ftplib::defaultfxp)
	{
		// PASV dst

		if (!dst->FtpSendCmd("PASV",'2',dst->mp_ftphandle)) 
            return -1;
		cp = strchr(dst->mp_ftphandle->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 (dst->mp_ftphandle->correctpasv) 
            if (!dst->CorrectPasvResponse(v)) 
                return -1;

		// PORT src

		sprintf(buf, "PORT %d,%d,%d,%d,%d,%d", v[2],v[3],v[4],v[5],v[0],v[1]);
		if (!src->FtpSendCmd(buf,'2',src->mp_ftphandle)) 
            return -1;

		// RETR src

		strcpy(buf,"RETR");
		if (pathSrc != NULL)
		{
			int i = strlen(buf);
			buf[i++] = ' ';
			if ((strlen(pathSrc) + i) >= sizeof(buf)) 
                return 0;
			strcpy(&buf[i],pathSrc);
		}
		if (!src->FtpSendCmd(buf, '1', src->mp_ftphandle)) 
            return 0;

		// STOR dst

		strcpy(buf,"STOR");
		if (pathDst != NULL)
		{
			int i = strlen(buf);
			buf[i++] = ' ';
			if ((strlen(pathDst) + i) >= sizeof(buf)) 
                return 0;
			strcpy(&buf[i],pathDst);
		}
		if (!dst->FtpSendCmd(buf, '1', dst->mp_ftphandle))
		{
			/* this closes the data connection, to abort the RETR on
			the source ftp. all hail pftp, it took me several
			hours and i was absolutely clueless, playing around with
			ABOR and whatever, when i desperately checked the pftp
			source which gave me this final hint. thanks dude(s). */
		
			dst->FtpSendCmd("PASV", '2', dst->mp_ftphandle);
			src->readresp('4', src->mp_ftphandle);
			return 0;
		}

		retval = (src->readresp('2', src->mp_ftphandle)) & (dst->readresp('2', dst->mp_ftphandle));

	}
	else 
	{
		// PASV src

		if (!src->FtpSendCmd("PASV",'2',src->mp_ftphandle)) 
            return -1;
		cp = strchr(src->mp_ftphandle->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 (src->mp_ftphandle->correctpasv) 
            if (!src->CorrectPasvResponse(v)) 
                return -1;

		// PORT dst

		sprintf(buf, "PORT %d,%d,%d,%d,%d,%d", v[2],v[3],v[4],v[5],v[0],v[1]);
		if (!dst->FtpSendCmd(buf,'2',dst->mp_ftphandle)) 
            return -1;

		// STOR dest

		strcpy(buf,"STOR");
		if (pathDst != NULL)
		{
			int i = strlen(buf);
			buf[i++] = ' ';
			if ((strlen(pathDst) + i) >= sizeof(buf)) 
                return 0;
			strcpy(&buf[i],pathDst);
		}
		if (!dst->FtpSendCmd(buf, '1', dst->mp_ftphandle)) 
            return 0;

		// RETR src

		strcpy(buf,"RETR");
		if (pathSrc != NULL)
		{
			int i = strlen(buf);
			buf[i++] = ' ';
			if ((strlen(pathSrc) + i) >= sizeof(buf)) 
                return 0;
			strcpy(&buf[i],pathSrc);
		}
		if (!src->FtpSendCmd(buf, '1', src->mp_ftphandle))
		{
			src->FtpSendCmd("PASV", '2', src->mp_ftphandle);
			dst->readresp('4', dst->mp_ftphandle);
			return 0;
		}

		// wait til its finished!

		retval = (src->readresp('2', src->mp_ftphandle)) & (dst->readresp('2', dst->mp_ftphandle));

	}

	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %r9d, %r13d
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x35b9(%rip), %rsi      # 0x117d2
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movl %r8d, %edx
xorl %eax, %eax
callq 0x4090
movq (%rbx), %rcx
pushq $0x32
popq %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xc9ae
pushq $-0x1
popq %rbp
testl %eax, %eax
je 0xe669
movq (%r14), %rcx
leaq 0x10(%rsp), %rsi
pushq $0x32
popq %rdx
movq %r14, %rdi
callq 0xc9ae
testl %eax, %eax
je 0xe669
testl %r13d, %r13d
je 0xe440
movq (%r14), %rcx
leaq 0x35f5(%rip), %rsi      # 0x1186a
pushq $0x32
popq %rdx
movq %r14, %rdi
callq 0xc9ae
testl %eax, %eax
je 0xe669
movl $0x88, %edi
addq (%r14), %rdi
pushq $0x28
popq %rsi
callq 0x4140
testq %rax, %rax
je 0xe669
incq %rax
leaq 0xc(%rsp), %rdx
leaq 0xd(%rsp), %rcx
leaq 0xe(%rsp), %r8
leaq 0xf(%rsp), %r9
leaq 0xb(%rsp), %r10
leaq 0xa(%rsp), %r11
leaq 0x3583(%rip), %rsi      # 0x1184c
movq %rax, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x43b0
popq %rcx
popq %rdx
movq (%r14), %rax
cmpb $0x1, 0x1b8(%rax)
jne 0xe2fa
leaq 0xa(%rsp), %rsi
movq %r14, %rdi
callq 0xd7b8
testl %eax, %eax
je 0xe669
movzbl 0xc(%rsp), %edx
movzbl 0xd(%rsp), %ecx
movzbl 0xe(%rsp), %r8d
movzbl 0xf(%rsp), %r9d
movzbl 0xa(%rsp), %r10d
movzbl 0xb(%rsp), %r11d
leaq 0x36a2(%rip), %rsi      # 0x119c5
leaq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4090
popq %rcx
popq %rdx
movq (%rbx), %rcx
pushq $0x32
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc9ae
testl %eax, %eax
je 0xe669
movb $0x0, 0x14(%rsp)
movl $0x524f5453, 0x10(%rsp) # imm = 0x524F5453
testq %r15, %r15
je 0xe3a3
leaq 0x10(%rsp), %rdi
callq 0x4160
movslq %eax, %r13
movb $0x20, 0x10(%rsp,%r13)
movq %r15, %rdi
callq 0x4160
incl %r13d
movslq %r13d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0xe625
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r15, %rsi
callq 0x4370
movq (%rbx), %rcx
leaq 0x10(%rsp), %rsi
pushq $0x31
popq %rdx
movq %rbx, %rdi
callq 0xc9ae
xorl %ebp, %ebp
testl %eax, %eax
je 0xe669
movb $0x0, 0x14(%rsp)
movl $0x52544552, 0x10(%rsp) # imm = 0x52544552
testq %r12, %r12
je 0xe412
leaq 0x10(%rsp), %rdi
callq 0x4160
movslq %eax, %r15
movb $0x20, 0x10(%rsp,%r15)
movq %r12, %rdi
callq 0x4160
incl %r15d
movslq %r15d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0xe669
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r12, %rsi
callq 0x4370
movq (%r14), %rcx
leaq 0x10(%rsp), %rsi
pushq $0x31
popq %rdx
movq %r14, %rdi
callq 0xc9ae
movq (%r14), %rcx
testl %eax, %eax
je 0xe629
pushq $0x32
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
movq %rcx, %rdx
jmp 0xe609
movq (%rbx), %rcx
leaq 0x3420(%rip), %rsi      # 0x1186a
pushq $0x32
popq %rdx
movq %rbx, %rdi
callq 0xc9ae
testl %eax, %eax
je 0xe669
movl $0x88, %edi
addq (%rbx), %rdi
pushq $0x28
popq %rsi
callq 0x4140
testq %rax, %rax
je 0xe669
incq %rax
leaq 0xc(%rsp), %rdx
leaq 0xd(%rsp), %rcx
leaq 0xe(%rsp), %r8
leaq 0xf(%rsp), %r9
leaq 0xb(%rsp), %r10
leaq 0xa(%rsp), %r11
leaq 0x33ae(%rip), %rsi      # 0x1184c
movq %rax, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x43b0
popq %rcx
popq %rdx
movq (%rbx), %rax
cmpb $0x1, 0x1b8(%rax)
jne 0xe4cf
leaq 0xa(%rsp), %rsi
movq %rbx, %rdi
callq 0xd7b8
testl %eax, %eax
je 0xe669
movzbl 0xc(%rsp), %edx
movzbl 0xd(%rsp), %ecx
movzbl 0xe(%rsp), %r8d
movzbl 0xf(%rsp), %r9d
movzbl 0xa(%rsp), %r10d
movzbl 0xb(%rsp), %r11d
leaq 0x34cd(%rip), %rsi      # 0x119c5
leaq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x4090
popq %rcx
popq %rdx
movq (%r14), %rcx
pushq $0x32
popq %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0xc9ae
testl %eax, %eax
je 0xe669
movb $0x0, 0x14(%rsp)
movl $0x52544552, 0x10(%rsp) # imm = 0x52544552
testq %r12, %r12
je 0xe578
leaq 0x10(%rsp), %rdi
callq 0x4160
movslq %eax, %r13
movb $0x20, 0x10(%rsp,%r13)
movq %r12, %rdi
callq 0x4160
incl %r13d
movslq %r13d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0xe625
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r12, %rsi
callq 0x4370
movq (%r14), %rcx
leaq 0x10(%rsp), %rsi
pushq $0x31
popq %rdx
movq %r14, %rdi
callq 0xc9ae
xorl %ebp, %ebp
testl %eax, %eax
je 0xe669
movb $0x0, 0x14(%rsp)
movl $0x524f5453, 0x10(%rsp) # imm = 0x524F5453
testq %r15, %r15
je 0xe5e7
leaq 0x10(%rsp), %rdi
callq 0x4160
movslq %eax, %r12
movb $0x20, 0x10(%rsp,%r12)
movq %r15, %rdi
callq 0x4160
incl %r12d
movslq %r12d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0xe669
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r15, %rsi
callq 0x4370
movq (%rbx), %rcx
leaq 0x10(%rsp), %rsi
pushq $0x31
popq %rdx
movq %rbx, %rdi
callq 0xc9ae
testl %eax, %eax
je 0xe646
movq (%r14), %rdx
pushq $0x32
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0xc65c
movl %eax, %r14d
movq (%rbx), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0xc65c
movl %eax, %ebp
andl %r14d, %ebp
jmp 0xe669
xorl %ebp, %ebp
jmp 0xe669
leaq 0x323a(%rip), %rsi      # 0x1186a
pushq $0x32
popq %rdx
movq %r14, %rdi
callq 0xc9ae
movq (%rbx), %rdx
pushq $0x34
popq %rsi
movq %rbx, %rdi
jmp 0xe664
movq (%rbx), %rcx
leaq 0x321a(%rip), %rsi      # 0x1186a
pushq $0x32
popq %rdx
movq %rbx, %rdi
callq 0xc9ae
movq (%r14), %rdx
pushq $0x34
popq %rsi
movq %r14, %rdi
callq 0xc65c
movl %ebp, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop