// File: /workspace/github_cpp_makefile_star2_O1/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 0x32d9(%rip), %rsi      # 0x13822
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movl %r8d, %edx
xorl %eax, %eax
callq 0x40a0
movq (%rbx), %rcx
movq %rbx, %rdi
movq %rbp, %rsi
movl $0x32, %edx
callq 0xeba0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x109c1
movq (%r14), %rcx
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
movl $0x32, %edx
callq 0xeba0
testl %eax, %eax
je 0x109c1
testl %r13d, %r13d
je 0x10784
movq (%r14), %rcx
leaq 0x330f(%rip), %rsi      # 0x138ba
movq %r14, %rdi
movl $0x32, %edx
callq 0xeba0
testl %eax, %eax
je 0x109c1
movl $0x88, %edi
addq (%r14), %rdi
movl $0x28, %esi
callq 0x4160
testq %rax, %rax
je 0x109c1
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 0x3299(%rip), %rsi      # 0x1389c
movq %rax, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x43c0
addq $0x10, %rsp
movq (%r14), %rax
cmpb $0x1, 0x1b8(%rax)
jne 0x10636
leaq 0xa(%rsp), %rsi
movq %r14, %rdi
callq 0xfa86
testl %eax, %eax
je 0x109c1
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 0x33b6(%rip), %rsi      # 0x13a15
leaq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x40a0
addq $0x10, %rsp
movq (%rbx), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movl $0x32, %edx
callq 0xeba0
testl %eax, %eax
je 0x109c1
movb $0x0, 0x14(%rsp)
movl $0x524f5453, 0x10(%rsp) # imm = 0x524F5453
testq %r15, %r15
je 0x106e3
leaq 0x10(%rsp), %rdi
callq 0x4180
movslq %eax, %r13
movb $0x20, 0x10(%rsp,%r13)
movq %r15, %rdi
callq 0x4180
incl %r13d
movslq %r13d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0x1097a
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r15, %rsi
callq 0x4380
movq (%rbx), %rcx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
movl $0x31, %edx
callq 0xeba0
xorl %ebp, %ebp
testl %eax, %eax
je 0x109c1
movb $0x0, 0x14(%rsp)
movl $0x52544552, 0x10(%rsp) # imm = 0x52544552
testq %r12, %r12
je 0x10754
leaq 0x10(%rsp), %rdi
callq 0x4180
movslq %eax, %r15
movb $0x20, 0x10(%rsp,%r15)
movq %r12, %rdi
callq 0x4180
incl %r15d
movslq %r15d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0x109c1
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r12, %rsi
callq 0x4380
movq (%r14), %rcx
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
movl $0x31, %edx
callq 0xeba0
movq (%r14), %rcx
testl %eax, %eax
je 0x1097e
movq %r14, %rdi
movl $0x32, %esi
movq %rcx, %rdx
jmp 0x1095b
movq (%rbx), %rcx
leaq 0x312c(%rip), %rsi      # 0x138ba
movq %rbx, %rdi
movl $0x32, %edx
callq 0xeba0
testl %eax, %eax
je 0x109c1
movl $0x88, %edi
addq (%rbx), %rdi
movl $0x28, %esi
callq 0x4160
testq %rax, %rax
je 0x109c1
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 0x30b6(%rip), %rsi      # 0x1389c
movq %rax, %rdi
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x43c0
addq $0x10, %rsp
movq (%rbx), %rax
cmpb $0x1, 0x1b8(%rax)
jne 0x10819
leaq 0xa(%rsp), %rsi
movq %rbx, %rdi
callq 0xfa86
testl %eax, %eax
je 0x109c1
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 0x31d3(%rip), %rsi      # 0x13a15
leaq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x40a0
addq $0x10, %rsp
movq (%r14), %rcx
movq %r14, %rdi
movq %r13, %rsi
movl $0x32, %edx
callq 0xeba0
testl %eax, %eax
je 0x109c1
movb $0x0, 0x14(%rsp)
movl $0x52544552, 0x10(%rsp) # imm = 0x52544552
testq %r12, %r12
je 0x108c6
leaq 0x10(%rsp), %rdi
callq 0x4180
movslq %eax, %r13
movb $0x20, 0x10(%rsp,%r13)
movq %r12, %rdi
callq 0x4180
incl %r13d
movslq %r13d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0x1097a
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r12, %rsi
callq 0x4380
movq (%r14), %rcx
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
movl $0x31, %edx
callq 0xeba0
xorl %ebp, %ebp
testl %eax, %eax
je 0x109c1
movb $0x0, 0x14(%rsp)
movl $0x524f5453, 0x10(%rsp) # imm = 0x524F5453
testq %r15, %r15
je 0x10937
leaq 0x10(%rsp), %rdi
callq 0x4180
movslq %eax, %r12
movb $0x20, 0x10(%rsp,%r12)
movq %r15, %rdi
callq 0x4180
incl %r12d
movslq %r12d, %rcx
addq %rcx, %rax
cmpq $0xff, %rax
ja 0x109c1
leaq (%rsp,%rcx), %rdi
addq $0x10, %rdi
movq %r15, %rsi
callq 0x4380
movq (%rbx), %rcx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
movl $0x31, %edx
callq 0xeba0
testl %eax, %eax
je 0x1099a
movq (%r14), %rdx
movq %r14, %rdi
movl $0x32, %esi
callq 0xe74c
movl %eax, %r14d
movq (%rbx), %rdx
movq %rbx, %rdi
movl $0x32, %esi
callq 0xe74c
movl %eax, %ebp
andl %r14d, %ebp
jmp 0x109c1
xorl %ebp, %ebp
jmp 0x109c1
leaq 0x2f35(%rip), %rsi      # 0x138ba
movq %r14, %rdi
movl $0x32, %edx
callq 0xeba0
movq (%rbx), %rdx
movq %rbx, %rdi
jmp 0x109b7
movq (%rbx), %rcx
leaq 0x2f16(%rip), %rsi      # 0x138ba
movq %rbx, %rdi
movl $0x32, %edx
callq 0xeba0
movq (%r14), %rdx
movq %r14, %rdi
movl $0x34, %esi
callq 0xe74c
movl %ebp, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop