// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]oatk/path.c
// Function: annot_sequence_og_type
og_component_v *annot_sequence_og_type(hmm_annot_db_t *annot_db, asg_t *asg, int no_trn, int no_rrn, double max_eval,
        int n_core, int min_len, int min_score, int fix_og, int verbose)
{
    uint32_t i, j, k, n, n_seg, n_gene, m_gene, gid, imax, smax;
    uint32_t *asg_snameid_map;
    hmm_annot_t *annots, *annot;
    double a_s[4], *annot_score;
    OG_TYPE_t og_t;
    og_component_t *component;
    og_component_v *component_v;

    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);

    MYMALLOC(annot_score, m_gene * 4);
    MYCALLOC(component_v, 1);
    MYCALLOC(component_v->a, n_seg);
    component_v->n = n_seg;
    component_v->m = n_seg;
    for (i = 0; i < n_seg; ++i) {
        component_v->a[i].type = (uint32_t) OG_UNCLASSIFIED;
        if (asg->asmg->vtx[i].del)
            continue;

        MYBZERO(annot_score, m_gene * 4);
        annots = hmm_annot_db_index_query(annot_db, asg_snameid_map[i], &n);
        if (annots) {
            for (k = 0; k < n; ++k) {
                annot = &annots[k];
                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 (annot_score[gid] < annot->score)
                    annot_score[gid] = annot->score;
            }
        }

        // sort annotation scores for each og_type
        // and get the sum annotation score
        MYBZERO(a_s, 4);
        for (j = 0; j < 4; ++j) {
            qsort(&annot_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] += annot_score[j * m_gene + k];
        }
        imax = max2(a_s, 4, &smax);
        og_t = OG_UNCLASSIFIED;
        if (a_s[imax] >= min_score)
            // 1, mito; 2, pltd; 3, mini
            og_t = a_s[imax] == a_s[smax]? OG_UNCLASSIFIED : imax;

        if (og_t != OG_UNCLASSIFIED) {
            // make seg list
            uint32_t *comp_v;
            MYMALLOC(comp_v, 1);
            comp_v[0] = i;
            // make gene list
            // get all genes in the component
            kvec_t(uint64_t) comp_g;
            kv_init(comp_g);
            annots = hmm_annot_db_index_query(annot_db, asg_snameid_map[i], &n);
            if (annots) {
                for (k = 0; k < n; ++k) {
                    annot = &annots[k];
                    if (annot->evalue > max_eval ||
                            (no_trn && is_trn(annot)) ||
                            (no_rrn && is_rrn(annot)))
                        continue;
                    // gid:30 | OG_TYPE:2 | score:32
                    kv_push(uint64_t, comp_g,
                            ((uint64_t) annot->gid << 2 | annot->og_type) << 32 |
                            (uint32_t) annot->score);
                }
            }
            // sort by gid and then score - descending order
            qsort(comp_g.a, comp_g.n, sizeof(uint64_t), u64_cmpfunc_r);
            // keep only the best hit for each gene
            k = 0;
            gid = UINT32_MAX;
            for (j = 0; j < comp_g.n; ++j) {
                if (comp_g.a[j] >> 32 != gid) {
                    comp_g.a[k++] = comp_g.a[j];
                    gid = comp_g.a[j] >> 32;
                }
            }
            comp_g.n = comp_g.m = k;
            MYREALLOC(comp_g.a, k);

            // add og_component
            component = &component_v->a[i];
            component->type = (uint32_t) og_t;
            component->score = a_s[imax];
            component->sscore = a_s[smax];
            component->len = asg->seg[i].len;
            component->nv = 1;
            component->v = comp_v;
            component->ng = comp_g.n;
            component->g = comp_g.a;
            component->asmg = 0;
        }

        if (verbose > 0)
            fprintf(stderr, "[M::%s] sequence %s: size, %d; mito score, %.3f; pltd score, %.3f; mini score, %.3f; classification, %d\n",
                    __func__, asg->seg[i].name, asg->seg[i].len, a_s[OG_MITO], a_s[OG_PLTD], a_s[OG_MINI], og_t);
    }

    free(annot_score);
    free(asg_snameid_map);
    
    if (fix_og) fix_og_misclassification(component_v, verbose);

    return component_v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm0, 0x88(%rsp)
