// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: lchain_qdp_fix(k_mer_hit*, long, Chain_Data*, long, long, long, double, double, double, long, long, long, long, long)
uint64_t lchain_qdp_fix(k_mer_hit* a, int64_t a_n, Chain_Data* dp, 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 left_fix, int64_t right_fix)
{
    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, must_p = 1, is_reorder = 0; k_mer_hit z;
    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(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))));
    if((right_fix) && (!left_fix)) {
        // fprintf(stderr, "\n[M::%s::] a_n::%ld\n", __func__, a_n);
        // for (i = 0; i < a_n; ++i) {
        //     fprintf(stderr, "+[M::%s::] x::[%u, %u), y::[%u, %u)\n", __func__, 
        //     a[i].self_offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].self_offset+1,
        //     a[i].offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].offset+1);
        // }
        n_skip = a_n>>1;
        for (i=0; i<n_skip; ++i) {
            z = a[i]; a[i] = a[a_n-i-1]; a[a_n-i-1] = z; 
            rev_khit(a[i], xl, yl); rev_khit(a[a_n-i-1], xl, yl);
        }
        if(a_n&1) rev_khit(a[i], xl, yl);
        // for (i = 0; i < a_n; ++i) {
        //     fprintf(stderr, "-[M::%s::] x::[%u, %u), y::[%u, %u)\n", __func__, 
        //     a[i].self_offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].self_offset+1,
        //     a[i].offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].offset+1);
        // }
        is_reorder = 1;
    } 

    if((!right_fix) && (!left_fix)) must_p = 0;

    for (i = st = 0, max_ii = -1; i < a_n; ++i) {
        max_f = a[i].cnt&(0xffu); if(must_p) max_f = INT32_MIN;
        n_skip = 0; max_j = end_j = -1;
        if ((i-st) > max_iter) st = i-max_iter;
        // if(a[0].self_offset == 171728) {
        //     fprintf(stderr, "i::%ld[M::%s::] x::[%u, %u), y::[%u, %u)\n", i, __func__, 
        //         a[i].self_offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].self_offset+1,
        //         a[i].offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].offset+1);
        // }
        for (j = i - 1; j >= 0; --j) {
            sc = comput_sc_ff(&a[i], &a[j], bw_rate, chn_pen_gap, chn_pen_skip, xl, yl);
            // if(a[0].self_offset == 171728) {
            //     fprintf(stderr, "j::%ld[M::%s::sc->%ld] x::[%u, %u), y::[%u, %u)\n", j, __func__, sc,
            //     a[j].self_offset+1-(a[j].cnt&((uint32_t)(0xffu))), a[j].self_offset+1,
            //     a[j].offset+1-(a[j].cnt&((uint32_t)(0xffu))), a[j].offset+1);
            // }
            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) {
                    if((max_j != -1) || (must_p == 0)) break;
                } 
            }
            if (p[j] >= 0) t[p[j]] = i;
            ///put it here will allow at least one prefix no matter max_dis
            ///this is special for gap filling, not for chaining
            if (a[i].self_offset > (max_dis + a[j].self_offset)) {
                if((max_j != -1)) break;
            }
            if (j < st) {
                if((max_j != -1) || (must_p == 0)) break;
            }
        }
        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_ff(&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;
        }
        if(max_j == -1) {
            f[i] = 0; p[i] = max_j;
        } else {
            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(a[0].self_offset == 171728) {
        //     fprintf(stderr, "i::%ld[M::%s::] f::%d, p::%ld\n", i, __func__, f[i], p[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;
            }
        }
    }
    
    skip_ldp:
    if(right_fix && left_fix) msc_i = a_n-1;
    ///a[] has been sorted by self_offset
    i = msc_i; cL = 0; 
    while (i >= 0) {
        t[cL++] = i; msc_i = i; i = p[i];
    }
    // if((right_fix) && (!left_fix)) {
    //     fprintf(stderr, "[M::%s::] cL::%ld\n", __func__, cL);
    // }

    if(is_reorder) {
        n_skip = a_n>>1;
        for (i=0; i<n_skip; ++i) {
            z = a[i]; a[i] = a[a_n-i-1]; a[a_n-i-1] = z; 
            rev_khit(a[i], xl, yl); rev_khit(a[a_n-i-1], xl, yl);
        }
        if(a_n&1) rev_khit(a[i], xl, yl);
        for (i = 0; i < cL; i++) {
            t[i] = a_n - t[i] - 1;
            // fprintf(stderr, ">[M::%s::] t[%ld]::%ld\n", __func__, i, t[i]);
        }
        // for (i = 0; i < a_n; ++i) {
        //     fprintf(stderr, ">[M::%s::] x::[%u, %u), y::[%u, %u)\n", __func__, 
        //     a[i].self_offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].self_offset+1,
        //     a[i].offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].offset+1);
        // }
    } else {
        n_skip = cL>>1;
        for (i = 0; i < n_skip; i++) {
            msc_i = t[i]; t[i] = t[cL-i-1]; t[cL-i-1] = msc_i;
        }
    } 
    // if(cL != a_n) {
    //     fprintf(stderr, "\n[M::%s::] a_n::%ld, left_fix::%ld, right_fix::%ld\n", __func__, a_n, left_fix, right_fix);
    //     for (i = 0; i < a_n; ++i) {
    //         fprintf(stderr, "+[M::%s::] x::[%u, %u), y::[%u, %u)\n", __func__, 
    //         a[i].self_offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].self_offset+1,
    //         a[i].offset+1-(a[i].cnt&((uint32_t)(0xffu))), a[i].offset+1);
    //     }
    //     for (i = 0; i < cL; i++) {
    //         fprintf(stderr, ">[M::%s::] t[%ld]::%ld\n", __func__, i, t[i]);
    //     }
    // }
    return cL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x88(%rsp)
