// File: /workspace/github_c_makefile_star2_O1/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 0x55ed
movl %eax, 0xc(%rbx)
cmpl $-0x1, %eax
je 0x5b3c
movl %eax, %ebp
leaq 0x818(%rbx), %r12
movq %rsp, %rsi
movq %r12, %rdi
callq 0x3330
cmpl $-0x1, %eax
je 0x5b2b
movl $0xf000, %eax           # imm = 0xF000
andl 0x18(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
je 0x5b2b
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3440
cmpl $-0x1, %eax
je 0x5b2b
movl %eax, %r14d
movl %eax, %edi
callq 0x3cc5
cmpl $-0x1, %eax
je 0x5b61
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 0x2af7(%rip), %rsi      # 0x85e8
movq %r13, %rdi
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x34c0
movq %rbx, %rdi
movl $0x96, %esi
movq %r13, %rdx
callq 0x4d4c
cmpl $0x0, 0x10(%rbx)
je 0x5b8e
leaq 0x2b00(%rip), %rdx      # 0x861e
movq %rbx, %rdi
movl $0xe2, %esi
jmp 0x5c18
leaq 0x2aa0(%rip), %rdx      # 0x85d2
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
jmp 0x5b4b
leaq 0x2a78(%rip), %rdx      # 0x85bb
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 0x4d4c
leaq 0x2a6a(%rip), %rdx      # 0x85d2
movq %rbx, %rdi
movl $0x1c3, %esi            # imm = 0x1C3
callq 0x4d4c
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
xorl %r13d, %r13d
testq %r15, %r15
je 0x5c00
movq %rbx, %rdi
callq 0x4875
callq 0x52b0
movl $0x800, %eax            # imm = 0x800
movq %r15, %rcx
cmpq %rax, %r15
jl 0x5bbc
movl $0x800, %ecx            # imm = 0x800
movl %ebp, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0x33d0
movq %rax, %r12
cmpq $-0x1, %rax
je 0x5bf4
subq %r12, %r15
leaq 0x670d(%rip), %rax      # 0xc2e8
movl (%rax), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x48b9
movl $0x1, 0x671e(%rip)      # 0xc310
jmp 0x5bfa
movl $0x1, %r13d
cmpq $-0x1, %r12
jne 0x5b98
testl %r13d, %r13d
je 0x5b17
leaq 0x2a21(%rip), %rdx      # 0x8631
movq %rbx, %rdi
movl $0x1aa, %esi            # imm = 0x1AA
callq 0x4d4c
cmpl $0x1, 0xc40(%rbx)
jne 0x5c44
movl $0x0, 0xc40(%rbx)
leaq 0x26ee(%rip), %rdx      # 0x8325
movq %rbx, %rdi
movl $0xe2, %esi
callq 0x4d4c
movl %r14d, %edi
callq 0x3d13
movl %ebp, %edi
callq 0x3260
movl %r14d, %edi
callq 0x3260
movq %rbx, %rdi
callq 0x47e7
movq $0x0, 0xc30(%rbx)
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq