// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: dp_commen_sketch(kv_ul_ov_t*, overlap_region_alloc*, unsigned long*, long, unsigned long*, long, unsigned long*, long, long, long, long, double, long)
uint64_t dp_commen_sketch(kv_ul_ov_t *aln, overlap_region_alloc* ol, uint64_t *id_a, int64_t id_n, 
uint64_t *win_a, int64_t win_n, uint64_t *dp, int64_t n_skip, int64_t wl, int64_t cov, int64_t max_lgap, 
double sgap_rate, int64_t sgap)
{
    if(!win_n) return 0;
    int64_t k, ws, ws0, i, m, wid, wid0, sc, max_sc, p, long_sc, long_idx; overlap_region *z; 
    // fprintf(stderr, "\n[M::%s::] n_skip::%ld, win_n::%ld\n", __func__, n_skip, win_n);
    // for (k = 0; k < win_n; k++) {
    //     fprintf(stderr, "[M::%s::] win_a[%ld]::%lu\n", __func__, k, win_a[k]);
    // }
    if(n_skip < win_n) {
        long_sc = long_idx = -1;
        for (k = 0; k < n_skip; k++) {
            dp[k] = ((k>0)?(k-1):((uint32_t)-1)); dp[k] <<= 32; dp[k] |= k+1; max_sc = k+1;
            if(long_sc < max_sc) {
                long_sc = max_sc; long_idx = k;
            }
        }

        for (; k < win_n; k++) {
            ws = win_a[k]; sc = 1; p = -1; max_sc = sc;
            for (m = k-1; m >= 0; m--) {
                ws0 = win_a[m];
                for (i = 0; i < id_n; i++) {
                    z = &(ol->list[aln->a[(uint32_t)id_a[i]].qn]);
                    wid = get_win_id_by_s(z, ws, wl, NULL);
                    wid0 = get_win_id_by_s(z, ws0, wl, NULL);
                    if(!ovlp_win_check(z, wid0, wid, max_lgap, sgap_rate, sgap)) break;
                }
                if((i >= id_n) && ((sc + ((uint32_t)dp[m])) > max_sc)) {
                    max_sc = (sc + ((uint32_t)dp[m])); p = m;
                }
            }
            dp[k] = ((p>=0)?(p):((uint32_t)-1)); dp[k] <<= 32; dp[k] |= max_sc;
            if(long_sc < max_sc) {
                long_sc = max_sc; long_idx = k;
            }
        }
        // fprintf(stderr, "[M::%s::] long_idx::%lu, long_sc::%lu\n", __func__, long_idx, long_sc);
        k = long_idx; 
        while (k >= 0) {
            win_a[k] |= ((uint64_t)0x8000000000000000); 
            k = ((dp[k]>>32)!=((uint32_t)-1))?(dp[k]>>32):(-1);
        }

        for (k = 0, m = 0; k < win_n; k++) {
            if(!(win_a[k]&((uint64_t)0x8000000000000000))) continue;
            win_a[m++] = (win_a[k]<<1)>>1;
        }
        win_n = m;
        // fprintf(stderr, "[M::%s::] win_n::%lu\n", __func__, win_n);
    }

    for (k = 0, cov = -cov; k < win_n; k++) {
        ws = win_a[k];
        for (i = 0; i < id_n; i++) {
            z = &(ol->list[aln->a[(uint32_t)id_a[i]].qn]);
            wid = get_win_id_by_s(z, ws, wl, NULL);
            // if(wid >= z->w_list.n) {
            //     fprintf(stderr, "[M::%s::] ws::%ld, wl::%ld, wid::%ld, wn::%ld\n", 
            //         __func__, ws, wl, wid, ((int64_t)z->w_list.n));
            // }
            if(cov > INT16_MIN) z->w_list.a[wid].extra_end = cov; 
            else z->w_list.a[wid].extra_end = INT16_MIN;
            z->align_length = wid;///for conliner
        }
    }
    return win_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, -0x50(%rsp)
movq %rdi, -0x58(%rsp)
testq %r9, %r9
je 0x7a3ef
movq %rdx, %r13
movq 0x48(%rsp), %r12
movq 0x40(%rsp), %r14
cmpq %r9, %r14
jge 0x7a3f7
movq 0x38(%rsp), %r11
testq %r14, %r14
jle 0x7a3ff
movq $-0x1, %rbx
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
xorl %edx, %edx
movabsq $0x100000000, %r10      # imm = 0x100000000
movq $-0x1, %r15
leaq 0x1(%rdx), %rsi
movq %rax, %rdi
orq %rsi, %rdi
cmpq %rdx, %r15
cmovleq %rsi, %r15
cmovleq %rdx, %rbx
movq %rdi, (%r11,%rdx,8)
addq %r10, %rax
movq %rsi, %rdx
cmpq %rsi, %r14
jne 0x7a3c9
jmp 0x7a410
xorl %r11d, %r11d
jmp 0x7a80b
movq %r9, %r11
jmp 0x7a773
movq $-0x1, %r15
xorl %r14d, %r14d
movq $-0x1, %rbx
cmpq %r9, %r14
jge 0x7a710
testq %rcx, %rcx
setg -0x71(%rsp)
movq %rcx, %rax
negq %rax
movq %rax, -0x18(%rsp)
movq %r8, -0x60(%rsp)
movq %r9, -0x38(%rsp)
movq %r15, -0x30(%rsp)
movq %rbx, -0x20(%rsp)
testq %r14, %r14
movq %r14, -0x28(%rsp)
je 0x7a6aa
movq (%r8,%r14,8), %rax
movq %rax, -0x10(%rsp)
movl $0x1, %eax
movq %rax, -0x68(%rsp)
movq $-0x1, -0x70(%rsp)
movq %r14, -0x40(%rsp)
leaq -0x1(%r14), %rax
movq %rax, -0x48(%rsp)
testq %rcx, %rcx
jle 0x7a653
movq -0x60(%rsp), %rax
movq -0x40(%rsp), %rdx
movq -0x8(%rax,%rdx,8), %rbp
movq -0x50(%rsp), %rax
movq (%rax), %r12
movq -0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, -0x8(%rsp)
movl $0x1, %edi
movb -0x71(%rsp), %r8b
movq %rcx, %rsi
movq %r13, %r9
movl -0x8(%r13,%rdi,8), %eax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movq -0x8(%rsp), %rax
movl (%rax,%rcx), %eax
imulq $0xa8, %rax, %r15
movl 0x4(%r12,%r15), %ebx
movq %rbx, %rax
xorl %edx, %edx
movq 0x48(%rsp), %rcx
idivq %rcx
subq %rdx, %rbx
movq -0x10(%rsp), %rax
subq %rbx, %rax
cqto
idivq %rcx
movq %rax, %r11
movq %rbp, %rax
subq %rbx, %rax
cqto
idivq %rcx
cmpl $-0x1, %r11d
je 0x7a62e
cmpl $-0x1, %eax
je 0x7a62e
addq %r12, %r15
movl %eax, %ebx
movb $0x1, %r10b
testl %eax, %eax
je 0x7a553
movq 0x58(%r15), %rcx
movq %rbx, %rdx
shlq $0x5, %rdx
movslq 0xc(%rcx,%rdx), %rax
cmpq $-0x1, %rax
je 0x7a553
movslq -0x14(%rcx,%rdx), %rdx
cmpq $-0x1, %rdx
je 0x7a553
cmpl %edx, %eax
jle 0x7a553
incq %rdx
incq %rax
xorl %r10d, %r10d
jmp 0x7a553
testb %r10b, %r10b
jne 0x7a62e
movq 0x58(%r15), %r10
movl %r11d, %r13d
movb $0x1, %cl
testl %r11d, %r11d
je 0x7a596
movq %r13, %r14
shlq $0x5, %r14
movslq 0xc(%r10,%r14), %r11
cmpq $-0x1, %r11
je 0x7a596
movslq -0x14(%r10,%r14), %r15
cmpq $-0x1, %r15
je 0x7a596
cmpl %r15d, %r11d
jle 0x7a596
incq %r15
incq %r11
xorl %ecx, %ecx
jmp 0x7a596
testb %cl, %cl
jne 0x7a62e
shlq $0x5, %rbx
movslq (%r10,%rbx), %r14
shlq $0x5, %r13
movl (%r10,%r13), %ecx
cmpl %r14d, %ecx
jl 0x7a692
movl 0x4(%r10,%rbx), %ebx
movl 0x4(%r10,%r13), %r10d
cmpl %ebx, %r10d
jl 0x7a692
cmpq %rdx, %r15
jl 0x7a692
cmpq %rax, %r11
jl 0x7a692
movslq %r10d, %r10
subq %r14, %r10
incq %r10
subq %rdx, %r11
movq %r10, %r14
subq %r11, %r14
movq %r14, %rdx
negq %rdx
cmovsq %r14, %rdx
cmpq %rax, %r15
jge 0x7a627
incl %ebx
cmpl %ecx, %ebx
jne 0x7a627
cmpq %r11, %r10
cmovlq %r10, %r11
cmpq 0x60(%rsp), %rdx
jle 0x7a62e
cvtsi2sd %rdx, %xmm1
cvtsi2sd %r11, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x7a62e
jmp 0x7a692
cmpq 0x58(%rsp), %rdx
jg 0x7a692
movq %rsi, %rcx
cmpq %rsi, %rdi
setl %r8b
movq -0x18(%rsp), %rax
addq %rdi, %rax
incq %rax
incq %rdi
cmpq $0x1, %rax
movq %r9, %r13
jne 0x7a4af
movq 0x38(%rsp), %r11
movq -0x48(%rsp), %r14
movl (%r11,%r14,8), %eax
leaq 0x1(%rax), %rdx
movq -0x68(%rsp), %rsi
cmpq %rsi, %rax
cmovgeq %rdx, %rsi
movq %rsi, -0x68(%rsp)
movq -0x70(%rsp), %rax
cmovgeq %r14, %rax
movq %rax, -0x70(%rsp)
cmpq $0x1, -0x40(%rsp)
jg 0x7a46a
jmp 0x7a6bd
testb $0x1, %r8b
movq %rsi, %rcx
movq %r9, %r13
je 0x7a653
movq 0x38(%rsp), %r11
movq -0x48(%rsp), %r14
jmp 0x7a684
movl $0x1, %eax
movq %rax, -0x68(%rsp)
movq $-0x1, -0x70(%rsp)
movq -0x70(%rsp), %rdx
testq %rdx, %rdx
movq $-0x1, %rax
cmovsq %rax, %rdx
shlq $0x20, %rdx
movq -0x68(%rsp), %rax
orq %rax, %rdx
movq -0x28(%rsp), %r14
movq %rdx, (%r11,%r14,8)
movq -0x30(%rsp), %r15
cmpq %rax, %r15
cmovleq %rax, %r15
movq -0x20(%rsp), %rbx
cmovlq %r14, %rbx
incq %r14
movq -0x38(%rsp), %r9
cmpq %r9, %r14
movq -0x60(%rsp), %r8
jne 0x7a436
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
testq %rbx, %rbx
js 0x7a744
leaq 0x1(%rax), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq $-0x1, %rsi
orq %rdi, (%r8,%rbx,8)
movq (%r11,%rbx,8), %rbx
shrq $0x20, %rbx
cmpq %rdx, %rbx
cmoveq %rsi, %rbx
jne 0x7a72f
testq %r9, %r9
movq 0x48(%rsp), %r12
jle 0x7a770
xorl %r11d, %r11d
xorl %esi, %esi
movq (%r8,%rsi,8), %rdx
testq %rdx, %rdx
jns 0x7a766
andq %rax, %rdx
movq %rdx, (%r8,%r11,8)
incq %r11
incq %rsi
cmpq %rsi, %r9
jne 0x7a753
jmp 0x7a773
xorl %r11d, %r11d
testq %r11, %r11
jle 0x7a80b
movq 0x50(%rsp), %rax
movl %eax, %edx
negl %edx
cmpq $0x8000, %rax           # imm = 0x8000
movl $0x8000, %ebp           # imm = 0x8000
cmovll %edx, %ebp
xorl %esi, %esi
testq %rcx, %rcx
jle 0x7a803
movq (%r8,%rsi,8), %rdi
movq -0x50(%rsp), %rax
movq (%rax), %r15
movq -0x58(%rsp), %rax
movq (%rax), %r10
xorl %ebx, %ebx
movl (%r13,%rbx,8), %eax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl (%r10,%rdx), %eax
imulq $0xa8, %rax, %r14
movl 0x4(%r15,%r14), %r9d
movq %r9, %rax
xorl %edx, %edx
idivq %r12
leaq (%rdx,%rdi), %rax
subq %r9, %rax
cqto
idivq %r12
movq 0x58(%r15,%r14), %rdx
movq %rax, %r9
shlq $0x5, %r9
movw %bp, 0x12(%rdx,%r9)
movl %eax, 0x28(%r15,%r14)
incq %rbx
cmpq %rbx, %rcx
jne 0x7a7b0
incq %rsi
cmpq %r11, %rsi
jne 0x7a795
movq %r11, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq