// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/blatSrc/blat.c
// Function: bigBlat
void bigBlat(struct dnaSeq *untransList, int queryCount, char *queryFiles[], struct lineFile *lf[], boolean transQuery,
             boolean qIsDna, FILE *out[], struct gfOutput *gvo[], boolean showStatus)
/* Run query against translated DNA database (3 frames on each strand). */
{
    int             frame, cnt, i;
    struct dnaSeq   *seq;
    struct genoFind *gfs[3];
    aaSeq           *dbSeqLists[3];
    struct trans3   *t3List = NULL;
    int             isRc;
    struct hash     *t3Hash = NULL;
    boolean         forceUpper = FALSE;
    boolean         forceLower = FALSE;
    boolean         toggle = FALSE;
    boolean         maskUpper = FALSE;
    char            buf[1024];

    pthread_t*      thd = (pthread_t*)malloc(sizeof(pthread_t)*threads);
    void***         args = (void***)malloc(sizeof(void*)*threads);
    int*            id = (int*)malloc(sizeof(int)*threads);
    off_t*          offset = (off_t*)malloc(sizeof(off_t)*threads);


    if (showStatus)
        printf("Blatx %d sequences in database, %d sequences in each query\n", slCount(untransList), queryCount);

    /* Figure out how to manage query case.  Proteins want to be in
     * upper case, generally, nucleotides in lower case.  But there
     * may be repeatMasking based on case as well. */
    if (transQuery)
    {
        if (qMask == NULL)
            forceLower = TRUE;
        else
        {
            maskUpper = TRUE;
            toggle = !sameString(qMask, "upper");
        }
    }
    else
    {
        forceUpper = TRUE;
    }

    if (gvo[0]->fileHead != NULL)
        gvo[0]->fileHead(gvo[0], out[0]);

    for (i=0; i<threads; i++)
        offset[i] = lineFileTell(lf[i]);

    for (isRc = FALSE; isRc <= 1; ++isRc)
    {
        /* Initialize local pointer arrays to NULL to prevent surprises. */
        for (frame = 0; frame < 3; ++frame)
        {
            gfs[frame] = NULL;
            dbSeqLists[frame] = NULL;
        }

        t3List = seqListToTrans3List(untransList, dbSeqLists, &t3Hash);
        for (frame = 0; frame < 3; ++frame)
        {
            gfs[frame] = gfIndexSeq(dbSeqLists[frame], minMatch, maxGap, tileSize,
                                    repMatch, ooc, TRUE, oneOff, FALSE, stepSize);
        }

        /* multi-threads */
        for (i=0; i<threads; i++)
        {
            args[i]=(void**)malloc(sizeof(void*)*15);
            args[i][1]=&queryCount;
            args[i][2]=queryFiles;
            args[i][4]=gfs;
            args[i][5]=t3Hash;
            args[i][6]=&isRc;
            args[i][7]=&qIsDna;

            args[i][9]=&transQuery;
            args[i][10]=&forceLower;
            args[i][11]=&forceUpper;
            args[i][12]=&maskUpper;
            args[i][13]=&toggle;

            id[i]=i;
            args[i][0]=&(id[i]);
            args[i][3]=lf[i];
            args[i][8]=out[i];
            args[i][14]=gvo[i];
            if (pthread_create(&(thd[i]), NULL, performBigblat, (void*)(args[i])) != 0)
            {
                printf("Failed to create threads\n");
                return;
            }
        }

        for (i=0; i<threads; i++)
            pthread_join(thd[i], NULL);
        for (i=0; i<threads; i++)
            free(args[i]);


        /* Clean up time. */
        trans3FreeList(&t3List);
        freeHash(&t3Hash);
        for (frame = 0; frame < 3; ++frame)
        {
            genoFindFree(&gfs[frame]);
        }

        for (seq = untransList; seq != NULL; seq = seq->next)
        {
            reverseComplement(seq->dna, seq->size);
        }

        /* Rewind each input file pointer for RC run */
        if (isRc == FALSE)
        {
            for (i=0; i<threads; i++)
                lineFileSeek(lf[i], offset[i], SEEK_SET);
            if (threads > 1)
            {
                for (i=1; i<threads; i++)
                {
                    rewind(out[i]);
                    while((cnt=fread(buf, 1, 1024, out[i])) > 0)
                    {
                        frame = fwrite(buf, 1, cnt, out[0]);
                        if (frame != cnt)
                        {
                            free(thd);
                            free(args);
                            free(id);
                            free(offset);
                            errAbort("Merge files failed\n");
                        }
                    }
                    out[i] = freopen(NULL, "w+", out[i]);
                }
            }
        }
    }

    free(thd);
    free(args);
    free(id);
    free(offset);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movl %r8d, %ebx
andq $0x0, 0x60(%rsp)
movq %rcx, %r13
andq $0x0, 0x38(%rsp)
movq %rdx, 0x68(%rsp)
andl $0x0, 0x14(%rsp)
movl %esi, %r14d
andl $0x0, 0x10(%rsp)
movq %rdi, %rbp
andl $0x0, 0xc(%rsp)
movl %esi, 0x4c(%rsp)
andl $0x0, 0x8(%rsp)
movl %r8d, 0x48(%rsp)
movl %r9d, 0x44(%rsp)
movslq 0xabbf3(%rip), %r15     # 0xb3760
leaq (,%r15,8), %r12
movq %r12, %rdi
callq 0x69d0
movq %rax, 0x18(%rsp)
movq %r12, %rdi
callq 0x69d0
movq %rax, 0x28(%rsp)
shlq $0x2, %r15
movq %r15, %rdi
callq 0x69d0
movq %rax, 0x30(%rsp)
movq %r12, %rdi
callq 0x69d0
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x500(%rsp)
je 0x7bd2
movq %rbp, %rdi
callq 0x1c4aa
leaq 0x74a45(%rip), %rdi     # 0x7c60b
movl %eax, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x6070
testl %ebx, %ebx
movq %rbp, 0x50(%rsp)
je 0x7c08
movq 0xad2ee(%rip), %rdi     # 0xb4ed0
testq %rdi, %rdi
je 0x7c12
movl $0x1, 0x8(%rsp)
leaq 0x74a51(%rip), %rsi     # 0x7c647
callq 0x6a50
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
movl %ecx, 0xc(%rsp)
jmp 0x7c1a
movl $0x1, 0x14(%rsp)
jmp 0x7c1a
movl $0x1, 0x10(%rsp)
movq 0x4f0(%rsp), %rbp
movq 0x4f8(%rsp), %rax
movq (%rax), %rdi
movq 0x20(%rdi), %rax
testq %rax, %rax
je 0x7c3c
movq (%rbp), %rsi
callq *%rax
movl 0xabb1e(%rip), %eax     # 0xb3760
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rcx, %rax
je 0x7c69
movq (%r13,%rcx,8), %rdx
movslq 0x2c(%rdx), %rsi
addq 0x18(%rdx), %rsi
movq 0x20(%rsp), %rdx
movq %rsi, (%rdx,%rcx,8)
incq %rcx
jmp 0x7c49
leaq 0x70(%rsp), %rsi
pushq $0x1
popq %r15
leaq 0xb0(%rsp), %rbx
xorl %eax, %eax
movq %r13, 0x58(%rsp)
movl %eax, 0x4(%rsp)
cmpl $0x1, %eax
jg 0x7fba
xorl %eax, %eax
cmpq $0x3, %rax
je 0x7caa
andq $0x0, 0x90(%rsp,%rax,8)
andq $0x0, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x7c90
movq 0x50(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x77e6
movq %rax, 0x60(%rsp)
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0x7d16
movq 0x70(%rsp,%r14,8), %rdi
movl 0xaba96(%rip), %esi     # 0xb3768
movl 0xaba98(%rip), %edx     # 0xb3770
movl 0xaba86(%rip), %ecx     # 0xb3764
movl 0xaba8f(%rip), %r8d     # 0xb3774
movq 0xad1c4(%rip), %r9      # 0xb4eb0
movl 0xad19e(%rip), %eax     # 0xb4e90
movl 0xad18f(%rip), %r10d    # 0xb4e88
pushq %r10
pushq $0x0
pushq %rax
pushq $0x1
callq 0xb6df
addq $0x20, %rsp
movq %rax, 0x90(%rsp,%r14,8)
incq %r14
jmp 0x7cc1
movq 0x30(%rsp), %r14
movq 0x18(%rsp), %r12
xorl %r13d, %r13d
movslq 0xaba36(%rip), %rax     # 0xb3760
cmpq %rax, %r13
jge 0x7e02
pushq $0x78
popq %rdi
callq 0x69d0
movq 0x28(%rsp), %rcx
movq %rax, (%rcx,%r13,8)
leaq 0x4c(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x68(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x90(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rax)
leaq 0x4(%rsp), %rcx
movq %rcx, 0x30(%rax)
leaq 0x44(%rsp), %rcx
movq %rcx, 0x38(%rax)
leaq 0x48(%rsp), %rcx
movq %rcx, 0x48(%rax)
leaq 0x10(%rsp), %rcx
movq %rcx, 0x50(%rax)
leaq 0x14(%rsp), %rcx
movq %rcx, 0x58(%rax)
leaq 0x8(%rsp), %rcx
movq %rcx, 0x60(%rax)
leaq 0xc(%rsp), %rcx
movq %rcx, 0x68(%rax)
movl %r13d, (%r14)
movq %r14, (%rax)
movq 0x58(%rsp), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, 0x18(%rax)
movq (%rbp,%r13,8), %rcx
movq %rcx, 0x40(%rax)
movq 0x4f8(%rsp), %rcx
movq (%rcx,%r13,8), %rcx
movq %rcx, 0x70(%rax)
movq %r12, %rdi
xorl %esi, %esi
leaq -0x4b2(%rip), %rdx      # 0x7930
movq %rax, %rcx
callq 0x6dc0
incq %r13
addq $0x8, %r12
addq $0x4, %r14
testl %eax, %eax
je 0x7d23
jmp 0x7f9c
xorl %r14d, %r14d
movq 0x58(%rsp), %r13
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0x7e2d
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rdi
xorl %esi, %esi
callq 0x6610
incq %r14
movl 0xab935(%rip), %eax     # 0xb3760
jmp 0x7e0a
xorl %r14d, %r14d
cltq
cmpq %rax, %r14
jge 0x7e50
movq 0x28(%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x68a0
incq %r14
movl 0xab912(%rip), %eax     # 0xb3760
jmp 0x7e30
leaq 0x60(%rsp), %rdi
callq 0x13027
leaq 0x38(%rsp), %rdi
callq 0x2494c
xorl %r14d, %r14d
cmpq $0x18, %r14
je 0x7e83
leaq (%rsp,%r14), %rdi
addq $0x90, %rdi
callq 0xa8f7
addq $0x8, %r14
jmp 0x7e67
movq 0x50(%rsp), %r14
testq %r14, %r14
je 0x7e9f
movq 0x10(%r14), %rdi
movslq 0x18(%r14), %rsi
callq 0x20d38
movq (%r14), %r14
jmp 0x7e88
cmpl $0x0, 0x4(%rsp)
jne 0x7f8c
xorl %r14d, %r14d
movslq 0xab8ac(%rip), %rax     # 0xb3760
cmpq %rax, %r14
jge 0x7ed3
movq (%r13,%r14,8), %rdi
movq 0x20(%rsp), %rax
movq (%rax,%r14,8), %rsi
xorl %edx, %edx
callq 0x25595
incq %r14
jmp 0x7ead
cmpl $0x2, %eax
jl 0x7f8c
movq %r15, %r14
cltq
cmpq %rax, %r14
jge 0x7f8c
movq (%rbp,%r14,8), %rdi
callq 0x6110
movq (%rbp,%r14,8), %rcx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6b20
movq %rax, %r12
testl %r12d, %r12d
jle 0x7f66
movl %r12d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%rbp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6e70
cmpl %r12d, %eax
je 0x7ef4
movq 0x18(%rsp), %rdi
callq 0x68a0
movq 0x28(%rsp), %rdi
callq 0x68a0
movq 0x30(%rsp), %rdi
callq 0x68a0
movq 0x20(%rsp), %rdi
callq 0x68a0
leaq 0x746f0(%rip), %rdi     # 0x7c64d
xorl %eax, %eax
callq 0x223fb
jmp 0x7ef4
movq (%rbp,%r14,8), %rdx
xorl %edi, %edi
leaq 0x746ed(%rip), %rsi     # 0x7c661
callq 0x66a0
movq %rax, (%rbp,%r14,8)
incq %r14
movl 0xab7d9(%rip), %eax     # 0xb3760
jmp 0x7edf
movl 0x4(%rsp), %eax
incl %eax
leaq 0x70(%rsp), %rsi
jmp 0x7c81
leaq 0x748b5(%rip), %rdi     # 0x7c858
callq 0x6bb0
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rdi
callq 0x68a0
movq 0x28(%rsp), %rdi
callq 0x68a0
movq 0x30(%rsp), %rdi
callq 0x68a0
movq 0x20(%rsp), %rdi
callq 0x68a0
jmp 0x7fa8