// File: /workspace/github_c_makefile_star2_O1/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
movq %rcx, %r12
movq %rdx, 0x68(%rsp)
movq %rdi, %rbp
movl %esi, 0x24(%rsp)
movl %r8d, 0x20(%rsp)
movl %r9d, 0x4c(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x60(%rsp)
movq %rbx, 0x40(%rsp)
movl %ebx, 0x1c(%rsp)
movl %ebx, 0x18(%rsp)
movl %ebx, 0x14(%rsp)
movl %ebx, 0x10(%rsp)
movslq 0xb62d4(%rip), %r14     # 0xbf760
leaq (,%r14,8), %r15
movq %r15, %rdi
callq 0x69d0
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x69d0
movq %rax, %r13
shlq $0x2, %r14
movq %r14, %rdi
callq 0x69d0
movq %rax, 0x38(%rsp)
movq %r15, %rdi
callq 0x69d0
movq %rax, %r14
cmpl %ebx, 0x500(%rsp)
je 0x94ed
movq %rbp, %rdi
callq 0x1fbec
movl 0x24(%rsp), %edx
leaq 0x84127(%rip), %rdi     # 0x8d60b
movl %eax, %esi
xorl %eax, %eax
callq 0x6070
cmpl $0x0, 0x20(%rsp)
movq %rbp, 0x50(%rsp)
je 0x9526
movq 0xb79b0(%rip), %rdi     # 0xc0eb0
testq %rdi, %rdi
je 0x9530
movl $0x1, 0x10(%rsp)
leaq 0x84133(%rip), %rsi     # 0x8d647
callq 0x6a50
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
movl %ecx, 0x14(%rsp)
jmp 0x9538
movl $0x1, 0x1c(%rsp)
jmp 0x9538
movl $0x1, 0x18(%rsp)
movq 0x4f0(%rsp), %rbp
movq 0x4f8(%rsp), %rax
movq (%rax), %rdi
movq 0x20(%rdi), %rax
testq %rax, %rax
je 0x955a
movq (%rbp), %rsi
callq *%rax
movslq 0xb61ff(%rip), %rax     # 0xbf760
testq %rax, %rax
jle 0x9580
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
movslq 0x2c(%rdx), %rsi
addq 0x18(%rdx), %rsi
movq %rsi, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x9568
movq %r14, 0x30(%rsp)
movq %r12, 0x58(%rsp)
leaq 0xc(%rsp), %rax
movl $0x0, (%rax)
leaq 0xb0(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %rax, 0xa0(%rsp)
movq 0x50(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x90e2
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movq 0x90(%rsp,%rbx,8), %rdi
movl 0xb617d(%rip), %esi     # 0xbf768
movl 0xb617f(%rip), %edx     # 0xbf770
movl 0xb616d(%rip), %ecx     # 0xbf764
movl 0xb6176(%rip), %r8d     # 0xbf774
movq 0xb788b(%rip), %r9      # 0xc0e90
movl 0xb7865(%rip), %eax     # 0xc0e70
movl 0xb7856(%rip), %r10d    # 0xc0e68
pushq %r10
pushq $0x0
pushq %rax
pushq $0x1
callq 0xd6a2
addq $0x20, %rsp
movq %rax, 0x70(%rsp,%rbx,8)
incq %rbx
cmpq $0x3, %rbx
jne 0x95dd
cmpl $0x0, 0xb6129(%rip)     # 0xbf760
jle 0x971e
movq 0x38(%rsp), %r15
movq 0x28(%rsp), %r12
xorl %ebx, %ebx
movl $0x78, %edi
callq 0x69d0
movq %rax, (%r13,%rbx,8)
leaq 0x24(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x68(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x40(%rsp), %rcx
movq %rcx, 0x28(%rax)
leaq 0xc(%rsp), %rcx
movq %rcx, 0x30(%rax)
leaq 0x4c(%rsp), %rcx
movq %rcx, 0x38(%rax)
leaq 0x20(%rsp), %rcx
movq %rcx, 0x48(%rax)
leaq 0x18(%rsp), %rcx
movq %rcx, 0x50(%rax)
leaq 0x1c(%rsp), %rcx
movq %rcx, 0x58(%rax)
leaq 0x10(%rsp), %rcx
movq %rcx, 0x60(%rax)
leaq 0x14(%rsp), %rcx
movq %rcx, 0x68(%rax)
movl %ebx, (%r15)
movq %r15, (%rax)
movq 0x58(%rsp), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rcx, 0x18(%rax)
movq (%rbp,%rbx,8), %rcx
movq %rcx, 0x40(%rax)
movq 0x4f8(%rsp), %rcx
movq (%rcx,%rbx,8), %rcx
movq %rcx, 0x70(%rax)
movq %r12, %rdi
xorl %esi, %esi
leaq -0x4b2(%rip), %rdx      # 0x9241
movq %rax, %rcx
callq 0x6dc0
testl %eax, %eax
jne 0x9928
incq %rbx
movslq 0xb6053(%rip), %rax     # 0xbf760
addq $0x8, %r12
addq $0x4, %r15
cmpq %rax, %rbx
jl 0x9649
cmpl $0x0, 0xb603b(%rip)     # 0xbf760
movq 0x28(%rsp), %r15
jle 0x9748
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rdi
xorl %esi, %esi
callq 0x6610
incq %rbx
movslq 0xb601d(%rip), %rax     # 0xbf760
cmpq %rax, %rbx
jl 0x972e
cmpl $0x0, 0xb6011(%rip)     # 0xbf760
jle 0x976c
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
callq 0x68a0
incq %rbx
movslq 0xb5ff9(%rip), %rax     # 0xbf760
cmpq %rax, %rbx
jl 0x9753
leaq 0x60(%rsp), %rdi
callq 0x15e1f
leaq 0x40(%rsp), %rdi
callq 0x29565
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x70, %rdi
callq 0xc6e0
addq $0x8, %rbx
cmpq $0x18, %rbx
jne 0x9782
movq 0x50(%rsp), %rax
movq %rax, %rbx
testq %rax, %rax
je 0x97bb
movq 0x10(%rbx), %rdi
movslq 0x18(%rbx), %rsi
callq 0x250f6
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x97a6
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rbx
jne 0x98ef
cmpl $0x0, 0xb5f8e(%rip)     # 0xbf760
jle 0x980b
xorl %ebx, %ebx
movq 0x58(%rsp), %r15
movq 0x30(%rsp), %r12
movq (%r15,%rbx,8), %rdi
movq (%r12,%rbx,8), %rsi
xorl %edx, %edx
callq 0x2a336
incq %rbx
movslq 0xb5f67(%rip), %rax     # 0xbf760
cmpq %rax, %rbx
jl 0x97e0
cmpl $0x2, %eax
setge %al
movq 0x28(%rsp), %rbx
jmp 0x980d
xorl %eax, %eax
testb %al, %al
je 0x98ef
cmpl $0x2, 0xb5f44(%rip)     # 0xbf760
jl 0x98ef
movl $0x1, %r15d
movq (%rbp,%r15,8), %rdi
callq 0x6110
movq (%rbp,%r15,8), %rcx
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
callq 0x6b20
movq %rax, %r12
testl %r12d, %r12d
jle 0x98c4
movq %r12, %rdx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%rbp), %rcx
movl $0x1, %esi
movq %r14, %rdi
callq 0x6e70
cmpl %eax, %r12d
je 0x98a2
movq %rbx, %rdi
callq 0x68a0
movq %r13, %rdi
callq 0x68a0
movq 0x38(%rsp), %rdi
callq 0x68a0
movq 0x30(%rsp), %rdi
callq 0x68a0
leaq 0x83db2(%rip), %rdi     # 0x8d64d
xorl %eax, %eax
callq 0x26b6f
movq (%rbp,%r15,8), %rcx
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
callq 0x6b20
movq %rax, %r12
movq %rax, %rdx
testl %r12d, %r12d
jg 0x9854
movq (%rbp,%r15,8), %rdx
xorl %edi, %edi
leaq 0x83d8f(%rip), %rsi     # 0x8d661
callq 0x66a0
movq %rax, (%rbp,%r15,8)
incq %r15
movslq 0xb5e7a(%rip), %rax     # 0xbf760
cmpq %rax, %r15
jl 0x9828
movl 0xc(%rsp), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0xc(%rsp)
testl %eax, %eax
jle 0x959d
movq %rbx, %rdi
callq 0x68a0
movq %r13, %rdi
callq 0x68a0
movq 0x38(%rsp), %rdi
callq 0x68a0
movq 0x30(%rsp), %rdi
callq 0x68a0
jmp 0x9934
leaq 0x83f29(%rip), %rdi     # 0x8d858
callq 0x6bb0
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq