// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movl $0x0, -0x144(%rbp)
leaq -0x140(%rbp), %rdi
movl -0x2c(%rbp), %edx
leaq 0x4006(%rip), %rsi      # 0x1d8c4
movb $0x0, %al
callq 0x40d0
movq -0x18(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movl $0x32, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x198f2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x32, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x1991f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
cmpl $0x0, -0x30(%rbp)
jne 0x19c4d
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
leaq 0x4021(%rip), %rsi      # 0x1d95c
movl $0x32, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19956
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
movq -0x18(%rbp), %rax
movq (%rax), %rdi
addq $0x88, %rdi
movl $0x28, %esi
callq 0x4180
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x19985
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x3e(%rbp), %rdx
addq $0x2, %rdx
leaq -0x3e(%rbp), %rcx
addq $0x3, %rcx
leaq -0x3e(%rbp), %r8
addq $0x4, %r8
leaq -0x3e(%rbp), %r9
addq $0x5, %r9
leaq -0x3e(%rbp), %r10
leaq -0x3e(%rbp), %rax
addq $0x1, %rax
leaq 0x3f76(%rip), %rsi      # 0x1d93e
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x4480
movq -0x18(%rbp), %rax
movq (%rax), %rax
testb $0x1, 0x1b8(%rax)
je 0x19a08
movq -0x18(%rbp), %rdi
leaq -0x3e(%rbp), %rsi
callq 0x18760
cmpl $0x0, %eax
jne 0x19a06
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
jmp 0x19a08
leaq -0x140(%rbp), %rdi
movzbl -0x3c(%rbp), %edx
movzbl -0x3b(%rbp), %ecx
movzbl -0x3a(%rbp), %r8d
movzbl -0x39(%rbp), %r9d
movzbl -0x3e(%rbp), %r10d
movzbl -0x3d(%rbp), %eax
leaq 0x4086(%rip), %rsi      # 0x1dab7
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x40d0
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x32, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19a6d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
leaq -0x140(%rbp), %rdi
leaq 0x3e5f(%rip), %rsi      # 0x1d8da
callq 0x4430
cmpq $0x0, -0x20(%rbp)
je 0x19af5
leaq -0x140(%rbp), %rdi
callq 0x41d0
movl %eax, -0x148(%rbp)
movl -0x148(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x148(%rbp)
cltq
movb $0x20, -0x140(%rbp,%rax)
movq -0x20(%rbp), %rdi
callq 0x41d0
movslq -0x148(%rbp), %rcx
addq %rcx, %rax
cmpq $0x100, %rax            # imm = 0x100
jb 0x19adb
movl $0x0, -0x4(%rbp)
jmp 0x19f72
movslq -0x148(%rbp), %rax
leaq -0x140(%rbp), %rdi
addq %rax, %rdi
movq -0x20(%rbp), %rsi
callq 0x4430
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x31, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19b22
movl $0x0, -0x4(%rbp)
jmp 0x19f72
leaq -0x140(%rbp), %rdi
leaq 0x3daf(%rip), %rsi      # 0x1d8df
callq 0x4430
cmpq $0x0, -0x28(%rbp)
je 0x19baa
leaq -0x140(%rbp), %rdi
callq 0x41d0
movl %eax, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x14c(%rbp)
cltq
movb $0x20, -0x140(%rbp,%rax)
movq -0x28(%rbp), %rdi
callq 0x41d0
movslq -0x14c(%rbp), %rcx
addq %rcx, %rax
cmpq $0x100, %rax            # imm = 0x100
jb 0x19b90
movl $0x0, -0x4(%rbp)
jmp 0x19f72
movslq -0x14c(%rbp), %rax
leaq -0x140(%rbp), %rdi
addq %rax, %rdi
movq -0x28(%rbp), %rsi
callq 0x4430
movq -0x18(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movl $0x31, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19c08
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
leaq 0x3d7f(%rip), %rsi      # 0x1d95c
movl $0x32, %edx
callq 0x171c0
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movl $0x34, %esi
callq 0x16cc0
movl $0x0, -0x4(%rbp)
jmp 0x19f72
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movl $0x32, %esi
callq 0x16cc0
movl %eax, -0x158(%rbp)
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movl $0x32, %esi
callq 0x16cc0
movl %eax, %ecx
movl -0x158(%rbp), %eax
andl %ecx, %eax
movl %eax, -0x144(%rbp)
jmp 0x19f69
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
leaq 0x3cfd(%rip), %rsi      # 0x1d95c
movl $0x32, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19c7a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq $0x88, %rdi
movl $0x28, %esi
callq 0x4180
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x19ca9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x3e(%rbp), %rdx
addq $0x2, %rdx
leaq -0x3e(%rbp), %rcx
addq $0x3, %rcx
leaq -0x3e(%rbp), %r8
addq $0x4, %r8
leaq -0x3e(%rbp), %r9
addq $0x5, %r9
leaq -0x3e(%rbp), %r10
leaq -0x3e(%rbp), %rax
addq $0x1, %rax
leaq 0x3c52(%rip), %rsi      # 0x1d93e
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x4480
movq -0x10(%rbp), %rax
movq (%rax), %rax
testb $0x1, 0x1b8(%rax)
je 0x19d2c
movq -0x10(%rbp), %rdi
leaq -0x3e(%rbp), %rsi
callq 0x18760
cmpl $0x0, %eax
jne 0x19d2a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
jmp 0x19d2c
leaq -0x140(%rbp), %rdi
movzbl -0x3c(%rbp), %edx
movzbl -0x3b(%rbp), %ecx
movzbl -0x3a(%rbp), %r8d
movzbl -0x39(%rbp), %r9d
movzbl -0x3e(%rbp), %r10d
movzbl -0x3d(%rbp), %eax
leaq 0x3d62(%rip), %rsi      # 0x1dab7
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x40d0
movq -0x18(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movl $0x32, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19d91
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x19f72
leaq -0x140(%rbp), %rdi
leaq 0x3b40(%rip), %rsi      # 0x1d8df
callq 0x4430
cmpq $0x0, -0x28(%rbp)
je 0x19e19
leaq -0x140(%rbp), %rdi
callq 0x41d0
movl %eax, -0x150(%rbp)
movl -0x150(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x150(%rbp)
cltq
movb $0x20, -0x140(%rbp,%rax)
movq -0x28(%rbp), %rdi
callq 0x41d0
movslq -0x150(%rbp), %rcx
addq %rcx, %rax
cmpq $0x100, %rax            # imm = 0x100
jb 0x19dff
movl $0x0, -0x4(%rbp)
jmp 0x19f72
movslq -0x150(%rbp), %rax
leaq -0x140(%rbp), %rdi
addq %rax, %rdi
movq -0x28(%rbp), %rsi
callq 0x4430
movq -0x18(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movl $0x31, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19e46
movl $0x0, -0x4(%rbp)
jmp 0x19f72
leaq -0x140(%rbp), %rdi
leaq 0x3a86(%rip), %rsi      # 0x1d8da
callq 0x4430
cmpq $0x0, -0x20(%rbp)
je 0x19ece
leaq -0x140(%rbp), %rdi
callq 0x41d0
movl %eax, -0x154(%rbp)
movl -0x154(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x154(%rbp)
cltq
movb $0x20, -0x140(%rbp,%rax)
movq -0x20(%rbp), %rdi
callq 0x41d0
movslq -0x154(%rbp), %rcx
addq %rcx, %rax
cmpq $0x100, %rax            # imm = 0x100
jb 0x19eb4
movl $0x0, -0x4(%rbp)
jmp 0x19f72
movslq -0x154(%rbp), %rax
leaq -0x140(%rbp), %rdi
addq %rax, %rdi
movq -0x20(%rbp), %rsi
callq 0x4430
movq -0x10(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x31, %edx
callq 0x171c0
cmpl $0x0, %eax
jne 0x19f29
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rcx
leaq 0x3a5b(%rip), %rsi      # 0x1d95c
movl $0x32, %edx
callq 0x171c0
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movl $0x34, %esi
callq 0x16cc0
movl $0x0, -0x4(%rbp)
jmp 0x19f72
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movl $0x32, %esi
callq 0x16cc0
movl %eax, -0x15c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movl $0x32, %esi
callq 0x16cc0
movl %eax, %ecx
movl -0x15c(%rbp), %eax
andl %ecx, %eax
movl %eax, -0x144(%rbp)
movl -0x144(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nop