// File: /workspace/github_c_makefile_star2_O3/icysunlight[P]miniftpd/ftpproto.c
// Function: do_retr
void do_retr(session_t *sess)
{
    //打开数据连接
    int data_fd = get_datafd(sess);
    sess->datafd = data_fd;
    if ( -1 == data_fd )
    {
        ftp_reply(sess,FTP_BADSENDCONN,"Use port or pasv first");
        return;
    }

    //打开文件并加锁
    if ( is_dir(sess->arg) )
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Read local file fail.");
        return;
    }
    int filefd = open(sess->arg,O_RDONLY);
    if ( -1 == filefd )
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Read local file fail.");
        return;
    }
    if ( -1 == file_rdlock(filefd)) 
    {
        ftp_reply(sess,FTP_BADSENDFILE,"Read local file fail.");
        close(filefd);
        return;
    }

    //读取文件大小
    off_t filesize = lseek(filefd,0,SEEK_END);
    lseek(filefd,sess->rest_pos,SEEK_SET);
    
    //发送开始信息
    char buf[2048] = {0};
    sprintf(buf,"Openning BINARY mode data connect for %s (%lld bytes)",sess->arg,(long long)filesize);
    ftp_reply(sess,FTP_DATACONN,buf);

    int flag = 0;
    //发送文件
    if ( 0 == sess->bASCII)
    {
        off_t sizetosend = filesize - sess->rest_pos;
        while ( 0 != sizetosend )
        {
            ratelim_init(sess);
            data_disconn_start();
            ssize_t sizecur = sizetosend > 2048 ? 2048 : sizetosend;
            ssize_t ret = sendfile(data_fd,filefd,NULL,sizecur);
            if ( -1 == ret )
            {
                flag = 1;
                break; 
            } 
            sizetosend -= ret;
            ratelim_do(sess,tunable_download_max_rate,ret);
            b_dataconn = 1; 
        }
    }

    switch (flag)
    {
        case 0:
            ftp_reply(sess,FTP_TRANSFEROK,"Transfer complete.");
            break;
        case 1:
            ftp_reply(sess,FTP_BADSENDNET,"Send file failure.");
            break;
    }

    is_urg(sess);
    file_unlock(filefd);
    close(data_fd);
    close(filefd);
    clear_transfer(sess);
    sess->rest_pos = 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x808, %rsp            # imm = 0x808
movq %rdi, %rbx
callq 0x559c
movl %eax, 0xc(%rbx)
cmpl $-0x1, %eax
je 0x5b3b
movl %eax, %ebp
leaq 0x818(%rbx), %r12
movq %rsp, %rsi
movq %r12, %rdi
callq 0x3340
cmpl $-0x1, %eax
je 0x5b2a
movl $0xf000, %eax           # imm = 0xF000
andl 0x18(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
je 0x5b2a
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3450
cmpl $-0x1, %eax
je 0x5b2a
movl %eax, %r14d
movl %eax, %edi
callq 0x3cd5
cmpl $-0x1, %eax
je 0x5b60
movl %r14d, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x31f0
movq %rax, %r15
movq 0xc30(%rbx), %rsi
movl %r14d, %edi
xorl %edx, %edx
callq 0x31f0
movq %rsp, %r13
movl $0x800, %edx            # imm = 0x800
movq %r13, %rdi
xorl %esi, %esi
callq 0x3220
leaq 0x2b5b(%rip), %rsi      # 0x85e4
movq %r13, %rdi
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x34d0
movq %rbx, %rdi
movl $0x96, %esi
movq %r13, %rdx
callq 0x4cf4
cmpl $0x0, 0x10(%rbx)
je 0x5b8d
leaq 0x2b60(%rip), %rdx      # 0x861a
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x4cf4
cmpl $0x1, 0xc40(%rbx)
jne 0x5aee
movl $0x0, 0xc40(%rbx)
leaq 0x2844(%rip), %rdx      # 0x8325
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x4cf4
movl %r14d, %edi
callq 0x3d23
movl %ebp, %edi
callq 0x3260
movl %r14d, %edi
callq 0x3260
movq %rbx, %rdi
callq 0x479a
movq $0x0, 0xc30(%rbx)
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2a9d(%rip), %rdx      # 0x85ce
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
jmp 0x5b4a
leaq 0x2a75(%rip), %rdx      # 0x85b7
movq %rbx, %rdi
movl $0x1a9, %esi            # imm = 0x1A9
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4cf4
leaq 0x2a67(%rip), %rdx      # 0x85ce
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
callq 0x4cf4
movl %r14d, %edi
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3260
subq 0xc30(%rbx), %r15
je 0x5ab3
leaq 0x674f(%rip), %r13      # 0xc2f0
movq %rbx, %rdi
callq 0x4828
callq 0x5264
movl $0x800, %eax            # imm = 0x800
movq %r15, %rcx
cmpq %rax, %r15
jl 0x5bc0
movl $0x800, %ecx            # imm = 0x800
movl %ebp, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0x33e0
cmpq $-0x1, %rax
je 0x5bf8
movq %rax, %r12
movl (%r13), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x4863
movl $0x1, 0x6732(%rip)      # 0xc320
subq %r12, %r15
jne 0x5ba1
jmp 0x5ab3
leaq 0x2a2e(%rip), %rdx      # 0x862d
movq %rbx, %rdi
movl $0x1aa, %esi            # imm = 0x1AA
jmp 0x5ac2