// File: /workspace/github_c_makefile_star2_O1/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 $0xc8, %rsp
movsd %xmm0, 0x88(%rsp)
movl %ecx, 0x24(%rsp)
cmpl $0x0, (%rdi)
je 0xd0d0
movl %edx, 0x20(%rsp)
movq %rsi, %rbx
movq 0x8(%rsi), %r15
movl %r15d, %r14d
testl %r8d, %r8d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovnel %r8d, %eax
movl %eax, 0xc(%rsp)
movl 0x18(%rdi), %r13d
movq %rdi, %rbp
movl $0x5, %esi
callq 0x1637d
movq %r14, 0x30(%rsp)
leaq (,%r14,4), %rdi
callq 0x32f0
movq %rax, 0x38(%rsp)
movq %r15, 0x40(%rsp)
testl %r15d, %r15d
je 0xcb7a
movq 0x30(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %r14
xorl %r15d, %r15d
movq 0x38(%rsp), %r12
movq 0x10(%rbx), %rax
movq (%rax,%r15), %rsi
movq %rbp, %rdi
callq 0x161b5
movl %eax, (%r12)
addq $0x18, %r15
addq $0x4, %r12
cmpq %r15, %r14
jne 0xcb59
movq %rbp, 0x80(%rsp)
leal (,%r13,4), %edi
shlq $0x3, %rdi
movq %rdi, 0x98(%rsp)
callq 0x32f0
movq %rax, %r12
movl $0x1, %edi
movl $0x18, %esi
callq 0x3240
movq %rax, %r14
movl $0x40, %esi
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
callq 0x3240
movq %rax, 0x10(%r14)
movq %rbp, (%r14)
movq %rbp, 0x8(%r14)
cmpl $0x0, 0x40(%rsp)
movl 0xc(%rsp), %r15d
movq %r14, 0x10(%rsp)
je 0xd09b
cmpl %r13d, %r15d
cmovael %r13d, %r15d
cvtsi2sdl 0x100(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
cmpl $0x1, %r15d
movl %r15d, %eax
adcl $0x0, %eax
movq %rax, 0x40(%rsp)
xorl %edx, %edx
movq %rbx, 0x78(%rsp)
movl %r15d, 0xc(%rsp)
movq 0x10(%r14), %rax
movq %rdx, %rsi
shlq $0x6, %rsi
movl $0x0, (%rax,%rsi)
movq 0x20(%rbx), %rax
movq 0x10(%rax), %rax
leaq (%rdx,%rdx,4), %rcx
testb $0x40, 0x23(%rax,%rcx,8)
jne 0xd03a
movq %rsi, 0xb8(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq %rdx, %rbx
movq 0x98(%rsp), %rdx
callq 0x31b0
movq 0x38(%rsp), %rax
movq %rbx, 0x18(%rsp)
movl (%rax,%rbx,4), %esi
movq 0x80(%rsp), %rdi
movl %esi, 0x28(%rsp)
leaq 0x8(%rsp), %rdx
callq 0x165d9
testq %rax, %rax
movl 0x20(%rsp), %r15d
je 0xcd03
cmpl $0x0, 0x8(%rsp)
je 0xcd03
movq %rax, %rbx
xorl %r14d, %r14d
movsd 0x30(%rbx), %xmm0
ucomisd 0x88(%rsp), %xmm0
ja 0xccf3
testl %r15d, %r15d
je 0xccb7
movq %rbx, %rdi
callq 0x16c6a
testl %eax, %eax
jne 0xccf3
cmpl $0x0, 0x24(%rsp)
je 0xccca
movq %rbx, %rdi
callq 0x16c8a
testl %eax, %eax
jne 0xccf3
movl 0x48(%rbx), %eax
movl %eax, %ecx
shrl $0x1d, %ecx
andl $0x3, %ecx
imull %r13d, %ecx
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
addl %ecx, %eax
movsd 0x38(%rbx), %xmm0
ucomisd (%r12,%rax,8), %xmm0
jbe 0xccf3
movsd %xmm0, (%r12,%rax,8)
incq %r14
movl 0x8(%rsp), %eax
addq $0x50, %rbx
cmpq %rax, %r14
jb 0xcc96
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movl 0xc(%rsp), %r15d
movl %r13d, %eax
imull %r14d, %eax
leaq (%r12,%rax,8), %rdi
movl $0x8, %edx
movq %r13, %rsi
leaq -0x28b(%rip), %rcx      # 0xcaac
callq 0x30c0
movl %r15d, 0x8(%rsp)
testq %r13, %r13
je 0xcd6a
movsd 0x50(%rsp,%r14,8), %xmm0
movq 0x40(%rsp), %rax
movl %ebx, %ecx
movl %ecx, %edx
addsd (%r12,%rdx,8), %xmm0
incl %ecx
decq %rax
jne 0xcd54
movsd %xmm0, 0x50(%rsp,%r14,8)
incq %r14
addl %r13d, %ebx
cmpq $0x4, %r14
jne 0xcd1d
xorl %eax, %eax
movsd 0xc7a8(%rip), %xmm0     # 0x19528
movapd %xmm0, %xmm1
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x78(%rsp), %rbx
movq 0x10(%rsp), %r14
movl %edx, %esi
movsd 0x50(%rsp,%rax,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0xcdaa
movl %eax, %edx
movl %esi, %ecx
movapd %xmm3, %xmm2
jmp 0xcdc6
movl %esi, %edx
ucomisd %xmm1, %xmm3
jbe 0xcdbe
movl %eax, %ecx
movapd %xmm0, %xmm2
movapd %xmm3, %xmm0
jmp 0xcdc6
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
incq %rax
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
cmpq $0x4, %rax
jne 0xcd92
movl %edx, %eax
movsd 0x50(%rsp,%rax,8), %xmm0
xorl %r9d, %r9d
ucomisd 0x90(%rsp), %xmm0
movl %ecx, %ecx
jb 0xce03
ucomisd 0x50(%rsp,%rcx,8), %xmm0
movl $0x0, %r9d
cmovnel %edx, %r9d
cmovpl %edx, %r9d
testl %r9d, %r9d
je 0xd02b
movq %rcx, 0xa8(%rsp)
movq %rax, 0xb0(%rsp)
movl %r9d, 0x4c(%rsp)
movl $0x4, %edi
callq 0x32f0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0xa0(%rsp)
movl %eax, (%rcx)
movq 0x80(%rsp), %rdi
movl 0x28(%rsp), %esi
leaq 0x8(%rsp), %rdx
callq 0x165d9
testq %rax, %rax
je 0xcf42
cmpl $0x0, 0x8(%rsp)
movl $0x0, %ebx
movl $0x0, %r14d
je 0xcf47
movq %rax, %rbp
xorl %r15d, %r15d
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
xorl %ebx, %ebx
movsd 0x30(%rbp), %xmm0
ucomisd 0x88(%rsp), %xmm0
ja 0xcf2c
cmpl $0x0, 0x20(%rsp)
je 0xcead
movq %rbp, %rdi
callq 0x16c6a
testl %eax, %eax
jne 0xcf2c
cmpl $0x0, 0x24(%rsp)
je 0xcec0
movq %rbp, %rdi
callq 0x16c8a
testl %eax, %eax
jne 0xcf2c
movq 0x28(%rsp), %rax
cmpq %rax, %r14
jne 0xcf02
movq %r14, 0xc0(%rsp)
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %rbx, %rdi
callq 0x3330
movq %rax, %rbx
movq %r14, 0x28(%rsp)
movq 0xc0(%rsp), %r14
movl 0x48(%rbp), %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(%rbp), %rcx
movl %ecx, %ecx
orq %rax, %rcx
movq %rcx, (%rbx,%r14,8)
incq %r14
incq %r15
movl 0x8(%rsp), %eax
addq $0x50, %rbp
cmpq %rax, %r15
jb 0xce86
jmp 0xcf47
xorl %ebx, %ebx
xorl %r14d, %r14d
movl $0x8, %edx
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x192(%rip), %rcx       # 0xd0eb
callq 0x30c0
testq %r14, %r14
je 0xcf93
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %ebp, %ebp
movq (%rbx,%rax,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpl %ecx, %esi
je 0xcf87
movl %ebp, %ecx
incl %ebp
movq %rdx, (%rbx,%rcx,8)
movl 0x4(%rbx,%rax,8), %ecx
incq %rax
movl %eax, %edx
cmpq %rdx, %r14
ja 0xcf6c
jmp 0xcf95
xorl %ebp, %ebp
movl %ebp, %esi
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x3330
movq 0x10(%rsp), %r14
movq 0x10(%r14), %rcx
movq 0xb8(%rsp), %rdi
movl 0x4c(%rsp), %r9d
movl %r9d, (%rcx,%rdi)
movq 0xb0(%rsp), %rdx
movsd 0x50(%rsp,%rdx,8), %xmm0
movsd %xmm0, 0x8(%rcx,%rdi)
movq 0xa8(%rsp), %rdx
movsd 0x50(%rsp,%rdx,8), %xmm0
movsd %xmm0, 0x10(%rcx,%rdi)
movq 0x78(%rsp), %rbx
movq 0x10(%rbx), %rdx
movq 0x18(%rsp), %rsi
leaq (%rsi,%rsi,2), %rsi
movl 0x10(%rdx,%rsi,8), %edx
movl %edx, 0x18(%rcx,%rdi)
movl $0x1, 0x1c(%rcx,%rdi)
movq 0xa0(%rsp), %rdx
movq %rdx, 0x20(%rcx,%rdi)
movl %ebp, 0x28(%rcx,%rdi)
movq %rax, 0x30(%rcx,%rdi)
movq $0x0, 0x38(%rcx,%rdi)
movq 0x30(%rsp), %rbp
cmpl $0x0, 0x110(%rsp)
movq 0x18(%rsp), %rdx
jg 0xd048
incq %rdx
cmpq %rbp, %rdx
jne 0xcc16
jmp 0xd09b
movq 0x11fa9(%rip), %rax     # 0x1eff8
movq (%rax), %rdi
movq 0x10(%rbx), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,2), %rdx
movq (%rax,%rdx,8), %rcx
movl 0x10(%rax,%rdx,8), %r8d
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
leaq 0xccbd(%rip), %rsi      # 0x19d3e
leaq 0xcd1f(%rip), %rdx      # 0x19da7
movb $0x3, %al
callq 0x3290
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r14
jmp 0xd03a
movq %r12, %rdi
callq 0x3060
movq 0x38(%rsp), %rdi
callq 0x3060
cmpl $0x0, 0x108(%rsp)
je 0xd0d4
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl 0x110(%rsp), %esi
callq 0xd0fc
movq %rbx, %rax
jmp 0xd0d9
xorl %eax, %eax
jmp 0xd0d9
movq 0x10(%rsp), %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq