// File: /workspace/github_c_makefile_star2_O1/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, 0x38(%rsp)
movsd %xmm0, 0x60(%rsp)
movl %edx, 0x3c(%rsp)
cmpl $0x0, (%rdi)
je 0xca9b
movq %rdi, %r15
movl %ecx, 0x34(%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 0x1637d
leaq (,%r12,4), %rax
movabsq $0x3fffffffc, %rdi      # imm = 0x3FFFFFFFC
movq %rax, 0x28(%rsp)
andq %rax, %rdi
callq 0x32f0
movq %rax, 0x18(%rsp)
movq %r12, 0x10(%rsp)
testl %r12d, %r12d
movq %r15, 0x20(%rsp)
je 0xc8c1
movl 0x10(%rsp), %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r15
xorl %ebp, %ebp
movq 0x18(%rsp), %r12
movq 0x10(%rbx), %rax
movq (%rax,%rbp), %rsi
movq 0x20(%rsp), %rdi
callq 0x161b5
movl %eax, (%r12)
addq $0x18, %rbp
addq $0x4, %r12
cmpq %rbp, %r15
jne 0xc89e
movq %rbx, 0x40(%rsp)
movq 0x28(%rsp), %rdi
andl $-0x4, %edi
movl $0x8, %esi
callq 0x3240
movq %rax, 0x48(%rsp)
leal (,%r13,4), %edi
shlq $0x3, %rdi
movq %rdi, 0x50(%rsp)
callq 0x32f0
movq %rax, %rbx
movq 0x10(%rsp), %rcx
testl %ecx, %ecx
movq 0x20(%rsp), %r15
je 0xca82
cmpl %r13d, %r14d
cmovael %r13d, %r14d
addq $0x10, 0x40(%rsp)
cmpl $0x1, %r14d
movl %r14d, %eax
adcl $0x0, %eax
movq %rax, 0x28(%rsp)
movl %ecx, %eax
movq %rax, 0x58(%rsp)
xorl %r12d, %r12d
movq 0x18(%rsp), %rax
movl (%rax,%r12,4), %esi
movq %r15, %rdi
leaq 0xc(%rsp), %rdx
callq 0x165d9
testq %rax, %rax
je 0xca60
movq %rax, %rbp
movq %r12, 0x10(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq 0x50(%rsp), %rdx
callq 0x31b0
cmpl $0x0, 0xc(%rsp)
movl 0x34(%rsp), %r15d
je 0xc9db
xorl %r12d, %r12d
movsd 0x30(%rbp), %xmm0
ucomisd 0x60(%rsp), %xmm0
ja 0xc9cb
cmpl $0x0, 0x3c(%rsp)
je 0xc993
movq %rbp, %rdi
callq 0x16c6a
testl %eax, %eax
jne 0xc9cb
testl %r15d, %r15d
je 0xc9a4
movq %rbp, %rdi
callq 0x16c8a
testl %eax, %eax
jne 0xc9cb
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 0xc9cb
movsd %xmm0, (%rbx,%rax,8)
incq %r12
movl 0xc(%rsp), %eax
addq $0x50, %rbp
cmpq %rax, %r12
jb 0xc973
movq 0x10(%rsp), %rax
leal (,%rax,4), %eax
movq 0x48(%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        # 0xcaac
callq 0x30c0
movl %r14d, 0xc(%rsp)
testq %r13, %r13
je 0xca43
movsd (%rbp,%r15,8), %xmm0
movq 0x28(%rsp), %rax
movl %r12d, %ecx
movl %ecx, %edx
addsd (%rbx,%rdx,8), %xmm0
incl %ecx
decq %rax
jne 0xca2e
movsd %xmm0, (%rbp,%r15,8)
incq %r15
addl %r13d, %r12d
cmpq $0x4, %r15
jne 0xc9f6
cmpl $0x0, 0x38(%rsp)
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %r12
jg 0xca70
incq %r12
cmpq 0x58(%rsp), %r12
jne 0xc92e
jmp 0xca82
movq 0x40(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x3943
jmp 0xca60
movq %rbx, %rdi
callq 0x3060
movq 0x18(%rsp), %rdi
callq 0x3060
movq 0x48(%rsp), %rax
jmp 0xca9d
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq