// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/path.c
// Function: get_sequence_annot_score
double *get_sequence_annot_score(hmm_annot_db_t *annot_db, asg_t *asg, int no_trn, int no_rrn, double max_eval,
        int n_core, int verbose)
{
    uint32_t i, j, k, n, n_seg, n_gene, m_gene, gid;
    uint32_t *asg_snameid_map;
    hmm_annot_t *annots, *annot;
    double *annot_score, *gene_score, *a_s;

    n_gene = annot_db->n;
    n_seg = asg->n_seg;

    if (n_gene == 0) return 0;
    if (n_core == 0) n_core = INT32_MAX;
    
    m_gene = annot_db->n_gene;

    // sort annotations by sid - og_type - gid - score
    hmm_annot_db_sort(annot_db, ORDER_SID_OG);
    // build asg sname to annot_db sname id map
    MYMALLOC(asg_snameid_map, n_seg);
    for (i = 0; i < n_seg; ++i)
        asg_snameid_map[i] = hmm_annot_sname2id(annot_db, asg->seg[i].name);

    // calculate annotation score for each seg
    MYCALLOC(annot_score, n_seg * 4);
    MYMALLOC(gene_score, m_gene * 4);
    for (i = 0; i < n_seg; ++i) {
        annots = hmm_annot_db_index_query(annot_db, asg_snameid_map[i], &n);
        if (!annots) continue;
        
        MYBZERO(gene_score, m_gene * 4);
        for (j = 0; j < n; ++j) {
            annot = &annots[j];
            if (annot->evalue > max_eval ||
                    (no_trn && is_trn(annot)) ||
                    (no_rrn && is_rrn(annot)))
                continue;
            gid = annot->og_type * m_gene + annot->gid;
            if (gene_score[gid] < annot->score)
                gene_score[gid] = annot->score;
        }

        // sort annotation scores for each og_type
        // and get the sum annotation score
        a_s = &annot_score[i * 4];
        for (j = 0; j < 4; ++j) {
            qsort(&gene_score[j * m_gene], m_gene, sizeof(double), dbl_cmpfunc_r);
            n = MIN((uint32_t) n_core, m_gene);
            for (k = 0 ; k < n; ++k)
                a_s[j] += gene_score[j * m_gene + k];
        }
        
        if (verbose > 0)
            fprintf(stderr, "[M::%s] sequence %s: size, %d; mito score, %.3f; pltd score, %.3f; mini score, %.3f\n",
                    __func__, asg->seg[i].name, asg->seg[i].len, a_s[OG_MITO], a_s[OG_PLTD], a_s[OG_MINI]);
    }
    
    free(gene_score);
    free(asg_snameid_map);

    return annot_score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, 0x40(%rsp)
movsd %xmm0, 0x60(%rsp)
movl %edx, 0x44(%rsp)
cmpl $0x0, (%rdi)
je 0xcebd
movq %rdi, %r15
movl %ecx, 0x3c(%rsp)
movq 0x8(%rsi), %r12
testl %r8d, %r8d
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmovnel %r8d, %r14d
movl 0x18(%rdi), %r13d
movq %rsi, %rbx
movl $0x5, %esi
callq 0x17c65
leaq (,%r12,4), %rax
movabsq $0x3fffffffc, %rdi      # imm = 0x3FFFFFFFC
movq %rax, 0x10(%rsp)
andq %rax, %rdi
callq 0x32f0
movq %rax, 0x28(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r12, 0x20(%rsp)
andq %r12, %rax
movq %rax, 0x18(%rsp)
movq %r15, 0x30(%rsp)
je 0xcce2
movq 0x18(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rbp
xorl %r12d, %r12d
movq 0x28(%rsp), %r15
movq 0x10(%rbx), %rax
movq (%rax,%r12), %rsi
movq 0x30(%rsp), %rdi
callq 0x17aaa
movl %eax, (%r15)
addq $0x18, %r12
addq $0x4, %r15
cmpq %r12, %rbp
jne 0xccc0
movq %rbx, 0x48(%rsp)
movq 0x10(%rsp), %rdi
andl $-0x4, %edi
movl $0x8, %esi
callq 0x3240
movq %rax, 0x50(%rsp)
leal (,%r13,4), %edi
shlq $0x3, %rdi
movq %rdi, 0x58(%rsp)
callq 0x32f0
movq %rax, %rbx
cmpq $0x0, 0x18(%rsp)
movq 0x30(%rsp), %r15
je 0xcea4
cmpl %r13d, %r14d
cmovael %r13d, %r14d
addq $0x10, 0x48(%rsp)
cmpl $0x1, %r14d
movl %r14d, %eax
adcl $0x0, %eax
movq %rax, 0x18(%rsp)
movl 0x20(%rsp), %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movq 0x28(%rsp), %rax
movl (%rax,%r12,4), %esi
movq %r15, %rdi
leaq 0xc(%rsp), %rdx
callq 0x17ec6
testq %rax, %rax
je 0xce82
movq %rax, %rbp
movq %r12, 0x10(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq 0x58(%rsp), %rdx
callq 0x31b0
cmpl $0x0, 0xc(%rsp)
movl 0x3c(%rsp), %r15d
je 0xcdfd
xorl %r12d, %r12d
movsd 0x30(%rbp), %xmm0
ucomisd 0x60(%rsp), %xmm0
ja 0xcded
cmpl $0x0, 0x44(%rsp)
je 0xcdb5
movq %rbp, %rdi
callq 0x18562
testl %eax, %eax
jne 0xcded
testl %r15d, %r15d
je 0xcdc6
movq %rbp, %rdi
callq 0x1857c
testl %eax, %eax
jne 0xcded
movl 0x48(%rbp), %eax
movl %eax, %ecx
shrl $0x1d, %ecx
andl $0x3, %ecx
imull %r13d, %ecx
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
addl %ecx, %eax
movsd 0x38(%rbp), %xmm0
ucomisd (%rbx,%rax,8), %xmm0
jbe 0xcded
movsd %xmm0, (%rbx,%rax,8)
incq %r12
movl 0xc(%rsp), %eax
addq $0x50, %rbp
cmpq %rax, %r12
jb 0xcd95
movq 0x10(%rsp), %rax
leal (,%rax,4), %eax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %r13d, %eax
imull %r15d, %eax
leaq (%rbx,%rax,8), %rdi
movl $0x8, %edx
movq %r13, %rsi
leaq 0x9c(%rip), %rcx        # 0xcece
callq 0x30c0
movl %r14d, 0xc(%rsp)
testq %r13, %r13
je 0xce65
movsd (%rbp,%r15,8), %xmm0
movq 0x18(%rsp), %rax
movl %r12d, %ecx
movl %ecx, %edx
addsd (%rbx,%rdx,8), %xmm0
incl %ecx
decq %rax
jne 0xce50
movsd %xmm0, (%rbp,%r15,8)
incq %r15
addl %r13d, %r12d
cmpq $0x4, %r15
jne 0xce18
cmpl $0x0, 0x40(%rsp)
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %r12
jg 0xce92
incq %r12
cmpq 0x20(%rsp), %r12
jne 0xcd50
jmp 0xcea4
movq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x38db
jmp 0xce82
movq %rbx, %rdi
callq 0x3060
movq 0x28(%rsp), %rdi
callq 0x3060
movq 0x50(%rsp), %rax
jmp 0xcebf
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq