// File: /workspace/github_c_makefile_star2_O1/c-zhou[P]oatk/path.c
// Function: fix_og_misclassification
static void fix_og_misclassification(og_component_v *component_v, int verbose)
{
    // TODO fix this by maybe improving the specificity of the HMM database
    // revisit to deal with mito misclassification
    // find the best pltd component
    uint32_t i, j, m, n, p_b, p_b1, genid, *mito_gen, *pltd_gen;
    uint64_t x;
    double p_s, p_s1;
    og_component_t *component;
    kvec_t(uint64_t) gen_list;
    OG_TYPE_t og_type;
    
    n = component_v->n;
    kv_init(gen_list);
    // collect best gene hits in each subgraph
    // pack in the component id
    for (i = 0; i < n; ++i) {
        component = &component_v->a[i];
        m = gen_list.n;
        kv_pushn(uint64_t, gen_list, component->g, component->ng);
        for (j = m; j < gen_list.n; ++j) {
            x = gen_list.a[j];
            gen_list.a[j] = (x & 0xFFFFFFFF00000000ULL) | ((uint32_t) x << 16 | i);
        }
    }
    
    if (gen_list.n == 0)
        return;

    // sort by gid and then score - descending order
    qsort(gen_list.a, gen_list.n, sizeof(uint64_t), u64_cmpfunc_r);
    
    MYCALLOC(mito_gen, n);
    MYCALLOC(pltd_gen, n);
    genid = gen_list.a[0] >> 32;
    for (i = 0, j = 0, m = gen_list.n; i < m; ++i) {
        if (gen_list.a[i] >> 32 != genid ||
                i == m - 1) {
            og_type = (gen_list.a[i] >> 32) & 0x3;
            if (og_type != OG_MITO &&
                    og_type != OG_PLTD)
                continue;
            if (i == j || 
                   (double) ((gen_list.a[j+1] >> 16) & 0xFFFFULL) < 
                   (double) ((gen_list.a[j] >> 16) & 0xFFFFULL) * 0.8) {
                x = gen_list.a[j] & 0xFFFFULL;
                if (og_type == OG_MITO)
                    ++mito_gen[x];
                else
                    ++pltd_gen[x];
            }
            genid = gen_list.a[i] >> 32;
            j = i;
        }
    }

    p_b = p_b1 = UINT32_MAX;
    p_s = p_s1 = 0;
    for (i = 0; i < component_v->n; ++i) {
        component = &component_v->a[i];
        if (component->type != OG_PLTD)
            continue;
        if (component->score > p_s && component->len >= COMMON_MIN_PLTD_SIZE) {
            if (component->len <= COMMON_MAX_PLTD_SIZE) {
                p_b = i;
                p_s = component->score;
            }
            p_b1 = i;
            p_s1 = component->score;
        }
    }
    if (p_b == UINT32_MAX) p_b = p_b1;
    if (p_b != UINT32_MAX) {
        // PLTD is likely there
        for (i = 0; i < component_v->n; ++i) {
            if (i == p_b) continue;
            component = &component_v->a[i];
            if (component->type != OG_PLTD)
                continue;
            if (pltd_gen[i] > mito_gen[i] * PLTD_TO_MITO_FST[1])
                continue;
            if (component->score > component->sscore * PLTD_TO_MITO_FST[1])
                continue;
            if (component->score < component->sscore * PLTD_TO_MITO_FST[0] ||
                    (component->len < COMMON_MIN_PLTD_SIZE ||
                     component->len > COMMON_MAX_PLTD_SIZE)) {
                double tmp = component->score;
                component->score = component->sscore;
                component->sscore = tmp;
                component->type = component->score > .0? OG_MITO : OG_UNCLASSIFIED;
                if (verbose > 0)
                    fprintf(stderr, "[M::%s] change subgraph organelle type annotation: PLTD [S=%.3f G=%u] -> MITO [S=%.3f G=%u]\n",
                            __func__, component->sscore, pltd_gen[i], component->score, mito_gen[i]);
            }
        }
    }

    free(mito_gen);
    free(pltd_gen);
    kv_destroy(gen_list);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, 0xc(%rsp)
movl (%rdi), %eax
movq %rax, 0x10(%rsp)
testq %rax, %rax
movq %rdi, (%rsp)
je 0xd1e3
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x10(%rdi), %rbx
movq %r12, %r13
shlq $0x6, %r13
movl 0x28(%rbx,%r13), %eax
addq %rbp, %rax
cmpq %r14, %rax
jbe 0xd18f
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r14
shrq $0x10, %r14
orq %rax, %r14
leaq 0x8(,%r14,8), %rsi
incq %r14
movq %r15, %rdi
callq 0x3330
movq %rax, %r15
addq %r13, %rbx
leaq (%r15,%rbp,8), %rdi
movq 0x30(%rbx), %rsi
movl 0x28(%rbx), %edx
shlq $0x3, %rdx
callq 0x32c0
movl 0x28(%rbx), %ebx
addq %rbp, %rbx
movl %ebp, %eax
cmpq %rax, %rbx
jbe 0xd1cc
incl %ebp
movl (%r15,%rax,8), %ecx
shll $0x10, %ecx
orl %r12d, %ecx
movl %ecx, (%r15,%rax,8)
movl %ebp, %eax
incl %ebp
cmpq %rax, %rbx
ja 0xd1b5
incq %r12
movq %rbx, %rbp
cmpq 0x10(%rsp), %r12
movq (%rsp), %rdi
jne 0xd12d
jmp 0xd1e8
xorl %r15d, %r15d
xorl %ebx, %ebx
testq %rbx, %rbx
je 0xd324
leaq -0x10d(%rip), %rcx      # 0xd0eb
movl $0x8, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x30c0
movl $0x4, %esi
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x3240
movq %rax, %r12
movl $0x4, %esi
movq %r14, %rdi
callq 0x3240
movq %rax, %r13
testl %ebx, %ebx
je 0xd2c1
movl 0x4(%r15), %r8d
leal -0x1(%rbx), %eax
movl %ebx, %ecx
xorl %edx, %edx
movsd 0xc2e8(%rip), %xmm0     # 0x19530
xorl %edi, %edi
movl 0x4(%r15,%rdx,8), %esi
cmpq %rdx, %rax
je 0xd25c
movl %r8d, %r9d
cmpl %r9d, %esi
je 0xd26d
movl %esi, %r9d
andl $0x3, %r9d
leal -0x3(%r9), %r10d
cmpl $-0x2, %r10d
jae 0xd272
movl %r8d, %esi
jmp 0xd2b6
movl %edi, %r8d
cmpq %r8, %rdx
je 0xd2a0
incl %edi
movzwl 0x2(%r15,%rdi,8), %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
movzwl 0x2(%r15,%r8,8), %edi
xorps %xmm2, %xmm2
cvtsi2sd %edi, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jbe 0xd2b4
movzwl (%r15,%r8,8), %edi
cmpl $0x1, %r9d
movq %r13, %r8
cmoveq %r12, %r8
incl (%r8,%rdi,4)
movl %edx, %edi
incq %rdx
movl %esi, %r8d
cmpq %rdx, %rcx
jne 0xd24a
movq (%rsp), %r9
movq (%r9), %rcx
testq %rcx, %rcx
je 0xd333
movq 0x10(%r9), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorpd %xmm0, %xmm0
movl $0x1, %esi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x2, (%rdx)
jne 0xd315
movsd 0x8(%rdx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xd315
movl 0x18(%rdx), %edi
cmpl $0x13880, %edi          # imm = 0x13880
jb 0xd315
leal -0x1(%rsi), %ebp
cmpl $0x3d091, %edi          # imm = 0x3D091
cmovbl %ebp, %eax
jb 0xd311
movapd %xmm0, %xmm1
movapd %xmm1, %xmm0
movl %esi, %edi
addq $0x40, %rdx
incl %esi
cmpq %rdi, %rcx
ja 0xd2e4
jmp 0xd33d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
cmovnel %eax, %ebp
cmpl $-0x1, %ebp
je 0xd450
cmpq $0x0, (%r9)
je 0xd450
negl %ebp
movl $0x1, %ebx
xorl %r8d, %r8d
movsd 0xc1d0(%rip), %xmm3     # 0x19538
movsd 0xc150(%rip), %xmm4     # 0x194c0
xorpd %xmm5, %xmm5
leaq 0xd1cc(%rip), %rsi      # 0x1a547
leaq 0xd222(%rip), %rdx      # 0x1a5a4
movl $0xfffc2f6f, %r14d      # imm = 0xFFFC2F6F
leal (%rbx,%rbp), %eax
cmpl $0x1, %eax
je 0xd407
movq 0x10(%r9), %rax
movq %r8, %rdi
shlq $0x6, %rdi
cmpl $0x2, (%rax,%rdi)
jne 0xd407
movl (%r13,%r8,4), %ecx
movl (%r12,%r8,4), %r8d
leaq (%r8,%r8,4), %r10
cmpq %rcx, %r10
jb 0xd407
addq %rdi, %rax
movsd 0x8(%rax), %xmm0
movsd 0x10(%rax), %xmm1
movapd %xmm1, %xmm2
mulsd %xmm3, %xmm2
ucomisd %xmm2, %xmm0
ja 0xd407
movapd %xmm1, %xmm2
mulsd %xmm4, %xmm2
ucomisd %xmm0, %xmm2
ja 0xd3ea
movl 0x18(%rax), %edi
addl %r14d, %edi
cmpl $0xfffd67ee, %edi       # imm = 0xFFFD67EE
ja 0xd407
xorl %edi, %edi
ucomisd %xmm5, %xmm1
seta %dil
movsd %xmm1, 0x8(%rax)
movsd %xmm0, 0x10(%rax)
movl %edi, (%rax)
cmpl $0x0, 0xc(%rsp)
jg 0xd417
movl %ebx, %r8d
incl %ebx
cmpq %r8, (%r9)
ja 0xd388
jmp 0xd450
movq 0x11bda(%rip), %rax     # 0x1eff8
movq (%rax), %rdi
movb $0x2, %al
callq 0x3290
leaq 0xd175(%rip), %rdx      # 0x1a5a4
leaq 0xd111(%rip), %rsi      # 0x1a547
xorpd %xmm5, %xmm5
movsd 0xc07e(%rip), %xmm4     # 0x194c0
movsd 0xc0ee(%rip), %xmm3     # 0x19538
movq (%rsp), %r9
jmp 0xd407
movq %r12, %rdi
callq 0x3060
movq %r13, %rdi
callq 0x3060
movq %r15, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3060