// File: /workspace/github_c_makefile_star2_O3/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, 0x14(%rsp)
movq %rdi, 0x8(%rsp)
movl (%rdi), %r13d
testq %r13, %r13
je 0xd84b
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq %r12, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rbp
movl 0x28(%rax,%rcx), %edx
leaq (%rbx,%rdx), %rax
cmpq %r14, %rax
jbe 0xd5a2
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
movl 0x28(%rbp), %edx
leaq (%r15,%rbx,8), %rdi
movq 0x30(%rbp), %rsi
shlq $0x3, %rdx
callq 0x32c0
movl 0x28(%rbp), %ebp
addq %rbx, %rbp
movl %ebx, %eax
cmpq %rax, %rbp
jbe 0xd5d9
incl %ebx
movl (%r15,%rax,8), %ecx
shll $0x10, %ecx
orl %r12d, %ecx
movl %ecx, (%r15,%rax,8)
movl %ebx, %eax
incl %ebx
cmpq %rax, %rbp
ja 0xd5c2
incq %r12
movq %rbp, %rbx
cmpq %r13, %r12
jne 0xd534
testq %rbp, %rbp
je 0xd84b
leaq -0x103(%rip), %rcx      # 0xd4f5
movl $0x8, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x30c0
movl $0x4, %esi
movq %r13, %rdi
callq 0x3240
movq %rax, %r12
movl $0x4, %esi
movq %r13, %rdi
callq 0x3240
movq %rax, %r13
movl 0x4(%r15), %edi
leal -0x1(%rbp), %eax
xorl %ecx, %ecx
movsd 0xcef7(%rip), %xmm0     # 0x1a530
xorl %esi, %esi
movq (%r15,%rcx,8), %r8
movq %r8, %rdx
shrq $0x20, %rdx
cmpq %rcx, %rax
je 0xd653
movl %edi, %r9d
cmpl %r9d, %edx
je 0xd664
movl %edx, %r9d
andl $0x3, %r9d
leal -0x3(%r9), %r10d
cmpl $-0x2, %r10d
jae 0xd668
movl %edi, %edx
jmp 0xd6ad
movl %esi, %edi
cmpq %rdi, %rcx
je 0xd699
incl %esi
movzwl 0x2(%r15,%rsi,8), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movq (%r15,%rdi,8), %r8
movl %r8d, %esi
shrl $0x10, %esi
xorps %xmm2, %xmm2
cvtsi2sd %esi, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jbe 0xd6ab
movzwl %r8w, %esi
cmpl $0x1, %r9d
movq %r13, %rdi
cmoveq %r12, %rdi
incl (%rdi,%rsi,4)
movl %ecx, %esi
incq %rcx
movl %edx, %edi
cmpq %rcx, %rbp
jne 0xd63b
movq 0x8(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0xd85a
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorpd %xmm0, %xmm0
movl $0x1, %esi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x2, (%rdx)
jne 0xd715
movsd 0x8(%rdx), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xd715
movl 0x18(%rdx), %edi
cmpl $0x13880, %edi          # imm = 0x13880
jb 0xd715
leal -0x1(%rsi), %ebp
cmpl $0x3d091, %edi          # imm = 0x3D091
cmovbl %ebp, %ecx
jb 0xd711
movapd %xmm0, %xmm1
movapd %xmm1, %xmm0
movl %esi, %edi
addq $0x40, %rdx
incl %esi
cmpq %rdi, %rax
ja 0xd6e4
cmpl $-0x1, %ecx
cmovnel %ecx, %ebp
cmpl $-0x1, %ebp
je 0xd85a
negl %ebp
movl $0x1, %ebx
xorl %r8d, %r8d
movsd 0xcdf5(%rip), %xmm3     # 0x1a538
movsd 0xcd75(%rip), %xmm4     # 0x1a4c0
xorpd %xmm5, %xmm5
movq 0x128a2(%rip), %r10     # 0x1fff8
leaq 0xddee(%rip), %rsi      # 0x1b54b
leaq 0xde44(%rip), %rdx      # 0x1b5a8
movl $0xfffc2f6f, %r14d      # imm = 0xFFFC2F6F
leal (%rbx,%rbp), %ecx
cmpl $0x1, %ecx
je 0xd7f9
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %r11
movq %r8, %rdi
shlq $0x6, %rdi
cmpl $0x2, (%r11,%rdi)
jne 0xd7f9
movl (%r13,%r8,4), %ecx
movl (%r12,%r8,4), %r8d
leaq (%r8,%r8,4), %r9
cmpq %rcx, %r9
jb 0xd7f9
addq %rdi, %r11
movsd 0x8(%r11), %xmm0
movsd 0x10(%r11), %xmm1
movapd %xmm1, %xmm2
mulsd %xmm3, %xmm2
ucomisd %xmm2, %xmm0
ja 0xd7f9
movapd %xmm1, %xmm2
mulsd %xmm4, %xmm2
ucomisd %xmm0, %xmm2
ja 0xd7d9
movl 0x18(%r11), %edi
addl %r14d, %edi
cmpl $0xfffd67ee, %edi       # imm = 0xFFFD67EE
ja 0xd7f9
xorl %edi, %edi
ucomisd %xmm5, %xmm1
seta %dil
movsd %xmm1, 0x8(%r11)
movsd %xmm0, 0x10(%r11)
movl %edi, (%r11)
cmpl $0x0, 0x14(%rsp)
jg 0xd809
movl %ebx, %r8d
incl %ebx
cmpq %r8, %rax
ja 0xd76a
jmp 0xd85a
movq (%r10), %rdi
movb $0x2, %al
movq %rsi, %r14
callq 0x3290
leaq 0xdd8b(%rip), %rdx      # 0x1b5a8
movq 0x127d4(%rip), %r10     # 0x1fff8
movq %r14, %rsi
movl $0xfffc2f6f, %r14d      # imm = 0xFFFC2F6F
xorpd %xmm5, %xmm5
movsd 0xcc87(%rip), %xmm4     # 0x1a4c0
movsd 0xccf7(%rip), %xmm3     # 0x1a538
movq 0x8(%rsp), %rax
movq (%rax), %rax
jmp 0xd7f9
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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