// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x68, %rsp
movsd %xmm0, 0x60(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
testq %r9, %r9
je 0x47628
movq %rcx, %r15
movq %rdi, %rbp
movq 0xb8(%rsp), %rcx
movq 0xb0(%rsp), %rbx
movq 0xa8(%rsp), %r11
movq %r9, 0x8(%rsp)
xorl %eax, %eax
cmpq %r9, %r11
jge 0x4762f
testq %r11, %r11
cmovleq %rax, %r11
pushq $-0x1
popq %r14
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
movabsq $0x100000000, %rdx      # imm = 0x100000000
movq %r14, %r10
cmpq %rax, %r11
je 0x47640
leaq 0x1(%rax), %rsi
movq %rcx, %rdi
orq %rsi, %rdi
movq 0xa0(%rsp), %r9
movq %rdi, (%r9,%rax,8)
cmpq %rax, %r14
cmovleq %rsi, %r14
cmovleq %rax, %r10
addq %rdx, %rcx
movq %rsi, %rax
jmp 0x475fa
xorl %eax, %eax
jmp 0x478b9
testq %r15, %r15
cmovleq %rax, %r15
movq 0x8(%rsp), %rdx
jmp 0x47822
xorl %eax, %eax
testq %r15, %r15
cmovleq %rax, %r15
movq %r8, 0x20(%rsp)
cmpq 0x8(%rsp), %r11
jge 0x477b5
movq %r14, 0x40(%rsp)
movq %r10, 0x50(%rsp)
movq (%r8,%r11,8), %r14
movq %r11, 0x48(%rsp)
pushq $0x1
popq %rax
movq %rax, 0x18(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x10(%rsp)
testq %r11, %r11
jle 0x4776c
leaq -0x1(%r11), %rax
movq %rax, 0x28(%rsp)
movq %r11, 0x58(%rsp)
movq -0x8(%r8,%r11,8), %r12
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x47729
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rbp), %rcx
movq 0x38(%rsp), %rdx
movl (%rdx,%r13,8), %edx
imulq $0x1c, %rdx, %rdx
movl (%rcx,%rdx), %ecx
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %rdi
movl 0x4(%rax,%rcx), %ecx
movq %rcx, %rax
xorl %edx, %edx
idivq %rbx
subq %rdx, %rcx
movq %r14, %rax
subq %rcx, %rax
cqto
idivq %rbx
movq %rax, %r8
movq %r12, %rax
subq %rcx, %rax
cqto
idivq %rbx
movl %eax, %esi
movl %r8d, %edx
movq 0xc0(%rsp), %rcx
movsd 0x60(%rsp), %xmm0
movq 0xc8(%rsp), %r8
callq 0x62d83
incq %r13
testl %eax, %eax
jne 0x4769b
movq 0x28(%rsp), %r11
movq 0x20(%rsp), %r8
jmp 0x4767c
movq 0xa0(%rsp), %rax
movq 0x58(%rsp), %rcx
movl -0x8(%rax,%rcx,8), %eax
leaq 0x1(%rax), %rcx
movq 0x18(%rsp), %rdx
cmpq %rdx, %rax
cmovgeq %rcx, %rdx
movq %rdx, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %r11
cmovgeq %r11, %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %r8
jmp 0x4767c
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
pushq $-0x1
popq %rax
cmovsq %rax, %rcx
shlq $0x20, %rcx
movq 0x18(%rsp), %rax
orq %rax, %rcx
movq 0x40(%rsp), %r14
cmpq %rax, %r14
cmovleq %rax, %r14
movq 0xa0(%rsp), %rax
movq 0x48(%rsp), %r11
movq %rcx, (%rax,%r11,8)
movq 0x50(%rsp), %r10
cmovlq %r11, %r10
incq %r11
jmp 0x4764e
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x1(%rax), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
pushq $-0x1
popq %rsi
testq %r10, %r10
js 0x477ed
orq %rcx, (%r8,%r10,8)
movq 0xa0(%rsp), %rdi
movq (%rdi,%r10,8), %r10
shrq $0x20, %r10
cmpq %rdx, %r10
cmoveq %rsi, %r10
jmp 0x477cb
xorl %ecx, %ecx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
cmovleq %rcx, %rdi
xorl %edx, %edx
cmpq %rcx, %rdi
je 0x4781a
movq (%r8,%rcx,8), %rsi
testq %rsi, %rsi
jns 0x47815
andq %rax, %rsi
movq %rsi, (%r8,%rdx,8)
incq %rdx
incq %rcx
jmp 0x477fd
movq 0xb8(%rsp), %rcx
movl %ecx, %eax
negl %eax
xorl %esi, %esi
testq %rdx, %rdx
movl $0x0, %edi
movq %rdx, %r12
cmovgq %rdx, %rdi
cmpq $0x8000, %rcx           # imm = 0x8000
movl $0x8000, %r13d          # imm = 0x8000
cmovll %eax, %r13d
cmpq %rdi, %rsi
je 0x478b6
movq (%r8,%rsi,8), %r9
xorl %r10d, %r10d
cmpq %r10, %r15
je 0x478b1
movq 0x30(%rsp), %rax
movq (%rax), %r11
movq (%rbp), %rax
movq 0x38(%rsp), %rcx
movl (%rcx,%r10,8), %ecx
imulq $0x1c, %rcx, %rcx
movl (%rax,%rcx), %eax
imulq $0xa8, %rax, %r14
movl 0x4(%r11,%r14), %ecx
movq %rcx, %rax
xorl %edx, %edx
idivq %rbx
leaq (%rdx,%r9), %rax
subq %rcx, %rax
cqto
idivq %rbx
movq 0x58(%r11,%r14), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
movw %r13w, 0x12(%rcx,%rdx)
movl %eax, 0x28(%r11,%r14)
incq %r10
jmp 0x47854
incq %rsi
jmp 0x47848
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq