// File: /workspace/github_cpp_makefile_star2_O3/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, -0x30(%rsp)
movq %rdi, -0x38(%rsp)
testq %r9, %r9
je 0x7a478
movq %rdx, %r10
movq 0x48(%rsp), %r14
movq 0x40(%rsp), %r12
cmpq %r9, %r12
jge 0x7a0e9
movq 0x38(%rsp), %rbx
testq %r12, %r12
jle 0x7a0f1
movq $-0x1, %r15
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
xorl %esi, %esi
movabsq $0x100000000, %rdx      # imm = 0x100000000
movq $-0x1, %r13
leaq 0x1(%rsi), %rdi
movq %rax, %r11
orq %rdi, %r11
cmpq %rsi, %r13
cmovleq %rdi, %r13
cmovleq %rsi, %r15
movq %r11, (%rbx,%rsi,8)
addq %rdx, %rax
movq %rdi, %rsi
cmpq %rdi, %r12
jne 0x7a0c3
jmp 0x7a102
movq %r9, %r11
jmp 0x7a3de
movq $-0x1, %r13
xorl %r12d, %r12d
movq $-0x1, %r15
cmpq %r9, %r12
jge 0x7a38c
movq %r8, -0x40(%rsp)
movq %r9, -0x20(%rsp)
movq %r13, -0x18(%rsp)
movq %r15, -0x8(%rsp)
testq %r12, %r12
movq %r12, -0x10(%rsp)
je 0x7a337
movq (%r8,%r12,8), %rsi
movl $0x1, %edi
movq $-0x1, %rdx
movq %rdx, -0x58(%rsp)
movq %rdi, -0x50(%rsp)
movq %r12, -0x28(%rsp)
leaq -0x1(%r12), %rax
movq %rax, -0x60(%rsp)
testq %rcx, %rcx
jle 0x7a2ec
movq -0x40(%rsp), %rax
movq -0x28(%rsp), %rdx
movq -0x8(%rax,%rdx,8), %r8
movq -0x30(%rsp), %rax
movq (%rax), %rbp
movq -0x38(%rsp), %rax
movq (%rax), %r12
xorl %r9d, %r9d
movl (%r10,%r9,8), %eax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl (%r12,%rdx), %eax
imulq $0xa8, %rax, %rdi
movl 0x4(%rbp,%rdi), %ebx
movq %rbx, %rax
xorl %edx, %edx
idivq %r14
subq %rdx, %rbx
movq %rsi, %rax
subq %rbx, %rax
cqto
idivq %r14
movq %rax, %r11
movq %r8, %rax
subq %rbx, %rax
cqto
idivq %r14
cmpl $-0x1, %r11d
je 0x7a2e0
cmpl $-0x1, %eax
je 0x7a2e0
testl %eax, %eax
je 0x7a2e0
addq %rbp, %rdi
movl %eax, %r15d
movq 0x58(%rdi), %rdi
shlq $0x5, %r15
movl 0xc(%rdi,%r15), %eax
cmpl $-0x1, %eax
je 0x7a2e0
movslq -0x14(%rdi,%r15), %rbx
cmpq $-0x1, %rbx
je 0x7a2e0
cmpl %ebx, %eax
jle 0x7a2e0
testl %r11d, %r11d
je 0x7a2e0
movl %r11d, %r13d
shlq $0x5, %r13
movslq 0xc(%rdi,%r13), %rdx
cmpq $-0x1, %rdx
je 0x7a2e0
movl -0x14(%rdi,%r13), %r11d
cmpl $-0x1, %r11d
je 0x7a2e0
cmpl %r11d, %edx
jle 0x7a2e0
cmpl %eax, %edx
jl 0x7a321
cmpl %ebx, %r11d
jl 0x7a321
movl %r11d, -0x44(%rsp)
addq %rdi, %r15
movslq (%r15), %r11
addq %rdi, %r13
movl (%r13), %edi
cmpl %r11d, %edi
jl 0x7a321
movl 0x4(%r15), %r15d
movl 0x4(%r13), %r13d
cmpl %r15d, %r13d
jl 0x7a321
incq %rbx
movslq %r13d, %r13
subq %r11, %r13
incq %r13
subq %rbx, %rdx
incq %rdx
movq %r13, %r11
subq %rdx, %r11
movq %r11, %rbx
negq %rbx
cmovsq %r11, %rbx
cmpl %eax, -0x44(%rsp)
jge 0x7a2d9
incl %r15d
cmpl %edi, %r15d
jne 0x7a2d9
cmpq %rdx, %r13
cmovlq %r13, %rdx
cmpq 0x60(%rsp), %rbx
jle 0x7a2e0
cvtsi2sd %rbx, %xmm1
cvtsi2sd %rdx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x7a2e0
jmp 0x7a321
cmpq 0x58(%rsp), %rbx
jg 0x7a321
incq %r9
cmpq %r9, %rcx
jne 0x7a181
movq 0x38(%rsp), %rbx
movq -0x60(%rsp), %r12
movl (%rbx,%r12,8), %eax
leaq 0x1(%rax), %rdx
movq -0x50(%rsp), %rdi
cmpq %rdi, %rax
cmovgeq %rdx, %rdi
movq -0x58(%rsp), %rdx
cmovgeq %r12, %rdx
cmpq $0x1, -0x28(%rsp)
jg 0x7a13d
jmp 0x7a343
movq 0x38(%rsp), %rbx
movq -0x50(%rsp), %rdi
movq -0x58(%rsp), %rdx
movq -0x60(%rsp), %r12
jmp 0x7a313
movl $0x1, %edi
movq $-0x1, %rdx
testq %rdx, %rdx
movq $-0x1, %rax
cmovsq %rax, %rdx
shlq $0x20, %rdx
orq %rdi, %rdx
movq -0x10(%rsp), %r12
movq %rdx, (%rbx,%r12,8)
movq -0x18(%rsp), %r13
cmpq %rdi, %r13
cmovleq %rdi, %r13
movq -0x8(%rsp), %r15
cmovlq %r12, %r15
incq %r12
movq -0x20(%rsp), %r9
cmpq %r9, %r12
movq -0x40(%rsp), %r8
jne 0x7a115
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
testq %r15, %r15
js 0x7a3b5
leaq 0x1(%rax), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
orq %rdx, (%r8,%r15,8)
movq (%rbx,%r15,8), %r15
shrq $0x20, %r15
cmpq %rsi, %r15
jne 0x7a3a4
testq %r9, %r9
jle 0x7a478
xorl %r11d, %r11d
xorl %edx, %edx
movq (%r8,%rdx,8), %rsi
testq %rsi, %rsi
jns 0x7a3d6
andq %rax, %rsi
movq %rsi, (%r8,%r11,8)
incq %r11
incq %rdx
cmpq %rdx, %r9
jne 0x7a3c3
testq %r11, %r11
jle 0x7a47b
movq 0x50(%rsp), %rax
movl %eax, %edx
negl %edx
cmpq $0x8000, %rax           # imm = 0x8000
movl $0x8000, %esi           # imm = 0x8000
cmovll %edx, %esi
xorl %edi, %edi
testq %rcx, %rcx
jle 0x7a46e
movq (%r8,%rdi,8), %rbp
movq -0x30(%rsp), %rax
movq (%rax), %rbx
movq -0x38(%rsp), %rax
movq (%rax), %r15
xorl %r12d, %r12d
movl (%r10,%r12,8), %eax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl (%r15,%rdx), %eax
imulq $0xa8, %rax, %r13
movl 0x4(%rbx,%r13), %r9d
movq %r9, %rax
xorl %edx, %edx
idivq %r14
leaq (%rdx,%rbp), %rax
subq %r9, %rax
cqto
idivq %r14
movq 0x58(%rbx,%r13), %rdx
movq %rax, %r9
shlq $0x5, %r9
movw %si, 0x12(%rdx,%r9)
movl %eax, 0x28(%rbx,%r13)
incq %r12
cmpq %r12, %rcx
jne 0x7a41c
incq %rdi
cmpq %r11, %rdi
jne 0x7a400
jmp 0x7a47b
xorl %r11d, %r11d
movq %r11, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq