// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movl %r9d, 0x20(%rsp)
movsd %xmm0, 0x50(%rsp)
movl %edx, 0x24(%rsp)
cmpl $0x0, (%rdi)
je 0xb254
movl %r8d, %ebx
movl %ecx, 0x8(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x8(%rsi), %r14
movl 0x18(%rdi), %r13d
pushq $0x5
popq %rsi
movq %rdi, 0x10(%rsp)
callq 0x1387d
leaq (,%r14,4), %r12
movabsq $0x3fffffffc, %rdi      # imm = 0x3FFFFFFFC
andq %r12, %rdi
callq 0x32f0
movl %r14d, %ecx
movq %rcx, 0x48(%rsp)
imulq $0x6, %rcx, %r14
xorl %r15d, %r15d
movq %rax, 0x30(%rsp)
movq %rax, %rbp
cmpq %r15, %r14
je 0xb06c
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r15,4), %rsi
movq 0x10(%rsp), %rdi
callq 0x1377d
movl %eax, (%rbp)
addq $0x6, %r15
addq $0x4, %rbp
jmp 0xb043
testl %ebx, %ebx
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
cmovnel %ebx, %r14d
andl $-0x4, %r12d
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x3240
movq %rax, 0x28(%rsp)
leal (,%r13,4), %edi
shlq $0x3, %rdi
movq %rdi, 0x40(%rsp)
callq 0x32f0
movq %rax, %rbx
cmpl %r13d, %r14d
cmovael %r13d, %r14d
xorl %ecx, %ecx
movl 0x8(%rsp), %r12d
movq 0x10(%rsp), %r15
cmpq 0x48(%rsp), %rcx
je 0xb23b
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rsp)
movl (%rax,%rcx,4), %esi
movq %r15, %rdi
leaq 0xc(%rsp), %rdx
callq 0x13ac4
testq %rax, %rax
je 0xb1e6
movq %rax, %rbp
movq %rbx, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x31b0
xorl %r15d, %r15d
movl 0xc(%rsp), %eax
cmpq %rax, %r15
jae 0xb165
movsd 0x30(%rbp), %xmm0
ucomisd 0x50(%rsp), %xmm0
ja 0xb15c
cmpl $0x0, 0x24(%rsp)
je 0xb124
movq %rbp, %rdi
callq 0x14057
testl %eax, %eax
jne 0xb15c
testl %r12d, %r12d
je 0xb135
movq %rbp, %rdi
callq 0x14071
testl %eax, %eax
jne 0xb15c
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 0xb15c
movsd %xmm0, (%rbx,%rax,8)
incq %r15
addq $0x50, %rbp
jmp 0xb0fb
movq 0x18(%rsp), %rax
leal (,%rax,4), %eax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq $0x4, %r12
je 0xb1d5
movl %r12d, %eax
imull %r13d, %eax
leaq (%rbx,%rax,8), %rdi
movq %r13, %rsi
pushq $0x8
popq %rdx
leaq 0xc7(%rip), %rcx        # 0xb265
callq 0x30c0
movl %r14d, 0xc(%rsp)
movl %r15d, %eax
movq %r14, %rcx
subq $0x1, %rcx
jb 0xb1cd
movl %eax, %edx
movsd (%rbx,%rdx,8), %xmm0
addsd (%rbp,%r12,8), %xmm0
movsd %xmm0, (%rbp,%r12,8)
incl %eax
jmp 0xb1ae
incq %r12
addl %r13d, %r15d
jmp 0xb180
cmpl $0x0, 0x20(%rsp)
movl 0x8(%rsp), %r12d
movq 0x10(%rsp), %r15
jg 0xb1f3
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0xb0b8
movq 0x11dfe(%rip), %rax     # 0x1cff8
movq (%rax), %rdi
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x18, 0x18(%rsp), %rdx
movq (%rax,%rdx), %rcx
movl 0x10(%rax,%rdx), %r8d
movsd 0x8(%rbp), %xmm0
movsd 0x10(%rbp), %xmm1
movsd 0x18(%rbp), %xmm2
leaq 0xbac5(%rip), %rsi      # 0x16cf0
leaq 0xbb13(%rip), %rdx      # 0x16d45
movb $0x3, %al
callq 0x3290
jmp 0xb1e6
movq %rbx, %rdi
callq 0x3060
movq 0x30(%rsp), %rdi
callq 0x3060
movq 0x28(%rsp), %rax
jmp 0xb256
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq