// File: /workspace/github_c_makefile_star2_O3/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, 0x7c(%rsp)
movsd %xmm0, 0xa8(%rsp)
movl %ecx, 0x38(%rsp)
movl %edx, 0x3c(%rsp)
movl (%rdi), %eax
movq %rax, 0x90(%rsp)
testq %rax, %rax
je 0xe763
movl %r8d, %ebx
movq %rsi, %rbp
movq %rdi, %r14
movq 0x10(%rdi), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rsi), %rax
movq %rax, 0x20(%rsp)
movl 0x1c(%rdi), %r15d
movl $0x5, %esi
callq 0x17c65
leaq (,%r15,4), %rdi
callq 0x32f0
movq %rax, %r13
testq %r15, %r15
je 0xe0fb
movq 0x28(%r14), %r14
xorl %r12d, %r12d
movq (%r14,%r12,8), %rsi
movq %rbp, %rdi
callq 0x8bd0
movl %eax, (%r13,%r12,4)
incq %r12
cmpq %r12, %r15
jne 0xe0e2
movq %rbp, 0xa0(%rsp)
movl 0x110(%rsp), %ebp
movq 0x88(%rsp), %r14
movl 0x4c(%r14), %r15d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0xb0(%rsp)
movq 0x20(%rsp), %rax
leal (,%rax,4), %edi
movl $0x8, %esi
callq 0x3240
movq %rax, %rsi
movl $0x0, 0xc(%rsp)
movq 0x90(%rsp), %r12
movq %rax, 0x30(%rsp)
movl 0x4c(%r14), %eax
cmpl %r15d, %eax
je 0xe1a9
movl %r15d, %ecx
movl (%r13,%rcx,4), %ecx
cmpl $-0x1, %ecx
je 0xe18d
shll $0x2, %ecx
movapd 0x40(%rsp), %xmm0
movapd 0x50(%rsp), %xmm1
movupd %xmm1, 0x10(%rsi,%rcx,8)
movupd %xmm0, (%rsi,%rcx,8)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0xb0(%rsp)
movl %eax, %r15d
movsd 0x30(%r14), %xmm0
ucomisd 0xa8(%rsp), %xmm0
ja 0xe21b
cmpl $0x0, 0x3c(%rsp)
je 0xe1d2
movq %r14, %rdi
callq 0x18562
movq 0x30(%rsp), %rsi
testl %eax, %eax
jne 0xe21b
cmpl $0x0, 0x38(%rsp)
je 0xe1ea
movq %r14, %rdi
callq 0x1857c
movq 0x30(%rsp), %rsi
testl %eax, %eax
jne 0xe21b
movl 0x48(%r14), %eax
shrl $0x1d, %eax
andl $0x3, %eax
movl 0xb0(%rsp,%rax,4), %ecx
leal 0x1(%rcx), %edx
movl %edx, 0xb0(%rsp,%rax,4)
cmpl %ebx, %ecx
jge 0xe21b
movsd 0x38(%r14), %xmm0
addsd 0x40(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x40(%rsp,%rax,8)
addq $0x50, %r14
decq %r12
jne 0xe15d
movl %r15d, %eax
movl (%r13,%rax,4), %eax
cmpl $-0x1, %eax
je 0xe24f
shll $0x2, %eax
movapd 0x40(%rsp), %xmm0
movapd 0x50(%rsp), %xmm1
movupd %xmm1, 0x10(%rsi,%rax,8)
movupd %xmm0, (%rsi,%rax,8)
movl $0x1, %edi
movl $0x18, %esi
callq 0x3240
movq %rax, 0x60(%rsp)
movq 0x20(%rsp), %rbx
movl %ebx, %edi
movl $0x1, %esi
callq 0x3240
movl $0x0, 0xc(%rsp)
xorl %ecx, %ecx
testl %ebx, %ebx
je 0xe767
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0xd0(%rsp)
movl %ebx, %ebp
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %r13, 0x70(%rsp)
movq %rax, 0x28(%rsp)
movl %ecx, %edx
cmpb $0x0, (%rax,%rdx)
jne 0xe6f6
movq 0xa0(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq 0x10(%rdi), %rcx
leaq (%rdx,%rdx,4), %rdx
testb $0x40, 0x23(%rcx,%rdx,8)
jne 0xe6f6
xorl %ebx, %ebx
xorl %esi, %esi
callq 0xb1fb
movq %rax, %r14
movl $0x1, (%rsp)
movq %rax, %rdi
leaq 0xc(%rsp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x14ff2
movq %r14, %rdi
movq 0x28(%rsp), %r8
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x50(%rsp)
movq 0x10(%r14), %r12
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x30(%rsp), %rsi
leaq (%rbx,%rbx,4), %rax
testb $0x40, 0x23(%r12,%rax,8)
jne 0xe378
movl %ebx, %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
shlq $0x5, %rcx
addq %rsi, %rcx
xorl %edx, %edx
movupd (%rcx,%rdx,8), %xmm0
addpd 0x40(%rsp,%rdx,8), %xmm0
movapd %xmm0, 0x40(%rsp,%rdx,8)
addq $0x2, %rdx
cmpq $0x4, %rdx
jne 0xe350
incl %r15d
addl 0x18(%r12,%rax,8), %r14d
movb $0x1, (%r8,%rbx)
incq %rbx
cmpq %rbp, %rbx
jne 0xe333
xorl %ecx, %ecx
movsd 0xc19e(%rip), %xmm0     # 0x1a528
movapd %xmm0, %xmm1
xorl %eax, %eax
xorl %ebx, %ebx
movl %ebx, %edx
movsd 0x40(%rsp,%rcx,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0xe3aa
movl %ecx, %ebx
movl %edx, %eax
movapd %xmm3, %xmm2
jmp 0xe3c6
movl %edx, %ebx
ucomisd %xmm1, %xmm3
jbe 0xe3be
movl %ecx, %eax
movapd %xmm0, %xmm2
movapd %xmm3, %xmm0
jmp 0xe3c6
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
incq %rcx
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
cmpq $0x4, %rcx
jne 0xe392
movl %ebx, %edx
movsd 0x40(%rsp,%rdx,8), %xmm0
cmpl 0x7c(%rsp), %r14d
jge 0xe3f1
ucomisd 0xd0(%rsp), %xmm0
jb 0xe40c
movl %eax, %esi
ucomisd 0x40(%rsp,%rsi,8), %xmm0
setnp %al
sete %cl
andb %al, %cl
testl %ebx, %ebx
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0xe420
callq 0x11ef0
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %rax
jmp 0xe6f6
movq %rsi, 0xc8(%rsp)
movq %rdx, 0x80(%rsp)
movq %rdi, 0x10(%rsp)
movslq %r15d, %rdi
shlq $0x2, %rdi
callq 0x32f0
addq $0x20, %r12
xorl %edi, %edi
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
testb $0x40, 0x3(%r12)
jne 0xe45e
movl %ecx, %edx
incl %ecx
movl %edi, (%rax,%rdx,4)
incq %rdi
addq $0x28, %r12
cmpq %rdi, %rbp
jne 0xe44f
movq 0x90(%rsp), %r12
movq 0x88(%rsp), %r13
xorl %esi, %esi
movq $0x0, 0x98(%rsp)
movq $0x0, 0x68(%rsp)
movq 0x10(%rsp), %rdx
movq %rax, 0xc0(%rsp)
movl 0x4c(%r13), %eax
movq 0x70(%rsp), %rcx
movl (%rcx,%rax,4), %eax
cmpq %r8, %rax
je 0xe5a5
movq 0x10(%rdx), %rcx
leaq (%rax,%rax,4), %rax
testb $0x40, 0x23(%rcx,%rax,8)
jne 0xe5a5
movsd 0x30(%r13), %xmm0
ucomisd 0xa8(%rsp), %xmm0
ja 0xe5a5
cmpl $0x0, 0x3c(%rsp)
movq %rsi, 0x18(%rsp)
je 0xe507
movq %r13, %rdi
callq 0x18562
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xe5a5
cmpl $0x0, 0x38(%rsp)
je 0xe52a
movq %r13, %rdi
callq 0x1857c
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xe5a5
cmpq 0x98(%rsp), %rsi
jne 0xe570
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x98(%rsp)
leaq (,%rcx,8), %rsi
movq 0x68(%rsp), %rdi
callq 0x3330
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
jmp 0xe575
movq 0x68(%rsp), %rax
movl 0x48(%r13), %edi
movl %edi, %ecx
andl $0x1fffffff, %ecx       # imm = 0x1FFFFFFF
shrl $0x1d, %edi
andl $0x3, %edi
leal (%rdi,%rcx,4), %edi
shlq $0x20, %rdi
cvttsd2si 0x38(%r13), %rcx
movl %ecx, %ecx
orq %rdi, %rcx
movq %rax, 0x68(%rsp)
movq %rcx, (%rax,%rsi,8)
incq %rsi
addq $0x50, %r13
decq %r12
jne 0xe49e
movl $0x8, %edx
movq 0x68(%rsp), %r12
movq %r12, %rdi
leaq -0x10d1(%rip), %rcx     # 0xd4f5
movq %rsi, %r13
callq 0x30c0
testq %r13, %r13
je 0xe60a
movq %r13, %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %r13d, %r13d
movq (%r12,%rax,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpl %ecx, %esi
je 0xe5fe
movl %r13d, %ecx
incl %r13d
movq %rdx, (%r12,%rcx,8)
movl 0x4(%r12,%rax,8), %ecx
incq %rax
movl %eax, %edx
cmpq %rdx, %rdi
ja 0xe5e0
jmp 0xe60d
xorl %r13d, %r13d
movl %r13d, %esi
shlq $0x3, %rsi
movq %r12, %rdi
callq 0x3330
movq %rax, %r9
movq 0x60(%rsp), %r10
movq (%r10), %r12
movq 0x10(%r10), %rdi
cmpq 0x8(%r10), %r12
jne 0xe66b
leaq (%r12,%r12), %rsi
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r10)
shlq $0x6, %rsi
movq %r9, 0x18(%rsp)
callq 0x3330
movq 0x60(%rsp), %r10
movq 0x18(%rsp), %r9
movq %rax, %rsi
movq %rax, 0x10(%r10)
movq 0x28(%rsp), %rax
jmp 0xe673
movq 0x28(%rsp), %rax
movq %rdi, %rsi
movq 0x10(%rsp), %rcx
movq 0x80(%rsp), %rdx
leaq 0x1(%r12), %r8
shlq $0x6, %r12
movq %r8, 0x10(%rsp)
movq %r8, (%r10)
movl %ebx, (%rsi,%r12)
movsd 0x40(%rsp,%rdx,8), %xmm0
movsd %xmm0, 0x8(%rsi,%r12)
movq 0xc8(%rsp), %rdx
movsd 0x40(%rsp,%rdx,8), %xmm0
movsd %xmm0, 0x10(%rsi,%r12)
movl %r14d, 0x18(%rsi,%r12)
movl %r15d, 0x1c(%rsi,%r12)
movq 0xc0(%rsp), %rdx
movq %rdx, 0x20(%rsi,%r12)
movl %r13d, 0x28(%rsi,%r12)
movq %r9, 0x30(%rsi,%r12)
movq %rsi, 0x18(%rsp)
movq %rcx, 0x38(%rsi,%r12)
cmpl $0x0, 0x120(%rsp)
jg 0xe70a
movq 0x70(%rsp), %r13
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %ecx
incl %ecx
movl %ecx, 0xc(%rsp)
cmpl %ebx, %ecx
jb 0xe2b4
jmp 0xe779
movq 0x118e7(%rip), %rax     # 0x1fff8
movq (%rax), %rdi
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xc(%rsp), %ecx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rcx
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movl %ebx, (%rsp)
leaq 0xc686(%rip), %rsi      # 0x1adce
leaq 0xc716(%rip), %rdx      # 0x1ae65
movl %r15d, %r8d
movl %r14d, %r9d
movb $0x3, %al
callq 0x3290
movq 0x28(%rsp), %rax
jmp 0xe6ec
xorl %ebx, %ebx
jmp 0xe7d9
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %rax, %rdi
callq 0x3060
movq 0x118(%rsp), %rax
testq %rax, %rax
je 0xe798
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
jmp 0xe7a2
movq 0x30(%rsp), %rdi
callq 0x3060
movq 0x60(%rsp), %rbx
movq %r13, %rdi
callq 0x3060
movq %rbx, %rdi
movl 0x120(%rsp), %esi
callq 0xd506
leaq -0x784(%rip), %rcx      # 0xe041
movl $0x40, %edx
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x30c0
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq