// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movsd %xmm2, 0x50(%rsp)
movsd %xmm1, 0x80(%rsp)
movsd %xmm0, 0x78(%rsp)
movq %r9, 0xa8(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, %r12
movq %rdx, 0x88(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
movq %rcx, %rdi
xorl %edx, %edx
callq 0x3ecb0
movq 0x28(%r12), %rdi
movq (%r12), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%r12), %rbx
cmpq $0x0, 0x120(%rsp)
movq %rdi, 0x30(%rsp)
je 0x40206
movq %rbp, %rdi
movl %r14d, %esi
movq %r12, %rdx
movsd 0x50(%rsp), %xmm0
callq 0x3f795
movq 0x30(%rsp), %rdi
testl %eax, %eax
jle 0x40206
movl %eax, %r8d
movq 0x18(%rsp), %rax
movl -0x4(%rax,%r8,4), %r11d
decq %r8
jmp 0x40825
leaq (,%r14,8), %rdx
xorl %esi, %esi
callq 0x20e0
testq %r14, %r14
jle 0x40813
movq 0x110(%rsp), %rax
decq %rax
movq %rax, 0x90(%rsp)
leaq -0x8(%rbp), %r8
movq $-0x1, 0x70(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x68(%rsp)
xorl %r15d, %r15d
xorl %ecx, %ecx
movq $-0x1, 0x60(%rsp)
movq $-0x1, %r11
movsd 0x50(%rsp), %xmm4
movsd 0x80(%rsp), %xmm5
movsd 0x78(%rsp), %xmm6
movq %rbp, 0x98(%rsp)
movq %rbx, 0x10(%rsp)
movq %r14, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %rcx, %rax
movq %r15, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rbp), %r11
movl 0xc(%rbp,%rcx), %edi
movzbl %dil, %r13d
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
movq 0x100(%rsp), %rsi
subq %rsi, %rcx
cmpq %rsi, %rdx
cmovleq %rax, %rcx
leaq -0x1(%r15), %r9
cmpq %rcx, %r15
movq %r11, 0x38(%rsp)
movq %r8, 0x48(%rsp)
movq %r15, 0x8(%rsp)
movq %rdi, 0xa0(%rsp)
jle 0x404b5
movl 0x8(%r11), %esi
leal 0x1(%rsi), %edx
movq 0x110(%rsp), %r10
movq %rdx, 0xc0(%rsp)
subq %rdx, %r10
movq %r10, 0xb8(%rsp)
movl %edi, %eax
shrl $0x8, %eax
movl %eax, 0x44(%rsp)
movslq %r15d, %rax
movq %rax, 0xb0(%rsp)
movq $-0x1, (%rsp)
movq %r8, %rbx
movq %r9, %r14
movq %r13, %r10
xorl %edi, %edi
movq %r11, %r15
movl (%rbx), %r8d
movl %esi, %ebp
subl %r8d, %ebp
testl %ebp, %ebp
jle 0x403c4
movl 0x4(%r15), %r11d
movl -0x4(%rbx), %r12d
movl %r11d, %eax
subl %r12d, %eax
testl %eax, %eax
jle 0x403c4
movl %ebp, %edx
subl %eax, %edx
negl %edx
movl %ebp, %r15d
subl %eax, %r15d
cmovbl %edx, %r15d
cmpl $0x11, %r15d
jl 0x403cc
incl %r11d
movq 0x118(%rsp), %rdx
subq %r11, %rdx
movq %r12, %r11
subq %r8, %r11
cmpl %r12d, %r8d
movl $0x0, %r8d
cmovbeq %r8, %r11
movq 0xc0(%rsp), %r8
addq %rdx, %r8
cmpq %rdx, 0xb8(%rsp)
cmovleq 0x110(%rsp), %r8
addq %r11, %r8
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %edx
cmpl %edx, %r15d
jle 0x403cc
movq %r10, %rdx
movq 0x38(%rsp), %r15
jmp 0x4049c
movq %r10, %rdx
jmp 0x4049c
cmpl %ebp, %eax
cmovbl %eax, %ebp
cmpl %ebp, %r13d
movl %ebp, %r8d
cmovbl %r13d, %r8d
movl $0x1, %eax
cmpl 0x44(%rsp), %r8d
jb 0x403f2
movzbl %r8b, %eax
divb 0x44(%rsp)
movzbl %al, %eax
cmpl %ebp, %r13d
jb 0x403fc
testl %r15d, %r15d
je 0x40435
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movapd %xmm0, %xmm1
cvtsi2sd %eax, %xmm2
mulsd %xmm6, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %ebp, %xmm3
divsd %xmm3, %xmm0
divsd %xmm4, %xmm0
mulsd %xmm2, %xmm0
minsd %xmm1, %xmm0
mulsd %xmm5, %xmm3
addsd %xmm0, %xmm3
cvttsd2si %xmm3, %edx
subl %edx, %eax
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r15
cltq
movq 0x18(%rsp), %rdx
movslq (%rdx,%r14,4), %rdx
addq %rax, %rdx
cmpq %r10, %rdx
jle 0x40463
xorl %eax, %eax
testq %rdi, %rdi
setg %al
subq %rax, %rdi
movq %r14, (%rsp)
jmp 0x40485
movq 0xb0(%rsp), %rax
cmpq %rax, (%r8,%r14,8)
jne 0x40482
cmpq 0xa8(%rsp), %rdi
jge 0x40801
incq %rdi
movq %r10, %rdx
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rax
testq %rax, %rax
js 0x4049c
movq 0x8(%rsp), %r10
movq %r10, (%r8,%rax,8)
leaq -0x1(%r14), %rax
addq $-0x10, %rbx
movq %rdx, %r10
cmpq %rcx, %r14
movq %rax, %r14
jg 0x4032a
jmp 0x404c6
movq $-0x1, (%rsp)
movq %r13, %rdx
movq %r9, %rax
movq %r11, %r15
movq 0x70(%rsp), %r8
testq %r8, %r8
movq 0x98(%rsp), %rbp
movq 0x108(%rsp), %r14
js 0x404fa
movl 0x8(%r15), %edi
movq %r8, %rsi
shlq $0x4, %rsi
movl 0x8(%rbp,%rsi), %esi
subq %rsi, %rdi
movq %r8, %rsi
cmpq %r14, %rdi
jle 0x40555
movq 0x8(%rsp), %r15
cmpq %rcx, %r15
jle 0x40627
movq 0x38(%rsp), %r15
movl 0x8(%r15), %edi
movq $-0x1, %rsi
movl $0x80000000, %r8d       # imm = 0x80000000
movq 0x48(%rsp), %r10
movl (%r10), %r11d
movq %rdi, %rbx
subq %r11, %rbx
cmpq %r14, %rbx
jg 0x40555
movq 0x18(%rsp), %r11
movl (%r11,%r9,4), %r11d
cmpl %r11d, %r8d
cmovlq %r9, %rsi
cmovlel %r11d, %r8d
leaq -0x1(%r9), %r11
addq $-0x10, %r10
cmpq %rcx, %r9
movq %r11, %r9
jg 0x40523
testq %rsi, %rsi
sets %dil
cmpq %rax, %rsi
setge %al
orb %dil, %al
jne 0x4064f
movq %rsi, %r8
shlq $0x4, %r8
movl 0x8(%r15), %eax
movl 0x8(%rbp,%r8), %ebx
movl %eax, %edi
subl %ebx, %edi
testl %edi, %edi
jle 0x4060a
addq %rbp, %r8
movl 0x4(%r15), %r14d
movl 0x4(%r8), %r8d
movl %r14d, %r10d
subl %r8d, %r10d
testl %r10d, %r10d
jle 0x4060a
movl %edi, %r11d
subl %r10d, %r11d
negl %r11d
movl %edi, %r9d
subl %r10d, %r9d
cmovbl %r11d, %r9d
cmpl $0x11, %r9d
jl 0x40685
incl %eax
incl %r14d
movq 0x110(%rsp), %r12
movq %r12, %r11
subq %rax, %r11
movq 0x118(%rsp), %r15
subq %r14, %r15
movq %r8, %r14
subq %rbx, %r14
cmpl %r8d, %ebx
movl $0x0, %r8d
cmovbeq %r8, %r14
addq %r15, %rax
cmpq %r15, %r11
cmovleq %r12, %rax
addq %r14, %rax
cvtsi2sd %rax, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %eax
cmpl %eax, %r9d
jle 0x40685
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r11
movq 0x8(%rsp), %r15
movq (%rsp), %r8
jmp 0x40727
movq 0x18(%rsp), %rax
movl %edx, (%rax,%r15,4)
movq 0x10(%rsp), %rbx
movq (%rsp), %rax
movq %rax, (%rbx,%r15,8)
movq %r15, %rax
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r11
jmp 0x40763
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %r15
movl %edx, (%rax,%r15,4)
movq 0x10(%rsp), %rbx
movq (%rsp), %rax
movq %rax, (%rbx,%r15,8)
movq %r15, %rax
testq %rsi, %rsi
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r11
jns 0x40734
jmp 0x40763
cmpl %edi, %r10d
cmovbl %r10d, %edi
cmpl %edi, %r13d
movl %edi, %r8d
cmovbl %r13d, %r8d
movq 0xa0(%rsp), %r10
shrl $0x8, %r10d
movl $0x1, %eax
cmpl %r10d, %r8d
jb 0x406b6
movzbl %r8b, %eax
divb %r10b
movzbl %al, %eax
cmpl %edi, %r13d
jb 0x406c0
testl %r9d, %r9d
je 0x406f6
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movapd %xmm0, %xmm1
cvtsi2sd %eax, %xmm2
mulsd %xmm6, %xmm1
cvtsi2sd %edi, %xmm3
divsd %xmm3, %xmm0
divsd %xmm4, %xmm0
mulsd %xmm2, %xmm0
minsd %xmm1, %xmm0
mulsd %xmm5, %xmm3
addsd %xmm0, %xmm3
cvttsd2si %xmm3, %edi
subl %edi, %eax
movq 0x10(%rsp), %rbx
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r11
movq 0x8(%rsp), %r15
movq (%rsp), %r8
movq 0x18(%rsp), %rdi
movslq (%rdi,%rsi,4), %rdi
cltq
addq %rdi, %rax
cmpq %rax, %rdx
cmovlq %rsi, %r8
cmovleq %rax, %rdx
movq 0x18(%rsp), %rax
movl %edx, (%rax,%r15,4)
movq %r8, (%rbx,%r15,8)
movq 0x38(%rsp), %rax
movl 0x8(%rax), %eax
movq %rsi, %rdi
shlq $0x4, %rdi
movl 0x8(%rbp,%rdi), %edi
subq %rdi, %rax
cmpq 0x108(%rsp), %rax
jg 0x40760
movq 0x18(%rsp), %rax
cmpl %edx, (%rax,%rsi,4)
cmovlq %r15, %rsi
movq %rsi, %rax
movq %rax, 0x70(%rsp)
movslq %edx, %rax
cmpq %rax, %r11
jle 0x4077c
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %r8
jmp 0x407ef
movq 0x38(%rsp), %rdx
movl 0x4(%rdx), %esi
movl 0x8(%rdx), %edi
movq %rsi, %rdx
subq %rdi, %rdx
incq %rdx
movq %rdi, %r8
notq %r8
addq 0x110(%rsp), %r8
movq %rsi, %r9
notq %r9
addq 0x118(%rsp), %r9
movq %rdi, %r10
addq %r9, %r10
cmpq %r9, %r8
cmovleq 0x90(%rsp), %r10
cmpl %esi, %edi
movl $0x1, %esi
cmovbeq %rsi, %rdx
addq %r10, %rdx
cmpq %rax, %r11
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %r8
jl 0x407e2
cmpq 0x68(%rsp), %rdx
jge 0x407ef
movq %rax, %r11
movq %r15, 0x60(%rsp)
movq %rdx, 0x68(%rsp)
incq %r15
addq $0x10, %r8
cmpq %r14, %r15
jne 0x40284
jmp 0x4080c
movq %r10, %rdx
movq %r14, %rax
jmp 0x404c6
movq 0x60(%rsp), %r8
jmp 0x40825
movq $-0x1, %r8
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x30(%rsp), %rdi
movq %r8, %rax
shlq $0x4, %rax
movl 0x8(%rbp,%rax), %ecx
movq 0x58(%rsp), %rsi
movl %ecx, 0x8(%rsi)
movl 0x4(%rbp,%rax), %edx
movl %edx, 0x18(%rsi)
movl %r11d, 0x24(%rsi)
xorl %eax, %eax
testq %r8, %r8
js 0x40857
movq %r8, (%rdi,%rax,8)
incq %rax
movq (%rbx,%r8,8), %r8
jmp 0x40845
movq -0x8(%rdi,%rax,8), %rsi
shlq $0x4, %rsi
movl 0x8(%rbp,%rsi), %edi
movq 0x58(%rsp), %r9
movl %edi, 0x4(%r9)
movl 0x4(%rbp,%rsi), %esi
movl %esi, 0x14(%r9)
movq %rcx, %r8
notq %r8
movq 0x110(%rsp), %r10
addq %r10, %r8
notq %rdx
addq 0x118(%rsp), %rdx
decl %r10d
addl %edx, %ecx
subl %edi, %esi
leal 0x1(%rsi), %esi
movl $0x1, %edi
cmovbl %esi, %edi
cmpq %rdx, %r8
movq 0x30(%rsp), %r8
cmovlel %r10d, %ecx
addl %edi, %ecx
movl %ecx, 0x20(%r9)
testq %rax, %rax
movq 0x88(%rsp), %rsi
je 0x408e1
movq %rax, %rcx
movq -0x8(%r8,%rcx,8), %rdx
shlq $0x4, %rdx
movupd (%rbp,%rdx), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, %rsi
decq %rcx
jne 0x408c5
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq