// File: /workspace/github_c_makefile_star2_O3/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, 0x7c(%rsp)
movsd %xmm0, 0xb0(%rsp)
movl %ecx, 0x40(%rsp)
movl %edx, 0x44(%rsp)
cmpl $0x0, (%rdi)
je 0xdfc2
movq %rsi, %r13
movq %rdi, %rbp
movq 0x8(%rsi), %rbx
movl %ebx, %r14d
testl %r8d, %r8d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovnel %r8d, %eax
movl %eax, 0x20(%rsp)
movl 0x18(%rdi), %r15d
movl $0x5, %esi
callq 0x17c65
movq %r14, 0x60(%rsp)
leaq (,%r14,4), %rdi
callq 0x32f0
movq %rax, 0x58(%rsp)
movq %rbx, 0x28(%rsp)
testl %ebx, %ebx
je 0xd935
movq 0x60(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rbx
xorl %r14d, %r14d
movq 0x58(%rsp), %r12
movq 0x10(%r13), %rax
movq (%rax,%r14), %rsi
movq %rbp, %rdi
callq 0x17aaa
movl %eax, (%r12)
addq $0x18, %r14
addq $0x4, %r12
cmpq %r14, %rbx
jne 0xd914
movq %r13, 0xa0(%rsp)
leal (,%r15,4), %edi
shlq $0x3, %rdi
movq %rdi, 0xd0(%rsp)
callq 0x32f0
movq %rax, %r13
movl $0x1, %edi
movl $0x18, %esi
callq 0x3240
movq %rax, 0x48(%rsp)
movl $0x1, %esi
movq 0x60(%rsp), %rdi
callq 0x3240
movq %rax, 0x70(%rsp)
xorl %eax, %eax
movl $0x0, 0x14(%rsp)
movq 0x28(%rsp), %rdx
testl %edx, %edx
je 0xdfc6
movl 0x20(%rsp), %r12d
cmpl %r15d, %r12d
cmovael %r15d, %r12d
cvtsi2sdl 0x120(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
cmpl $0x1, %r12d
movl %r12d, %ecx
adcl $0x0, %ecx
movq %rcx, 0xe0(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
movq %rbp, 0xa8(%rsp)
movl %r12d, 0x20(%rsp)
movl %eax, %eax
movq 0x70(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0xdf53
movq 0xa0(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq 0x10(%rdi), %rcx
leaq (%rax,%rax,4), %rax
testb $0x40, 0x23(%rcx,%rax,8)
jne 0xdf53
xorl %r14d, %r14d
xorl %esi, %esi
callq 0xb1fb
movq %rax, %rbx
movl $0x1, (%rsp)
movq %rax, %rdi
leaq 0x14(%rsp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x14ff2
movq %r13, %rdi
xorl %esi, %esi
movq 0xd0(%rsp), %rdx
callq 0x31b0
movq %rbx, 0x50(%rsp)
movq 0x10(%rbx), %rax
movl $0x0, 0x24(%rsp)
movl $0x0, 0x10(%rsp)
leaq (%r14,%r14,4), %r12
testb $0x40, 0x23(%rax,%r12,8)
jne 0xdb44
movq 0x58(%rsp), %rax
movl (%rax,%r14,4), %esi
movq %rbp, %rdi
leaq 0xc(%rsp), %rdx
callq 0x17ec6
testq %rax, %rax
je 0xdb18
cmpl $0x0, 0xc(%rsp)
je 0xdb18
movq %rax, %rbx
xorl %ebp, %ebp
movsd 0x30(%rbx), %xmm0
ucomisd 0xb0(%rsp), %xmm0
ja 0xdb08
cmpl $0x0, 0x44(%rsp)
je 0xdaca
movq %rbx, %rdi
callq 0x18562
testl %eax, %eax
jne 0xdb08
cmpl $0x0, 0x40(%rsp)
je 0xdadd
movq %rbx, %rdi
callq 0x1857c
testl %eax, %eax
jne 0xdb08
movl 0x48(%rbx), %eax
movl %eax, %ecx
shrl $0x1d, %ecx
andl $0x3, %ecx
imull %r15d, %ecx
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
addl %ecx, %eax
movsd 0x38(%rbx), %xmm0
ucomisd (%r13,%rax,8), %xmm0
jbe 0xdb08
movsd %xmm0, (%r13,%rax,8)
incq %rbp
movl 0xc(%rsp), %eax
addq $0x50, %rbx
cmpq %rax, %rbp
jb 0xdaa7
incl 0x24(%rsp)
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rsp), %ecx
addl 0x18(%rax,%r12,8), %ecx
movl %ecx, 0x10(%rsp)
movq 0x70(%rsp), %rcx
movb $0x1, (%rcx,%r14)
movq 0xa8(%rsp), %rbp
incq %r14
cmpq 0x60(%rsp), %r14
jne 0xda70
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movl 0x20(%rsp), %r12d
movl %r14d, %eax
imull %r15d, %eax
leaq (,%rax,8), %rdi
addq %r13, %rdi
movl $0x8, %edx
movq %r15, %rsi
leaq -0xcc5(%rip), %rcx      # 0xcece
callq 0x30c0
movl %r12d, 0xc(%rsp)
testq %r15, %r15
je 0xdbd0
movsd 0x80(%rsp,%r14,8), %xmm0
movq 0xe0(%rsp), %rax
movl %ebx, %ecx
movl %ecx, %edx
addsd (%r13,%rdx,8), %xmm0
incl %ecx
decq %rax
jne 0xdbb6
movsd %xmm0, 0x80(%rsp,%r14,8)
incq %r14
addl %r15d, %ebx
cmpq $0x4, %r14
jne 0xdb72
xorl %ecx, %ecx
movsd 0xc942(%rip), %xmm0     # 0x1a528
movapd %xmm0, %xmm1
xorl %eax, %eax
xorl %edi, %edi
movl 0x10(%rsp), %esi
movl %edi, %edx
movsd 0x80(%rsp,%rcx,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0xdc0d
movl %ecx, %edi
movl %edx, %eax
movapd %xmm3, %xmm2
jmp 0xdc29
movl %edx, %edi
ucomisd %xmm1, %xmm3
jbe 0xdc21
movl %ecx, %eax
movapd %xmm0, %xmm2
movapd %xmm3, %xmm0
jmp 0xdc29
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
incq %rcx
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
cmpq $0x4, %rcx
jne 0xdbf2
movl %edi, %edx
movsd 0x80(%rsp,%rdx,8), %xmm0
cmpl 0x7c(%rsp), %esi
jge 0xdc56
ucomisd 0xc8(%rsp), %xmm0
jb 0xdc74
movl %eax, %esi
ucomisd 0x80(%rsp,%rsi,8), %xmm0
setnp %al
sete %cl
andb %al, %cl
testl %edi, %edi
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0xdc88
movq 0x50(%rsp), %rdi
callq 0x11ef0
movq 0x28(%rsp), %rdx
jmp 0xdf53
movq %rsi, 0xb8(%rsp)
movq %rdx, 0xc0(%rsp)
movl %edi, 0x3c(%rsp)
movl 0x24(%rsp), %ebx
movslq %ebx, %rdi
shlq $0x2, %rdi
callq 0x32f0
movq %rax, 0x68(%rsp)
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x20, %rax
xorl %ecx, %ecx
xorl %edx, %edx
testb $0x40, 0x3(%rax)
jne 0xdcd4
movl %edx, %esi
incl %edx
movq 0x68(%rsp), %rdi
movl %ecx, (%rdi,%rsi,4)
incq %rcx
addq $0x28, %rax
cmpq %rcx, 0x60(%rsp)
jne 0xdcc2
testl %ebx, %ebx
je 0xde5b
movl %ebx, %eax
movq %rax, 0xd8(%rsp)
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movq 0x68(%rsp), %rax
movl (%rax,%rbx,4), %eax
movq 0x58(%rsp), %rcx
movl (%rcx,%rax,4), %esi
movq %rbp, %rdi
leaq 0xc(%rsp), %rdx
callq 0x17ec6
testq %rax, %rax
movq %rbx, 0x30(%rsp)
je 0xddeb
cmpl $0x0, 0xc(%rsp)
je 0xddeb
movq %rax, %rbp
xorl %ebx, %ebx
movsd 0x30(%rbp), %xmm0
ucomisd 0xb0(%rsp), %xmm0
ja 0xddd7
cmpl $0x0, 0x44(%rsp)
je 0xdd67
movq %rbp, %rdi
callq 0x18562
testl %eax, %eax
jne 0xddd7
cmpl $0x0, 0x40(%rsp)
je 0xdd7a
movq %rbp, %rdi
callq 0x1857c
testl %eax, %eax
jne 0xddd7
cmpq %r14, %r12
jne 0xdda3
leaq (%r12,%r12), %r14
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x3330
jmp 0xdda8
movq 0x18(%rsp), %rax
movl 0x48(%rbp), %edx
movl %edx, %ecx
andl $0x1fffffff, %ecx       # imm = 0x1FFFFFFF
shrl $0x1d, %edx
andl $0x3, %edx
leal (%rdx,%rcx,4), %edx
shlq $0x20, %rdx
cvttsd2si 0x38(%rbp), %rcx
movl %ecx, %ecx
orq %rdx, %rcx
movq %rax, 0x18(%rsp)
movq %rcx, (%rax,%r12,8)
incq %r12
incq %rbx
movl 0xc(%rsp), %eax
addq $0x50, %rbp
cmpq %rax, %rbx
jb 0xdd40
movq 0x30(%rsp), %rbx
incq %rbx
cmpq 0xd8(%rsp), %rbx
movq 0xa8(%rsp), %rbp
jne 0xdd05
movl $0x8, %edx
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
leaq -0x92b(%rip), %rcx      # 0xd4f5
callq 0x30c0
testq %r12, %r12
je 0xde73
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 0xde4f
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 0xde33
jmp 0xde75
xorl %r14d, %r14d
movl $0x8, %edx
xorl %edi, %edi
xorl %esi, %esi
leaq -0x979(%rip), %rcx      # 0xd4f5
callq 0x30c0
xorl %ebx, %ebx
movl %ebx, %esi
shlq $0x3, %rsi
movq %r14, %rdi
callq 0x3330
movq %rax, %r12
movq 0x48(%rsp), %rax
movq (%rax), %r14
movq 0x10(%rax), %rcx
cmpq 0x8(%rax), %r14
jne 0xdec4
leaq (%r14,%r14), %rsi
testq %r14, %r14
movq %rcx, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rax)
shlq $0x6, %rsi
callq 0x3330
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rdx
movl 0x3c(%rsp), %r10d
leaq 0x1(%r14), %rdi
shlq $0x6, %r14
movq %rdi, 0x30(%rsp)
movq %rdi, (%rax)
movl %r10d, (%rcx,%r14)
movq 0xc0(%rsp), %rax
movsd 0x80(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x8(%rcx,%r14)
movq 0xb8(%rsp), %rax
movsd 0x80(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rcx,%r14)
movl 0x10(%rsp), %r9d
movl %r9d, 0x18(%rcx,%r14)
movl 0x24(%rsp), %r8d
movl %r8d, 0x1c(%rcx,%r14)
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rcx,%r14)
movl %ebx, 0x28(%rcx,%r14)
movq %r12, 0x30(%rcx,%r14)
movq 0x50(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq %rax, 0x38(%rcx,%r14)
cmpl $0x0, 0x130(%rsp)
jg 0xdf67
movl 0x14(%rsp), %eax
incl %eax
movl %eax, 0x14(%rsp)
cmpl %edx, %eax
jb 0xd9e7
jmp 0xdfd8
movq 0x1208a(%rip), %rax     # 0x1fff8
movq (%rax), %rdi
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rcx
movsd 0x88(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
movl %r10d, (%rsp)
leaq 0xce1f(%rip), %rsi      # 0x1adce
leaq 0xce98(%rip), %rdx      # 0x1ae4e
movb $0x3, %al
callq 0x3290
jmp 0xdc7e
xorl %eax, %eax
jmp 0xe02f
movq $0x0, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
movq %r13, %rdi
callq 0x3060
movq 0x70(%rsp), %rdi
callq 0x3060
movq 0x58(%rsp), %rdi
callq 0x3060
cmpl $0x0, 0x128(%rsp)
je 0xe00f
movq 0x48(%rsp), %rdi
movl 0x130(%rsp), %esi
callq 0xd506
leaq 0x2b(%rip), %rcx        # 0xe041
movl $0x40, %edx
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x30c0
movq 0x48(%rsp), %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq