// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: lchain_qdp(k_mer_hit*, long, k_mer_hit*, Chain_Data*, overlap_region*, long, long, long, double, double, double, long, long, long)
uint64_t lchain_qdp(k_mer_hit* a, int64_t a_n, k_mer_hit* des, Chain_Data* dp, overlap_region* res, 
              int64_t max_skip, int64_t max_iter, int64_t max_dis, double chn_pen_gap, double chn_pen_skip, double bw_rate, 
              int64_t xl, int64_t yl, int64_t quick_check)
{
    int64_t *p, *t, max_f, n_skip, st, max_j, end_j, sc, msc, msc_i, bw, max_ii, ovl, movl; 
    int32_t *f, max, tmp; int64_t i, j, ret, cL = 0;
    resize_Chain_Data(dp, a_n, NULL);
    t = dp->tmp; f = dp->score; p = dp->pre;
    bw = ((xl < yl)?xl:yl); bw *= bw_rate;
    msc = msc_i = -1; movl = INT32_MAX;
    // if(a_n && a[0].readID == 0) {
    //     fprintf(stderr, "---[M::%s::utg%.6dl::%c]\n", 
    //                     __func__, (int32_t)a[0].readID+1, "+-"[a[0].strand]);
    // }
    if(quick_check) {
        ret = lchain_qcheck(a, a_n, dp, bw_rate);
        if (ret > 0) {
            a_n = ret; msc_i = a_n-1; msc = f[msc_i];
            goto skip_ldp;
        }
    }

    memset(t, 0, (a_n*sizeof((*t))));
    for (i = st = 0, max_ii = -1; i < a_n; ++i) {
        max_f = a[i].cnt&(0xffu); 
        n_skip = 0; max_j = end_j = -1;
        if ((i-st) > max_iter) st = i-max_iter;

        for (j = i - 1; j >= st; --j) {
            sc = comput_sc_ch(&a[i], &a[j], bw_rate, chn_pen_gap, chn_pen_skip, xl, yl);
            if (sc == INT32_MIN) continue;
            sc += f[j];
            if (sc > max_f) {
                max_f = sc, max_j = j;
                if (n_skip > 0) --n_skip;
            } else if (t[j] == (int32_t)i) {
                if (++n_skip > max_skip)
                    break;
            }
            if (p[j] >= 0) t[p[j]] = i;
        }
        end_j = j;

        if (max_ii < 0 || ((int64_t)a[i].self_offset) - ((int64_t)a[max_ii].self_offset) > max_dis) {
            max = INT32_MIN; max_ii = -1;
            for (j = i - 1; (j >= st) && ((((int64_t)a[i].self_offset)-((int64_t)a[j].self_offset))<=max_dis); --j) {
                if (max < f[j]) {
                    max = f[j], max_ii = j;
                }
            }
        }

        if (max_ii >= 0 && max_ii < end_j) {///just have a try with a[i]<->a[max_ii]
            tmp = comput_sc_ch(&a[i], &a[max_ii], bw_rate, chn_pen_gap, chn_pen_skip, xl, yl);
            if (tmp != INT32_MIN && max_f < tmp + f[max_ii])
                max_f = tmp + f[max_ii], max_j = max_ii;
        }
        f[i] = max_f; p[i] = max_j;
        if ((max_ii < 0) || (((((int64_t)a[i].self_offset)-((int64_t)a[max_ii].self_offset))<=max_dis) && (f[max_ii]<f[i]))) {
            max_ii = i;
        }
        if(f[i] >= msc) {
            ovl = get_chainLen(a[i].self_offset, a[i].self_offset, xl, a[i].offset, a[i].offset, yl);
            if(f[i] > msc || ovl < movl) {
                msc = f[i]; msc_i = i; movl = ovl;
            }
        }
        // if(a_n && a[0].readID == 0) {
        //     fprintf(stderr, "i::%ld[M::%s::utg%.6dl::%c] x::%u, y::%u, st::%ld, max_ii::%ld, f[i]::%d, p[i]::%ld, msc_i::%ld, msc::%ld, movl::%ld\n", 
        //                     i, __func__, (int32_t)a[i].readID+1, "+-"[a[i].strand], 
        //                     a[i].self_offset, a[i].offset, st, max_ii, f[i], p[i], msc_i, msc, movl);
        // }
    }
    
    skip_ldp:
    ///a[] has been sorted by self_offset
    i = msc_i; 
    res->x_pos_s = res->x_pos_e = a[i].self_offset;
    res->y_pos_s = res->y_pos_e = a[i].offset;
    res->shared_seed = msc;

    cL = 0; 
    while (i >= 0) {
        t[cL++] = i; msc_i = i; i = p[i];
    }

    res->x_pos_s = a[t[cL-1]].self_offset;
    res->y_pos_s = a[t[cL-1]].offset;
    res->overlapLen = get_chainLen(res->x_pos_s, res->x_pos_e, xl, res->y_pos_s, res->y_pos_e, yl);
    for (i = 0; i < cL; i++) {
        des[i] = a[t[cL-i-1]];
        // if(a_n && a[0].readID == 0) {
        //     fprintf(stderr, "i::%ld[M::%s::utg%.6dl::%c] x::%u, y::%u, cL::%ld\n", 
        //                     i, __func__, (int32_t)des[i].readID+1, "+-"[des[i].strand], des[i].self_offset, des[i].offset, cL);
        // }
    }
    return cL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movsd %xmm2, (%rsp)
movsd %xmm1, 0x70(%rsp)
movsd %xmm0, 0x68(%rsp)
movq %r9, 0x98(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, %rbx
movq %rdx, 0x78(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
movq %rcx, %rdi
xorl %edx, %edx
callq 0x1383e
movq 0x28(%rbx), %rax
movq %rax, 0x10(%rsp)
movq (%rbx), %r13
movq 0x8(%rbx), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x100(%rsp)
je 0x14773
movq %rbp, %rdi
movl %r14d, %esi
movq %rbx, %rdx
movsd (%rsp), %xmm0
callq 0x1403e
testl %eax, %eax
jle 0x14773
movl %eax, %edx
movl -0x4(%r13,%rdx,4), %r15d
decq %rdx
jmp 0x14ae4
leaq (,%r14,8), %rdx
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
testq %r14, %r14
movl $0x0, %eax
movq %rax, 0x38(%rsp)
cmovleq %rbx, %r14
leaq -0x10(%rbp), %rbx
leaq -0x8(%rbp), %rax
movq %rax, 0x60(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x50(%rsp)
pushq $-0x1
popq %rax
movq %rax, %r15
movq %rax, 0x48(%rsp)
movq %rax, 0x20(%rsp)
xorl %ecx, %ecx
movsd (%rsp), %xmm0
movq %rbp, 0x80(%rsp)
movq %r14, 0x58(%rsp)
movq %r13, 0x30(%rsp)
cmpq %r14, %rcx
je 0x14adf
movq %r15, 0x90(%rsp)
movq %rcx, %rax
shlq $0x4, %rax
movq %rcx, %rdi
leaq (%rax,%rbp), %r12
movzbl 0xc(%rbp,%rax), %r15d
movq %rcx, %rax
movq 0x38(%rsp), %rsi
subq %rsi, %rax
movq 0xe0(%rsp), %rdx
subq %rdx, %rcx
cmpq %rdx, %rax
cmovgq %rcx, %rsi
movq %rsi, 0x38(%rsp)
leaq -0x1(%rdi), %r13
movq %rdi, 0x8(%rsp)
movslq %edi, %rax
movq %rax, 0xa0(%rsp)
movq %rbx, 0x88(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
movq %r13, %r14
cmpq 0x38(%rsp), %r14
jl 0x14912
movq %r12, %rdi
movq %rbx, %rsi
movsd 0x68(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
movq 0xf0(%rsp), %rdx
movq 0xf8(%rsp), %rcx
callq 0x17d1a
movl %eax, %ecx
negl %ecx
jno 0x1488c
movq %r15, %rax
movsd (%rsp), %xmm0
jmp 0x14903
movslq %eax, %rcx
movq 0x30(%rsp), %rax
movslq (%rax,%r14,4), %rax
addq %rcx, %rax
cmpq %r15, %rax
jle 0x148b5
xorl %ecx, %ecx
testq %rbp, %rbp
setg %cl
subq %rcx, %rbp
movq %r14, %rcx
movsd (%rsp), %xmm0
jmp 0x148e2
movq 0x10(%rsp), %rax
movq 0xa0(%rsp), %rcx
cmpq %rcx, (%rax,%r14,8)
movsd (%rsp), %xmm0
jne 0x148da
cmpq 0x98(%rsp), %rbp
jge 0x14912
incq %rbp
movq %r15, %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq 0x28(%rsp), %rcx
movq (%rcx,%r14,8), %rcx
testq %rcx, %rcx
js 0x14903
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rsi
movq %rsi, (%rdx,%rcx,8)
decq %r14
addq $-0x10, %rbx
movq %rax, %r15
jmp 0x1484a
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
movq 0x80(%rsp), %rbp
js 0x14941
movl 0x8(%r12), %eax
movq %rbx, %rcx
shlq $0x4, %rcx
movl 0x8(%rbp,%rcx), %ecx
subq %rcx, %rax
cmpq 0xe8(%rsp), %rax
jle 0x14984
movl $0x80000000, %eax       # imm = 0x80000000
movq 0x60(%rsp), %rcx
pushq $-0x1
popq %rbx
cmpq 0x38(%rsp), %r13
jl 0x14984
movl 0x8(%r12), %edx
movl (%rcx), %esi
subq %rsi, %rdx
cmpq 0xe8(%rsp), %rdx
jg 0x14984
movq 0x30(%rsp), %rdx
movl (%rdx,%r13,4), %edx
cmpl %edx, %eax
cmovlq %r13, %rbx
cmovlel %edx, %eax
decq %r13
addq $-0x10, %rcx
jmp 0x1494e
testq %rbx, %rbx
sets %al
cmpq %r14, %rbx
setge %cl
orb %al, %cl
jne 0x149d9
movq %rbx, %rsi
shlq $0x4, %rsi
addq %rbp, %rsi
movq %r12, %rdi
movsd 0x68(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
movq 0xf0(%rsp), %rdx
movq 0xf8(%rsp), %rcx
callq 0x17d1a
movl %eax, %ecx
negl %ecx
movq 0x58(%rsp), %r14
movq 0x30(%rsp), %r13
jno 0x14a07
movq 0x18(%rsp), %rdx
jmp 0x14a21
movq 0x30(%rsp), %r13
movq 0x8(%rsp), %rcx
movl %r15d, (%r13,%rcx,4)
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %rdx
movq %rdx, (%rax,%rcx,8)
movq %rcx, 0x20(%rsp)
testq %rbx, %rbx
movq 0x58(%rsp), %r14
jns 0x14a39
jmp 0x14a66
movslq (%r13,%rbx,4), %rcx
cltq
addq %rcx, %rax
cmpq %rax, %r15
movq 0x18(%rsp), %rdx
cmovlq %rbx, %rdx
cmovleq %rax, %r15
movq 0x8(%rsp), %rcx
movl %r15d, (%r13,%rcx,4)
movq 0x28(%rsp), %rax
movq %rdx, (%rax,%rcx,8)
movsd (%rsp), %xmm0
movl 0x8(%r12), %eax
movq %rbx, %rcx
shlq $0x4, %rcx
movl 0x8(%rbp,%rcx), %ecx
subq %rcx, %rax
cmpq 0xe8(%rsp), %rax
jg 0x14a61
cmpl %r15d, (%r13,%rbx,4)
cmovlq 0x8(%rsp), %rbx
movq %rbx, 0x20(%rsp)
movslq %r15d, %rbx
movq 0x90(%rsp), %r15
cmpq %rbx, %r15
jle 0x14a7d
movq 0x8(%rsp), %rcx
jmp 0x14ac5
movl 0x4(%r12), %ecx
movl 0x8(%r12), %edi
movq %rdi, %rsi
movq 0xf0(%rsp), %rdx
movq %rcx, %r8
movq 0xf8(%rsp), %r9
callq 0x12ff0
movsd (%rsp), %xmm0
cmpq %rbx, %r15
movq 0x8(%rsp), %rcx
jl 0x14ab8
cmpq 0x50(%rsp), %rax
jge 0x14ac5
movq %rbx, %r15
movq %rcx, 0x48(%rsp)
movq %rax, 0x50(%rsp)
incq %rcx
movq 0x88(%rsp), %rbx
addq $0x10, %rbx
addq $0x10, 0x60(%rsp)
jmp 0x147da
movq 0x48(%rsp), %rdx
movq %rdx, %rax
shlq $0x4, %rax
movl 0x8(%rbp,%rax), %esi
movq 0x40(%rsp), %rcx
movl %esi, 0x8(%rcx)
movl 0x4(%rbp,%rax), %r8d
movl %r8d, 0x18(%rcx)
movl %r15d, 0x24(%rcx)
xorl %ebx, %ebx
testq %rdx, %rdx
js 0x14b22
movq 0x10(%rsp), %rax
movq %rdx, (%rax,%rbx,8)
incq %rbx
movq 0x28(%rsp), %rax
movq (%rax,%rdx,8), %rdx
jmp 0x14b06
movq 0x10(%rsp), %rax
movq -0x8(%rax,%rbx,8), %rax
shlq $0x4, %rax
movl 0x8(%rbp,%rax), %edi
movq 0x40(%rsp), %r14
movl %edi, 0x4(%r14)
movl 0x4(%rbp,%rax), %ecx
movl %ecx, 0x14(%r14)
movq 0xf0(%rsp), %rdx
movq 0xf8(%rsp), %r9
callq 0x12ff0
movl %eax, 0x20(%r14)
movq %rbx, %rax
movq 0x78(%rsp), %rdx
subq $0x1, %rax
jb 0x14b87
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
shlq $0x4, %rcx
movups (%rbp,%rcx), %xmm0
movups %xmm0, (%rdx)
addq $0x10, %rdx
jmp 0x14b66
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq