// File: lianxings[P]ftp_benchmark/ftp_benchmark.cc
// Function: ftp2null_dispatcher_core(ftpInfo*, ftplib*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
static void ftp2null_dispatcher_core(ftpInfo * pFtp, ftplib * ftpClient, string ftpPath, string localPath) 
{
    ftplib::accesstype type = ftplib::dirverbose;
    ftplib::transfermode mode = ftplib::ascii;
    int ioUnit = 1 << 10;
    
    // check if ftp directory exists
    if (!ftpClient->Chdir(ftpPath.c_str())) {
        fprintf(stderr, "change to directory %s failed.\n", ftpPath.c_str());
        return;
    }

    // normalize directory path format
    // localDirPath is "null", do not need to deal with it
    string nullStr("");
    normalize_dir_format(nullStr, ftpPath);
    
    std::queue<task<fileInfo> > tempTaskQue;
    char *dataBuf = static_cast<char*>(malloc(ioUnit));
    char printBuf[1024];
    ftphandle *nData = NULL;
    std::queue<string> dirQue;
    dirQue.push(ftpPath);
    
    while (!dirQue.empty()) {
        int size = dirQue.size();
        for (int i = 0; i < size; i++) {
            string dirPath = dirQue.front();
            dirQue.pop();
            sprintf(printBuf, "\rlist directory %-45s", dirPath.c_str());
            write(1, printBuf, strlen(printBuf));
            ftpClient->mp_ftphandle->offset = 0;
            int ret = build_ftp_data_connection(dirPath, type, mode, ftpClient, pFtp, &nData);
            if (ret < 0) {
                if (ret == -1)
                    sprintf(printBuf, "List directory %s failed: get ftp data connection failed.\n", dirPath.c_str());
                else
                    sprintf(printBuf, "List directory %s failed: re-login ftp server %s failed.\n",\
                                                                 dirPath.c_str(), pFtp->ftpServer);
                fwrite(printBuf, 1, strlen(printBuf), errorFd);
                write(2, printBuf, strlen(printBuf));
                continue;
            }

            // read current direcory
            while (ftpClient->FtpRead(dataBuf, ioUnit, nData) > 0) {
                string name = "";
                string sizeStr = "";
                resolve_list_entry(dataBuf, name, sizeStr);
                if (name.compare(".") == 0 || name.compare("..") == 0)
                    continue;
                size_t dotLoc = name.find_last_of(".");
                if (dotLoc != std::string::npos && name.substr(dotLoc + 1).compare("md5") == 0)
                    continue;
                if (dataBuf[0] == 'd') {  // the entry is directory
                    string nowSrcPath = dirPath + name + "/";
                    dirQue.push(nowSrcPath);
                } else if (dataBuf[0] == '-') {  // the entry is file
                    int64_t fileSize = atoi(sizeStr.c_str());
                    task<fileInfo> taskTmp;
                    fileInfo src, dst;
                    src.name = dirPath + name;
                    dst.name = localPath;
                    src.size = dst.size = size;
                    taskTmp.src = src;
                    taskTmp.dst = dst;  // dst dir is "null"
                    if (put_task_to_queue(taskTmp) < 0)
                        break;
                    ++fileNum;
                    totalFileSize += fileSize;
                }
            }
            ftpClient->FtpClose(nData);  // close this data connection for read next directory
            if (liveWorkThread <= 0)     // if all of workers have exited, dispatcher also exits.
                break;
        }
        if (liveWorkThread <= 0)     // if all of workers have exited, dispatcher also exits.
            break;
    } 
    free(dataBuf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x7b0, %rsp            # imm = 0x7B0
movq %rdx, -0x718(%rbp)
movq %rdi, %rax
movq -0x718(%rbp), %rdi
movq %rdi, -0x710(%rbp)
movq %rcx, -0x708(%rbp)
movq %rax, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x2, -0x24(%rbp)
movl $0x41, -0x28(%rbp)
movl $0x400, -0x2c(%rbp)     # imm = 0x400
movq -0x10(%rbp), %rax
movq %rax, -0x700(%rbp)
callq 0x41b0
movq -0x700(%rbp), %rdi
movq %rax, %rsi
callq 0x18d00
cmpl $0x0, %eax
jne 0x96bd
movq -0x710(%rbp), %rdi
movq 0x1c937(%rip), %rax     # 0x25fc8
movq (%rax), %rax
movq %rax, -0x720(%rbp)
callq 0x41b0
movq -0x720(%rbp), %rdi
movq %rax, %rdx
leaq 0x1364c(%rip), %rsi     # 0x1ccfd
movb $0x0, %al
callq 0x4680
jmp 0x9fdb
leaq -0x51(%rbp), %rdi
movq %rdi, -0x728(%rbp)
callq 0x4820
movq -0x728(%rbp), %rdx
leaq 0x13271(%rip), %rsi     # 0x1c94c
leaq -0x50(%rbp), %rdi
callq 0xd720
jmp 0x96e6
leaq -0x51(%rbp), %rdi
callq 0x4510
movq -0x710(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x15d20
jmp 0x9701
leaq -0xb8(%rbp), %rdi
callq 0xd1e0
jmp 0x970f
movslq -0x2c(%rbp), %rdi
callq 0x4620
movq %rax, -0xc0(%rbp)
movq $0x0, -0x4c8(%rbp)
leaq -0x518(%rbp), %rdi
callq 0x11280
jmp 0x9738
movq -0x710(%rbp), %rsi
leaq -0x518(%rbp), %rdi
callq 0x112b0
jmp 0x974d
jmp 0x974f
leaq -0x518(%rbp), %rdi
callq 0x112e0
movb %al, -0x729(%rbp)
jmp 0x9763
movb -0x729(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x9774
jmp 0x9fae
leaq -0x518(%rbp), %rdi
callq 0x11300
movq %rax, -0x738(%rbp)
jmp 0x9789
movq -0x738(%rbp), %rax
movl %eax, -0x51c(%rbp)
movl $0x0, -0x520(%rbp)
movl -0x520(%rbp), %eax
cmpl -0x51c(%rbp), %eax
jge 0x9f96
leaq -0x518(%rbp), %rdi
callq 0x11320
movq %rax, -0x740(%rbp)
jmp 0x97c7
movq -0x740(%rbp), %rsi
leaq -0x540(%rbp), %rdi
callq 0x41c0
jmp 0x97dc
leaq -0x518(%rbp), %rdi
callq 0x11340
jmp 0x97ea
leaq -0x540(%rbp), %rdi
callq 0x41b0
movq %rax, %rdx
leaq 0x135c5(%rip), %rsi     # 0x1cdc5
xorl %eax, %eax
leaq -0x4c0(%rbp), %rdi
movq %rdi, -0x748(%rbp)
callq 0x40d0
movq -0x748(%rbp), %rdi
callq 0x41d0
movq -0x748(%rbp), %rsi
movq %rax, %rdx
movl $0x1, %edi
callq 0x4160
jmp 0x9837
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq $0x0, 0x1b0(%rax)
leaq -0x568(%rbp), %rdi
leaq -0x540(%rbp), %rsi
callq 0x41c0
jmp 0x985e
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %r8
leaq -0x568(%rbp), %rdi
leaq -0x4c8(%rbp), %r9
callq 0x5fd0
movl %eax, -0x74c(%rbp)
jmp 0x9887
leaq -0x568(%rbp), %rdi
callq 0x4280
movl -0x74c(%rbp), %eax
movl %eax, -0x544(%rbp)
cmpl $0x0, -0x544(%rbp)
jge 0x9a0c
cmpl $-0x1, -0x544(%rbp)
jne 0x996b
leaq -0x4c0(%rbp), %rax
movq %rax, -0x758(%rbp)
leaq -0x540(%rbp), %rdi
callq 0x41b0
movq -0x758(%rbp), %rdi
movq %rax, %rdx
leaq 0x134f7(%rip), %rsi     # 0x1cddb
movb $0x0, %al
callq 0x40d0
jmp 0x99a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x4510
jmp 0xa005
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x9ffc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x9ff0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x9fe4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x9f88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x568(%rbp), %rdi
callq 0x4280
jmp 0x9f88
leaq -0x4c0(%rbp), %rax
movq %rax, -0x760(%rbp)
leaq -0x540(%rbp), %rdi
callq 0x41b0
movq -0x760(%rbp), %rdi
movq %rax, %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rcx
leaq 0x13479(%rip), %rsi     # 0x1ce16
movb $0x0, %al
callq 0x40d0
leaq -0x4c0(%rbp), %rdi
movq %rdi, -0x768(%rbp)
callq 0x41d0
movq -0x768(%rbp), %rdi
movq %rax, %rdx
movq 0x1cc18(%rip), %rcx     # 0x265e0
movl $0x1, %esi
callq 0x4890
jmp 0x99d4
leaq -0x4c0(%rbp), %rdi
movq %rdi, -0x770(%rbp)
callq 0x41d0
movq -0x770(%rbp), %rsi
movq %rax, %rdx
movl $0x2, %edi
callq 0x4160
jmp 0x99fd
movl $0x6, -0x56c(%rbp)
jmp 0x9f45
jmp 0x9a0e
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl -0x2c(%rbp), %edx
movq -0x4c8(%rbp), %rcx
callq 0x18800
movl %eax, -0x774(%rbp)
jmp 0x9a30
movl -0x774(%rbp), %eax
cmpl $0x0, %eax
jle 0x9f0c
leaq -0x591(%rbp), %rdi
movq %rdi, -0x780(%rbp)
callq 0x4820
movq -0x780(%rbp), %rdx
leaq 0x12eec(%rip), %rsi     # 0x1c94c
leaq -0x590(%rbp), %rdi
callq 0xd720
jmp 0x9a6e
leaq -0x591(%rbp), %rdi
callq 0x4510
leaq -0x5b9(%rbp), %rdi
movq %rdi, -0x788(%rbp)
callq 0x4820
movq -0x788(%rbp), %rdx
leaq 0x12eb1(%rip), %rsi     # 0x1c94c
leaq -0x5b8(%rbp), %rdi
callq 0xd720
jmp 0x9aa9
leaq -0x5b9(%rbp), %rdi
callq 0x4510
movq -0xc0(%rbp), %rdi
leaq -0x590(%rbp), %rsi
leaq -0x5b8(%rbp), %rdx
callq 0x15e10
jmp 0x9ad1
leaq -0x590(%rbp), %rdi
leaq 0x13277(%rip), %rsi     # 0x1cd56
callq 0x4120
cmpl $0x0, %eax
je 0x9b01
leaq -0x590(%rbp), %rdi
leaq 0x1325e(%rip), %rsi     # 0x1cd55
callq 0x4120
cmpl $0x0, %eax
jne 0x9b5b
movl $0x7, -0x56c(%rbp)
jmp 0x9eb0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x591(%rbp), %rdi
callq 0x4510
jmp 0x9f88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x5b9(%rbp), %rdi
callq 0x4510
jmp 0x9efe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x9ef2
leaq -0x590(%rbp), %rdi
leaq 0x131ed(%rip), %rsi     # 0x1cd56
movq $-0x1, %rdx
callq 0x4440
movq %rax, -0x5c8(%rbp)
movq -0x5c8(%rbp), %rcx
movb $0x0, -0x5e9(%rbp)
xorl %eax, %eax
cmpq $-0x1, %rcx
movb %al, -0x789(%rbp)
je 0x9be4
movq -0x5c8(%rbp), %rdx
incq %rdx
leaq -0x5e8(%rbp), %rdi
leaq -0x590(%rbp), %rsi
movq $-0x1, %rcx
callq 0x44d0
jmp 0x9bbe
movb $0x1, -0x5e9(%rbp)
leaq -0x5e8(%rbp), %rdi
leaq 0x1277c(%rip), %rsi     # 0x1c34f
callq 0x4120
cmpl $0x0, %eax
sete %al
movb %al, -0x789(%rbp)
movb -0x789(%rbp), %al
movb %al, -0x78a(%rbp)
testb $0x1, -0x5e9(%rbp)
jne 0x9bfb
jmp 0x9c07
leaq -0x5e8(%rbp), %rdi
callq 0x4280
movb -0x78a(%rbp), %al
testb $0x1, %al
jne 0x9c13
jmp 0x9c22
movl $0x7, -0x56c(%rbp)
jmp 0x9eb0
movq -0xc0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
jne 0x9cd9
leaq -0x630(%rbp), %rdi
leaq -0x540(%rbp), %rsi
leaq -0x590(%rbp), %rdx
callq 0xd8f0
jmp 0x9c51
leaq 0x13225(%rip), %rdx     # 0x1ce7d
leaq -0x610(%rbp), %rdi
leaq -0x630(%rbp), %rsi
callq 0xd8a0
jmp 0x9c6d
leaq -0x630(%rbp), %rdi
callq 0x4280
leaq -0x518(%rbp), %rdi
leaq -0x610(%rbp), %rsi
callq 0x112b0
jmp 0x9c8e
leaq -0x610(%rbp), %rdi
callq 0x4280
jmp 0x9ea6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x630(%rbp), %rdi
callq 0x4280
jmp 0x9ef2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x610(%rbp), %rdi
callq 0x4280
jmp 0x9ef2
movq -0xc0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x9ea4
leaq -0x5b8(%rbp), %rdi
callq 0x41b0
movq %rax, %rdi
callq 0x4710
cltq
movq %rax, -0x638(%rbp)
leaq -0x688(%rbp), %rdi
callq 0xd410
jmp 0x9d17
leaq -0x6b0(%rbp), %rdi
callq 0xd460
jmp 0x9d25
leaq -0x6d8(%rbp), %rdi
callq 0xd460
jmp 0x9d33
leaq -0x6f8(%rbp), %rdi
leaq -0x540(%rbp), %rsi
leaq -0x590(%rbp), %rdx
callq 0xd8f0
jmp 0x9d4f
leaq -0x6b0(%rbp), %rdi
leaq -0x6f8(%rbp), %rsi
movq %rsi, -0x798(%rbp)
callq 0x45c0
movq -0x798(%rbp), %rdi
callq 0x4280
movq -0x708(%rbp), %rsi
leaq -0x6d8(%rbp), %rdi
callq 0x4050
jmp 0x9d8a
movslq -0x51c(%rbp), %rax
movq %rax, -0x6b8(%rbp)
movq %rax, -0x690(%rbp)
leaq -0x688(%rbp), %rdi
leaq -0x6b0(%rbp), %rsi
callq 0xd4d0
jmp 0x9db4
leaq -0x660(%rbp), %rdi
leaq -0x6d8(%rbp), %rsi
callq 0xd4d0
jmp 0x9dc9
leaq -0x688(%rbp), %rdi
callq 0xd0e0
movl %eax, -0x79c(%rbp)
jmp 0x9ddd
movl -0x79c(%rbp), %eax
cmpl $0x0, %eax
jge 0x9e2d
movl $0x8, -0x56c(%rbp)
jmp 0x9e58
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x9e96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x9e8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x6d8(%rbp), %rdi
callq 0xd640
jmp 0x9e8a
leaq 0x1c7c4(%rip), %rdi     # 0x265f8
callq 0xd510
movq -0x638(%rbp), %rax
addq 0x1c7e9(%rip), %rax     # 0x26630
movq %rax, 0x1c7e2(%rip)     # 0x26630
movl $0x0, -0x56c(%rbp)
leaq -0x6d8(%rbp), %rdi
callq 0xd640
leaq -0x6b0(%rbp), %rdi
callq 0xd640
leaq -0x688(%rbp), %rdi
callq 0xd660
movl -0x56c(%rbp), %eax
testl %eax, %eax
jne 0x9eb0
jmp 0x9e88
jmp 0x9ea4
leaq -0x6b0(%rbp), %rdi
callq 0xd640
leaq -0x688(%rbp), %rdi
callq 0xd660
jmp 0x9ef2
jmp 0x9ea6
movl $0x0, -0x56c(%rbp)
leaq -0x5b8(%rbp), %rdi
callq 0x4280
leaq -0x590(%rbp), %rdi
callq 0x4280
movl -0x56c(%rbp), %eax
movl %eax, -0x7a0(%rbp)
testl %eax, %eax
je 0x9eed
jmp 0x9eda
movl -0x7a0(%rbp), %eax
subl $0x7, %eax
je 0x9a0e
jmp 0x9eeb
jmp 0x9f0c
jmp 0x9a0e
leaq -0x5b8(%rbp), %rdi
callq 0x4280
leaq -0x590(%rbp), %rdi
callq 0x4280
jmp 0x9f88
movq -0x10(%rbp), %rdi
movq -0x4c8(%rbp), %rsi
callq 0x18690
jmp 0x9f1e
leaq 0x1c6cb(%rip), %rdi     # 0x265f0
callq 0xd230
cmpl $0x0, %eax
jg 0x9f3b
movl $0x4, -0x56c(%rbp)
jmp 0x9f45
movl $0x0, -0x56c(%rbp)
leaq -0x540(%rbp), %rdi
callq 0x4280
movl -0x56c(%rbp), %eax
movl %eax, -0x7a4(%rbp)
testl %eax, %eax
je 0x9f72
jmp 0x9f63
movl -0x7a4(%rbp), %eax
subl $0x4, %eax
je 0x9f96
jmp 0x9f70
jmp 0x9f74
jmp 0x9f74
movl -0x520(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x520(%rbp)
jmp 0x97a0
leaq -0x540(%rbp), %rdi
callq 0x4280
jmp 0x9fe4
leaq 0x1c653(%rip), %rdi     # 0x265f0
callq 0xd230
cmpl $0x0, %eax
jg 0x9fa9
jmp 0x9fae
jmp 0x974f
movq -0xc0(%rbp), %rdi
callq 0x45a0
leaq -0x518(%rbp), %rdi
callq 0x11360
leaq -0xb8(%rbp), %rdi
callq 0xd210
leaq -0x50(%rbp), %rdi
callq 0x4280
addq $0x7b0, %rsp            # imm = 0x7B0
popq %rbp
retq
leaq -0x518(%rbp), %rdi
callq 0x11360
leaq -0xb8(%rbp), %rdi
callq 0xd210
leaq -0x50(%rbp), %rdi
callq 0x4280
movq -0x60(%rbp), %rdi
callq 0x4810
nop