movl %ecx, 0x1c(%rsp)
movl %edx, 0x20(%rsp)
cmpl $0x0, (%rdi)
je 0xb7e1
movl %r8d, %ebp
movq %rsi, 0x10(%rsp)
movl 0x8(%rsi), %r15d
movl 0x18(%rdi), %ebx
pushq $0x5
popq %rsi
movq %rdi, 0x40(%rsp)
callq 0x1387d
leaq (,%r15,4), %rdi
callq 0x32f0
movq %r15, 0x80(%rsp)
imulq $0x6, %r15, %r15
xorl %r12d, %r12d
movq %rax, 0x70(%rsp)
movq %rax, %r13
cmpq %r12, %r15
je 0xb317
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r12,4), %rsi
movq 0x40(%rsp), %rdi
callq 0x1377d
movl %eax, (%r13)
addq $0x6, %r12
addq $0x4, %r13
jmp 0xb2ed
testl %ebp, %ebp
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmovnel %ebp, %r12d
leal (,%rbx,4), %edi
shlq $0x3, %rdi
movq %rdi, 0xa8(%rsp)
callq 0x32f0
movq %rax, %r13
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x3240
movq %rax, %r14
pushq $0x40
popq %rsi
movq 0x80(%rsp), %r15
movq %r15, %rdi
callq 0x3240
movq %rax, %rbp
movq %rax, 0x10(%r14)
movq %r15, (%r14)
movq %r14, 0x30(%rsp)
movq %r15, 0x8(%r14)
cmpl %ebx, %r12d
cmovael %ebx, %r12d
cvtsi2sdl 0xf0(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
xorl %r14d, %r14d
movq 0x10(%rsp), %r15
cmpq 0x80(%rsp), %r14
je 0xb7af
movq %r14, %rdx
shlq $0x6, %rdx
andl $0x0, (%rbp,%rdx)
movq 0x20(%r15), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r14, %rcx
testb $0x40, 0x23(%rax,%rcx)
jne 0xb763
movq %rdx, 0xb0(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq 0xa8(%rsp), %rdx
callq 0x31b0
movq 0x70(%rsp), %rax
movq %r14, 0x38(%rsp)
movl (%rax,%r14,4), %esi
movq 0x40(%rsp), %rdi
movl %esi, 0x28(%rsp)
leaq 0xc(%rsp), %rdx
callq 0x13ac4
testq %rax, %rax
je 0xb47f
movq %rax, %r15
xorl %r14d, %r14d
movl 0xc(%rsp), %eax
cmpq %rax, %r14
jae 0xb47f
movsd 0x30(%r15), %xmm0
ucomisd 0x88(%rsp), %xmm0
ja 0xb476
cmpl $0x0, 0x20(%rsp)
je 0xb437
movq %r15, %rdi
callq 0x14057
testl %eax, %eax
jne 0xb476
cmpl $0x0, 0x1c(%rsp)
je 0xb44a
movq %r15, %rdi
callq 0x14071
testl %eax, %eax
jne 0xb476
movl 0x48(%r15), %eax
movl %eax, %ecx
shrl $0x1d, %ecx
andl $0x3, %ecx
imull %ebx, %ecx
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
addl %ecx, %eax
movsd 0x38(%r15), %xmm0
ucomisd (%r13,%rax,8), %xmm0
jbe 0xb476
movsd %xmm0, (%r13,%rax,8)
incq %r14
addq $0x50, %r15
jmp 0xb40a
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0xb4f2
movl %r15d, %eax
imull %ebx, %eax
leaq (,%rax,8), %rdi
addq %r13, %rdi
movq %rbx, %rsi
pushq $0x8
popq %rdx
leaq -0x254(%rip), %rcx      # 0xb265
callq 0x30c0
movl %r12d, 0xc(%rsp)
movl %r14d, %eax
movq %r12, %rcx
subq $0x1, %rcx
jb 0xb4ea
movl %eax, %edx
movsd (%r13,%rdx,8), %xmm0
addsd 0x50(%rsp,%r15,8), %xmm0
movsd %xmm0, 0x50(%rsp,%r15,8)
incl %eax
jmp 0xb4c9
incq %r15
addl %ebx, %r14d
jmp 0xb495
leaq 0x50(%rsp), %rdi
leaq 0x24(%rsp), %rsi
callq 0xb7ff
movl %eax, %edx
movsd 0x50(%rsp,%rdx,8), %xmm0
xorl %r9d, %r9d
ucomisd 0xa0(%rsp), %xmm0
jae 0xb526
movq 0x10(%rsp), %r15
movq 0x38(%rsp), %r14
jmp 0xb759
testl %eax, %eax
movq 0x10(%rsp), %r15
movq 0x38(%rsp), %r14
je 0xb759
movl 0x24(%rsp), %ecx
ucomisd 0x50(%rsp,%rcx,8), %xmm0
jne 0xb54a
jnp 0xb759
movq %rdx, 0x98(%rsp)
movl %eax, 0x4c(%rsp)
pushq $0x4
popq %rdi
callq 0x32f0
movq %rax, 0x90(%rsp)
movl %r14d, (%rax)
movq 0x40(%rsp), %rdi
movl 0x28(%rsp), %esi
leaq 0xc(%rsp), %rdx
callq 0x13ac4
testq %rax, %rax
je 0xb67a
movq %rax, %r15
xorl %r14d, %r14d
xorl %ebp, %ebp
movq $0x0, 0x78(%rsp)
xorl %r8d, %r8d
movl 0xc(%rsp), %eax
cmpq %rax, %r14
jae 0xb67f
movsd 0x30(%r15), %xmm0
ucomisd 0x88(%rsp), %xmm0
ja 0xb66e
cmpl $0x0, 0x20(%rsp)
movq %r14, 0x28(%rsp)
je 0xb5e8
movq %r15, %rdi
movq %rbp, %r14
movq %r8, %rbp
callq 0x14057
movq %rbp, %r8
movq %r14, %rbp
movq 0x28(%rsp), %r14
testl %eax, %eax
jne 0xb66e
cmpl $0x0, 0x1c(%rsp)
je 0xb60c
movq %r15, %rdi
movq %rbp, %r14
movq %r8, %rbp
callq 0x14071
movq %rbp, %r8
movq %r14, %rbp
movq 0x28(%rsp), %r14
testl %eax, %eax
jne 0xb66e
movq 0x78(%rsp), %rax
cmpq %rax, %r8
jne 0xb642
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x78(%rsp)
leaq (,%rcx,8), %rsi
movq %rbp, %rdi
movq %r8, %rbp
callq 0x3330
movq %rbp, %r8
movq %rax, %rbp
movl 0x48(%r15), %eax
movl %eax, %ecx
andl $0x1fffffff, %ecx       # imm = 0x1FFFFFFF
shrl $0x1d, %eax
andl $0x3, %eax
leal (%rax,%rcx,4), %eax
shlq $0x20, %rax
cvttsd2si 0x38(%r15), %rcx
movl %ecx, %ecx
orq %rax, %rcx
movq %rcx, (%rbp,%r8,8)
incq %r8
incq %r14
addq $0x50, %r15
jmp 0xb599
xorl %ebp, %ebp
xorl %r8d, %r8d
movq %rbp, %rdi
movq %r8, %rsi
pushq $0x8
popq %rdx
leaq 0x1d5(%rip), %rcx       # 0xb864
movq %r8, %r14
callq 0x30c0
movq %r14, %r8
xorl %r14d, %r14d
pushq $-0x1
popq %rcx
xorl %eax, %eax
movq 0x10(%rsp), %r15
movl %eax, %eax
movl %r14d, %esi
cmpq %rax, %r8
jbe 0xb6d1
movq (%rbp,%rax,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
cmpl %ecx, %edi
je 0xb6cd
incl %r14d
movq %rdx, (%rbp,%rsi,8)
movl 0x4(%rbp,%rax,8), %ecx
incl %eax
jmp 0xb6a7
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x3330
movq 0x30(%rsp), %rcx
movq 0x10(%rcx), %rbp
movq 0xb0(%rsp), %rdi
movl 0x4c(%rsp), %r9d
movl %r9d, (%rbp,%rdi)
movq 0x98(%rsp), %rcx
movsd 0x50(%rsp,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rbp,%rdi)
movl 0x24(%rsp), %ecx
movsd 0x50(%rsp,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rbp,%rdi)
movq 0x10(%r15), %rcx
movq 0x38(%rsp), %rsi
imulq $0x18, %rsi, %rdx
movl 0x10(%rcx,%rdx), %ecx
movl %ecx, 0x18(%rbp,%rdi)
movl $0x1, 0x1c(%rbp,%rdi)
movq 0x90(%rsp), %rcx
movq %rcx, 0x20(%rbp,%rdi)
movl %r14d, 0x28(%rbp,%rdi)
movq %rsi, %r14
movq %rax, 0x30(%rbp,%rdi)
andq $0x0, 0x38(%rbp,%rdi)
cmpl $0x0, 0x100(%rsp)
jg 0xb76b
incq %r14
jmp 0xb393
movq 0x11886(%rip), %rax     # 0x1cff8
movq (%rax), %rdi
movq 0x10(%r15), %rax
imulq $0x18, %r14, %rdx
movq (%rax,%rdx), %rcx
movl 0x10(%rax,%rdx), %r8d
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
leaq 0xb5bf(%rip), %rsi      # 0x16d5e
leaq 0xb621(%rip), %rdx      # 0x16dc7
movb $0x3, %al
callq 0x3290
jmp 0xb763
movq %r13, %rdi
callq 0x3060
movq 0x70(%rsp), %rdi
callq 0x3060
cmpl $0x0, 0xf8(%rsp)
je 0xb7e5
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movl 0x100(%rsp), %esi
callq 0xb875
jmp 0xb7ea
xorl %ebx, %ebx
jmp 0xb7ea
movq 0x30(%rsp), %rbx
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq