// File: /workspace/github_cpp_makefile_star2_O3/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 $0x6d8, %rsp            # imm = 0x6D8
movl %edx, %ebp
movq %rdi, 0x20(%rsp)
leaq 0x80(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x140(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movl $0x40000, %edi          # imm = 0x40000
callq 0x4030
movq %rax, %r14
movq $0x0, 0x18(%rsp)
movl $0x8, %edi
callq 0x43f0
movq %rax, %r15
movq %rax, %rdi
callq 0xe220
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x5c9b
testl %eax, %eax
js 0x8b1d
movq %r15, 0x10(%rsp)
leaq 0x58(%rsp), %r12
leaq 0x2d0(%rsp), %r13
leaq 0xe0(%rsp), %rbx
movl %ebp, 0x94(%rsp)
movq %r14, 0x128(%rsp)
movq 0x1007e(%rip), %rax     # 0x18428
cmpq 0x10057(%rip), %rax     # 0x18408
jne 0x83c1
movl 0x1012f(%rip), %eax     # 0x184e8
testl %eax, %eax
jne 0x8af1
leaq 0x160(%rsp), %rax
movq %rax, 0x150(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x158(%rsp)
movb %cl, 0x160(%rsp)
movq %rcx, 0x170(%rsp)
leaq 0x188(%rsp), %rax
movq %rax, 0x178(%rsp)
movq %rcx, 0x180(%rsp)
movb %cl, 0x188(%rsp)
movq %rcx, 0x198(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x5d38
testl %eax, %eax
js 0x8675
movq %r12, 0x48(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x50(%rsp)
movb %cl, 0x58(%rsp)
movq %rcx, 0x68(%rsp)
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %rcx, 0xa0(%rsp)
movb %cl, 0xa8(%rsp)
movq %rcx, 0xb8(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x4200
movq 0x170(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x4200
movq 0x198(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4200
leaq 0x130(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x4200
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
xorl %esi, %esi
callq 0x4210
movq 0x68(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0xdc66
leaq 0xf0(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0xd3a8
movq %rbx, %rdi
movl $0x3, %esi
movl $0x49, %edx
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x5e39
movl %eax, %ebx
movq 0xe0(%rsp), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0x8558
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x4400
testl %ebx, %ebx
js 0x8687
xorps %xmm0, %xmm0
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
xorl %eax, %eax
movb %al, 0x120(%rsp)
movb %al, 0xe(%rsp)
movw $0x0, 0xc(%rsp)
testb %bpl, %bpl
je 0x85bd
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
xorl %eax, %eax
movb %al, 0x120(%rsp)
movb %al, 0xe(%rsp)
movw $0x0, 0xc(%rsp)
leaq 0x238(%rsp), %rdi
callq 0x10a64
xorl %r13d, %r13d
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movl $0x40000, %edx          # imm = 0x40000
callq 0xfa88
movl %eax, %ebx
testl %eax, %eax
jle 0x8603
movl %ebx, %eax
lock
addq %rax, 0xff39(%rip)      # 0x18520
addq %rax, %r13
testb %bpl, %bpl
je 0x85c0
leaq 0x238(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x10a7d
jmp 0x85c0
js 0x86d0
lock
incq 0xfeef(%rip)            # 0x18500
testb %bpl, %bpl
leaq 0x100(%rsp), %rbp
leaq 0x9cdd(%rip), %r15      # 0x12300
leaq 0xc(%rsp), %r14
je 0x8742
leaq 0x238(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x1143a
xorl %r12d, %r12d
movzbl 0x1a0(%rsp,%r12), %edx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x40a0
movq %rbp, %rdi
movq %r14, %rsi
callq 0x46f0
incq %r12
cmpq $0x10, %r12
jne 0x8646
jmp 0x8742
leaq 0x150(%rsp), %rdi
callq 0xbe24
jmp 0x83a3
lock
incq 0xfe79(%rip)            # 0x18508
movq 0x68(%rsp), %rax
lock
addq %rax, 0xfe8c(%rip)      # 0x18528
cmpl $-0x1, %ebx
je 0x8914
movq 0x20(%rsp), %rax
movq (%rax), %rdx
movq 0x70(%rsp), %rcx
movq %r13, %rdi
leaq 0x9e69(%rip), %rsi      # 0x12525
leaq 0x1b0(%rsp), %r8
xorl %eax, %eax
callq 0x40a0
jmp 0x8932
lock
incq 0xfe30(%rip)            # 0x18508
movq 0x68(%rsp), %rax
lock
addq %rax, 0xfe43(%rip)      # 0x18528
movq 0x70(%rsp), %rdx
leaq 0x2d0(%rsp), %r12
movq %r12, %rdi
leaq 0x9e72(%rip), %rsi      # 0x1256e
leaq 0x1b0(%rsp), %rcx
xorl %eax, %eax
callq 0x40a0
movq %r12, %rdi
callq 0x4180
movq 0xfdc6(%rip), %rcx      # 0x184e0
movl $0x1, %esi
movq %r12, %rdi
movq %rax, %rdx
callq 0x4750
movq %r12, %rdi
callq 0x4180
movl $0x1, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x4140
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0xf9cc
cmpb $0x1, 0xfd78(%rip)      # 0x184d0
jne 0x8782
movl $0xba, %edi
xorl %eax, %eax
callq 0x4070
movq 0x18(%rsp), %rcx
movl 0x10(%rcx), %edx
leaq 0x9e3c(%rip), %rdi      # 0x125b1
movq %rax, %rsi
movq %r13, %rcx
xorl %eax, %eax
callq 0x4060
testl %ebx, %ebx
setne %al
movl 0x94(%rsp), %ebp
movl %ebp, %ecx
xorb $0x1, %cl
orb %al, %cl
leaq 0x58(%rsp), %r12
movq 0x128(%rsp), %r14
jne 0x8a68
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0xd3a8
movq %rbx, %rdi
leaq 0x9b30(%rip), %rsi      # 0x12306
callq 0x4740
leaq 0xd0(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0xc0(%rsp), %rdi
callq 0xd3a8
leaq 0xc0(%rsp), %rdi
movl $0x3, %esi
movl $0x49, %edx
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x5e39
movl %eax, %r13d
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x8853
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x4400
testl %r13d, %r13d
js 0x896e
movl $0x40000, %edx          # imm = 0x40000
movq %r14, %rdi
xorl %esi, %esi
callq 0x4210
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movl $0x40000, %edx          # imm = 0x40000
callq 0xfa88
testl %eax, %eax
leaq 0x100(%rsp), %rdi
jle 0x899c
movl $0x20, %edx
movq %r14, %rsi
callq 0x41b0
testl %eax, %eax
je 0x89e1
lock
incq 0xfc69(%rip)            # 0x18518
movq 0x70(%rsp), %rdx
leaq 0x2d0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x9bd9(%rip), %rsi      # 0x1249f
movq %r14, %rcx
leaq 0x100(%rsp), %r8
xorl %eax, %eax
callq 0x40a0
movq %rbx, %rdi
callq 0x4180
movq 0xfbf9(%rip), %rcx      # 0x184e0
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x4750
movq %rbx, %rdi
callq 0x4180
movl $0x1, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x4140
jmp 0x89e1
movq 0x70(%rsp), %rdx
movq %r13, %rdi
leaq 0x9bb8(%rip), %rsi      # 0x124db
leaq 0x1b0(%rsp), %rcx
xorl %eax, %eax
callq 0x40a0
movq %r13, %rdi
callq 0x4180
movq 0xfb9f(%rip), %rcx      # 0x184e0
movl $0x1, %esi
movq %r13, %rdi
movq %rax, %rdx
callq 0x4750
movq %r13, %rdi
callq 0x4180
movl $0x2, %edi
movq %r13, %rsi
movq %rax, %rdx
callq 0x4140
jmp 0x8a88
cmpl $-0x1, %r13d
je 0x89f2
movq 0x20(%rsp), %rax
movq (%rax), %rdx
movq 0x28(%rsp), %rcx
leaq 0x2d0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x9c8d(%rip), %rsi      # 0x12620
xorl %eax, %eax
callq 0x40a0
jmp 0x8a10
lock
incq 0xfb74(%rip)            # 0x18518
movq 0x28(%rsp), %rdx
leaq 0x2d0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x9aca(%rip), %rsi      # 0x12485
xorl %eax, %eax
callq 0x40a0
movq %rbx, %rdi
callq 0x4180
movq 0xfb0f(%rip), %rcx      # 0x184e0
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x4750
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0xf9cc
jmp 0x8a47
movq 0x28(%rsp), %rdx
leaq 0x2d0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x9bd7(%rip), %rsi      # 0x125e0
xorl %eax, %eax
callq 0x40a0
movq %rbx, %rdi
callq 0x4180
movq 0xfac1(%rip), %rcx      # 0x184e0
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x4750
movq %rbx, %rdi
callq 0x4180
movl $0x2, %edi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x4140
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x8a63
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4400
testl %r13d, %r13d
js 0x8ae5
movq 0xfa99(%rip), %rax      # 0x18508
cmpq $0xb, %rax
leaq 0x2d0(%rsp), %r13
jl 0x8a88
lock
decl 0xfa6c(%rip)            # 0x184f0
movb $0x1, %bl
jmp 0x8a8a
xorl %ebx, %ebx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x8aaf
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x4400
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x8ac6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4400
leaq 0x150(%rsp), %rdi
callq 0xbe24
testb %bl, %bl
leaq 0xe0(%rsp), %rbx
je 0x83a3
jmp 0x8af1
xorl %ebx, %ebx
leaq 0x2d0(%rsp), %r13
jmp 0x8a8a
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x104b6
movq %rbx, %rdi
callq 0xe318
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4400
movq %r14, %rdi
callq 0x44e0
jmp 0x8b29
cmpl $-0x2, %eax
je 0x8b82
lock
decl 0xf9c7(%rip)            # 0x184f0
movq 0x130(%rsp), %rdi
leaq 0x140(%rsp), %rax
cmpq %rax, %rdi
je 0x8b4e
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x4400
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x8b70
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x4400
addq $0x6d8, %rsp            # imm = 0x6D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%rsp), %rdi
callq 0x4925
jmp 0x8b22
movq %rax, %rbx
movl $0x8, %esi
movq %r15, %rdi
callq 0x4400
jmp 0x8c87
jmp 0x8bad
movq %rax, %rbx
jmp 0x8cac
movq %rax, %rbx
jmp 0x8c87
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x8be8
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x4400
jmp 0x8be8
jmp 0x8be5
jmp 0x8c28
jmp 0x8c3b
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x8c2b
movq 0x38(%rsp), %rsi
jmp 0x8c1e
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
leaq 0xf0(%rsp), %rax
cmpq %rax, %rdi
je 0x8c2b
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x4400
jmp 0x8c2b
movq %rax, %rbx
leaq 0x58(%rsp), %r12
jmp 0x8c3e
jmp 0x8c3b
movq %rax, %rbx
jmp 0x8c7a
jmp 0x8c3b
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x8c63
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x4400
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x8c7a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4400
leaq 0x150(%rsp), %rdi
callq 0xbe24
movq 0x130(%rsp), %rdi
leaq 0x140(%rsp), %rax
cmpq %rax, %rdi
je 0x8cac
movq 0x140(%rsp), %rsi
incq %rsi
callq 0x4400
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x8cce
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x4400
movq %rbx, %rdi
callq 0x46e0