// File: /workspace/github_c_makefile_star2_O2/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
movsd %xmm0, 0xa8(%rsp)
movl %ecx, 0x54(%rsp)
movl %edx, 0x58(%rsp)
movl (%rdi), %eax
movq %rax, 0x98(%rsp)
testq %rax, %rax
je 0xc9bf
movl %r8d, %ebx
movq %rsi, %rbp
movq %rdi, %r14
movl %r9d, 0x28(%rsp)
movl 0x110(%rsp), %r15d
movq 0x10(%rdi), %rax
movq %rax, 0x90(%rsp)
movq 0x8(%rsi), %rax
movq %rax, 0x30(%rsp)
movl 0x1c(%rdi), %r12d
pushq $0x5
popq %rsi
callq 0x1387d
leaq (,%r12,4), %rdi
callq 0x32f0
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
cmpq %r13, %r12
je 0xc3ca
movq 0x28(%r14), %rax
movq (%rax,%r13,8), %rsi
movq %rbp, %rdi
callq 0x7588
movq 0x48(%rsp), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
jmp 0xc3a7
movq %rbp, 0xa0(%rsp)
movq 0x90(%rsp), %r14
movl 0x4c(%r14), %ebp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0xb0(%rsp)
movq 0x30(%rsp), %rax
leal (,%rax,4), %edi
pushq $0x8
popq %rsi
callq 0x3240
movq %rax, %rsi
movq 0x98(%rsp), %r12
movl 0x28(%rsp), %r13d
movq %rax, 0x40(%rsp)
movl %ebp, %ecx
subq $0x1, %r12
jb 0xc4ef
movl 0x4c(%r14), %eax
cmpl %ebp, %eax
je 0xc474
movq 0x48(%rsp), %rdx
movl (%rdx,%rcx,4), %ecx
cmpl $-0x1, %ecx
je 0xc459
shll $0x2, %ecx
movapd 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movups %xmm1, 0x10(%rsi,%rcx,8)
movupd %xmm0, (%rsi,%rcx,8)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0xb0(%rsp)
movl %eax, %ebp
movsd 0x30(%r14), %xmm0
ucomisd 0xa8(%rsp), %xmm0
ja 0xc4e6
cmpl $0x0, 0x58(%rsp)
je 0xc49d
movq %r14, %rdi
callq 0x14057
movq 0x40(%rsp), %rsi
testl %eax, %eax
jne 0xc4e6
cmpl $0x0, 0x54(%rsp)
je 0xc4b5
movq %r14, %rdi
callq 0x14071
movq 0x40(%rsp), %rsi
testl %eax, %eax
jne 0xc4e6
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 0xc4e6
movsd 0x38(%r14), %xmm0
addsd 0x60(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x60(%rsp,%rax,8)
addq $0x50, %r14
jmp 0xc420
movq 0x48(%rsp), %rax
movl (%rax,%rcx,4), %eax
cmpl $-0x1, %eax
je 0xc514
shll $0x2, %eax
movapd 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movups %xmm1, 0x10(%rsi,%rax,8)
movupd %xmm0, (%rsi,%rax,8)
pushq $0x1
popq %rbx
pushq $0x18
popq %rsi
movq %rbx, %rdi
callq 0x3240
movq %rax, 0x88(%rsp)
movq 0x30(%rsp), %r14
movl %r14d, %ebp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3240
cvtsi2sd %r15d, %xmm0
movsd %xmm0, 0xd0(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
movq %rax, 0x38(%rsp)
movl %ecx, 0x2c(%rsp)
cmpl %r14d, %ecx
jae 0xc9a0
movl %ecx, %edx
cmpb $0x0, (%rax,%rdx)
jne 0xc93c
movq 0xa0(%rsp), %rcx
movq 0x20(%rcx), %rdi
movq 0x10(%rdi), %rcx
imulq $0x28, %rdx, %rdx
testb $0x40, 0x23(%rcx,%rdx)
jne 0xc93c
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x9778
movq %rax, %rbx
movl $0x1, (%rsp)
movq %rax, %rdi
leaq 0x2c(%rsp), %rsi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x12281
movq 0x38(%rsp), %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x70(%rsp)
movq %rbx, 0x20(%rsp)
movq 0x10(%rbx), %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x40(%rsp), %rsi
cmpq %rbp, %r14
je 0xc641
imulq $0x28, %r14, %rax
testb $0x40, 0x23(%rbx,%rax)
jne 0xc63c
movl %r14d, %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
shlq $0x5, %rcx
addq %rsi, %rcx
xorl %edx, %edx
cmpq $0x4, %rdx
je 0xc62f
movsd (%rcx,%rdx,8), %xmm0
addsd 0x60(%rsp,%rdx,8), %xmm0
movsd %xmm0, 0x60(%rsp,%rdx,8)
incq %rdx
jmp 0xc613
incl %r12d
addl 0x18(%rbx,%rax), %r15d
movb $0x1, (%rdi,%r14)
incq %r14
jmp 0xc5f1
leaq 0x60(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0xb7ff
movl %eax, %esi
movsd 0x60(%rsp,%rsi,8), %xmm0
cmpl %r13d, %r15d
jge 0xc668
ucomisd 0xd0(%rsp), %xmm0
jb 0xc683
movl 0x5c(%rsp), %ecx
ucomisd 0x60(%rsp,%rcx,8), %xmm0
setp %dl
setne %cl
orb %dl, %cl
testl %eax, %eax
setne %dl
testb %dl, %cl
jne 0xc69c
movq 0x20(%rsp), %rdi
callq 0xf9e0
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rax
jmp 0xc93c
movq %rsi, 0xc8(%rsp)
movl %eax, 0x50(%rsp)
movslq %r12d, %rdi
shlq $0x2, %rdi
callq 0x32f0
addq $0x20, %rbx
xorl %edi, %edi
xorl %ecx, %ecx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %rdi, %rbp
je 0xc6dc
testb $0x40, 0x3(%rbx)
jne 0xc6d3
movl %ecx, %edx
incl %ecx
movl %edi, (%rax,%rdx,4)
incq %rdi
addq $0x28, %rbx
jmp 0xc6c1
movq %rax, 0xc0(%rsp)
movq 0x90(%rsp), %r14
movq 0x98(%rsp), %rbx
xorl %r13d, %r13d
xorl %edi, %edi
movq $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rdx
subq $0x1, %rbx
jb 0xc816
movl 0x4c(%r14), %eax
movq 0x48(%rsp), %rcx
movl (%rcx,%rax,4), %eax
cmpq %rsi, %rax
je 0xc80d
movq 0x10(%rdx), %rcx
imulq $0x28, %rax, %rax
testb $0x40, 0x23(%rcx,%rax)
jne 0xc80d
movsd 0x30(%r14), %xmm0
ucomisd 0xa8(%rsp), %xmm0
ja 0xc80d
cmpl $0x0, 0x58(%rsp)
movq %rdi, 0x18(%rsp)
je 0xc779
movq %r14, %rdi
callq 0x14057
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xc80d
cmpl $0x0, 0x54(%rsp)
je 0xc79b
movq %r14, %rdi
callq 0x14071
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0xc80d
cmpq %rdi, 0x10(%rsp)
jne 0xc7d7
leaq (%rdi,%rdi), %rcx
testq %rdi, %rdi
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x18(%rsp)
leaq (,%rcx,8), %rsi
movq %r13, %rdi
callq 0x3330
movq 0x20(%rsp), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rax, %r13
movq 0x18(%rsp), %rdi
movl 0x48(%r14), %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(%r14), %rcx
movl %ecx, %ecx
orq %rax, %rcx
movq 0x10(%rsp), %rax
movq %rcx, (%r13,%rax,8)
incq %rax
movq %rax, 0x10(%rsp)
addq $0x50, %r14
jmp 0xc707
movq %r13, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
pushq $0x8
popq %rdx
leaq -0xfc7(%rip), %rcx      # 0xb864
callq 0x30c0
xorl %r14d, %r14d
pushq $-0x1
popq %rcx
xorl %eax, %eax
movl %eax, %eax
movl %r14d, %esi
cmpq %rax, %rbx
jbe 0xc863
movq (%r13,%rax,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
cmpl %ecx, %edi
je 0xc85f
incl %r14d
movq %rdx, (%r13,%rsi,8)
movl 0x4(%r13,%rax,8), %ecx
incl %eax
jmp 0xc838
shlq $0x3, %rsi
movq %r13, %rdi
callq 0x3330
movq %rax, %rbx
movq 0x88(%rsp), %rcx
movq (%rcx), %r13
movq 0x10(%rcx), %rdi
cmpq 0x8(%rcx), %r13
jne 0xc8b5
leaq (,%r13,2), %rsi
testq %r13, %r13
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rcx)
shlq $0x6, %rsi
callq 0x3330
movq %rax, %rdi
movq 0x88(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x38(%rsp), %rax
movq 0x20(%rsp), %rdx
movl 0x50(%rsp), %esi
leaq 0x1(%r13), %r9
shlq $0x6, %r13
movq %r9, 0x10(%rsp)
movq %r9, (%rcx)
movl %esi, (%rdi,%r13)
movq 0xc8(%rsp), %rcx
movsd 0x60(%rsp,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rdi,%r13)
movl 0x5c(%rsp), %ecx
movsd 0x60(%rsp,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rdi,%r13)
movl %r15d, 0x18(%rdi,%r13)
movl %r12d, 0x1c(%rdi,%r13)
movq 0xc0(%rsp), %rcx
movq %rcx, 0x20(%rdi,%r13)
movl %r14d, 0x28(%rdi,%r13)
movq %rbx, 0x30(%rdi,%r13)
movq %rdi, 0x18(%rsp)
movq %rdx, 0x38(%rdi,%r13)
cmpl $0x0, 0x120(%rsp)
jg 0xc947
movl 0x28(%rsp), %r13d
movq 0x30(%rsp), %r14
movl 0x2c(%rsp), %ecx
incl %ecx
jmp 0xc564
movq 0x106aa(%rip), %rax     # 0x1cff8
movq (%rax), %rdi
movq 0xa0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %rcx
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movl %esi, (%rsp)
leaq 0xa459(%rip), %rsi      # 0x16dde
leaq 0xa4e9(%rip), %rdx      # 0x16e75
movl %r12d, %r8d
movl %r15d, %r9d
movb $0x3, %al
callq 0x3290
movq 0x38(%rsp), %rax
jmp 0xc932
movq %rax, %rdi
callq 0x3060
movq 0x118(%rsp), %rax
testq %rax, %rax
je 0xc9c3
movq 0x40(%rsp), %rcx
movq %rcx, (%rax)
jmp 0xc9cd
xorl %eax, %eax
jmp 0xca10
movq 0x40(%rsp), %rdi
callq 0x3060
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r15
movq 0x48(%rsp), %rdi
callq 0x3060
movq 0x88(%rsp), %rbx
movq %rbx, %rdi
movl 0x120(%rsp), %esi
callq 0xb875
leaq -0x6f8(%rip), %rcx      # 0xc307
pushq $0x40
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x30c0
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq