// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]oatk/path.c
// Function: annot_subgraph_og_type
og_component_v *annot_subgraph_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;
    int len, nv;
    uint8_t *visited;
    OG_TYPE_t og_t;
    og_component_t *component;
    og_component_v *component_v;
    asmg_t *g;

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

    // extract each subgraph and do classification
    // TODO deal with graphs when mito and pltd are in the same subgraph
    MYMALLOC(annot_score, m_gene * 4);
    MYCALLOC(component_v, 1);
    MYCALLOC(visited, n_seg);
    for (i = 0; i < n_seg; ++i) {
        if (visited[i] || asg->asmg->vtx[i].del)
            continue;
        g = asg_make_asmg_copy(asg->asmg, 0);
        asmg_subgraph(g, &i, 1, 0, 0, 0, 1);
        MYBZERO(annot_score, m_gene * 4);
        len = nv = 0;
        for (j = 0; j < n_seg; ++j) {
            if (!g->vtx[j].del) {
                annots = hmm_annot_db_index_query(annot_db, asg_snameid_map[j], &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;
                    }
                }
                ++nv;
                len += g->vtx[j].len;
                visited[j] = 1;
            }
        }

        // 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 (len >= min_len || 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) {
            asmg_destroy(g);
            continue;
        }
        
        // make seg list
        uint32_t *comp_v;
        MYMALLOC(comp_v, nv);
        k = 0;
        for (j = 0; j < n_seg; ++j)
            if (!g->vtx[j].del)
                comp_v[k++] = j;
        // make gene list
        // get all genes in the component
        kvec_t(uint64_t) comp_g;
        kv_init(comp_g);
        for (j = 0; j < nv; ++j) {
            annots = hmm_annot_db_index_query(annot_db, asg_snameid_map[comp_v[j]], &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
        kv_pushp(og_component_t, *component_v, &component);
        component->type = (uint32_t) og_t;
        component->score = a_s[imax];
        component->sscore = a_s[smax];
        component->len = len;
        component->nv = nv;
        component->v = comp_v;
        component->ng = comp_g.n;
        component->g = comp_g.a;
        component->asmg = g;
        
        if (verbose > 0)
            fprintf(stderr, "[M::%s] subgraph seeding from %s: segs, %d; size, %d; mito score, %.3f; pltd score, %.3f; mini score, %.3f; classification, %d\n",
                    __func__, asg->seg[i].name, nv, len, a_s[OG_MITO], a_s[OG_PLTD], a_s[OG_MINI], og_t);
    }
    
    free(annot_score);
    free(visited);
    free(asg_snameid_map);

    if (fix_og) fix_og_misclassification(component_v, verbose);

    qsort(component_v->a, component_v->n, sizeof(og_component_t), og_cmpfunc_r);

    return component_v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %r9d, 0x5c(%rsp)
movsd %xmm0, 0xa0(%rsp)
movl %ecx, 0x30(%rsp)
movl %edx, 0x34(%rsp)
cmpl $0x0, (%rdi)
je 0xdbd1
movq %rsi, %rbx
movq %rdi, %r13
movq 0x8(%rsi), %r14
movl %r14d, %r12d
testl %r8d, %r8d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovnel %r8d, %eax
movl %eax, 0x24(%rsp)
movl 0x18(%rdi), %r15d
movl $0x5, %esi
callq 0x1637d
leaq (,%r12,4), %rdi
callq 0x32f0
movq %rax, %rbp
movl $0x0, 0x14(%rsp)
testl %r14d, %r14d
je 0xd51a
xorl %eax, %eax
movq 0x10(%rbx), %rcx
movl %eax, %eax
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rsi
movq %r13, %rdi
callq 0x161b5
movl 0x14(%rsp), %ecx
movl %eax, (%rbp,%rcx,4)
leal 0x1(%rcx), %eax
movl %eax, 0x14(%rsp)
cmpl %r14d, %eax
jb 0xd4f0
movq %r15, 0x40(%rsp)
leal (,%r15,4), %edi
shlq $0x3, %rdi
movq %rdi, 0xc0(%rsp)
callq 0x32f0
movq %rax, %r15
movl $0x1, %edi
movl $0x18, %esi
callq 0x3240
movq %rax, 0x38(%rsp)
movl $0x1, %esi
movq %r12, %rdi
callq 0x3240
movq %rax, 0x48(%rsp)
movl $0x0, 0x14(%rsp)
movq %r14, 0xc8(%rsp)
testl %r14d, %r14d
je 0xdb7c
movl 0x24(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl %ecx, %eax
cmovael %ecx, %eax
cvtsi2sdl 0x120(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
cmpl $0x1, %eax
movl %eax, 0x24(%rsp)
adcl $0x0, %eax
movq %rax, 0xd8(%rsp)
xorl %eax, %eax
movq %r13, 0x98(%rsp)
movq %r12, 0x90(%rsp)
movq %rbx, 0x80(%rsp)
movq %rbp, 0x88(%rsp)
movl %eax, %eax
movq 0x48(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0xdb1d
movq 0x20(%rbx), %rdi
movq 0x10(%rdi), %rcx
leaq (%rax,%rax,4), %rax
testb $0x40, 0x23(%rcx,%rax,8)
jne 0xdb1d
xorl %ebx, %ebx
xorl %esi, %esi
callq 0xad23
movl $0x1, (%rsp)
movq %rax, 0x28(%rsp)
movq %rax, %rdi
leaq 0x14(%rsp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1495a
movq %r15, %rdi
xorl %esi, %esi
movq 0xc0(%rsp), %rdx
callq 0x31b0
movl $0x0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%rbx,%rbx,4), %r14
testb $0x40, 0x23(%rax,%r14,8)
jne 0xd730
movl (%rbp,%rbx,4), %esi
movq %r13, %rdi
leaq 0x18(%rsp), %rdx
callq 0x165d9
testq %rax, %rax
je 0xd6fd
cmpl $0x0, 0x18(%rsp)
je 0xd6fd
movq %rax, %r12
xorl %r13d, %r13d
movsd 0x30(%r12), %xmm0
ucomisd 0xa0(%rsp), %xmm0
ja 0xd6ed
cmpl $0x0, 0x34(%rsp)
je 0xd6ac
movq %r12, %rdi
callq 0x16c6a
testl %eax, %eax
jne 0xd6ed
cmpl $0x0, 0x30(%rsp)
je 0xd6bf
movq %r12, %rdi
callq 0x16c8a
testl %eax, %eax
jne 0xd6ed
movl 0x48(%r12), %eax
movl %eax, %ecx
shrl $0x1d, %ecx
andl $0x3, %ecx
imull 0x40(%rsp), %ecx
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
addl %ecx, %eax
movsd 0x38(%r12), %xmm0
ucomisd (%r15,%rax,8), %xmm0
jbe 0xd6ed
movsd %xmm0, (%r15,%rax,8)
incq %r13
movl 0x18(%rsp), %eax
addq $0x50, %r12
cmpq %rax, %r13
jb 0xd687
incl 0x20(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x1c(%rsp), %ecx
addl 0x18(%rax,%r14,8), %ecx
movl %ecx, 0x1c(%rsp)
movq 0x48(%rsp), %rax
movb $0x1, (%rax,%rbx)
movq 0x98(%rsp), %r13
movq 0x90(%rsp), %r12
incq %rbx
cmpq %r12, %rbx
jne 0xd647
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x40(%rsp), %rbp
movl %r14d, %eax
imull %ebp, %eax
leaq (%r15,%rax,8), %rdi
movl $0x8, %edx
movq %rbp, %rsi
leaq -0xcc3(%rip), %rcx      # 0xcaac
callq 0x30c0
movl 0x24(%rsp), %eax
movl %eax, 0x18(%rsp)
testq %rbp, %rbp
je 0xd7a8
movsd 0x60(%rsp,%r14,8), %xmm0
movq 0xd8(%rsp), %rax
movl %ebx, %ecx
movl %ecx, %edx
addsd (%r15,%rdx,8), %xmm0
incl %ecx
decq %rax
jne 0xd792
movsd %xmm0, 0x60(%rsp,%r14,8)
incq %r14
movq 0x40(%rsp), %rbp
addl %ebp, %ebx
cmpq $0x4, %r14
jne 0xd756
xorl %eax, %eax
movsd 0xbd66(%rip), %xmm0     # 0x19528
movapd %xmm0, %xmm1
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x80(%rsp), %rbx
movq 0x88(%rsp), %rbp
movl %ecx, %esi
movsd 0x60(%rsp,%rax,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0xd7f2
movl %eax, %ecx
movl %esi, %edx
movapd %xmm3, %xmm2
jmp 0xd80e
movl %esi, %ecx
ucomisd %xmm1, %xmm3
jbe 0xd806
movl %eax, %edx
movapd %xmm0, %xmm2
movapd %xmm3, %xmm0
jmp 0xd80e
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
incq %rax
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
cmpq $0x4, %rax
jne 0xd7da
movl %ecx, %esi
movl %edx, %edx
movl 0x5c(%rsp), %eax
cmpl %eax, 0x1c(%rsp)
jge 0xd840
movsd 0x60(%rsp,%rsi,8), %xmm0
xorl %eax, %eax
ucomisd 0xa8(%rsp), %xmm0
jb 0xd857
movsd 0x60(%rsp,%rsi,8), %xmm0
ucomisd 0x60(%rsp,%rdx,8), %xmm0
movl $0x0, %eax
cmovnel %ecx, %eax
cmovpl %ecx, %eax
testl %eax, %eax
je 0xd9db
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xb8(%rsp)
movl %eax, 0x58(%rsp)
movl 0x20(%rsp), %ebx
movslq %ebx, %rdi
shlq $0x2, %rdi
callq 0x32f0
movq %rax, 0x50(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x20, %rax
xorl %ecx, %ecx
xorl %edx, %edx
testb $0x40, 0x3(%rax)
jne 0xd8ab
movl %edx, %esi
incl %edx
movq 0x50(%rsp), %rdi
movl %ecx, (%rdi,%rsi,4)
incq %rcx
addq $0x28, %rax
cmpq %rcx, %r12
jne 0xd899
testl %ebx, %ebx
je 0xd9ea
movl %ebx, %eax
movq %rax, 0xd0(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0xe0(%rsp)
movl (%rax,%rcx,4), %eax
movl (%rbp,%rax,4), %esi
movq %r13, %rdi
leaq 0x18(%rsp), %rdx
callq 0x165d9
testq %rax, %rax
je 0xd9b0
cmpl $0x0, 0x18(%rsp)
je 0xd9b0
movq %rax, %rbp
xorl %r13d, %r13d
movsd 0x30(%rbp), %xmm0
ucomisd 0xa0(%rsp), %xmm0
ja 0xd99c
cmpl $0x0, 0x34(%rsp)
je 0xd934
movq %rbp, %rdi
callq 0x16c6a
testl %eax, %eax
jne 0xd99c
cmpl $0x0, 0x30(%rsp)
je 0xd947
movq %rbp, %rdi
callq 0x16c8a
testl %eax, %eax
jne 0xd99c
cmpq %rbx, %r12
jne 0xd972
movq %r14, %rdi
leaq (%rbx,%rbx), %r14
testq %rbx, %rbx
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
callq 0x3330
movq %r14, %rbx
movq %rax, %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, (%r14,%r12,8)
incq %r12
incq %r13
movl 0x18(%rsp), %eax
addq $0x50, %rbp
cmpq %rax, %r13
jb 0xd911
movq 0xe0(%rsp), %rax
incq %rax
cmpq 0xd0(%rsp), %rax
movq 0x98(%rsp), %r13
movq 0x88(%rsp), %rbp
jne 0xd8d3
jmp 0xd9f0
movq 0x28(%rsp), %rdi
callq 0x118c4
jmp 0xdb1d
xorl %r14d, %r14d
xorl %r12d, %r12d
movl $0x8, %edx
movq %r14, %rdi
movq %r12, %rsi
leaq -0x917(%rip), %rcx      # 0xd0eb
callq 0x30c0
testq %r12, %r12
je 0xda3d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %ebx, %ebx
movq (%r14,%rax,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpl %ecx, %esi
je 0xda31
movl %ebx, %ecx
incl %ebx
movq %rdx, (%r14,%rcx,8)
movl 0x4(%r14,%rax,8), %ecx
incq %rax
movl %eax, %edx
cmpq %rdx, %r12
ja 0xda15
jmp 0xda3f
xorl %ebx, %ebx
movl %ebx, %esi
shlq $0x3, %rsi
movq %r14, %rdi
callq 0x3330
movq %rax, %r12
movq 0x38(%rsp), %rdx
movq (%rdx), %r14
movq 0x8(%rdx), %rax
cmpq %rax, %r14
jne 0xda90
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rdx)
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
shlq $0x6, %rsi
callq 0x3330
movq 0x38(%rsp), %rdx
movq %rax, 0x10(%rdx)
leaq 0x1(%r14), %rax
shlq $0x6, %r14
movq 0x10(%rdx), %rcx
movq %rax, (%rdx)
movl 0x58(%rsp), %edx
movl %edx, (%rcx,%r14)
movq 0xb0(%rsp), %rax
movsd 0x60(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x8(%rcx,%r14)
movq 0xb8(%rsp), %rax
movsd 0x60(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rcx,%r14)
movl 0x1c(%rsp), %r9d
movl %r9d, 0x18(%rcx,%r14)
movl 0x20(%rsp), %r8d
movl %r8d, 0x1c(%rcx,%r14)
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rcx,%r14)
movl %ebx, 0x28(%rcx,%r14)
movq %r12, 0x30(%rcx,%r14)
movq 0x28(%rsp), %rax
movq %rax, 0x38(%rcx,%r14)
cmpl $0x0, 0x130(%rsp)
movq 0x80(%rsp), %rbx
movq 0x90(%rsp), %r12
jg 0xdb36
movl 0x14(%rsp), %eax
incl %eax
movl %eax, 0x14(%rsp)
cmpl 0xc8(%rsp), %eax
jb 0xd5ce
jmp 0xdb7c
movq 0x114bb(%rip), %rax     # 0x1eff8
movq (%rax), %rdi
movq 0x10(%rbx), %rax
movl 0x14(%rsp), %ecx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rcx
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movl %edx, (%rsp)
leaq 0xc252(%rip), %rsi      # 0x19dbe
leaq 0xc2cb(%rip), %rdx      # 0x19e3e
movb $0x3, %al
callq 0x3290
jmp 0xdb1d
movq %r15, %rdi
callq 0x3060
movq 0x48(%rsp), %rdi
callq 0x3060
movq %rbp, %rdi
callq 0x3060
cmpl $0x0, 0x128(%rsp)
movq 0x38(%rsp), %rbx
je 0xdbb4
movq %rbx, %rdi
movl 0x130(%rsp), %esi
callq 0xd0fc
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
leaq 0x23(%rip), %rcx        # 0xdbe5
movl $0x40, %edx
callq 0x30c0
movq %rbx, %rax
jmp 0xdbd3
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq