// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movsd %xmm2, 0x58(%rsp)
movsd %xmm1, 0x80(%rsp)
movsd %xmm0, 0x78(%rsp)
movq %r9, 0xb0(%rsp)
movq %r8, 0x88(%rsp)
movq %rcx, %r12
movq %rdx, 0x90(%rsp)
movq %rsi, %r14
movq %rdi, %r13
movq %rcx, %rdi
xorl %edx, %edx
callq 0x3f828
movq 0x28(%r12), %r9
movq (%r12), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x130(%rsp)
je 0x40a37
movq %r13, %rdi
movl %r14d, %esi
movq %r12, %rdx
movsd 0x58(%rsp), %xmm0
movq %r9, %rbx
callq 0x40042
movq %rbx, %r9
testl %eax, %eax
jle 0x40a37
movl %eax, %edi
movq 0x8(%rsp), %rax
movl -0x4(%rax,%rdi,4), %r11d
decq %rdi
jmp 0x41001
leaq (,%r14,8), %rdx
movq %r9, 0x38(%rsp)
movq %r9, %rdi
xorl %esi, %esi
callq 0x20e0
testq %r14, %r14
jle 0x40fef
movq 0x120(%rsp), %rax
decq %rax
movq %rax, 0x98(%rsp)
leaq -0x8(%r13), %r12
movq $-0x1, %r10
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x68(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movq $-0x1, 0x60(%rsp)
movq $-0x1, %r11
movsd 0x58(%rsp), %xmm4
movsd 0x80(%rsp), %xmm5
movsd 0x78(%rsp), %xmm6
movq %r13, 0xa0(%rsp)
movq %r14, 0x70(%rsp)
movq %r11, 0xb8(%rsp)
movq 0x20(%rsp), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
leaq (%rcx,%r13), %rdi
movl 0xc(%r13,%rcx), %r8d
movzbl %r8b, %r14d
movq %rbx, %rcx
subq %rax, %rcx
movq %rbx, %rsi
movq 0x110(%rsp), %rdx
subq %rdx, %rsi
cmpq %rdx, %rcx
cmovleq %rax, %rsi
leaq -0x1(%rbx), %r15
movq %rsi, 0x20(%rsp)
cmpq %rsi, %rbx
movq %rbx, 0x30(%rsp)
movq %rdi, 0x48(%rsp)
movq %r14, 0x40(%rsp)
movq %r12, 0x50(%rsp)
movq %r8, 0xa8(%rsp)
jle 0x40d03
movl 0x8(%rdi), %r13d
leal 0x1(%r13), %ecx
movq 0x120(%rsp), %rdx
movq %rcx, 0xc8(%rsp)
subq %rcx, %rdx
movq %rdx, 0xc0(%rsp)
movl %r8d, %eax
shrl $0x8, %eax
movl %eax, 0x2c(%rsp)
movslq %ebx, %rax
movq %rax, 0xd0(%rsp)
movq $-0x1, 0x10(%rsp)
movq %r15, %r8
xorl %ebp, %ebp
movl (%r12), %ecx
movl %r13d, %r11d
subl %ecx, %r11d
movl $0x80000000, %eax       # imm = 0x80000000
testl %r11d, %r11d
jle 0x40c75
movq 0x48(%rsp), %rdx
movl 0x4(%rdx), %r9d
movl -0x4(%r12), %edx
movl %r9d, %esi
subl %edx, %esi
testl %esi, %esi
jle 0x40c75
movl %esi, %edi
subl %r11d, %edi
movl %edi, %ebx
negl %ebx
cmovsl %edi, %ebx
cmpl $0x11, %ebx
jb 0x40c02
incl %r9d
movq 0x128(%rsp), %rdi
subq %r9, %rdi
movq %rdx, %r9
subq %rcx, %r9
cmpl %edx, %ecx
movl $0x0, %ecx
cmovbeq %rcx, %r9
movq 0xc8(%rsp), %rcx
addq %rdi, %rcx
cmpq %rdi, 0xc0(%rsp)
cmovleq 0x120(%rsp), %rcx
addq %r9, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %ebx
jg 0x40c75
cmpl %r11d, %esi
movl %r11d, %r9d
cmovll %esi, %r9d
movq 0x40(%rsp), %rax
cmpl %r9d, %eax
movl %r9d, %ecx
cmovll %eax, %ecx
movl $0x1, %eax
cmpl 0x2c(%rsp), %ecx
jl 0x40c2d
movl %ecx, %eax
xorl %edx, %edx
divl 0x2c(%rsp)
cmpl %r11d, %esi
jne 0x40c39
cmpl %r9d, 0x40(%rsp)
jge 0x40c75
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movapd %xmm0, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm6, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
divsd %xmm3, %xmm0
divsd %xmm4, %xmm0
mulsd %xmm2, %xmm0
minsd %xmm1, %xmm0
mulsd %xmm5, %xmm3
addsd %xmm0, %xmm3
cvttsd2si %xmm3, %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
jno 0x40c80
movq %r14, %r11
jmp 0x40ce8
cltq
movq 0x8(%rsp), %rcx
movslq (%rcx,%r8,4), %r11
addq %rax, %r11
cmpq %r14, %r11
jle 0x40ca5
xorl %eax, %eax
testq %rbp, %rbp
setg %al
subq %rax, %rbp
movq %r8, 0x10(%rsp)
jmp 0x40ccc
movq 0x38(%rsp), %rax
movq 0xd0(%rsp), %rcx
cmpq %rcx, (%rax,%r8,8)
jne 0x40cc9
cmpq 0xb0(%rsp), %rbp
jge 0x40fd8
incq %rbp
movq %r14, %r11
movq 0x18(%rsp), %rax
movq (%rax,%r8,8), %rax
testq %rax, %rax
js 0x40ce8
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdx
movq %rdx, (%rcx,%rax,8)
leaq -0x1(%r8), %rax
addq $-0x10, %r12
movq %r11, %r14
cmpq 0x20(%rsp), %r8
movq %rax, %r8
jg 0x40b6c
jmp 0x40d12
movq $-0x1, 0x10(%rsp)
movq %r14, %r11
movq %r15, %rax
testq %r10, %r10
movq 0xa0(%rsp), %r13
movq 0x70(%rsp), %r14
movq 0x118(%rsp), %r12
movq 0x30(%rsp), %rbx
movq 0x48(%rsp), %r9
js 0x40d4e
movl 0x8(%r9), %ecx
movq %r10, %rdx
shlq $0x4, %rdx
movl 0x8(%r13,%rdx), %edx
subq %rdx, %rcx
cmpq %r12, %rcx
jle 0x40d9b
movq $-0x1, %r10
cmpq 0x20(%rsp), %rbx
jle 0x40d9b
movl 0x8(%r9), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
movq 0x50(%rsp), %rdi
movl (%rdi), %esi
movq %rcx, %r8
subq %rsi, %r8
cmpq %r12, %r8
jg 0x40d9b
movq 0x8(%rsp), %rsi
movl (%rsi,%r15,4), %esi
cmpl %esi, %edx
cmovlq %r15, %r10
cmovlel %esi, %edx
leaq -0x1(%r15), %rsi
addq $-0x10, %rdi
cmpq 0x20(%rsp), %r15
movq %rsi, %r15
jg 0x40d6a
testq %r10, %r10
sets %cl
cmpq %rax, %r10
setge %al
orb %cl, %al
jne 0x40ed3
movq %r10, %rdi
shlq $0x4, %rdi
movl 0x8(%r9), %ecx
movl 0x8(%r13,%rdi), %edx
movl %ecx, %r8d
subl %edx, %r8d
movl $0x80000000, %eax       # imm = 0x80000000
testl %r8d, %r8d
jle 0x40ebc
addq %r13, %rdi
movl 0x4(%r9), %r9d
movl 0x4(%rdi), %edi
movl %r9d, %ebx
subl %edi, %ebx
testl %ebx, %ebx
jle 0x40ebc
movl %ebx, %esi
subl %r8d, %esi
movl %esi, %ebp
negl %ebp
cmovsl %esi, %ebp
cmpl $0x11, %ebp
jb 0x40e4d
incl %ecx
incl %r9d
movq 0x120(%rsp), %r15
movq %r15, %rsi
subq %rcx, %rsi
movq 0x128(%rsp), %r14
subq %r9, %r14
movq %rdi, %r9
subq %rdx, %r9
cmpl %edi, %edx
movl $0x0, %edx
cmovbeq %rdx, %r9
addq %r14, %rcx
cmpq %r14, %rsi
movq 0x70(%rsp), %r14
cmovleq %r15, %rcx
addq %r9, %rcx
cvtsi2sd %rcx, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %ebp
jg 0x40ebc
cmpl %r8d, %ebx
movl %r8d, %edi
cmovll %ebx, %edi
movq 0x40(%rsp), %rax
cmpl %edi, %eax
movl %edi, %ecx
cmovll %eax, %ecx
movq 0xa8(%rsp), %rsi
shrl $0x8, %esi
movl $0x1, %eax
cmpl %esi, %ecx
jl 0x40e7c
movl %ecx, %eax
xorl %edx, %edx
divl %esi
cmpl %r8d, %ebx
jne 0x40e87
cmpl %edi, 0x40(%rsp)
jge 0x40ebc
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %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, %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
jno 0x40edf
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rbx
movq 0x48(%rsp), %r9
jmp 0x40ed8
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rdx
jmp 0x40f0c
movq 0x8(%rsp), %rcx
movslq (%rcx,%r10,4), %rcx
cltq
addq %rcx, %rax
cmpq %rax, %r11
movq 0x10(%rsp), %rdx
cmovlq %r10, %rdx
cmovleq %rax, %r11
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rbx
movq 0x48(%rsp), %r9
movq 0x8(%rsp), %rcx
movl %r11d, (%rcx,%rbx,4)
movq %rdx, (%rax,%rbx,8)
testq %r10, %r10
js 0x40f41
movl 0x8(%r9), %eax
movq %r10, %rcx
shlq $0x4, %rcx
movl 0x8(%r13,%rcx), %ecx
subq %rcx, %rax
cmpq %r12, %rax
jg 0x40f44
movq 0x8(%rsp), %rax
cmpl %r11d, (%rax,%r10,4)
jge 0x40f44
movq %rbx, %r10
movslq %r11d, %rax
movq 0xb8(%rsp), %r11
cmpq %rax, %r11
jle 0x40f5b
movq 0x50(%rsp), %r12
jmp 0x40fc6
movl 0x4(%r9), %edx
movl 0x8(%r9), %esi
movq %rdx, %rcx
subq %rsi, %rcx
incq %rcx
movq %rsi, %rdi
notq %rdi
addq 0x120(%rsp), %rdi
movq %rdx, %r8
notq %r8
addq 0x128(%rsp), %r8
movq %rsi, %r9
addq %r8, %r9
cmpq %r8, %rdi
cmovleq 0x98(%rsp), %r9
cmpl %edx, %esi
movl $0x1, %edx
cmovbeq %rdx, %rcx
addq %r9, %rcx
cmpq %rax, %r11
movq 0x50(%rsp), %r12
jl 0x40fb9
cmpq 0x68(%rsp), %rcx
jge 0x40fc6
movq %rax, %r11
movq %rbx, 0x60(%rsp)
movq %rcx, 0x68(%rsp)
incq %rbx
addq $0x10, %r12
cmpq %r14, %rbx
jne 0x40abc
jmp 0x40fe3
movq %r14, %r11
movq %r8, %rax
jmp 0x40d12
movq 0x38(%rsp), %r9
movq 0x60(%rsp), %rdi
jmp 0x41001
movq $-0x1, %rdi
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x38(%rsp), %r9
movq %rdi, %rax
shlq $0x4, %rax
movl 0x8(%r13,%rax), %ecx
movq 0x88(%rsp), %r10
movl %ecx, 0x8(%r10)
movl %ecx, 0x4(%r10)
movl 0x4(%r13,%rax), %edx
movl %edx, 0x18(%r10)
movl %edx, 0x14(%r10)
movl %r11d, 0x24(%r10)
xorl %eax, %eax
testq %rdi, %rdi
js 0x4104a
movq 0x18(%rsp), %rsi
movq %rdi, (%r9,%rax,8)
incq %rax
movq (%rsi,%rdi,8), %rdi
testq %rdi, %rdi
jns 0x4103a
movq -0x8(%r9,%rax,8), %rsi
shlq $0x4, %rsi
movl 0x8(%r13,%rsi), %edi
movl %edi, 0x4(%r10)
movl 0x4(%r13,%rsi), %esi
movl %esi, 0x14(%r10)
movq %rcx, %r8
notq %r8
movq 0x120(%rsp), %r11
addq %r11, %r8
notq %rdx
addq 0x128(%rsp), %rdx
decl %r11d
addl %edx, %ecx
subl %edi, %esi
leal 0x1(%rsi), %esi
movl $0x1, %edi
cmovbl %esi, %edi
cmpq %rdx, %r8
cmovlel %r11d, %ecx
addl %edi, %ecx
movl %ecx, 0x20(%r10)
testq %rax, %rax
movq 0x90(%rsp), %rsi
je 0x410cd
movq %rax, %rcx
movq -0x8(%r9,%rcx,8), %rdx
shlq $0x4, %rdx
movupd (%r13,%rdx), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, %rsi
decq %rcx
jne 0x410b0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq