// File: /workspace/github_cpp_makefile_star2_O3/lianxings[P]ftp_benchmark/ftplib.cc
// Function: ftplib::FtpXfer(char const*, char const*, ftphandle*, ftplib::accesstype, ftplib::transfermode)
int ftplib::FtpXfer(const char *localfile, const char *path, ftphandle *nControl, accesstype type, transfermode mode)
{
	int l,c;
	char *dbuf;
	FILE *local = NULL;
	ftphandle *nData;

	if (localfile != NULL)
	{
		char ac[3] = "  ";
		if ((type == ftplib::dir) || (type == ftplib::dirverbose)) { ac[0] = 'w'; ac[1] = '\0'; }
		if (type == ftplib::fileread) { ac[0] = 'w'; ac[1] = '\0'; }
		if (type == ftplib::filewriteappend) { ac[0] = 'r'; ac[1] = '\0'; }
		if (type == ftplib::filereadappend) { ac[0] = 'a'; ac[1] = '\0'; }
		if (type == ftplib::filewrite) { ac[0] = 'r'; ac[1] = '\0'; }
		if (mode == ftplib::image) ac[1] = 'b';

		local = fopen64(localfile, ac);
		if (local == NULL)
		{
			strncpy(nControl->response, strerror(errno), sizeof(nControl->response));
			return 0;
		}
		if (type == ftplib::filewriteappend) fseeko64(local,mp_ftphandle->offset,SEEK_SET);
	}
	if (local == NULL) local = ((type == ftplib::filewrite)
		|| (type == ftplib::filewriteappend)) ? stdin : stdout;
	if (!FtpAccess(path, type, mode, nControl, &nData)) {
        if (localfile != NULL) 
            fclose(local);
        return 0;
    }

	dbuf = static_cast<char*>(malloc(FTPLIB_BUFSIZ));
    long long  dataSize =0;
	if ((type == ftplib::filewrite) || (type == ftplib::filewriteappend)) {
		while ((l = fread(dbuf, 1, FTPLIB_BUFSIZ, local)) > 0) 	{
			if ((c = FtpWrite(dbuf, l, nData)) < l) {
				printf("short write: passed %d, wrote %d\n", l, c);
				break;
			}
            dataSize += l;
		}
	} else 	{
		while ((l = FtpRead(dbuf, FTPLIB_BUFSIZ, nData)) > 0) 	{
			if (fwrite(dbuf, 1, l, local) <= 0) {
				perror("localfile write");
				break;
			}
            dataSize += l;
		}
	}

    if(gFtpDebug)
        printf("Thread:%-8ld  sock:%-4d  transfer size:%lld\n", syscall(SYS_gettid), nControl->handle, dataSize);
    
	free(dbuf);
	fflush(local);
	if (localfile != NULL) 
        fclose(local);
	return FtpClose(nData);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, %ebp
movl %r8d, %r13d
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rdi, %rbx
testq %rsi, %rsi
movq %rsi, 0x20(%rsp)
je 0xff64
movq %rsi, %rdx
movb $0x0, 0xe(%rsp)
movw $0x2020, 0xc(%rsp)      # imm = 0x2020
leal -0x1(%r13), %eax
cmpl $0x2, %eax
jae 0xff7a
movb $0x77, 0xc(%rsp)
xorl %r15d, %r15d
movb $0x0, 0xd(%rsp)
cmpl $0x49, %ebp
jne 0xff04
movb $0x62, 0xd(%rsp)
leaq 0xc(%rsp), %rsi
movq %rdx, %rdi
callq 0x4420
testq %rax, %rax
je 0xffa1
movq %rax, %r12
testb %r15b, %r15b
je 0xff36
movq (%rbx), %rax
movq 0x1b0(%rax), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x4660
leaq 0x18(%rsp), %r9
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
movl %ebp, %ecx
movq 0x10(%rsp), %r8
callq 0xefc2
testl %eax, %eax
je 0x100d5
andl $-0x3, %r13d
movl %r13d, %r15d
jmp 0xfffa
movl %r13d, %r15d
andl $-0x3, %r15d
cmpl $0x4, %r15d
jne 0xffce
movq 0x8040(%rip), %rax      # 0x17fb8
jmp 0xffd5
leal -0x3(%r13), %eax
cmpl $0x3, %eax
ja 0x10187
leaq 0x36e6(%rip), %rcx      # 0x13674
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x72, 0xc(%rsp)
jmp 0xfef2
movq 0x10(%rsp), %rbx
addq $0x88, %rbx
callq 0x4050
movl (%rax), %edi
callq 0x4130
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movq %rax, %rsi
callq 0x41e0
jmp 0x100dd
movq 0x7fc3(%rip), %rax      # 0x17f98
movq (%rax), %r12
leaq 0x18(%rsp), %r9
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
movl %ebp, %ecx
movq 0x10(%rsp), %r8
callq 0xefc2
testl %eax, %eax
je 0x100dd
movl $0x400, %edi            # imm = 0x400
callq 0x4500
movq %rax, %r13
cmpl $0x4, %r15d
jne 0x10078
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %r12, %rcx
callq 0x4570
movq %rax, %r14
testl %r14d, %r14d
jle 0x100d1
movq 0x18(%rsp), %r15
xorl %ebp, %ebp
movq %r13, %rsi
movl %r14d, %edx
movq %r15, %rcx
callq 0xfb38
cmpl %r14d, %eax
jl 0x100ee
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
addq %r14, %rbp
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %r12, %rcx
callq 0x4570
movq %rax, %r14
testl %r14d, %r14d
jg 0x10035
jmp 0x10101
movq 0x18(%rsp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rcx
callq 0xfa88
testl %eax, %eax
jle 0x100d1
xorl %ebp, %ebp
movl %eax, %r15d
movl $0x1, %esi
movq %r13, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x4750
testq %rax, %rax
je 0x1018f
addq %r15, %rbp
movq %rbx, %rdi
movq %r13, %rsi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rcx
callq 0xfa88
testl %eax, %eax
jg 0x10096
jmp 0x10101
xorl %ebp, %ebp
jmp 0x10101
movq %r12, %rdi
callq 0x43e0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3880(%rip), %rdi      # 0x13975
movl %r14d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x4060
leaq 0x83c8(%rip), %rax      # 0x184d0
cmpb $0x1, (%rax)
jne 0x10135
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movq 0x10(%rsp), %rcx
movl 0x10(%rcx), %edx
leaq 0x387f(%rip), %rdi      # 0x139a7
movq %rax, %rsi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x4060
movq %r13, %rdi
callq 0x44a0
movq %r12, %rdi
callq 0x4480
cmpq $0x0, 0x20(%rsp)
je 0x10155
movq %r12, %rdi
callq 0x43e0
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf9cc
movb $0x61, 0xc(%rsp)
jmp 0xfef2
movb $0x72, 0xc(%rsp)
movb $0x1, %r15b
jmp 0xfef5
xorl %r15d, %r15d
jmp 0xfefa
leaq 0x3801(%rip), %rdi      # 0x13997
callq 0x4310
jmp 0x10101