// File: /workspace/github_cpp_makefile_star2_O2/lianxings[P]ftp_benchmark/ftp_benchmark.cc
// Function: local2ftp_worker(ftpInfo*, int, bool)
void local2ftp_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::filewrite;
    ftplib::transfermode mode = ftplib::image;
    FILE *local = NULL;
    ftphandle *nData = NULL;
    char ac[3] = "rb";

    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(srcPath.c_str(), ac);
        if (local == NULL) {
            ++failed;
            errorSize += srcInfo.size;
            sprintf(printBuf, "\nopen 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));
            if (failed <= 10) {
                continue;
            } else {
                --liveWorkThread;
                break;
            }
        }
        // get ftp data connection 
        int ret = build_ftp_data_connection(dstPath, 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;
        }

        Md5Context md5Context;
        if (verify){
            Md5Initialise( &md5Context );
        }


        int readed = 0;
		while ((readed = fread(dataBuf, 1, ioUnit, local)) > 0) {
			if (ftpClient->FtpWrite(dataBuf, readed, nData) < readed) {
                ++failed;
                errorSize += srcInfo.size;
                sprintf(printBuf, "\nftp write 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;
        }
        ftpClient->FtpClose(nData);
        char md5str[33] = {'\0'};
        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) {
                MD5_HASH md5;
                char tmp[3] = {'\0'};
                Md5Finalise( &md5Context, &md5);
                memset(md5str, 0, sizeof(md5str));
                for(int j = 0; j < sizeof(md5); j++ ) {
                    sprintf( tmp,"%2.2x", md5.bytes[j]);
                    strcat(md5str, tmp);
                }
            }
        }
        
        fflush(local);
        if (local != NULL) fclose(local);
        
        if (verify) {   // if need to verify, upload a new file just store md5sum
            string newFilePath = dstPath + ".md5";
            ret = build_ftp_data_connection(newFilePath, type, mode, ftpClient, pFtp, &nData);
            if (ret < 0) {
                ++md5Failed;
                if (ret == -1)
                    sprintf(printBuf, "\nget ftp data connection failed: upload md5 file %s\n",\
                                                                          newFilePath.c_str());
                else
                    sprintf(printBuf, "\nre-login ftp server %s failed: upload md5 file %s\n",\
                                                       pFtp->ftpServer , newFilePath.c_str());
                fwrite(printBuf, 1, strlen(printBuf), errorFd);
                write(2, printBuf, strlen(printBuf));
                continue;
            }

            if (ftpClient->FtpWrite(md5str, strlen(md5str), nData) < strlen(md5str)) {
                ++md5Failed;
                sprintf(printBuf, "\nftp upload file %s failed\n", newFilePath.c_str());
                fwrite(printBuf, 1, strlen(printBuf), errorFd);
                write(2, printBuf, strlen(printBuf));
                break;
            }
            ftpClient->FtpClose(nData);
        }

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6b8, %rsp            # imm = 0x6B8
movl %edx, %r13d
movq %rdi, 0x10(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
movl $0x40000, %edi          # imm = 0x40000
callq 0x4030
movq %rax, %r14
andq $0x0, 0x18(%rsp)
pushq $0x8
popq %rdi
callq 0x43e0
movq %rax, %rbx
movq %rax, %rdi
callq 0xc110
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x54b6
movq %rbx, (%rsp)
testl %eax, %eax
js 0x535e
leaq 0x100(%rsp), %r15
leaq 0x68(%rsp), %r12
leaq 0xd8(%rsp), %rbp
movl %r13d, 0x24(%rsp)
movq 0x1277f(%rip), %rax     # 0x17418
cmpq 0x12758(%rip), %rax     # 0x173f8
jne 0x4cb0
movl 0x12830(%rip), %eax     # 0x174d8
testl %eax, %eax
jne 0x539b
movq %r15, %rdi
callq 0x9d06
movq %r15, %rdi
callq 0x553c
testl %eax, %eax
js 0x5251
movq %r12, %rdi
callq 0x9d36
movq %rbp, %rdi
callq 0x9d36
movq %r12, %rdi
movq %r15, %rsi
callq 0x9d5a
movq %rbp, %rdi
leaq 0x128(%rsp), %rsi
callq 0x9d5a
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x4200
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
callq 0x4200
movl $0x400, %edx            # imm = 0x400
leaq 0x2b0(%rsp), %rdi
xorl %esi, %esi
callq 0x4210
movq 0x88(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0xbcb2
movq 0x48(%rsp), %rdi
leaq 0xb460(%rip), %rsi      # 0x101a2
callq 0x4420
testq %rax, %rax
je 0x4e17
movq %rax, %r12
leaq 0x170(%rsp), %rbx
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4510
movq %rbx, %rdi
pushq $0x4
popq %rsi
pushq $0x49
popq %rdx
movq (%rsp), %rcx
movq 0x10(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x55ec
movl %eax, %r15d
movq %rbx, %rdi
callq 0x4170
testl %r15d, %r15d
js 0x4eb0
testb %r13b, %r13b
leaq 0x2b0(%rsp), %rbp
je 0x4db2
leaq 0x218(%rsp), %rdi
callq 0xe704
movl $0x40000, %edx          # imm = 0x40000
movq %r14, %rdi
pushq $0x1
popq %rsi
movq %r12, %rcx
callq 0x4570
movq %rax, %r15
testl %r15d, %r15d
jle 0x4f78
movq 0x18(%rsp), %rcx
movq (%rsp), %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0xd8cc
cmpl %r15d, %eax
jl 0x4f0a
testb %r13b, %r13b
je 0x4e06
leaq 0x218(%rsp), %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0xe71d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
lock
addq %r15, 0x126fb(%rip)     # 0x17510
jmp 0x4db2
lock
incq 0x126d9(%rip)           # 0x174f8
movq 0x88(%rsp), %rax
lock
addq %rax, 0x126e9(%rip)     # 0x17518
movq 0x48(%rsp), %rdx
movq 0x28(%rsp), %rcx
leaq 0x2b0(%rsp), %r15
movq %r15, %rdi
leaq 0xb35a(%rip), %rsi      # 0x101a5
leaq 0x190(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x12667(%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
leaq 0x68(%rsp), %r12
movq 0x1265f(%rip), %rax     # 0x174f8
cmpq $0xb, %rax
leaq 0x100(%rsp), %r15
jl 0x5241
jmp 0x5370
lock
incq 0x12640(%rip)           # 0x174f8
movq 0x88(%rsp), %rax
lock
addq %rax, 0x12650(%rip)     # 0x17518
cmpl $-0x1, %r15d
je 0x51ce
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq 0x48(%rsp), %rcx
movq 0x28(%rsp), %r8
leaq 0x2b0(%rsp), %r15
movq %r15, %rdi
leaq 0xb338(%rip), %rsi      # 0x1022e
leaq 0x190(%rsp), %r9
xorl %eax, %eax
callq 0x4090
jmp 0x51f9
lock
incq 0x125e6(%rip)           # 0x174f8
movq 0x88(%rsp), %rax
lock
addq %rax, 0x125f6(%rip)     # 0x17518
movq 0x48(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %rbp, %rdi
leaq 0xb33f(%rip), %rsi      # 0x10275
leaq 0x190(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %rbp, %rdi
callq 0x4160
movq 0x1257c(%rip), %rcx     # 0x174d0
movq %rbp, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %rbp, %rdi
callq 0x4160
pushq $0x1
popq %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4120
movq 0x18(%rsp), %rsi
movq (%rsp), %rdi
callq 0xd740
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movb $0x0, 0xb0(%rsp)
testl %r15d, %r15d
js 0x5037
lock
incq 0x1253e(%rip)           # 0x174f0
testb %r13b, %r13b
je 0x51b9
movb $0x0, 0xe(%rsp)
andw $0x0, 0xc(%rsp)
leaq 0x218(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xf0d6
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movb $0x0, 0xb0(%rsp)
xorl %r15d, %r15d
cmpq $0x10, %r15
je 0x50a5
movzbl 0xb8(%rsp,%r15), %edx
leaq 0xc(%rsp), %rbx
movq %rbx, %rdi
leaq 0xb2d5(%rip), %rsi      # 0x102f0
xorl %eax, %eax
callq 0x4090
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x46b0
incq %r15
jmp 0x4ff9
lock
incq 0x124b9(%rip)           # 0x174f8
movq 0x88(%rsp), %rax
lock
addq %rax, 0x124c9(%rip)     # 0x17518
movq 0x48(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %rbp, %rdi
leaq 0xb24c(%rip), %rsi      # 0x102af
leaq 0x190(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %rbp, %rdi
callq 0x4160
movq 0x1244f(%rip), %rcx     # 0x174d0
movq %rbp, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %rbp, %rdi
callq 0x4160
pushq $0x1
popq %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4120
movq %r12, %rdi
callq 0x4460
movq %r12, %rdi
callq 0x43d0
testb %r13b, %r13b
je 0x5305
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
leaq 0xb221(%rip), %rdx      # 0x102f6
callq 0x9d7c
leaq 0x150(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4510
movq %r12, %rdi
pushq $0x4
popq %rsi
pushq $0x49
popq %rdx
movq (%rsp), %rcx
movq 0x10(%rsp), %r8
leaq 0x18(%rsp), %r9
callq 0x55ec
movl %eax, %r15d
movq %r12, %rdi
callq 0x4170
testl %r15d, %r15d
js 0x525e
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x4160
movq 0x18(%rsp), %rcx
movq (%rsp), %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0xd8cc
movslq %eax, %r15
leaq 0x90(%rsp), %rdi
callq 0x4160
cmpq %r15, %rax
jbe 0x528f
lock
incq 0x1239f(%rip)           # 0x17500
movq 0xb8(%rsp), %rdx
xorl %r15d, %r15d
movq %rbp, %rdi
leaq 0xb1ee(%rip), %rsi      # 0x10364
xorl %eax, %eax
callq 0x4090
movq %rbp, %rdi
callq 0x4160
movq 0x12344(%rip), %rcx     # 0x174d0
movq %rbp, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %rbp, %rdi
callq 0x4160
pushq $0x2
popq %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4120
pushq $0x3
popq %r13
jmp 0x52f8
movq %r12, %rdi
callq 0x4460
movq %r12, %rdi
callq 0x43d0
jmp 0x5305
movq 0x48(%rsp), %rdx
movq 0x28(%rsp), %rcx
leaq 0x2b0(%rsp), %r15
movq %r15, %rdi
leaq 0xaffc(%rip), %rsi      # 0x101e6
leaq 0x190(%rsp), %r8
xorl %eax, %eax
callq 0x4090
movq %r15, %rdi
callq 0x4160
movq 0x122c8(%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 %r12, %rdi
callq 0x43d0
leaq 0x100(%rsp), %r15
leaq 0x68(%rsp), %r12
movq %rbp, %rdi
callq 0x4170
movq %r12, %rdi
callq 0x4170
movq %r15, %rdi
callq 0x9db4
jmp 0x4c92
lock
incq 0x1229a(%rip)           # 0x17500
cmpl $-0x1, %r15d
je 0x52a5
movq 0x10(%rsp), %rax
movq (%rax), %rdx
movq 0xb8(%rsp), %rcx
movq %rbp, %rdi
leaq 0xb0aa(%rip), %rsi      # 0x10330
xorl %eax, %eax
callq 0x4090
jmp 0x52be
movq 0x18(%rsp), %rsi
movq (%rsp), %rdi
callq 0xd740
xorl %r13d, %r13d
movb $0x1, %r15b
jmp 0x52f8
movq 0xb8(%rsp), %rdx
movq %rbp, %rdi
leaq 0xb044(%rip), %rsi      # 0x102fb
xorl %eax, %eax
callq 0x4090
movq %rbp, %rdi
callq 0x4160
movq 0x12203(%rip), %rcx     # 0x174d0
movq %rbp, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
callq 0x4720
movq %rbp, %rdi
callq 0x4160
pushq $0x2
popq %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x4120
xorl %r15d, %r15d
pushq $0x2
popq %r13
movq %rbx, %rdi
callq 0x4170
testb %r15b, %r15b
je 0x5320
movq 0x121ec(%rip), %rax     # 0x174f8
xorl %r13d, %r13d
cmpq $0xb, %rax
jl 0x5320
lock
decl 0x121c4(%rip)           # 0x174e0
pushq $0x3
popq %r13
leaq 0xd8(%rsp), %rbp
movq %rbp, %rdi
callq 0x4170
leaq 0x68(%rsp), %r12
movq %r12, %rdi
callq 0x4170
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x9db4
cmpl $0x3, %r13d
movl 0x24(%rsp), %r13d
jne 0x4c92
jmp 0x539b
cmpl $-0x2, %eax
je 0x53e8
lock
decl 0x12172(%rip)           # 0x174e0
jmp 0x53c2
lock
decl 0x12169(%rip)           # 0x174e0
leaq 0xd8(%rsp), %rdi
callq 0x4170
leaq 0x68(%rsp), %rdi
callq 0x4170
leaq 0x100(%rsp), %rdi
callq 0x9db4
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0xe196
movq %r14, %rdi
callq 0x44c0
movq %rbx, %rdi
callq 0xc1c4
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x43f0
leaq 0x28(%rsp), %rdi
callq 0x4170
leaq 0x48(%rsp), %rdi
callq 0x4170
addq $0x6b8, %rsp            # imm = 0x6B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x11bd9(%rip), %rax     # 0x16fc8
movq (%rax), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rdx
leaq 0xb9d8(%rip), %rsi      # 0x10dd9
xorl %eax, %eax
callq 0x4530
jmp 0x5367
movq %rax, %r14
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x43f0
jmp 0x549a
jmp 0x5467
jmp 0x5467
jmp 0x5467
jmp 0x5473
movq %rax, %r14
leaq 0x150(%rsp), %rdi
callq 0x4170
jmp 0x5440
jmp 0x5473
jmp 0x5473
jmp 0x5473
movq %rax, %r14
leaq 0xb8(%rsp), %rdi
jmp 0x5457
jmp 0x5473
movq %rax, %r14
leaq 0x170(%rsp), %rdi
callq 0x4170
jmp 0x5476
jmp 0x5473
movq %rax, %r14
jmp 0x5483
jmp 0x546c
movq %rax, %r14
jmp 0x549a
movq %rax, %r14
jmp 0x548d
jmp 0x5473
movq %rax, %r14
leaq 0xd8(%rsp), %rdi
callq 0x4170
leaq 0x68(%rsp), %rdi
callq 0x4170
leaq 0x100(%rsp), %rdi
callq 0x9db4
leaq 0x28(%rsp), %rdi
callq 0x4170
leaq 0x48(%rsp), %rdi
callq 0x4170
movq %r14, %rdi
callq 0x46a0