// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movl %r8d, %ebp
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
testq %rsi, %rsi
movq %rcx, 0x10(%rsp)
je 0xdca7
movb $0x0, 0x6(%rsp)
movw $0x2020, 0x4(%rsp)      # imm = 0x2020
leal -0x1(%rbp), %eax
cmpl $0x2, %eax
jae 0xdcbd
movb $0x77, 0x4(%rsp)
xorl %ebx, %ebx
movb $0x0, 0x5(%rsp)
cmpl $0x49, %r15d
jne 0xdc6e
movb $0x62, 0x5(%rsp)
leaq 0x4(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testq %rax, %rax
je 0xdce3
movq %rax, %r12
testb %bl, %bl
je 0xdca0
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x1b0(%rax), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x4640
movq 0x10(%rsp), %rbx
jmp 0xdd1a
movq %rcx, %rbx
movl %ebp, %eax
andl $-0x3, %eax
cmpl $0x4, %eax
jne 0xdd10
movq 0x92fd(%rip), %rax      # 0x16fb8
jmp 0xdd17
leal -0x3(%rbp), %eax
cmpl $0x3, %eax
ja 0xde99
leaq 0x3944(%rip), %rcx      # 0x11614
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x72, 0x4(%rsp)
jmp 0xdc5c
movq 0x10(%rsp), %rbx
addq $0x88, %rbx
callq 0x4040
movl (%rax), %edi
callq 0x4110
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movq %rax, %rsi
callq 0x41e0
jmp 0xdda4
movq 0x9281(%rip), %rax      # 0x16f98
movq (%rax), %r12
leaq 0x20(%rsp), %r9
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movl %ebp, %edx
movl %r15d, %ecx
movq %rbx, %r8
callq 0xcd82
testl %eax, %eax
je 0xdd97
movq %r14, 0x18(%rsp)
movl $0x400, %edi            # imm = 0x400
callq 0x44f0
movq %rax, %r15
andl $-0x3, %ebp
movq 0x20(%rsp), %r13
cmpl $0x4, %ebp
jne 0xddb5
xorl %ebp, %ebp
pushq $0x1
popq %r14
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x4570
movq %rax, %rbx
testl %ebx, %ebx
jle 0xde12
movq %r15, %rsi
movl %ebx, %edx
movq %r13, %rcx
callq 0xd8cc
cmpl %ebx, %eax
jl 0xde00
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rbx, %rbp
jmp 0xdd5d
testq %r14, %r14
je 0xdda4
movq %r12, %rdi
callq 0x43d0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
pushq $0x1
popq %r14
movq %rbx, %rbp
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rcx
callq 0xd81e
testl %eax, %eax
jle 0xde12
movl %eax, %ebx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x4720
addq %rbp, %rbx
testq %rax, %rax
jne 0xddbb
leaq 0x3b3e(%rip), %rdi      # 0x11937
callq 0x4300
jmp 0xde12
leaq 0x3b0e(%rip), %rdi      # 0x11915
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x4050
leaq 0x96a7(%rip), %rax      # 0x174c0
cmpb $0x1, (%rax)
movq 0x18(%rsp), %rbx
jne 0xde4b
movl $0xba, %edi
xorl %eax, %eax
callq 0x4060
movq 0x10(%rsp), %rcx
movl 0x10(%rcx), %edx
leaq 0x3b09(%rip), %rdi      # 0x11947
movq %rax, %rsi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x4050
movq %r15, %rdi
callq 0x4480
movq %r12, %rdi
callq 0x4460
testq %rbx, %rbx
je 0xde68
movq %r12, %rdi
callq 0x43d0
movq 0x8(%rsp), %rdi
movq %r13, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd740
movb $0x61, 0x4(%rsp)
jmp 0xdc5c
movb $0x72, 0x4(%rsp)
movb $0x1, %bl
jmp 0xdc5e
xorl %ebx, %ebx
jmp 0xdc63