movsd %xmm0, 0x80(%rsp)
movq %r9, %r13
movq %r8, 0xb0(%rsp)
movq %rcx, 0xc8(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq %rdx, %rdi
xorl %edx, %edx
callq 0x1383e
movq 0x28(%r14), %rax
movq %rax, 0x10(%rsp)
movq (%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x120(%rsp)
je 0x16e40
movq %r12, %rdi
movl %r15d, %esi
movq %r14, %rdx
movsd 0x18(%rsp), %xmm0
callq 0x1403e
testl %eax, %eax
jle 0x16e40
movl %eax, %r15d
leaq -0x1(%r15), %rax
movb $0x1, %dil
jmp 0x17381
leaq (,%r15,8), %rdx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
cmpq $0x0, 0x130(%rsp)
sete %al
cmpq $0x0, 0x128(%rsp)
setne %cl
orb %al, %cl
movb %cl, 0x7(%rsp)
movsd 0x18(%rsp), %xmm0
jne 0x16f75
movq %r15, %rsi
sarq %rsi
movq 0x110(%rsp), %rax
leal -0x1(%rax), %ecx
movq 0x118(%rsp), %rax
leal -0x1(%rax), %edx
xorl %eax, %eax
testq %rsi, %rsi
cmovgq %rsi, %rax
movq %r15, %rsi
shlq $0x4, %rsi
addq %r12, %rsi
addq $-0x10, %rsi
movq %r12, %rdi
movq %rax, %r8
subq $0x1, %r8
jb 0x16f31
movups (%rdi), %xmm1
movaps %xmm1, 0x90(%rsp)
movups (%rsi), %xmm1
movups %xmm1, (%rdi)
movaps 0x90(%rsp), %xmm1
movups %xmm1, (%rsi)
movl 0x4(%rdi), %r9d
movl 0x8(%rdi), %r10d
notl %r10d
movzbl 0xc(%rdi), %r11d
addl %ecx, %r10d
addl %r11d, %r10d
movl %r10d, 0x8(%rdi)
notl %r9d
addl %edx, %r11d
addl %r9d, %r11d
movl %r11d, 0x4(%rdi)
movl 0x4(%rsi), %r9d
movl 0x8(%rsi), %r10d
notl %r10d
movzbl 0xc(%rsi), %r11d
addl %ecx, %r10d
addl %r11d, %r10d
movl %r10d, 0x8(%rsi)
notl %r9d
addl %edx, %r11d
addl %r9d, %r11d
movl %r11d, 0x4(%rsi)
addq $-0x10, %rsi
addq $0x10, %rdi
jmp 0x16eb7
testb $0x1, %r15b
je 0x16f75
shlq $0x4, %rax
movl 0x4(%r12,%rax), %ecx
movl 0x8(%r12,%rax), %edx
movzbl 0xc(%r12,%rax), %esi
subl %esi, %edx
incl %edx
notl %edx
movq 0x110(%rsp), %rdi
addl %edi, %edx
movl %edx, 0x8(%r12,%rax)
subl %esi, %ecx
incl %ecx
notl %ecx
movq 0x118(%rsp), %rdx
addl %edx, %ecx
movl %ecx, 0x4(%r12,%rax)
movq 0x130(%rsp), %rax
orq 0x128(%rsp), %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x40(%rsp)
testq %r15, %r15
movl $0x0, %eax
cmovgq %r15, %rax
movq %rax, 0xb8(%rsp)
leaq -0x10(%r12), %rdi
leaq -0x8(%r12), %rax
movq %rax, 0x70(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x60(%rsp)
pushq $-0x1
popq %rax
movq %rax, %rbp
movq %rax, 0x58(%rsp)
movq %rax, %r9
xorl %ecx, %ecx
movq %r15, 0xa8(%rsp)
movq %r12, 0x68(%rsp)
movq %r13, 0x8(%rsp)
cmpq 0xb8(%rsp), %rcx
je 0x17377
movq %r9, 0x30(%rsp)
movq %rbp, 0x78(%rsp)
movq %rcx, %rax
shlq $0x4, %rax
movq %rcx, %r8
leaq (%r12,%rax), %r14
cmpq $0x0, 0x48(%rsp)
movzbl 0xc(%r12,%rax), %ecx
movq $-0x80000000, %rax      # imm = 0x80000000
cmovneq %rax, %rcx
movq %rcx, 0x38(%rsp)
movq %r8, %rax
movq 0x40(%rsp), %rsi
subq %rsi, %rax
movq %r8, %rcx
movq 0xb0(%rsp), %rdx
subq %rdx, %rcx
cmpq %rdx, %rax
cmovgq %rcx, %rsi
movq %rsi, 0x40(%rsp)
leaq -0x1(%r8), %r12
movq %r8, 0x20(%rsp)
movslq %r8d, %rax
movq %rax, 0xd0(%rsp)
movq %rdi, 0xc0(%rsp)
movq %rdi, %r15
pushq $-0x1
popq %rbx
xorl %r13d, %r13d
movq %r12, %rbp
testq %rbp, %rbp
js 0x171af
movq %r14, %rdi
movq %r15, %rsi
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movq 0x110(%rsp), %rdx
movq 0x118(%rsp), %rcx
callq 0x17f52
movl %eax, %ecx
negl %ecx
jno 0x170bb
movq %r13, %rax
movsd 0x18(%rsp), %xmm0
jmp 0x171a0
cltq
movq 0x28(%rsp), %rdi
movslq (%rdi,%rbp,4), %rdx
addq %rax, %rdx
cmpq 0x38(%rsp), %rdx
movsd 0x18(%rsp), %xmm0
jle 0x170f8
xorl %eax, %eax
testq %r13, %r13
setg %al
subq %rax, %r13
movq %rbp, %rcx
movq %r13, %rax
movq %rdx, 0x38(%rsp)
movq 0x8(%rsp), %r13
movq 0x30(%rsp), %r9
jmp 0x1714a
movq 0x10(%rsp), %rax
movq 0xd0(%rsp), %rcx
cmpq %rcx, (%rax,%rbp,8)
movq 0x30(%rsp), %r9
jne 0x17123
leaq 0x1(%r13), %rax
cmpq 0xc8(%rsp), %r13
jge 0x17130
movq %rbx, %rcx
jmp 0x17129
movq %rbx, %rcx
movq %r13, %rax
movq 0x8(%rsp), %r13
jmp 0x1714a
cmpq $0x0, 0x48(%rsp)
movq 0x8(%rsp), %r13
je 0x171c6
pushq $-0x1
popq %rcx
cmpq $-0x1, %rbx
jne 0x171c6
movq 0x50(%rsp), %rdx
movq (%rdx,%rbp,8), %rdx
testq %rdx, %rdx
js 0x17166
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %r8
movq %r8, (%rsi,%rdx,8)
movl 0x8(%r14), %edx
movl 0x8(%r15), %esi
addq %r13, %rsi
cmpq %rdx, %rsi
jge 0x1717c
cmpq $-0x1, %rcx
jne 0x171c3
cmpq 0x40(%rsp), %rbp
jge 0x1719d
cmpq $0x0, 0x48(%rsp)
je 0x1736a
pushq $-0x1
popq %rbx
cmpq $-0x1, %rcx
je 0x171a0
jmp 0x17362
movq %rcx, %rbx
decq %rbp
addq $-0x10, %r15
movq %rax, %r13
jmp 0x17071
pushq $-0x1
popq %rbp
movq 0x8(%rsp), %r13
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %r9
jmp 0x171c6
movq %rcx, %rbx
testq %r9, %r9
movq 0xa8(%rsp), %r15
js 0x171ef
movl 0x8(%r14), %eax
movq %r9, %rcx
shlq $0x4, %rcx
movq 0x68(%rsp), %rdx
movl 0x8(%rdx,%rcx), %ecx
subq %rcx, %rax
cmpq %r13, %rax
jle 0x17228
movl $0x80000000, %eax       # imm = 0x80000000
movq 0x70(%rsp), %rcx
pushq $-0x1
popq %r9
cmpq 0x40(%rsp), %r12
jl 0x17228
movl 0x8(%r14), %edx
movl (%rcx), %esi
subq %rsi, %rdx
cmpq %r13, %rdx
jg 0x17228
movl (%rdi,%r12,4), %edx
cmpl %edx, %eax
cmovlq %r12, %r9
cmovlel %edx, %eax
decq %r12
addq $-0x10, %rcx
jmp 0x171fd
testq %r9, %r9
sets %al
cmpq %rbp, %r9
setge %cl
orb %al, %cl
movq 0x68(%rsp), %r12
jne 0x1729b
movq %r9, %rsi
shlq $0x4, %rsi
addq %r12, %rsi
movq %r14, %rdi
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movq 0x110(%rsp), %rdx
movq 0x118(%rsp), %rcx
movq %r9, %rbp
callq 0x17f52
movq %rbp, %r9
movq 0x28(%rsp), %rdi
movsd 0x18(%rsp), %xmm0
movl %eax, %ecx
negl %ecx
jo 0x1729b
movslq (%rdi,%r9,4), %rcx
cltq
addq %rcx, %rax
movq %r9, %rcx
cmpq %rax, 0x38(%rsp)
jl 0x172ae
movq 0x38(%rsp), %rax
movq %rbx, %rcx
cmpq $-0x1, %rbx
jne 0x172ae
xorl %eax, %eax
pushq $-0x1
popq %rcx
movq 0x78(%rsp), %rbp
movq 0x20(%rsp), %rsi
movl %eax, (%rdi,%rsi,4)
movq 0x50(%rsp), %rdx
movq %rcx, (%rdx,%rsi,8)
testq %r9, %r9
js 0x172e7
movl 0x8(%r14), %ecx
movq %r9, %rdx
shlq $0x4, %rdx
movl 0x8(%r12,%rdx), %edx
subq %rdx, %rcx
cmpq %r13, %rcx
jg 0x172ec
cmpl %eax, (%rdi,%r9,4)
jge 0x172ec
movq 0x20(%rsp), %r9
movslq %eax, %rbx
cmpq %rbx, %rbp
jle 0x172fb
movq 0x20(%rsp), %rcx
jmp 0x17348
movl 0x4(%r14), %ecx
movl 0x8(%r14), %edi
movq %rdi, %rsi
movq 0x110(%rsp), %rdx
movq %rcx, %r8
movq %r9, %r14
movq 0x118(%rsp), %r9
callq 0x12ff0
movq %r14, %r9
movsd 0x18(%rsp), %xmm0
cmpq %rbx, %rbp
movq 0x20(%rsp), %rcx
jl 0x1733b
cmpq 0x60(%rsp), %rax
jge 0x17348
movq %rbx, %rbp
movq %rcx, 0x58(%rsp)
movq %rax, 0x60(%rsp)
incq %rcx
movq 0xc0(%rsp), %rdi
addq $0x10, %rdi
addq $0x10, 0x70(%rsp)
jmp 0x16fe2
movq %rcx, %rbx
jmp 0x171b2
movq %rcx, %rbx
movq 0x28(%rsp), %rdi
jmp 0x171c6
movb 0x7(%rsp), %dil
movq 0x58(%rsp), %rax
leaq -0x1(%r15), %rdx
cmpq $0x0, 0x130(%rsp)
cmoveq %rax, %rdx
cmpq $0x0, 0x128(%rsp)
cmoveq %rax, %rdx
movq 0x10(%rsp), %rax
leaq -0x8(%rax), %rcx
xorl %eax, %eax
movq 0x50(%rsp), %rsi
testq %rdx, %rdx
js 0x173c5
incq %rax
movq %rdx, 0x8(%rcx)
addq $0x8, %rcx
movq (%rsi,%rdx,8), %rdx
jmp 0x173af
testb %dil, %dil
je 0x173f7
movq %rax, %rdx
shrq %rdx
xorl %esi, %esi
movq 0x10(%rsp), %r9
cmpq %rsi, %rdx
je 0x1750c
movq (%r9,%rsi,8), %rdi
movq (%rcx), %r8
movq %r8, (%r9,%rsi,8)
movq %rdi, (%rcx)
incq %rsi
addq $-0x8, %rcx
jmp 0x173d7
movq %r15, %rdi
sarq %rdi
movq 0x110(%rsp), %rcx
leal -0x1(%rcx), %edx
movq 0x118(%rsp), %rcx
leal -0x1(%rcx), %esi
xorl %ecx, %ecx
testq %rdi, %rdi
cmovgq %rdi, %rcx
movq %r15, %rdi
shlq $0x4, %rdi
addq %r12, %rdi
addq $-0x10, %rdi
movq %r12, %r8
movq %rcx, %r9
subq $0x1, %r9
jb 0x174a8
movups (%r8), %xmm0
movaps %xmm0, 0x90(%rsp)
movups (%rdi), %xmm0
movups %xmm0, (%r8)
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%rdi)
movl 0x4(%r8), %r10d
movl 0x8(%r8), %r11d
notl %r11d
movzbl 0xc(%r8), %ebx
addl %edx, %r11d
addl %ebx, %r11d
movl %r11d, 0x8(%r8)
notl %r10d
addl %esi, %ebx
addl %r10d, %ebx
movl %ebx, 0x4(%r8)
movl 0x4(%rdi), %r10d
movl 0x8(%rdi), %r11d
notl %r11d
movzbl 0xc(%rdi), %ebx
addl %edx, %r11d
addl %ebx, %r11d
movl %r11d, 0x8(%rdi)
notl %r10d
addl %esi, %ebx
addl %r10d, %ebx
movl %ebx, 0x4(%rdi)
addq $-0x10, %rdi
addq $0x10, %r8
jmp 0x17430
testb $0x1, %r15b
je 0x174ed
shlq $0x4, %rcx
movl 0x4(%r12,%rcx), %edx
movl 0x8(%r12,%rcx), %esi
movzbl 0xc(%r12,%rcx), %edi
subl %edi, %esi
incl %esi
notl %esi
movq 0x110(%rsp), %r8
addl %esi, %r8d
movl %r8d, 0x8(%r12,%rcx)
subl %edi, %edx
incl %edx
notl %edx
movq 0x118(%rsp), %rsi
addl %edx, %esi
movl %esi, 0x4(%r12,%rcx)
xorl %ecx, %ecx
movq 0x10(%rsp), %rsi
cmpq %rcx, %rax
je 0x1750c
movq (%rsi,%rcx,8), %rdx
notq %rdx
addq %r15, %rdx
movq %rdx, (%rsi,%rcx,8)
incq %rcx
jmp 0x174f4
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq