// File: /workspace/github_c_makefile_star2_O2/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 $0xf8, %rsp
movl %r9d, 0x9c(%rsp)
movsd %xmm0, 0xd0(%rsp)
movl %edx, 0x50(%rsp)
cmpl $0x0, (%rdi)
je 0xc2f3
movl %r8d, %ebx
movq %rdi, %r13
movl %ecx, 0x14(%rsp)
movl 0x130(%rsp), %ebp
movq %rsi, 0x78(%rsp)
movq 0x8(%rsi), %rax
movq %rax, 0x38(%rsp)
movl %eax, %r14d
movl 0x18(%rdi), %eax
movq %rax, 0x70(%rsp)
pushq $0x5
popq %rsi
callq 0x1387d
leaq (,%r14,4), %rdi
callq 0x32f0
movq %r14, 0x30(%rsp)
imulq $0x6, %r14, %r14
xorl %r15d, %r15d
movq %rax, 0x90(%rsp)
movq %rax, %r12
cmpq %r15, %r14
je 0xbc7e
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r15,4), %rsi
movq %r13, %rdi
callq 0x1377d
movl %eax, (%r12)
addq $0x6, %r15
addq $0x4, %r12
jmp 0xbc56
testl %ebx, %ebx
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmovnel %ebx, %r15d
movq 0x70(%rsp), %r14
leal (,%r14,4), %edi
shlq $0x3, %rdi
movq %rdi, 0xe0(%rsp)
callq 0x32f0
movq %rax, %r12
pushq $0x1
popq %rbx
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x3240
movq %rax, 0x58(%rsp)
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %rdi, %rbx
callq 0x3240
cmpl %r14d, %r15d
cmovael %r14d, %r15d
movq %r15, 0xf0(%rsp)
movq %rax, 0x88(%rsp)
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0xd8(%rsp)
movq $0x0, 0x60(%rsp)
movq $0x0, 0x20(%rsp)
xorl %eax, %eax
movl 0x14(%rsp), %ebp
movq %r13, 0x68(%rsp)
movq 0x38(%rsp), %rdx
movl %eax, 0x2c(%rsp)
cmpl %edx, %eax
jae 0xc296
movl %eax, %eax
movq 0x88(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0xc231
movq 0x78(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq 0x10(%rdi), %rcx
imulq $0x28, %rax, %rax
testb $0x40, 0x23(%rcx,%rax)
jne 0xc231
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x9778
movl $0x1, (%rsp)
movq %rax, 0x40(%rsp)
movq %rax, %rdi
leaq 0x2c(%rsp), %rsi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x12281
movq %r12, %rdi
xorl %esi, %esi
movq 0xe0(%rsp), %rdx
callq 0x31b0
movl $0x0, 0x18(%rsp)
movl $0x0, 0x28(%rsp)
cmpq %rbx, %r14
je 0xbe8e
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r14, %r15
testb $0x40, 0x23(%rax,%r15)
jne 0xbe86
movq 0x90(%rsp), %rax
movl (%rax,%r14,4), %esi
movq %r13, %rdi
leaq 0x1c(%rsp), %rdx
callq 0x13ac4
testq %rax, %rax
je 0xbe55
movq %rax, %rbx
xorl %r13d, %r13d
movl 0x1c(%rsp), %eax
cmpq %rax, %r13
jae 0xbe55
movsd 0x30(%rbx), %xmm0
ucomisd 0xd0(%rsp), %xmm0
ja 0xbe4c
cmpl $0x0, 0x50(%rsp)
je 0xbe12
movq %rbx, %rdi
callq 0x14057
testl %eax, %eax
jne 0xbe4c
testl %ebp, %ebp
je 0xbe22
movq %rbx, %rdi
callq 0x14071
testl %eax, %eax
jne 0xbe4c
movl 0x48(%rbx), %eax
movl %eax, %ecx
shrl $0x1d, %ecx
andl $0x3, %ecx
imull 0x70(%rsp), %ecx
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
addl %ecx, %eax
movsd 0x38(%rbx), %xmm0
ucomisd (%r12,%rax,8), %xmm0
jbe 0xbe4c
movsd %xmm0, (%r12,%rax,8)
incq %r13
addq $0x50, %rbx
jmp 0xbde6
incl 0x28(%rsp)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x18(%rsp), %ecx
addl 0x18(%rax,%r15), %ecx
movl %ecx, 0x18(%rsp)
movq 0x88(%rsp), %rax
movb $0x1, (%rax,%r14)
movq 0x68(%rsp), %r13
movq 0x30(%rsp), %rbx
incq %r14
jmp 0xbda0
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
pushq $0x8
popq %r15
cmpq $0x4, %r14
je 0xbf0e
movl %r14d, %eax
movq 0x70(%rsp), %rsi
imull %esi, %eax
leaq (%r12,%rax,8), %rdi
movq %r15, %rdx
leaq -0xc67(%rip), %rcx      # 0xb265
callq 0x30c0
movq 0xf0(%rsp), %rcx
movl %ecx, 0x1c(%rsp)
movl %ebx, %eax
subq $0x1, %rcx
jb 0xbf05
movl %eax, %edx
movsd (%r12,%rdx,8), %xmm0
addsd 0xa0(%rsp,%r14,8), %xmm0
movsd %xmm0, 0xa0(%rsp,%r14,8)
incl %eax
jmp 0xbedf
incq %r14
addl 0x70(%rsp), %ebx
jmp 0xbead
leaq 0xa0(%rsp), %rdi
leaq 0x54(%rsp), %rsi
callq 0xb7ff
movl %eax, %edx
movl %eax, %esi
movsd 0xa0(%rsp,%rsi,8), %xmm0
movl 0x18(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0xbf45
ucomisd 0xd8(%rsp), %xmm0
jb 0xbf63
movl 0x54(%rsp), %eax
ucomisd 0xa0(%rsp,%rax,8), %xmm0
setp %al
setne %cl
orb %al, %cl
testl %edx, %edx
setne %al
testb %al, %cl
jne 0xbf77
movq 0x40(%rsp), %rdi
callq 0xf9e0
movq 0x38(%rsp), %rdx
jmp 0xc22c
movq %rsi, 0xc8(%rsp)
movl %edx, 0x4c(%rsp)
movl 0x28(%rsp), %ebx
movslq %ebx, %rdi
shlq $0x2, %rdi
callq 0x32f0
movq %rax, 0x80(%rsp)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x20, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x30(%rsp), %rdi
cmpq %rcx, %rdi
je 0xbfd5
testb $0x40, 0x3(%rax)
jne 0xbfcc
movl %edx, %esi
incl %edx
movq 0x80(%rsp), %r8
movl %ecx, (%r8,%rsi,4)
incq %rcx
addq $0x28, %rax
jmp 0xbfb1
movl %ebx, %eax
movq %rax, 0xe8(%rsp)
xorl %ecx, %ecx
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpq 0xe8(%rsp), %rcx
je 0xc0fb
movq 0x80(%rsp), %rax
movq %rcx, 0x60(%rsp)
movl (%rax,%rcx,4), %eax
movq 0x90(%rsp), %rcx
movl (%rcx,%rax,4), %esi
movq 0x68(%rsp), %rdi
leaq 0x1c(%rsp), %rdx
callq 0x13ac4
testq %rax, %rax
je 0xc0ee
movq %rax, %r14
xorl %r15d, %r15d
movl 0x1c(%rsp), %eax
cmpq %rax, %r15
jae 0xc0ea
movsd 0x30(%r14), %xmm0
ucomisd 0xd0(%rsp), %xmm0
ja 0xc0de
cmpl $0x0, 0x50(%rsp)
je 0xc06b
movq %r14, %rdi
callq 0x14057
testl %eax, %eax
jne 0xc0de
testl %ebp, %ebp
je 0xc07b
movq %r14, %rdi
callq 0x14071
testl %eax, %eax
jne 0xc0de
cmpq %rbx, %r13
jne 0xc0a9
leaq (%rbx,%rbx), %rbp
testq %rbx, %rbx
pushq $0x2
popq %rax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
movq 0x20(%rsp), %rdi
callq 0x3330
movq %rbp, %rbx
movl 0x14(%rsp), %ebp
jmp 0xc0ae
movq 0x20(%rsp), %rax
movl 0x48(%r14), %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(%r14), %rcx
movl %ecx, %ecx
orq %rdx, %rcx
movq %rax, 0x20(%rsp)
movq %rcx, (%rax,%r13,8)
incq %r13
incq %r15
addq $0x50, %r14
jmp 0xc036
pushq $0x8
popq %r15
movq 0x60(%rsp), %rcx
incq %rcx
jmp 0xbfef
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
leaq -0x8ac(%rip), %rcx      # 0xb864
callq 0x30c0
xorl %ebp, %ebp
pushq $-0x1
popq %rcx
xorl %eax, %eax
movl %eax, %eax
movl %ebp, %esi
cmpq %rax, %r13
jbe 0xc142
movq (%rbx,%rax,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
cmpl %ecx, %edi
je 0xc13e
incl %ebp
movq %rdx, (%rbx,%rsi,8)
movl 0x4(%rbx,%rax,8), %ecx
incl %eax
jmp 0xc11c
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x3330
movq %rax, %rbx
movq 0x58(%rsp), %rax
movq (%rax), %r14
movq 0x10(%rax), %rdi
cmpq 0x8(%rax), %r14
jne 0xc191
leaq (%r14,%r14), %rsi
testq %r14, %r14
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rax)
shlq $0x6, %rsi
callq 0x3330
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x68(%rsp), %r13
jmp 0xc199
movq 0x68(%rsp), %r13
movq %rdi, %rcx
movq 0x38(%rsp), %rdx
movl 0x4c(%rsp), %r10d
movq 0xc8(%rsp), %r8
leaq 0x1(%r14), %rdi
shlq $0x6, %r14
movq %rdi, 0x60(%rsp)
movq %rdi, (%rax)
movl %r10d, (%rcx,%r14)
movsd 0xa0(%rsp,%r8,8), %xmm0
movsd %xmm0, 0x8(%rcx,%r14)
movl 0x54(%rsp), %eax
movsd 0xa0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rcx,%r14)
movl 0x18(%rsp), %r9d
movl %r9d, 0x18(%rcx,%r14)
movl 0x28(%rsp), %r8d
movl %r8d, 0x1c(%rcx,%r14)
movq 0x80(%rsp), %rax
movq %rax, 0x20(%rcx,%r14)
movl %ebp, 0x28(%rcx,%r14)
movq %rbx, 0x30(%rcx,%r14)
movq 0x40(%rsp), %rax
movq %rcx, 0x20(%rsp)
movq %rax, 0x38(%rcx,%r14)
cmpl $0x0, 0x140(%rsp)
jg 0xc23c
movl 0x14(%rsp), %ebp
movq 0x30(%rsp), %rbx
movl 0x2c(%rsp), %eax
incl %eax
jmp 0xbd14
movq 0x10db5(%rip), %rax     # 0x1cff8
movq (%rax), %rdi
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %rcx
movsd 0xa8(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm2
movl %r10d, (%rsp)
leaq 0xab5d(%rip), %rsi      # 0x16dde
leaq 0xabd6(%rip), %rdx      # 0x16e5e
movb $0x3, %al
callq 0x3290
movq 0x38(%rsp), %rdx
jmp 0xc228
movq %r12, %rdi
callq 0x3060
movq 0x88(%rsp), %rdi
callq 0x3060
movq 0x90(%rsp), %rdi
callq 0x3060
cmpl $0x0, 0x138(%rsp)
je 0xc2d3
movq 0x58(%rsp), %rdi
movl 0x140(%rsp), %esi
callq 0xb875
leaq 0x2d(%rip), %rcx        # 0xc307
pushq $0x40
popq %rdx
movq 0x20(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x30c0
movq 0x58(%rsp), %rax
jmp 0xc2f5
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq