// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/path.c
// Function: annot_sequence_og_type
og_component_v *annot_sequence_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;
    OG_TYPE_t og_t;
    og_component_t *component;
    og_component_v *component_v;

    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);

    MYMALLOC(annot_score, m_gene * 4);
    MYCALLOC(component_v, 1);
    MYCALLOC(component_v->a, n_seg);
    component_v->n = n_seg;
    component_v->m = n_seg;
    for (i = 0; i < n_seg; ++i) {
        component_v->a[i].type = (uint32_t) OG_UNCLASSIFIED;
        if (asg->asmg->vtx[i].del)
            continue;

        MYBZERO(annot_score, m_gene * 4);
        annots = hmm_annot_db_index_query(annot_db, asg_snameid_map[i], &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;
            }
        }

        // 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 (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) {
            // make seg list
            uint32_t *comp_v;
            MYMALLOC(comp_v, 1);
            comp_v[0] = i;
            // make gene list
            // get all genes in the component
            kvec_t(uint64_t) comp_g;
            kv_init(comp_g);
            annots = hmm_annot_db_index_query(annot_db, asg_snameid_map[i], &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
            component = &component_v->a[i];
            component->type = (uint32_t) og_t;
            component->score = a_s[imax];
            component->sscore = a_s[smax];
            component->len = asg->seg[i].len;
            component->nv = 1;
            component->v = comp_v;
            component->ng = comp_g.n;
            component->g = comp_g.a;
            component->asmg = 0;
        }

        if (verbose > 0)
            fprintf(stderr, "[M::%s] sequence %s: size, %d; mito score, %.3f; pltd score, %.3f; mini score, %.3f; classification, %d\n",
                    __func__, asg->seg[i].name, asg->seg[i].len, a_s[OG_MITO], a_s[OG_PLTD], a_s[OG_MINI], og_t);
    }

    free(annot_score);
    free(asg_snameid_map);
    
    if (fix_og) fix_og_misclassification(component_v, verbose);

    return component_v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm0, 0x78(%rsp)
movl %ecx, 0x10(%rsp)
movl %edx, 0x14(%rsp)
cmpl $0x0, (%rdi)
je 0xd4d7
movl 0x8(%rsi), %ebx
testl %r8d, %r8d
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmovnel %r8d, %r15d
movl 0x18(%rdi), %r13d
movq %rdi, %rbp
movq %rsi, 0x20(%rsp)
movl $0x5, %esi
callq 0x17c65
leaq (,%rbx,4), %rdi
callq 0x32f0
movq %rax, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
testq %rbx, %rbx
je 0xcf94
movq 0x38(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rbx
xorl %r14d, %r14d
movq 0x30(%rsp), %r12
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r14), %rsi
movq %rbp, %rdi
callq 0x17aaa
movl %eax, (%r12)
addq $0x18, %r14
addq $0x4, %r12
cmpq %r14, %rbx
jne 0xcf6e
movq %rbp, 0x70(%rsp)
leal (,%r13,4), %edi
shlq $0x3, %rdi
movq %rdi, 0xa0(%rsp)
callq 0x32f0
movq %rax, %r12
movl $0x1, %edi
movl $0x18, %esi
callq 0x3240
movq %rax, %r14
movl $0x40, %esi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x3240
movq %rax, 0x10(%r14)
movq %rbx, (%r14)
movq %r14, 0x28(%rsp)
movq %rbx, 0x8(%r14)
testq %rbx, %rbx
je 0xd4a5
movq %rax, %rbp
cmpl %r13d, %r15d
cmovael %r13d, %r15d
cvtsi2sdl 0xf0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
cmpl $0x1, %r15d
movl %r15d, %eax
adcl $0x0, %eax
movq %rax, 0xb0(%rsp)
xorl %ebx, %ebx
movl %r15d, 0x4(%rsp)
movq %rbx, %rdx
shlq $0x6, %rdx
movl $0x0, (%rbp,%rdx)
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
leaq (%rbx,%rbx,4), %rcx
testb $0x40, 0x23(%rax,%rcx,8)
jne 0xd447
movq %rdx, 0xa8(%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq 0xa0(%rsp), %rdx
callq 0x31b0
movq 0x30(%rsp), %rax
movq %rbx, 0x18(%rsp)
movl (%rax,%rbx,4), %esi
movq 0x70(%rsp), %rdi
movl %esi, 0x8(%rsp)
movq %rsp, %rdx
callq 0x17ec6
testq %rax, %rax
je 0xd106
cmpl $0x0, (%rsp)
je 0xd106
movq %rax, %rbx
xorl %r14d, %r14d
movsd 0x30(%rbx), %xmm0
ucomisd 0x78(%rsp), %xmm0
ja 0xd0f7
cmpl $0x0, 0x14(%rsp)
je 0xd0bb
movq %rbx, %rdi
callq 0x18562
testl %eax, %eax
jne 0xd0f7
cmpl $0x0, 0x10(%rsp)
je 0xd0ce
movq %rbx, %rdi
callq 0x1857c
testl %eax, %eax
jne 0xd0f7
movl 0x48(%rbx), %eax
movl %eax, %ecx
shrl $0x1d, %ecx
andl $0x3, %ecx
imull %r13d, %ecx
andl $0x1fffffff, %eax       # imm = 0x1FFFFFFF
addl %ecx, %eax
movsd 0x38(%rbx), %xmm0
ucomisd (%r12,%rax,8), %xmm0
jbe 0xd0f7
movsd %xmm0, (%r12,%rax,8)
incq %r14
movl (%rsp), %eax
addq $0x50, %rbx
cmpq %rax, %r14
jb 0xd09b
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movl %r14d, %eax
imull %r13d, %eax
leaq (%r12,%rax,8), %rdi
movl $0x8, %edx
movq %r13, %rsi
leaq -0x267(%rip), %rcx      # 0xcece
callq 0x30c0
movl %r15d, (%rsp)
testq %r13, %r13
je 0xd16a
movsd 0x40(%rsp,%r14,8), %xmm0
movq 0xb0(%rsp), %rax
movl %ebx, %ecx
movl %ecx, %edx
addsd (%r12,%rdx,8), %xmm0
incl %ecx
decq %rax
jne 0xd154
movsd %xmm0, 0x40(%rsp,%r14,8)
incq %r14
addl %r13d, %ebx
cmpq $0x4, %r14
jne 0xd11b
xorl %eax, %eax
movsd 0xd3a8(%rip), %xmm0     # 0x1a528
movapd %xmm0, %xmm1
xorl %ecx, %ecx
xorl %ebx, %ebx
movl %ebx, %edx
movsd 0x40(%rsp,%rax,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0xd1a0
movl %eax, %ebx
movl %edx, %ecx
movapd %xmm3, %xmm2
jmp 0xd1bc
movl %edx, %ebx
ucomisd %xmm1, %xmm3
jbe 0xd1b4
movl %eax, %ecx
movapd %xmm0, %xmm2
movapd %xmm3, %xmm0
jmp 0xd1bc
movapd %xmm0, %xmm2
movapd %xmm1, %xmm0
incq %rax
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
cmpq $0x4, %rax
jne 0xd188
movl %ebx, %edx
movsd 0x40(%rsp,%rdx,8), %xmm0
xorl %r9d, %r9d
ucomisd 0x98(%rsp), %xmm0
jb 0xd438
movl %ecx, %esi
ucomisd 0x40(%rsp,%rsi,8), %xmm0
setnp %al
sete %cl
andb %al, %cl
testl %ebx, %ebx
sete %al
orb %cl, %al
jne 0xd438
movq %rsi, 0x88(%rsp)
movq %rdx, 0x90(%rsp)
movl $0x4, %edi
callq 0x32f0
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x80(%rsp)
movl %eax, (%rcx)
movq 0x70(%rsp), %rdi
movl 0x8(%rsp), %esi
movq %rsp, %rdx
callq 0x17ec6
testq %rax, %rax
je 0xd38d
cmpl $0x0, (%rsp)
je 0xd38d
movq %rax, %rbp
xorl %r14d, %r14d
xorl %esi, %esi
movq $0x0, 0x68(%rsp)
movq $0x0, 0x8(%rsp)
movsd 0x30(%rbp), %xmm0
ucomisd 0x78(%rsp), %xmm0
ja 0xd321
cmpl $0x0, 0x14(%rsp)
movq %rsi, %r15
je 0xd298
movq %rbp, %rdi
callq 0x18562
movq %r15, %rsi
testl %eax, %eax
jne 0xd2ae
cmpl $0x0, 0x10(%rsp)
je 0xd2b5
movq %rbp, %rdi
callq 0x1857c
movq %r15, %rsi
testl %eax, %eax
je 0xd2b5
movl 0x4(%rsp), %r15d
jmp 0xd321
cmpq 0x68(%rsp), %rsi
jne 0xd2e8
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x68(%rsp)
leaq (,%rcx,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x3330
movq %r15, %rsi
jmp 0xd2ed
movq 0x8(%rsp), %rax
movl 0x4(%rsp), %r15d
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, 0x8(%rsp)
movq %rcx, (%rax,%rsi,8)
incq %rsi
incq %r14
movl (%rsp), %eax
addq $0x50, %rbp
cmpq %rax, %r14
jb 0xd26e
movl $0x8, %edx
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x1ad(%rip), %rcx       # 0xd4f5
movq %rsi, %r14
callq 0x30c0
testq %r14, %r14
je 0xd3a4
movq %r14, %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %r14d, %r14d
movq (%rbp,%rax,8), %rdx
movq %rdx, %rsi
shrq $0x20, %rsi
cmpl %ecx, %esi
je 0xd381
movl %r14d, %ecx
incl %r14d
movq %rdx, (%rbp,%rcx,8)
movl 0x4(%rbp,%rax,8), %ecx
incq %rax
movl %eax, %edx
cmpq %rdx, %rdi
ja 0xd362
jmp 0xd3a7
xorl %ebp, %ebp
movl $0x8, %edx
xorl %edi, %edi
xorl %esi, %esi
leaq 0x156(%rip), %rcx       # 0xd4f5
callq 0x30c0
xorl %r14d, %r14d
movl %r14d, %esi
shlq $0x3, %rsi
movq %rbp, %rdi
callq 0x3330
movq 0x28(%rsp), %rcx
movq 0x10(%rcx), %rbp
movq 0xa8(%rsp), %rsi
movl %ebx, (%rbp,%rsi)
movq 0x90(%rsp), %rcx
movsd 0x40(%rsp,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rbp,%rsi)
movq 0x88(%rsp), %rcx
movsd 0x40(%rsp,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rbp,%rsi)
movq 0x20(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %ecx
movl %ecx, 0x18(%rbp,%rsi)
movl $0x1, 0x1c(%rbp,%rsi)
movq 0x80(%rsp), %rcx
movq %rcx, 0x20(%rbp,%rsi)
movl %r14d, 0x28(%rbp,%rsi)
movq %rax, 0x30(%rbp,%rsi)
movq $0x0, 0x38(%rbp,%rsi)
movl %ebx, %r9d
cmpl $0x0, 0x100(%rsp)
jg 0xd457
movq 0x18(%rsp), %rbx
incq %rbx
cmpq 0x38(%rsp), %rbx
jne 0xd027
jmp 0xd4a5
movq 0x12b9a(%rip), %rax     # 0x1fff8
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rbx
leaq (%rbx,%rbx,2), %rdx
movq (%rax,%rdx,8), %rcx
movl 0x10(%rax,%rdx,8), %r8d
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
leaq 0xd8b9(%rip), %rsi      # 0x1ad4e
leaq 0xd91b(%rip), %rdx      # 0x1adb7
movb $0x3, %al
callq 0x3290
jmp 0xd447
movq %r12, %rdi
callq 0x3060
movq 0x30(%rsp), %rdi
callq 0x3060
cmpl $0x0, 0xf8(%rsp)
je 0xd4db
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movl 0x100(%rsp), %esi
callq 0xd506
jmp 0xd4e0
xorl %ebx, %ebx
jmp 0xd4e0
movq 0x28(%rsp), %rbx
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq