// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]oatk/path.c
// Function: annot_subgraph_og_type1
og_component_v *annot_subgraph_og_type1(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, double **_annot_score, int verbose)
{
    uint32_t i, j, k, n_seg, m_seg, n_gene, sid, gid, imax, smax;
    uint32_t *ann_snameid_map;
    hmm_annot_t *annots, *annot;
    double a_s[4], *annot_score;
    int a_n[4], 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;
    annots = annot_db->a;
    n_seg = asg->n_seg;
    m_seg = annot_db->n_seg;

    if (n_gene == 0) return 0;

    // sort annotations by sid - score
    hmm_annot_db_sort(annot_db, ORDER_SID_OG);

    // build annot_db sname to asg sname id map
    MYMALLOC(ann_snameid_map, m_seg);
    for (i = 0; i < m_seg; ++i)
        ann_snameid_map[i] = asg_name2id(asg, annot_db->snames[i]);

    // calculate annotation score for each seg
    sid = annots[0].sid;
    MYBZERO(a_s, 4);
    MYBZERO(a_n, 4);
    MYCALLOC(annot_score, n_seg * 4);
    for (i = 0; i < n_gene; ++i) {
        annot = &annots[i];
        if (annot->sid != sid) {
            // here remember to convert annotation sid to asg sid
            if (ann_snameid_map[sid] != UINT32_MAX)
                memcpy(annot_score+ann_snameid_map[sid]*4, a_s, sizeof(double)*4);
            sid = annot->sid;
            MYBZERO(a_s, 4);
            MYBZERO(a_n, 4);
        }
        if (annot->evalue > max_eval || (no_trn && is_trn(annot)) || (no_rrn && is_rrn(annot)))
            continue;
        if (a_n[annot->og_type]++ < n_core)
            a_s[annot->og_type] += annot->score;
    }
    // here remember to convert annotation sid to asg sid
    if (ann_snameid_map[sid] != UINT32_MAX)
        memcpy(annot_score+ann_snameid_map[sid]*4, a_s, sizeof(double)*4);

    // extract each subgraph and do classification
    // TODO deal with graphs when mito and pltd are in the same subgraph
    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(a_s, 4);
        len = nv = 0;
        for (j = 0; j < n_seg; ++j) {
            if (!g->vtx[j].del) {
                for (k = 0; k < 4; ++k)
                    a_s[k] += annot_score[j*4+k];
                ++nv;
                len += g->vtx[j].len;
                visited[j] = 1;
            }
        }
        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 < n_gene; ++j) {
            annot = &annots[j];
            sid = ann_snameid_map[annot->sid];
            if (sid == UINT32_MAX ||
                    g->vtx[sid].del ||
                    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(visited);
    if (_annot_score)
        *_annot_score = annot_score;
    else
        free(annot_score);
    free(ann_snameid_map);

    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 $0xd8, %rsp
movl %r9d, 0x8c(%rsp)
movsd %xmm0, 0x98(%rsp)
movl %edx, 0x2c(%rsp)
movl (%rdi), %eax
testl %eax, %eax
je 0xe337
movl %r8d, %ebx
movq %rsi, %rbp
movq %rdi, %r14
movq %rax, 0x60(%rsp)
movl %ecx, 0x28(%rsp)
movq 0x10(%rdi), %rax
movq %rax, 0x78(%rsp)
movq 0x8(%rsi), %rax
movq %rax, 0x68(%rsp)
movl 0x1c(%rdi), %r15d
movl $0x5, %esi
callq 0x1637d
leaq (,%r15,4), %rdi
callq 0x32f0
movq %rax, %r13
movl $0x0, 0x14(%rsp)
testq %r15, %r15
je 0xdca8
movq 0x28(%r14), %r14
xorl %r12d, %r12d
movq (%r14,%r12,8), %rsi
movq %rbp, %rdi
callq 0x865c
movl %eax, (%r13,%r12,4)
incq %r12
movl %r12d, 0x14(%rsp)
cmpq %r12, %r15
jne 0xdc8a
movq %r13, 0x70(%rsp)
movq %rbp, 0x90(%rsp)
movl 0x110(%rsp), %ebp
movq 0x78(%rsp), %rax
movl 0x4c(%rax), %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0xa0(%rsp)
movq 0x68(%rsp), %rax
leal (,%rax,4), %edi
movl $0x8, %esi
callq 0x3240
movq %rax, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
xorl %eax, %eax
movl 0x28(%rsp), %r12d
movq 0x60(%rsp), %r13
movl %eax, %eax
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
movq 0x78(%rsp), %rcx
leaq (%rcx,%rax), %r14
cmpl %r15d, 0x4c(%rcx,%rax)
je 0xdd73
movl %r15d, %eax
movq 0x70(%rsp), %rcx
movl (%rcx,%rax,4), %eax
cmpl $-0x1, %eax
je 0xdd56
shll $0x2, %eax
movapd 0x30(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
movq 0x20(%rsp), %rcx
movupd %xmm1, 0x10(%rcx,%rax,8)
movupd %xmm0, (%rcx,%rax,8)
movl 0x4c(%r14), %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0xa0(%rsp)
movsd 0x30(%r14), %xmm0
ucomisd 0x98(%rsp), %xmm0
ja 0xddd9
cmpl $0x0, 0x2c(%rsp)
je 0xdd97
movq %r14, %rdi
callq 0x16c6a
testl %eax, %eax
jne 0xddd9
testl %r12d, %r12d
je 0xdda8
movq %r14, %rdi
callq 0x16c8a
testl %eax, %eax
jne 0xddd9
movl 0x48(%r14), %eax
shrl $0x1d, %eax
andl $0x3, %eax
movl 0xa0(%rsp,%rax,4), %ecx
leal 0x1(%rcx), %edx
movl %edx, 0xa0(%rsp,%rax,4)
cmpl %ebx, %ecx
jge 0xddd9
movsd 0x38(%r14), %xmm0
addsd 0x30(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x30(%rsp,%rax,8)
movl 0x14(%rsp), %eax
incl %eax
movl %eax, 0x14(%rsp)
cmpl %r13d, %eax
jb 0xdd0d
movl %r15d, %eax
movq 0x70(%rsp), %r14
movl (%r14,%rax,4), %eax
cmpl $-0x1, %eax
je 0xde1c
shll $0x2, %eax
movapd 0x30(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
movq 0x20(%rsp), %rcx
movupd %xmm1, 0x10(%rcx,%rax,8)
movupd %xmm0, (%rcx,%rax,8)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3240
movq %rax, 0x50(%rsp)
movq 0x68(%rsp), %r15
movl %r15d, %edi
movl $0x1, %esi
callq 0x3240
movl $0x0, 0x14(%rsp)
testl %r15d, %r15d
je 0xe318
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0xb0(%rsp)
cmpl $0x1, %r13d
adcl $0x0, %r13d
movl %r15d, %r12d
xorl %ecx, %ecx
movq %r13, 0x60(%rsp)
movq %rax, 0x58(%rsp)
movq %r12, 0xb8(%rsp)
movl %ecx, %edx
cmpb $0x0, (%rax,%rdx)
jne 0xe2b2
movq 0x90(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq 0x10(%rdi), %rcx
leaq (%rdx,%rdx,4), %rdx
testb $0x40, 0x23(%rcx,%rdx,8)
jne 0xe2b2
xorl %ebx, %ebx
xorl %esi, %esi
callq 0xad23
movq %rax, %rbp
movl $0x1, (%rsp)
movq %rax, %rdi
leaq 0x14(%rsp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1495a
movq 0x58(%rsp), %r8
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x40(%rsp)
movq %rbp, 0x18(%rsp)
movq 0x10(%rbp), %rax
movq 0xb8(%rsp), %r12
xorl %r10d, %r10d
xorl %r11d, %r11d
movq 0x20(%rsp), %rdi
movl 0x8c(%rsp), %r9d
leaq (%rbx,%rbx,4), %rcx
testb $0x40, 0x23(%rax,%rcx,8)
jne 0xdf56
movl %ebx, %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
shlq $0x5, %rdx
addq %rdi, %rdx
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
addsd 0x30(%rsp,%rsi,8), %xmm0
movsd %xmm0, 0x30(%rsp,%rsi,8)
incq %rsi
cmpq $0x4, %rsi
jne 0xdf2f
incl %r10d
addl 0x18(%rax,%rcx,8), %r11d
movb $0x1, (%r8,%rbx)
incq %rbx
cmpq %r12, %rbx
jne 0xdf13
xorl %eax, %eax
movsd 0xb5c0(%rip), %xmm0     # 0x19528
movapd %xmm0, %xmm1
xorl %edx, %edx
xorl %ecx, %ecx
movl %ecx, %esi
movsd 0x30(%rsp,%rax,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0xdf88
movl %eax, %ecx
movl %esi, %edx
movapd %xmm3, %xmm2
jmp 0xdfa4
movl %esi, %ecx
ucomisd %xmm1, %xmm3
jbe 0xdf9c
movl %eax, %edx
movapd %xmm0, %xmm2
movapd %xmm3, %xmm0
jmp 0xdfa4
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
incq %rax
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
cmpq $0x4, %rax
jne 0xdf70
movl %ecx, %esi
movl %edx, %edx
cmpl %r9d, %r11d
jge 0xdfd1
movsd 0x30(%rsp,%rsi,8), %xmm0
xorl %eax, %eax
ucomisd 0xb0(%rsp), %xmm0
jb 0xdfe8
movsd 0x30(%rsp,%rsi,8), %xmm0
ucomisd 0x30(%rsp,%rdx,8), %xmm0
movl $0x0, %eax
cmovnel %ecx, %eax
cmovpl %ecx, %eax
testl %eax, %eax
je 0xe1bb
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
movl %eax, 0x80(%rsp)
movl %r11d, 0x84(%rsp)
movl %r10d, 0x88(%rsp)
movslq %r10d, %rdi
shlq $0x2, %rdi
callq 0x32f0
movq 0x18(%rsp), %rdi
movq 0x10(%rdi), %r8
addq $0x20, %r8
xorl %ecx, %ecx
xorl %edx, %edx
testb $0x40, 0x3(%r8)
jne 0xe042
movl %edx, %esi
incl %edx
movl %ecx, (%rax,%rsi,4)
incq %rcx
addq $0x28, %r8
cmpq %rcx, %r12
jne 0xe034
movq %rax, 0xc0(%rsp)
movq %r13, %rbx
movq 0x78(%rsp), %rbp
xorl %r12d, %r12d
xorl %esi, %esi
xorl %r15d, %r15d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl 0x4c(%rbp), %eax
movl (%r14,%rax,4), %eax
cmpq %rdx, %rax
je 0xe15c
movq 0x10(%rdi), %rcx
leaq (%rax,%rax,4), %rax
testb $0x40, 0x23(%rcx,%rax,8)
jne 0xe15c
movsd 0x30(%rbp), %xmm0
ucomisd 0x98(%rsp), %xmm0
ja 0xe15c
cmpl $0x0, 0x2c(%rsp)
je 0xe0cf
movq %rbp, %rdi
movq %r14, %r13
movq %rsi, %r14
callq 0x16c6a
movq %r14, %rsi
movq %r13, %r14
movq 0x18(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xe15c
cmpl $0x0, 0x28(%rsp)
je 0xe0f8
movq %rbp, %rdi
movq %r14, %r13
movq %rsi, %r14
callq 0x16c8a
movq %r14, %rsi
movq %r13, %r14
movq 0x18(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xe15c
cmpq %rsi, %r12
jne 0xe132
leaq (%rsi,%rsi), %r14
testq %rsi, %rsi
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r15, %rdi
callq 0x3330
movq 0x18(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rax, %r15
movq %r14, %rsi
movq 0x70(%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, (%r15,%r12,8)
incq %r12
addq $0x50, %rbp
decq %rbx
jne 0xe06b
movl $0x8, %edx
movq %r15, %rdi
movq %r12, %rsi
leaq -0x1090(%rip), %rcx     # 0xd0eb
callq 0x30c0
testq %r12, %r12
je 0xe1cf
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %ebx, %ebx
movq 0x50(%rsp), %rbp
movq (%r15,%rax,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpl %ecx, %esi
je 0xe1af
movl %ebx, %ecx
incl %ebx
movq %rdx, (%r15,%rcx,8)
movl 0x4(%r15,%rax,8), %ecx
incq %rax
movl %eax, %edx
cmpq %rdx, %r12
ja 0xe193
jmp 0xe1d6
movq 0x18(%rsp), %rdi
callq 0x118c4
movq 0x58(%rsp), %rax
jmp 0xe2b2
xorl %ebx, %ebx
movq 0x50(%rsp), %rbp
movl %ebx, %esi
shlq $0x3, %rsi
movq %r15, %rdi
callq 0x3330
movq %rax, %r15
movq (%rbp), %r13
movq 0x8(%rbp), %rax
cmpq %rax, %r13
jne 0xe219
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x6, %rsi
callq 0x3330
movq %rax, 0x10(%rbp)
leaq 0x1(%r13), %rax
shlq $0x6, %r13
movq 0x10(%rbp), %rcx
movq %rax, (%rbp)
movl 0x80(%rsp), %edx
movl %edx, (%rcx,%r13)
movq 0xc8(%rsp), %rax
movsd 0x30(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x8(%rcx,%r13)
movq 0xd0(%rsp), %rax
movsd 0x30(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rcx,%r13)
movl 0x84(%rsp), %r9d
movl %r9d, 0x18(%rcx,%r13)
movl 0x88(%rsp), %r8d
movl %r8d, 0x1c(%rcx,%r13)
movq 0xc0(%rsp), %rax
movq %rax, 0x20(%rcx,%r13)
movl %ebx, 0x28(%rcx,%r13)
movq %r15, 0x30(%rcx,%r13)
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rcx,%r13)
cmpl $0x0, 0x120(%rsp)
movq 0x60(%rsp), %r13
movq 0x68(%rsp), %r15
movq 0x58(%rsp), %rax
jg 0xe2c7
movl 0x14(%rsp), %ecx
incl %ecx
movl %ecx, 0x14(%rsp)
cmpl %r15d, %ecx
jb 0xde82
jmp 0xe318
movq 0x10d2a(%rip), %rax     # 0x1eff8
movq (%rax), %rdi
movq 0x90(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %ecx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rcx
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
movl %edx, (%rsp)
leaq 0xbab9(%rip), %rsi      # 0x19dbe
leaq 0xbb49(%rip), %rdx      # 0x19e55
movb $0x3, %al
callq 0x3290
jmp 0xe1c5
movq %rax, %rdi
callq 0x3060
movq 0x118(%rsp), %rax
testq %rax, %rax
je 0xe33b
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
jmp 0xe345
xorl %ebx, %ebx
jmp 0xe379
movq 0x20(%rsp), %rdi
callq 0x3060
movq 0x50(%rsp), %rbx
movq %r14, %rdi
callq 0x3060
movq %rbx, %rdi
movl 0x120(%rsp), %esi
callq 0xd0fc
movq (%rbx), %rsi
movq 0x10(%rbx), %rdi
leaq -0x78a(%rip), %rcx      # 0xdbe5
movl $0x40, %edx
callq 0x30c0
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq