// File: /workspace/github_cpp_makefile_star2_O2/lianxings[P]ftp_benchmark/ftp_benchmark.cc
// Function: ftp2local_worker(ftpInfo*, int, bool)
void ftp2local_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;
    FILE *local = NULL;
    ftphandle *nData = NULL;
    char ac[3] = "wb";

    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);
		local = fopen64(dstPath.c_str(), ac);
		if (local == NULL) {
            ++failed;
            errorSize += srcInfo.size;
            sprintf(printBuf, "\nopen destination file failed: source: %s, destination: %s, size: %s\n", 
                                                           srcPath.c_str(), dstPath.c_str(), sizeStrBuf);
            fwrite(printBuf, 1, strlen(printBuf), errorFd);
            write(1, printBuf, strlen(printBuf));
            if (failed <= 10) {
                continue;
            } else {
                --liveWorkThread;
                break;
            }
		}
        
        // 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: %s, size: %s\n",\
                                                                srcPath.c_str(), dstPath.c_str(), sizeStrBuf);
            else
                sprintf(printBuf, "\nre-login ftp server %s failed: source: %s, destination: %s, size: %s\n",\
                                             pFtp->ftpServer , srcPath.c_str(), dstPath.c_str(), sizeStrBuf);
            fwrite(printBuf, 1, strlen(printBuf), errorFd);
            write(2, printBuf, strlen(printBuf));
            if (local != NULL) fclose(local);
            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;
        while ((readed = ftpClient->FtpRead(dataBuf, ioUnit, nData)) > 0) {
            if (fwrite(dataBuf, 1, readed, local) <= 0) {
                ++failed;
                errorSize += srcInfo.size;
                sprintf(printBuf, "\nwrite destination file failed: source: %s, destination: %s, size: %s\n", 
                                                                srcPath.c_str(), dstPath.c_str(), sizeStrBuf);
                fwrite(printBuf, 1, strlen(printBuf), errorFd);
                write(1, printBuf, strlen(printBuf));
                break;
            }
            if (verify) {
                Md5Update( &md5Context, (const void*)dataBuf, readed);
            }
            finishSize += readed;
        }
        if (readed < 0) {
            ++failed;
            errorSize += srcInfo.size;
            sprintf(printBuf, "\nread source file failed: source: %s, destination: %s, size: %s\n", 
                                                      srcPath.c_str(), dstPath.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);
                }
            }
        }
        fflush(local);
        if (local != NULL) fclose(local);
        ftpClient->FtpClose(nData);

        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: read md5 file %s\n", newSrcPath.c_str());
                else
                    sprintf(printBuf, "\nre-login ftp server %s failed: read md5 file %s\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, 0xc(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x80(%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, 0x18(%rsp)
pushq $0x8
popq %rdi
callq 0x43e0
movq %rax, %r14
movq %rax, %rdi
callq 0xc110
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x54b6
testl %eax, %eax
js 0x6517
movl 0xc(%rsp), %eax
xorb $0x1, %al
movb %al, 0x7(%rsp)
leaq 0x100(%rsp), %rbp
leaq 0xe0(%rsp), %r12
movq 0x115e9(%rip), %rax     # 0x17418
cmpq 0x115c2(%rip), %rax     # 0x173f8
jne 0x5e46
movl 0x1169a(%rip), %eax     # 0x174d8
testl %eax, %eax
jne 0x6550
leaq 0x130(%rsp), %r15
movq %r15, %rdi
callq 0x9d06
movq %r15, %rdi
callq 0x553c
testl %eax, %eax
js 0x6505
leaq 0x48(%rsp), %rdi
callq 0x9d36
leaq 0xb8(%rsp), %rdi
callq 0x9d36
leaq 0x48(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x9d5a
leaq 0xb8(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x9d5a
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4200
leaq 0x70(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x4200
movl $0x400, %edx            # imm = 0x400
leaq 0x2c0(%rsp), %rdi
xorl %esi, %esi
callq 0x4210
movq 0x68(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0xbcb2
movq 0x70(%rsp), %rdi
leaq 0xa489(%rip), %rsi      # 0x10380
callq 0x4420
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x600e
leaq 0x180(%rsp), %r13
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4510
movq %r13, %rdi
pushq $0x3
popq %rsi
pushq $0x49
popq %rdx
movq %r14, %rcx
movq 0x10(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x55ec
movl %eax, %r15d
movq %r13, %rdi
callq 0x4170
testl %r15d, %r15d
js 0x6086
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
xorl %eax, %eax
movb %al, 0xb0(%rsp)
movb %al, 0xa(%rsp)
andw $0x0, 0x8(%rsp)
cmpb $0x0, 0xc(%rsp)
je 0x5fab
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
xorl %eax, %eax
movb %al, 0xb0(%rsp)
movb %al, 0xa(%rsp)
andw $0x0, 0x8(%rsp)
leaq 0x228(%rsp), %rdi
callq 0xe704
movq 0x18(%rsp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x40000, %edx          # imm = 0x40000
callq 0xd81e
movl %eax, %r15d
testl %eax, %eax
jle 0x60dd
movl %r15d, %r13d
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
callq 0x4720
testq %rax, %rax
je 0x615b
cmpb $0x0, 0xc(%rsp)
je 0x6004
leaq 0x228(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0xe71d
lock
addq %r13, 0x11504(%rip)     # 0x17510
jmp 0x5fab
lock
incq 0x114e2(%rip)           # 0x174f8
movq 0x68(%rsp), %rax
lock
addq %rax, 0x114f5(%rip)     # 0x17518
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0xa344(%rip), %rsi      # 0x10383
leaq 0x1a0(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x11473(%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
movq %rax, %rdx
callq 0x4120
jmp 0x64e1
lock
incq 0x1146a(%rip)           # 0x174f8
movq 0x68(%rsp), %rax
lock
addq %rax, 0x1147d(%rip)     # 0x17518
cmpl $-0x1, %r15d
je 0x6381
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq 0x28(%rsp), %rcx
movq 0x70(%rsp), %r8
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0xa165(%rip), %rsi      # 0x1022e
leaq 0x1a0(%rsp), %r9
xorl %eax, %eax
callq 0x4090
jmp 0x63ac
jns 0x61ce
lock
incq 0x1140d(%rip)           # 0x174f8
movq 0x68(%rsp), %rax
lock
addq %rax, 0x11420(%rip)     # 0x17518
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
leaq 0x2c0(%rsp), %r13
movq %r13, %rdi
leaq 0xa19b(%rip), %rsi      # 0x102af
leaq 0x1a0(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r13, %rdi
callq 0x4160
movq 0x1139e(%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
jmp 0x6249
lock
incq 0x11395(%rip)           # 0x174f8
movq 0x68(%rsp), %rax
lock
addq %rax, 0x113a8(%rip)     # 0x17518
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
leaq 0x2c0(%rsp), %r13
movq %r13, %rdi
leaq 0xa23d(%rip), %rsi      # 0x103c9
leaq 0x1a0(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r13, %rdi
callq 0x4160
movq 0x11326(%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
lock
incq 0x1131a(%rip)           # 0x174f0
cmpb $0x0, 0xc(%rsp)
je 0x6249
leaq 0x228(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0xf0d6
xorl %r13d, %r13d
cmpq $0x10, %r13
je 0x6249
movzbl 0x120(%rsp,%r13), %edx
movq %rbx, %r12
movq %r14, %rbx
movq %rbp, %r14
leaq 0x8(%rsp), %rbp
movq %rbp, %rdi
leaq 0xa0d4(%rip), %rsi      # 0x102f0
xorl %eax, %eax
callq 0x4090
leaq 0x90(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rbp
movq %rbx, %r14
movq %r12, %rbx
leaq 0xe0(%rsp), %r12
callq 0x46b0
incq %r13
jmp 0x61f5
movq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x4460
movq %r13, %rdi
callq 0x43d0
movq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0xd740
testl %r15d, %r15d
sets %al
orb 0x7(%rsp), %al
jne 0x64e1
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
leaq 0xa06c(%rip), %rdx      # 0x102f6
callq 0x9d7c
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4510
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
pushq $0x3
popq %rsi
pushq $0x49
popq %rdx
movq %r14, %rcx
movq 0x10(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x55ec
movl %eax, %r12d
movq %r15, %rdi
callq 0x4170
testl %r12d, %r12d
js 0x63ee
movl $0x40000, %edx          # imm = 0x40000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4210
movq 0x18(%rsp), %rcx
movq %r14, %rdi
movq %rbx, %rsi
movl $0x40000, %edx          # imm = 0x40000
callq 0xd81e
testl %eax, %eax
jle 0x6423
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
pushq $0x20
popq %rdx
callq 0x41a0
testl %eax, %eax
je 0x6469
lock
incq 0x111e8(%rip)           # 0x17508
movq 0x28(%rsp), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0xa158(%rip), %rsi      # 0x1048f
movq %rbx, %rcx
leaq 0x90(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x11178(%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
movq %rax, %rdx
callq 0x4120
jmp 0x6469
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x9e49(%rip), %rsi      # 0x101e6
leaq 0x1a0(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x11115(%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
movq 0x20(%rsp), %rdi
callq 0x43d0
jmp 0x64ee
cmpl $-0x1, %r12d
je 0x6478
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq 0x100(%rsp), %rcx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0xa029(%rip), %rsi      # 0x10443
xorl %eax, %eax
callq 0x4090
jmp 0x6499
lock
incq 0x110dd(%rip)           # 0x17508
movq 0x100(%rsp), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0xa030(%rip), %rsi      # 0x10475
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x11075(%rip), %rcx     # 0x174d0
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0xd740
jmp 0x64cc
movq 0x100(%rsp), %rdx
leaq 0x2c0(%rsp), %r15
movq %r15, %rdi
leaq 0x9f7e(%rip), %rsi      # 0x10410
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x11028(%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
movq %rbp, %rdi
callq 0x4170
testl %r12d, %r12d
leaq 0xe0(%rsp), %r12
js 0x64ee
movq 0x11010(%rip), %rax     # 0x174f8
cmpq $0xb, %rax
jge 0x6525
leaq 0xb8(%rsp), %rdi
callq 0x4170
leaq 0x48(%rsp), %rdi
callq 0x4170
leaq 0x130(%rsp), %rdi
callq 0x9db4
jmp 0x5e28
cmpl $-0x2, %eax
je 0x6599
lock
decl 0x10fbd(%rip)           # 0x174e0
jmp 0x6573
lock
decl 0x10fb4(%rip)           # 0x174e0
leaq 0xb8(%rsp), %rdi
callq 0x4170
leaq 0x48(%rsp), %rdi
callq 0x4170
leaq 0x130(%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 0x70(%rsp), %rdi
callq 0x4170
leaq 0x28(%rsp), %rdi
callq 0x4170
addq $0x6c8, %rsp            # imm = 0x6C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10a28(%rip), %rax     # 0x16fc8
movq (%rax), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rdx
leaq 0xa827(%rip), %rsi      # 0x10dd9
xorl %eax, %eax
callq 0x4530
jmp 0x651c
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x43f0
jmp 0x6647
jmp 0x6619
jmp 0x6619
jmp 0x6619
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x4170
jmp 0x65ed
jmp 0x6620
jmp 0x6620
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
jmp 0x6602
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0x4170
jmp 0x6623
jmp 0x6620
jmp 0x6620
movq %rax, %rbx
jmp 0x6630
jmp 0x6614
movq %rax, %rbx
jmp 0x663a
movq %rax, %rbx
jmp 0x6647
jmp 0x6620
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x4170
leaq 0x48(%rsp), %rdi
callq 0x4170
leaq 0x130(%rsp), %rdi
callq 0x9db4
leaq 0x70(%rsp), %rdi
callq 0x4170
leaq 0x28(%rsp), %rdi
callq 0x4170
movq %rbx, %rdi
callq 0x46a0