// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_rphase_dp0_multiple_path(SnpStats*, long, haplotype_evdience*, Chain_Data*, asg64_v*, asg64_v*)
void gen_rphase_dp0_multiple_path(SnpStats *a, int64_t an, haplotype_evdience *za, Chain_Data *dp, asg64_v *idx, asg64_v *res)
{
    if(an <= 0) return;
    int64_t *p, *t, i, k, j, max_f, sc, ch_n, plus = 0, rn; int32_t *f, *ii; uint64_t m;
    resize_Chain_Data(dp, an, NULL); idx->n = res->n = 0;
    t = dp->tmp; f = dp->score; p = dp->pre; ii = dp->occ;

    for (i = 0; i < an; ++i) {
        max_f = 1; ch_n = idx->n;
        for (j = i - 1; j >= 0; --j) {
            sc = comput_sc_rphase(&a[i], i, &a[j], j, za, 0);
            if (sc == INT64_MIN) continue;
            sc += f[j];
            if (sc > max_f) {
                max_f = sc; 
                idx->n = ch_n; kv_push(uint64_t, *idx, j);
            } else if (sc == max_f) {
                kv_push(uint64_t, *idx, j);
            }
        }

        f[i] = max_f; p[i] = idx->n; ii[i] = 0;
        if(f[i] < plus) plus = f[i];
    }
    ch_n = idx->n;

    for (i = 0; i < an; i++) {
        f[i] -= plus;
        m = f[i]; m = ((uint32_t)-1) - m; m <<= 32; m |= i;
        kv_push(uint64_t, *idx, m);
    }
    radix_sort_bc64(idx->a + ch_n, idx->a + idx->n);

    for (i = 0; i < an; i++) {///idx->a:: [0, ch_n) -> tree; [ch_n, ch_n + an) -> sort; [ch_n + an, -1) -> path
        k = (uint32_t)idx->a[ch_n + i];
        if(ii[k]) continue;
        gen_rphase_path(idx, p, ii, t, k, res);
    }

    rn = res->n;
    for (k = 1, i = 0; k <= rn; k++) {
        if(k == rn || (res->a[i]>>32) != (res->a[k]>>32)) {
            if(k - i > 0) {
                fprintf(stderr, "\n[M::%s]\tcnt::%lu\n", __func__, k - i);
                for (j = i; j < k; j++) {
                    fprintf(stderr, "pos::%u\n", a[(uint32_t)res->a[j]].site);
                    // if(k - i == 2) {
                    //     dbg_prt_rphase_vec(&(a[(uint32_t)res->a[j]]), (uint32_t)res->a[j], za);
                    // }
                }
            }
            i = k;
        }
    }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, (%rsp)
movq %rdx, 0x60(%rsp)
movq %rdi, 0x8(%rsp)
testq %rsi, %rsi
jle 0x544e9
movq %r8, %r15
movq %rcx, %r14
movq %rsi, %r12
xorl %ebx, %ebx
movq %rcx, %rdi
xorl %edx, %edx
callq 0x3f828
movq (%rsp), %rax
movq %rbx, (%rax)
movq %rbx, (%r15)
movq 0x28(%r14), %rax
movq %rax, 0x38(%rsp)
movq (%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rbp
movq 0x20(%r14), %rcx
movq 0x8(%rsp), %rax
leaq -0x28(%rax), %rsi
movq $-0x1, %rdi
movq $0x0, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movq %rbp, 0x40(%rsp)
testq %rbx, %rbx
je 0x54321
movq (%r15), %rax
movq %rax, 0x58(%rsp)
leaq (%rbx,%rbx,4), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x30(%rsp)
movl $0x1, %r13d
movq %rsi, 0x50(%rsp)
movq %rsi, %r12
movq %rdi, 0x48(%rsp)
movq %rdi, %rbp
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %rbp, %rcx
movq 0x60(%rsp), %r8
xorl %r9d, %r9d
callq 0x8792b
movq %rax, %r14
negq %rax
jo 0x542ee
movq 0x28(%rsp), %rax
movslq (%rax,%rbp,4), %rax
addq %rax, %r14
cmpq %r13, %r14
jle 0x542de
movq 0x58(%rsp), %rcx
movq %rcx, (%r15)
movq 0x8(%r15), %rax
cmpq %rax, %rcx
jne 0x542c7
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %rbp, (%rcx,%rax,8)
movq %r14, %r13
jmp 0x542ee
jne 0x542ee
movq 0x8(%r15), %rax
movq %r13, %r14
cmpq %rax, (%r15)
je 0x542a2
jmp 0x542c7
leaq -0x1(%rbp), %rax
incq %rbp
addq $-0x28, %r12
cmpq $0x1, %rbp
movq %rax, %rbp
jg 0x5425d
movq 0x40(%rsp), %rbp
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r12
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdi
jmp 0x54327
movl $0x1, %r13d
movq 0x28(%rsp), %rdx
movl %r13d, (%rdx,%rbx,4)
movq (%r15), %rax
movq %rax, (%rbp,%rbx,8)
movl $0x0, (%rcx,%rbx,4)
movslq (%rdx,%rbx,4), %rax
movq 0x20(%rsp), %rdx
cmpq %rax, %rdx
cmovgeq %rax, %rdx
movq %rdx, 0x20(%rsp)
incq %rbx
incq %rdi
addq $0x28, %rsi
cmpq %r12, %rbx
jne 0x54224
movq (%r15), %rbx
xorl %r14d, %r14d
movl $0x2, %r12d
movq 0x28(%rsp), %rax
movl (%rax,%r14,4), %r13d
subl 0x20(%rsp), %r13d
movl %r13d, (%rax,%r14,4)
movq 0x8(%r15), %rax
cmpq %rax, (%r15)
jne 0x543ae
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
notl %r13d
shlq $0x20, %r13
orq %r14, %r13
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r15)
movq %r13, (%rcx,%rax,8)
incq %r14
cmpq %r14, 0x18(%rsp)
jne 0x54373
movq (%r15), %rax
movq 0x10(%r15), %rcx
leaq (%rcx,%rbx,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x45d47
movq 0x18(%rsp), %rax
cmpq $0x2, %rax
movl $0x1, %r14d
cmovgeq %rax, %r14
movq 0x38(%rsp), %r12
movq 0x10(%rsp), %rdx
movq 0x10(%r15), %rax
movl (%rax,%rbx,8), %r8d
cmpl $0x0, (%rdx,%r8,4)
jne 0x5442b
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rcx
movq (%rsp), %r9
callq 0x53f75
movq 0x10(%rsp), %rdx
incq %rbx
decq %r14
jne 0x54405
movq (%rsp), %rax
movq (%rax), %rbp
testq %rbp, %rbp
jle 0x544e9
movq (%rsp), %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
negq %rbp
movl $0x1, %r14d
movq 0x235b80(%rip), %rbx    # 0x289fe0
leaq 0x1d193d(%rip), %r15    # 0x225da4
leaq 0x1d1949(%rip), %r12    # 0x225db7
xorl %r13d, %r13d
movq %rbp, %rax
addq %r14, %rax
je 0x54494
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%r14,8), %rcx
xorq (%rax,%r13,8), %rcx
shrq $0x20, %rcx
jne 0x54494
movq %r13, %rax
jmp 0x544a2
movq %r14, %rcx
subq %r13, %rcx
movq %r14, %rax
testq %rcx, %rcx
jg 0x544b7
leaq (%r14,%rbp), %rcx
incq %rcx
incq %r14
movq %rax, %r13
cmpq $0x1, %rcx
jne 0x54471
jmp 0x544e9
movq (%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x22b0
movq %r14, %rax
cmpq %r14, %r13
jge 0x544a2
movq %r13, %rdi
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rcx
callq 0x5be6
movq %r14, %rax
jmp 0x544a2
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq