// File: /workspace/github_cpp_makefile_star2_O2/lianxings[P]ftp_benchmark/ftp_benchmark.cc
// Function: ftp2null_worker(ftpInfo*, int, bool)
void ftp2null_worker(ftpInfo * pFtp, int id, bool verify) 
{
    string srcPath, dstPath;
    int ioUnit = 256 << 10;
    char * dataBuf = new char[ioUnit];
    char printBuf[1024];
    char sizeStrBuf[128];
    ftplib::accesstype type = ftplib::fileread;
    ftplib::transfermode mode = ftplib::image;
    ftphandle *nData = NULL;

    ftplib * ftpClient = new ftplib();
    int ret = connect_and_login_ftp (ftpClient, pFtp, 0);
    if (ret < 0) {
        if (ret == -2)
            fprintf(stderr, "login ftp server %s failed.\n", pFtp->ftpServer);
        --liveWorkThread;
        return;
    }

    while((!tasksQueue.empty()) || (!exitFlag)) {
        task<fileInfo> taskTmp;
        if (get_task_from_queue(taskTmp) < 0) 
            continue;

        fileInfo srcInfo, dstInfo;
        srcInfo = taskTmp.src;
        dstInfo = taskTmp.dst;
        srcPath = srcInfo.name;
        dstPath = dstInfo.name;
        memset(printBuf, 0, sizeof(printBuf));
        memset(printBuf, 0, sizeof(sizeStrBuf));
        size_to_string(srcInfo.size, sizeStrBuf);
        // get ftp data connection 
        int ret = build_ftp_data_connection(srcPath, type, mode, ftpClient, pFtp, &nData);
        if (ret < 0) {
            ++failed;
            errorSize += srcInfo.size;
            if (ret == -1)
                sprintf(printBuf, "\nget ftp data connection failed: source: %s, destination: null, size: %s\n",\
                                                                                   srcPath.c_str(), sizeStrBuf);
            else
                sprintf(printBuf, "\nre-login ftp server %s failed: source: %s, destination: null, size: %s\n",\
                                                                pFtp->ftpServer , srcPath.c_str(), sizeStrBuf);
            fwrite(printBuf, 1, strlen(printBuf), errorFd);
            write(2, printBuf, strlen(printBuf));
            continue;
        }

		char checkSumBuf[33] = {'\0'};
		char tmp[3] = {'\0'};
        Md5Context md5Context;
        MD5_HASH md5;
        if (verify){
            memset(checkSumBuf, 0, sizeof(checkSumBuf));
            memset(tmp, 0, sizeof(tmp));
            Md5Initialise( &md5Context );
        }

        int readed = 0;
        long long file_size = 0;
        while ((readed = ftpClient->FtpRead(dataBuf, ioUnit, nData)) > 0) {
            finishSize += readed;
            file_size += readed;
            if (verify) {
                Md5Update( &md5Context, (const void*)dataBuf, readed);
            }
        }
        if (readed < 0) {
            ++failed;
            errorSize += srcInfo.size;
            sprintf(printBuf, "\nread source file failed: source: %s, destination: null, size: %s\n",\
                srcPath.c_str(), sizeStrBuf);
            fwrite(printBuf, 1, strlen(printBuf), errorFd);
            write(1, printBuf, strlen(printBuf));
        } else {
            ++finished;
            if (verify) {
                Md5Finalise( &md5Context, &md5);
                for(int j = 0; j < sizeof(md5); j++) {
                    sprintf( tmp,"%2.2x", md5.bytes[j]);
                    strcat(checkSumBuf, tmp);
                }
            }
        }

        ftpClient->FtpClose(nData);

        if(gFtpDebug)
            printf("Thread:%-8ld  sock:%-4d  read file size: %lld\n", syscall(SYS_gettid), nData->handle, file_size);

        if (readed >= 0 && verify) {
            string newSrcPath = srcPath + ".md5";
            int ret = build_ftp_data_connection(newSrcPath, type, mode, ftpClient, pFtp, &nData);
            if (ret < 0) {
                if (ret == -1)
                    sprintf(printBuf, "\nget ftp data connection failed: source: %s, destination: null\n", newSrcPath.c_str());
                else
                    sprintf(printBuf, "\nre-login ftp server %s failed: source: %s, destination: null\n",\
                                                                   pFtp->ftpServer , newSrcPath.c_str());
                fwrite(printBuf, 1, strlen(printBuf), errorFd);
                write(2, printBuf, strlen(printBuf));
                continue;
            }

            memset(dataBuf, 0, ioUnit);
            if (ftpClient->FtpRead(dataBuf, ioUnit, nData) <= 0) {
                ++verifyFailed;
                sprintf(printBuf, "\nread md5 file %s failed\n", newSrcPath.c_str());
                fwrite(printBuf, 1, strlen(printBuf), errorFd);
                //write(1, printBuf, strlen(printBuf));
            } else if (strncmp(checkSumBuf, dataBuf, 32) != 0) {
                ++verifyFailed;
				sprintf(printBuf, "\nverify file '%s' failed: upload md5: %s, download md5: %s\n", \
                                                           srcPath.c_str(), dataBuf, checkSumBuf);
                fwrite(printBuf, 1, strlen(printBuf), errorFd);
                write(1, printBuf, strlen(printBuf));
			}
            ftpClient->FtpClose(nData);
        }

        if (failed > 10) {
            --liveWorkThread;
            break;
        }
    }

    ftpClient->Quit();
    delete ftpClient;
    delete [] dataBuf;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6c8, %rsp            # imm = 0x6C8
movl %edx, %ebp
movq %rdi, %r13
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x110(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
movl $0x40000, %edi          # imm = 0x40000
callq 0x4030
movq %rax, %rbx
andq $0x0, 0x10(%rsp)
pushq $0x8
popq %rdi
callq 0x43e0
movq %rax, %r14
movq %rax, %rdi
callq 0xc110
movq %r14, %rdi
movq %r13, %rsi
callq 0x54b6
testl %eax, %eax
js 0x75d4
leaq 0x150(%rsp), %r12
leaq 0x98(%rsp), %r15
movl %ebp, %eax
xorb $0x1, %al
movb %al, 0xb(%rsp)
movq %r13, 0x68(%rsp)
movq %r14, 0x18(%rsp)
movq 0x10426(%rip), %rax     # 0x17418
cmpq 0x103ff(%rip), %rax     # 0x173f8
jne 0x7009
movl 0x104d7(%rip), %eax     # 0x174d8
testl %eax, %eax
jne 0x7611
movq %r12, %rdi
callq 0x9d06
movq %r12, %rdi
callq 0x553c
testl %eax, %eax
js 0x75c7
leaq 0x70(%rsp), %rdi
callq 0x9d36
leaq 0xd8(%rsp), %rdi
callq 0x9d36
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x9d5a
leaq 0xd8(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x9d5a
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4200
leaq 0x100(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x4200
movl $0x400, %edx            # imm = 0x400
leaq 0x2c0(%rsp), %rdi
xorl %esi, %esi
callq 0x4210
movq 0x90(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0xbcb2
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
callq 0x4510
movq %r15, %rdi
pushq $0x3
popq %rsi
pushq $0x49
popq %rdx
movq %r14, %rcx
movq %r13, %r8
leaq 0x10(%rsp), %r9
callq 0x55ec
movq %r15, %rdi
movl %eax, %r15d
callq 0x4170
testl %r15d, %r15d
js 0x71e1
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movb %al, 0x60(%rsp)
movb %al, 0xe(%rsp)
andw $0x0, 0xc(%rsp)
testb %bpl, %bpl
je 0x712a
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movb %al, 0x60(%rsp)
movb %al, 0xe(%rsp)
andw $0x0, 0xc(%rsp)
leaq 0x228(%rsp), %rdi
callq 0xe704
xorl %r12d, %r12d
movq 0x10(%rsp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x40000, %edx          # imm = 0x40000
callq 0xd81e
movl %eax, %r15d
testl %eax, %eax
jle 0x7171
movl %r15d, %eax
lock
addq %rax, 0x103bc(%rip)     # 0x17510
addq %rax, %r12
testb %bpl, %bpl
je 0x712d
leaq 0x228(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0xe71d
jmp 0x712d
js 0x7232
lock
incq 0x10371(%rip)           # 0x174f0
testb %bpl, %bpl
je 0x72a3
leaq 0x228(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0xf0d6
xorl %r13d, %r13d
cmpq $0x10, %r13
je 0x72a3
movzbl 0x120(%rsp,%r13), %edx
movl %ebp, %r14d
leaq 0xc(%rsp), %rbp
movq %rbp, %rdi
leaq 0x912b(%rip), %rsi      # 0x102f0
xorl %eax, %eax
callq 0x4090
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
movl %r14d, %ebp
callq 0x46b0
incq %r13
jmp 0x71a0
lock
incq 0x1030f(%rip)           # 0x174f8
movq 0x90(%rsp), %rax
lock
addq %rax, 0x1031f(%rip)     # 0x17518
cmpl $-0x1, %r15d
je 0x741d
movq (%r13), %rdx
movq 0x20(%rsp), %rcx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x92f7(%rip), %rsi      # 0x10515
leaq 0x1a0(%rsp), %r8
xorl %eax, %eax
callq 0x4090
jmp 0x7443
lock
incq 0x102be(%rip)           # 0x174f8
movq 0x90(%rsp), %rax
lock
addq %rax, 0x102ce(%rip)     # 0x17518
movq 0x20(%rsp), %rdx
leaq 0x2c0(%rsp), %r13
movq %r13, %rdi
leaq 0x92fd(%rip), %rsi      # 0x1055e
leaq 0x1a0(%rsp), %rcx
xorl %eax, %eax
callq 0x4090
movq %r13, %rdi
callq 0x4160
movq 0x10251(%rip), %rcx     # 0x174d0
movq %r13, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %r13, %rdi
callq 0x4160
pushq $0x1
popq %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x4120
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0xd740
cmpb $0x1, 0x10207(%rip)     # 0x174c0
jne 0x72e3
movl $0xba, %edi
xorl %eax, %eax
callq 0x4060
movq 0x10(%rsp), %rcx
movl 0x10(%rcx), %edx
leaq 0x92cb(%rip), %rdi      # 0x105a1
movq %rax, %rsi
movq %r12, %rcx
xorl %eax, %eax
callq 0x4050
testl %r15d, %r15d
setne %al
orb 0xb(%rsp), %al
movq 0x68(%rsp), %r13
leaq 0x98(%rsp), %r15
movq 0x18(%rsp), %r14
jne 0x759b
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x8fda(%rip), %rdx      # 0x102f6
callq 0x9d7c
leaq 0x130(%rsp), %rdi
movq %r12, %rsi
callq 0x4510
movq %r14, %rcx
movq %r13, %r8
leaq 0x130(%rsp), %r14
movq %r14, %rdi
pushq $0x3
popq %rsi
pushq $0x49
popq %rdx
leaq 0x10(%rsp), %r9
callq 0x55ec
movl %eax, %r12d
movq %r14, %rdi
callq 0x4170
testl %r12d, %r12d
js 0x749a
movl $0x40000, %edx          # imm = 0x40000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4210
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x40000, %edx          # imm = 0x40000
callq 0xd81e
testl %eax, %eax
jle 0x74d0
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
pushq $0x20
popq %rdx
callq 0x41a0
testl %eax, %eax
je 0x751e
lock
incq 0x10151(%rip)           # 0x17508
movq 0x20(%rsp), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x90c1(%rip), %rsi      # 0x1048f
movq %rbx, %rcx
leaq 0x40(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x100e4(%rip), %rcx     # 0x174d0
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %r15, %rdi
callq 0x4160
pushq $0x1
popq %rdi
movq %r15, %rsi
leaq 0x98(%rsp), %r15
movq %rax, %rdx
callq 0x4120
jmp 0x751e
movq 0x20(%rsp), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x9097(%rip), %rsi      # 0x104cb
leaq 0x1a0(%rsp), %rcx
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x1007e(%rip), %rcx     # 0x174d0
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %r15, %rdi
callq 0x4160
pushq $0x2
popq %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4120
leaq 0x98(%rsp), %r15
leaq 0xd8(%rsp), %rdi
callq 0x4170
leaq 0x70(%rsp), %rdi
callq 0x4170
jmp 0x75c7
cmpl $-0x1, %r12d
movq 0x18(%rsp), %r14
je 0x752d
movq (%r13), %rdx
movq 0xb8(%rsp), %rcx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x9149(%rip), %rsi      # 0x10610
xorl %eax, %eax
callq 0x4090
jmp 0x754e
lock
incq 0x10030(%rip)           # 0x17508
movq 0xb8(%rsp), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x8f83(%rip), %rsi      # 0x10475
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0xffc8(%rip), %rcx      # 0x174d0
movq %r15, %rdi
leaq 0x98(%rsp), %r15
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0xd740
jmp 0x7589
movq 0xb8(%rsp), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x9089(%rip), %rsi      # 0x105d0
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0xff73(%rip), %rcx      # 0x174d0
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %r15, %rdi
callq 0x4160
pushq $0x2
popq %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4120
leaq 0x98(%rsp), %r15
leaq 0xb8(%rsp), %rdi
callq 0x4170
testl %r12d, %r12d
js 0x75a8
movq 0xff56(%rip), %rax      # 0x174f8
cmpq $0xb, %rax
jge 0x75e6
leaq 0xd8(%rsp), %rdi
callq 0x4170
leaq 0x70(%rsp), %rdi
callq 0x4170
leaq 0x150(%rsp), %r12
movq %r12, %rdi
callq 0x9db4
jmp 0x6feb
cmpl $-0x2, %eax
je 0x765d
lock
decl 0xfefc(%rip)            # 0x174e0
jmp 0x7634
lock
decl 0xfef3(%rip)            # 0x174e0
leaq 0xd8(%rsp), %rdi
callq 0x4170
leaq 0x70(%rsp), %rdi
callq 0x4170
leaq 0x150(%rsp), %rdi
callq 0x9db4
movq %r14, %rdi
callq 0xe196
movq %r14, %rdi
callq 0xc1c4
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x43f0
movq %rbx, %rdi
callq 0x44c0
leaq 0x100(%rsp), %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
addq $0x6c8, %rsp            # imm = 0x6C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf964(%rip), %rax      # 0x16fc8
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0x9767(%rip), %rsi      # 0x10dd9
xorl %eax, %eax
callq 0x4530
jmp 0x75dd
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x43f0
jmp 0x7707
jmp 0x76d9
jmp 0x76d9
jmp 0x76d9
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x4170
jmp 0x76ad
jmp 0x76e0
jmp 0x76e0
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x76c7
movq %rax, %rbx
jmp 0x76f0
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x4170
jmp 0x76e3
jmp 0x76d4
jmp 0x76e0
jmp 0x76e0
movq %rax, %rbx
jmp 0x76fa
movq %rax, %rbx
jmp 0x7707
jmp 0x76e0
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x4170
leaq 0x70(%rsp), %rdi
callq 0x4170
leaq 0x150(%rsp), %rdi
callq 0x9db4
leaq 0x100(%rsp), %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
movq %rbx, %rdi
callq 0x46a0