// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %rcx, 0x28(%rsp)
movq %rdx, 0x68(%rsp)
movq %rdi, %rbp
movl %esi, 0x10(%rsp)
movl %esi, 0x5c(%rsp)
movl %r8d, 0x58(%rsp)
movl %r9d, 0x54(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x60(%rsp)
movq %rbx, 0x48(%rsp)
movl %ebx, 0x24(%rsp)
movl %ebx, 0x20(%rsp)
movl %ebx, 0x1c(%rsp)
movl %ebx, 0x18(%rsp)
movslq 0xb63b0(%rip), %r15     # 0xbf760
leaq (,%r15,8), %r12
movq %r12, %rdi
callq 0x69d0
movq %rax, 0x30(%rsp)
movq %r12, %rdi
callq 0x69d0
movq %rax, %r13
shlq $0x2, %r15
movq %r15, %rdi
callq 0x69d0
movq %rax, 0x40(%rsp)
movq %r12, %rdi
callq 0x69d0
movq %rax, %r12
cmpl %ebx, 0x500(%rsp)
je 0x9411
movq %rbp, %rdi
callq 0x200ac
leaq 0x84207(%rip), %rdi     # 0x8d60b
movl %eax, %esi
movl 0x10(%rsp), %edx
xorl %eax, %eax
callq 0x6070
movq %rbp, %rbx
testl %r14d, %r14d
je 0x944b
movq 0xb7a90(%rip), %rdi     # 0xc0eb0
testq %rdi, %rdi
movq 0x28(%rsp), %r15
je 0x945a
movl $0x1, 0x18(%rsp)
leaq 0x8420e(%rip), %rsi     # 0x8d647
callq 0x6a50
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
movl %ecx, 0x1c(%rsp)
jmp 0x9462
movl $0x1, 0x24(%rsp)
movq 0x28(%rsp), %r15
jmp 0x9462
movl $0x1, 0x20(%rsp)
movq 0x4f0(%rsp), %rbp
movq 0x4f8(%rsp), %rax
movq (%rax), %rdi
movq 0x20(%rdi), %rax
testq %rax, %rax
je 0x9484
movq (%rbp), %rsi
callq *%rax
movslq 0xb62d5(%rip), %rax     # 0xbf760
testq %rax, %rax
jle 0x94aa
xorl %ecx, %ecx
movq (%r15,%rcx,8), %rdx
movslq 0x2c(%rdx), %rsi
addq 0x18(%rdx), %rsi
movq %rsi, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x9492
leaq 0xc(%rsp), %rax
movl $0x0, (%rax)
leaq 0xb0(%rsp), %r14
movq %r12, 0x10(%rsp)
movq %rbx, %rdi
movq %rbx, 0x38(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
movaps %xmm0, 0x90(%rsp)
movq %rax, 0xa0(%rsp)
leaq 0x90(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x8ffc
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movq 0x90(%rsp,%rbx,8), %rdi
movl 0xb6255(%rip), %esi     # 0xbf768
movl 0xb6257(%rip), %edx     # 0xbf770
movl 0xb6245(%rip), %ecx     # 0xbf764
movl 0xb624e(%rip), %r8d     # 0xbf774
movq 0xb7963(%rip), %r9      # 0xc0e90
movl 0xb793d(%rip), %eax     # 0xc0e70
movl 0xb792e(%rip), %r10d    # 0xc0e68
pushq %r10
pushq $0x0
pushq %rax
pushq $0x1
callq 0xd753
addq $0x20, %rsp
movq %rax, 0x70(%rsp,%rbx,8)
incq %rbx
cmpq $0x3, %rbx
jne 0x9505
cmpl $0x0, 0xb6201(%rip)     # 0xbf760
jle 0x969a
movq 0x40(%rsp), %rbx
movq 0x30(%rsp), %r12
xorl %r15d, %r15d
movl $0x78, %edi
callq 0x69d0
movq %rax, (%r13,%r15,8)
leaq 0x5c(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x68(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq 0x48(%rsp), %rcx
movq %rcx, 0x28(%rax)
leaq 0xc(%rsp), %rcx
movq %rcx, 0x30(%rax)
leaq 0x54(%rsp), %rcx
movq %rcx, 0x38(%rax)
leaq 0x58(%rsp), %rcx
movq %rcx, 0x48(%rax)
leaq 0x20(%rsp), %rcx
movq %rcx, 0x50(%rax)
leaq 0x24(%rsp), %rcx
movq %rcx, 0x58(%rax)
leaq 0x18(%rsp), %rcx
movq %rcx, 0x60(%rax)
leaq 0x1c(%rsp), %rcx
movq %rcx, 0x68(%rax)
movl %r15d, (%rbx)
movq %rbx, (%rax)
movq 0x28(%rsp), %rcx
movq (%rcx,%r15,8), %rcx
movq %rcx, 0x18(%rax)
movq (%rbp,%r15,8), %rcx
movq %rcx, 0x40(%rax)
movq 0x4f8(%rsp), %rcx
movq (%rcx,%r15,8), %rcx
movq %rcx, 0x70(%rax)
movq %r12, %rdi
xorl %esi, %esi
leaq -0x4c3(%rip), %rdx      # 0x9159
movq %rax, %rcx
callq 0x6dc0
testl %eax, %eax
jne 0x9846
incq %r15
movslq 0xb612a(%rip), %rax     # 0xbf760
addq $0x8, %r12
addq $0x4, %rbx
cmpq %rax, %r15
jl 0x9572
testl %eax, %eax
movq 0x28(%rsp), %r15
movq 0x10(%rsp), %r12
jle 0x969a
xorl %ebx, %ebx
movq 0x30(%rsp), %r12
movq (%r12,%rbx,8), %rdi
xorl %esi, %esi
callq 0x6610
incq %rbx
movslq 0xb60ef(%rip), %rax     # 0xbf760
cmpq %rax, %rbx
jl 0x965c
testl %eax, %eax
movq 0x10(%rsp), %r12
jle 0x969a
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rdi
callq 0x68a0
incq %rbx
movslq 0xb60cb(%rip), %rax     # 0xbf760
cmpq %rax, %rbx
jl 0x9681
leaq 0x60(%rsp), %rdi
callq 0x15e67
leaq 0x48(%rsp), %rdi
callq 0x298bd
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x70, %rdi
callq 0xc5cc
addq $0x8, %rbx
cmpq $0x18, %rbx
jne 0x96b0
movq 0x38(%rsp), %rax
movq %rax, %rbx
testq %rax, %rax
je 0x96e9
movq 0x10(%rbx), %rdi
movslq 0x18(%rbx), %rsi
callq 0x25423
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x96d4
movl 0xc(%rsp), %eax
testl %eax, %eax
movq 0x38(%rsp), %rdi
jne 0x9811
cmpl $0x0, 0xb605f(%rip)     # 0xbf760
jle 0x9803
xorl %ebx, %ebx
movq 0x10(%rsp), %r12
movq (%r15,%rbx,8), %rdi
movq (%r12,%rbx,8), %rsi
xorl %edx, %edx
callq 0x2a68a
incq %rbx
movslq 0xb6039(%rip), %rax     # 0xbf760
cmpq %rax, %rbx
jl 0x970e
cmpl $0x2, %eax
jl 0x9803
movl $0x1, %ebx
movq (%rbp,%rbx,8), %rdi
callq 0x6110
movq (%rbp,%rbx,8), %rcx
movl $0x1, %esi
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
callq 0x6b20
movq %rax, %r12
testl %r12d, %r12d
jle 0x97d8
movq %r12, %rdx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq (%rbp), %rcx
movl $0x1, %esi
movq %r14, %rdi
callq 0x6e70
cmpl %eax, %r12d
je 0x97b6
movq 0x30(%rsp), %rdi
callq 0x68a0
movq %r13, %rdi
callq 0x68a0
movq 0x40(%rsp), %rdi
callq 0x68a0
movq 0x10(%rsp), %rdi
callq 0x68a0
leaq 0x83e9e(%rip), %rdi     # 0x8d64d
xorl %eax, %eax
callq 0x26e94
movq (%rbp,%rbx,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 0x9766
movq (%rbp,%rbx,8), %rdx
xorl %edi, %edi
leaq 0x83e7b(%rip), %rsi     # 0x8d661
callq 0x66a0
movq %rax, (%rbp,%rbx,8)
incq %rbx
movslq 0xb5f66(%rip), %rax     # 0xbf760
cmpq %rax, %rbx
jl 0x973a
movl 0xc(%rsp), %eax
movq 0x38(%rsp), %rdi
movq 0x10(%rsp), %r12
leal 0x1(%rax), %ecx
movl %ecx, 0xc(%rsp)
testl %eax, %eax
jle 0x94ca
movq 0x30(%rsp), %rdi
callq 0x68a0
movq %r13, %rdi
callq 0x68a0
movq 0x40(%rsp), %rdi
callq 0x68a0
movq %r12, %rdi
callq 0x68a0
jmp 0x9852
leaq 0x8400b(%rip), %rdi     # 0x8d858
callq 0x6bb0
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq