// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x884c4
movq $0x0, -0x8(%rbp)
jmp 0x889ea
movq 0x18(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x888cd
movq $-0x1, -0xa0(%rbp)
movq $-0x1, -0x98(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0x885ae
cmpq $0x0, -0x48(%rbp)
jle 0x88516
movq -0x48(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x88524
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0xb0(%rbp)
jmp 0x88524
movq -0xb0(%rbp), %rdx
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rdx
addq $0x1, %rdx
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x8859b
movq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x8859d
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x884f0
jmp 0x885b0
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x887da
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq $0x1, -0x80(%rbp)
movq $-0x1, -0x90(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x48(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jl 0x88738
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x886d6
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movq -0x20(%rbp), %rdx
movq -0x60(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq 0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x70(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq 0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x78(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x78(%rbp), %rax
movl %eax, %esi
movq -0x70(%rbp), %rax
movl %eax, %edx
movq 0x30(%rbp), %rcx
movsd -0x40(%rbp), %xmm0
movq 0x38(%rbp), %r8
callq 0xc1b90
cmpl $0x0, %eax
jne 0x886c3
jmp 0x886d6
jmp 0x886c5
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x8861b
movq -0x60(%rbp), %rax
cmpq -0x28(%rbp), %rax
jl 0x88725
movq -0x80(%rbp), %rax
movq 0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
addq %rcx, %rax
cmpq -0x88(%rbp), %rax
jle 0x88725
movq -0x80(%rbp), %rax
movq 0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x88727
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x885f8
cmpq $0x0, -0x90(%rbp)
jl 0x88752
movq -0x90(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x88760
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0xb8(%rbp)
jmp 0x88760
movq -0xb8(%rbp), %rdx
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x88(%rbp), %rdx
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x98(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x887c7
movq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x887c9
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x885b0
movq -0xa0(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0x88856
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x88839
movq 0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0xc0(%rbp)
jmp 0x88849
movq $-0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x88849
movq -0xc0(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x887e5
movq $0x0, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x888c5
movq -0x30(%rbp), %rcx
movq -0x48(%rbp), %rdx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
jne 0x8888e
jmp 0x888b7
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq %rdx
shrq %rdx
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x88866
movq -0x68(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
xorl %eax, %eax
subq 0x28(%rbp), %rax
movq %rax, 0x28(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x889e2
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x889cf
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movq -0x20(%rbp), %rdx
movq -0x60(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq 0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x70(%rbp)
cmpq $-0x8000, 0x28(%rbp)    # imm = 0x8000
jle 0x88992
movq 0x28(%rbp), %rax
movw %ax, %cx
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x70(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movw %cx, 0x12(%rax)
jmp 0x889ae
movq -0xa8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x70(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movw $0x8000, 0x12(%rax)     # imm = 0x8000
movq -0x70(%rbp), %rax
movl %eax, %ecx
movq -0xa8(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x88905
jmp 0x889d1
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x888df
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw (%rax,%rax)