// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, 0x10(%rsp)
testq %rsi, %rsi
je 0xff58
movb $0x0, 0x6(%rsp)
movw $0x2020, 0x4(%rsp)      # imm = 0x2020
leal -0x1(%rbp), %eax
cmpl $0x1, %eax
ja 0xff32
movw $0x77, 0x4(%rsp)
cmpl $0x3, %ebp
jne 0xff3e
movw $0x77, 0x4(%rsp)
cmpl $0x6, %ebp
jne 0xff4a
movw $0x72, 0x4(%rsp)
cmpl $0x5, %ebp
je 0xff5d
cmpl $0x4, %ebp
jne 0xff68
movb $0x72, %al
jmp 0xff5f
xorl %r12d, %r12d
jmp 0xffd8
movb $0x61, %al
movb %al, 0x4(%rsp)
movb $0x0, 0x5(%rsp)
cmpl $0x49, %r13d
jne 0xff73
movb $0x62, 0x5(%rsp)
leaq 0x4(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x4420
movq %rax, %r12
testq %rax, %rax
je 0xffa5
cmpl $0x6, %ebp
jne 0xffcf
movq (%rbx), %rax
movq 0x1b0(%rax), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x4670
jmp 0xffcf
leaq 0x88(%r15), %rax
movq %rax, 0x18(%rsp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movl $0x100, %edx            # imm = 0x100
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x41e0
testq %r12, %r12
je 0x100b0
testq %r12, %r12
jne 0xfffa
movl %ebp, %eax
andl $-0x3, %eax
cmpl $0x4, %eax
jne 0xfff0
movq 0x7fca(%rip), %rax      # 0x17fb8
jmp 0xfff7
movq 0x7fa1(%rip), %rax      # 0x17f98
movq (%rax), %r12
leaq 0x8(%rsp), %r9
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %r13d, %ecx
movq %r15, %r8
callq 0xefd2
testl %eax, %eax
je 0x100a0
movl $0x400, %edi            # imm = 0x400
callq 0x4500
movq %rax, %r13
andl $-0x3, %ebp
cmpl $0x4, %ebp
movq %r15, 0x20(%rsp)
jne 0x100c1
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %r12, %rcx
callq 0x4580
movq %rax, %r15
testl %r15d, %r15d
jle 0x10119
xorl %ebp, %ebp
movq 0x8(%rsp), %rcx
movq %r13, %rsi
movl %r15d, %edx
callq 0xfb84
cmpl %r15d, %eax
jl 0x1011d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
addq %r15, %rbp
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %r12, %rcx
callq 0x4580
movq %rax, %r15
testl %r15d, %r15d
jg 0x1005b
jmp 0x10130
cmpq $0x0, 0x10(%rsp)
je 0x100b0
movq %r12, %rdi
callq 0x43e0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0xfad4
testl %eax, %eax
jle 0x10119
xorl %ebp, %ebp
movl %eax, %r15d
movl $0x1, %esi
movq %r13, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x4760
testq %rax, %rax
je 0x1019f
addq %r15, %rbp
movq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0xfad4
testl %eax, %eax
jg 0x100dc
jmp 0x10130
xorl %ebp, %ebp
jmp 0x10130
leaq 0x3841(%rip), %rdi      # 0x13965
movl %r15d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x4060
leaq 0x83a1(%rip), %rax      # 0x184d8
cmpb $0x1, (%rax)
jne 0x10164
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movq 0x20(%rsp), %rcx
movl 0x10(%rcx), %edx
leaq 0x3840(%rip), %rdi      # 0x13997
movq %rax, %rsi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x4060
movq %r13, %rdi
callq 0x44a0
movq %r12, %rdi
callq 0x4480
cmpq $0x0, 0x10(%rsp)
je 0x10184
movq %r12, %rdi
callq 0x43e0
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfa02
leaq 0x37e1(%rip), %rdi      # 0x13987
callq 0x4310
jmp 0x10130
nop