// File: /workspace/github_c_makefile_star2_O2/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)
movq %rdi, (%rsp)
movl (%rdi), %eax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq 0x10(%rsp), %rbx
je 0xb955
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq %rbx, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %r14
movl 0x28(%rax,%rcx), %edx
leaq (%rdx,%rbp), %rax
cmpq %r12, %rax
jbe 0xb915
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, %r12
shrq $0x10, %r12
orq %rax, %r12
leaq 0x8(,%r12,8), %rsi
incq %r12
movq %r15, %rdi
callq 0x3330
movq %rax, %r15
movl 0x28(%r14), %edx
movl %ebp, %r13d
leaq (%r15,%rbp,8), %rdi
movq 0x30(%r14), %rsi
shlq $0x3, %rdx
callq 0x32c0
movl 0x28(%r14), %eax
addq %rax, %rbp
movl %r13d, %eax
cmpq %rax, %rbp
jbe 0xb94d
movl (%r15,%rax,8), %ecx
shll $0x10, %ecx
movl %ebx, %edx
orl %ecx, %edx
movl %edx, (%r15,%rax,8)
leal 0x1(%rax), %r13d
jmp 0xb930
incq %rbx
jmp 0xb89c
testq %rbp, %rbp
je 0xbbc2
leaq -0x101(%rip), %rcx      # 0xb864
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x30c0
pushq $0x4
popq %rbx
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3240
movq %rax, %r12
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3240
movq %rax, %r13
movl 0x4(%r15), %edi
leal -0x1(%rbp), %eax
xorl %ecx, %ecx
movsd 0xab90(%rip), %xmm0     # 0x16538
xorl %esi, %esi
cmpq %rcx, %rbp
je 0xba22
movq (%r15,%rcx,8), %r8
movq %r8, %rdx
shrq $0x20, %rdx
cmpq %rcx, %rax
je 0xb9c7
movl %edi, %r9d
cmpl %r9d, %edx
je 0xb9d8
movl %edx, %r9d
andl $0x3, %r9d
leal -0x3(%r9), %r10d
cmpl $-0x2, %r10d
jae 0xb9dc
movl %edi, %edx
jmp 0xba1b
movl %esi, %edi
cmpq %rdi, %rcx
je 0xba07
incl %esi
movzwl 0x2(%r15,%rsi,8), %esi
cvtsi2sd %esi, %xmm1
movq (%r15,%rdi,8), %r8
movl %r8d, %esi
shrl $0x10, %esi
cvtsi2sd %esi, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
jbe 0xba19
movzwl %r8w, %esi
cmpl $0x1, %r9d
movq %r13, %rdi
cmoveq %r12, %rdi
incl (%rdi,%rsi,4)
movl %ecx, %esi
incq %rcx
movl %edx, %edi
jmp 0xb9aa
movq (%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
pushq $-0x1
popq %rbx
movl %ebx, %edx
movl %ecx, %esi
cmpq %rsi, %rax
jbe 0xba7f
movq (%rsp), %rdi
movq 0x10(%rdi), %rdi
shlq $0x6, %rsi
cmpl $0x2, (%rdi,%rsi)
jne 0xba7b
addq %rsi, %rdi
movsd 0x8(%rdi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0xba7b
movl 0x18(%rdi), %esi
cmpl $0x13880, %esi          # imm = 0x13880
jb 0xba7b
cmpl $0x3d091, %esi          # imm = 0x3D091
cmovbl %ecx, %edx
jb 0xba75
movapd %xmm0, %xmm1
movl %ecx, %ebx
movapd %xmm1, %xmm0
incl %ecx
jmp 0xba34
cmpl $-0x1, %edx
cmovnel %edx, %ebx
cmpl $-0x1, %ebx
je 0xbb9c
xorl %ebp, %ebp
movsd 0xaaa8(%rip), %xmm3     # 0x16540
movsd 0xaa58(%rip), %xmm4     # 0x164f8
xorpd %xmm5, %xmm5
movq 0x1154d(%rip), %r10     # 0x1cff8
leaq 0xbab5(%rip), %rsi      # 0x17567
leaq 0xbb0b(%rip), %rdx      # 0x175c4
movl $0xfffc2f6f, %r14d      # imm = 0xFFFC2F6F
movl %ebp, %ebp
cmpq %rbp, %rax
jbe 0xbb9c
cmpl %ebp, %ebx
je 0xbb54
movq (%rsp), %rcx
movq 0x10(%rcx), %r11
movq %rbp, %r9
shlq $0x6, %r9
cmpl $0x2, (%r11,%r9)
jne 0xbb54
movl (%r13,%rbp,4), %ecx
movl (%r12,%rbp,4), %r8d
leaq (%r8,%r8,4), %rdi
cmpq %rcx, %rdi
jb 0xbb54
addq %r9, %r11
movsd 0x8(%r11), %xmm0
movsd 0x10(%r11), %xmm1
movapd %xmm1, %xmm2
mulsd %xmm3, %xmm2
ucomisd %xmm2, %xmm0
ja 0xbb54
movapd %xmm1, %xmm2
mulsd %xmm4, %xmm2
ucomisd %xmm0, %xmm2
ja 0xbb34
movl 0x18(%r11), %edi
addl %r14d, %edi
cmpl $0xfffd67ee, %edi       # imm = 0xFFFD67EE
ja 0xbb54
xorl %edi, %edi
ucomisd %xmm5, %xmm1
seta %dil
movsd %xmm1, 0x8(%r11)
movsd %xmm0, 0x10(%r11)
movl %edi, (%r11)
cmpl $0x0, 0xc(%rsp)
jg 0xbb5b
incl %ebp
jmp 0xbabf
movq (%r10), %rdi
movb $0x2, %al
movq %rsi, %r14
callq 0x3290
leaq 0xba55(%rip), %rdx      # 0x175c4
movq 0x11482(%rip), %r10     # 0x1cff8
movq %r14, %rsi
movl $0xfffc2f6f, %r14d      # imm = 0xFFFC2F6F
xorpd %xmm5, %xmm5
movsd 0xa96d(%rip), %xmm4     # 0x164f8
movsd 0xa9ad(%rip), %xmm3     # 0x16540
movq (%rsp), %rax
movq (%rax), %rax
jmp 0xbb54
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
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq