// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: lchain_dp(k_mer_hit*, long, k_mer_hit*, Chain_Data*, overlap_region*, long, long, long, double, double, double, long, long, long)
uint64_t lchain_dp(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(quick_check) {
        ret = lchain_check(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].offset) - ((int64_t)a[max_ii].offset) > max_dis) {
			max = INT32_MIN; max_ii = -1;
            for (j = i - 1; (j >= st) && ((((int64_t)a[i].offset)-((int64_t)a[j].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].offset)-((int64_t)a[max_ii].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;
            }
        }
    }
    
    skip_ldp:
    ///a[] has been sorted by 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]];
    return cL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movsd %xmm2, 0x48(%rsp)
movsd %xmm1, 0x88(%rsp)
movsd %xmm0, 0x80(%rsp)
movq %r9, 0xb0(%rsp)
movq %r8, 0x50(%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, (%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x130(%rsp)
je 0x402dd
movq %r13, %rdi
movl %r14d, %esi
movq %r12, %rdx
movsd 0x48(%rsp), %xmm0
movq %r9, %rbx
callq 0x3fe4b
movq %rbx, %r9
testl %eax, %eax
jle 0x402dd
movl %eax, %edi
movq (%rsp), %rax
movl -0x4(%rax,%rdi,4), %r11d
decq %rdi
jmp 0x408b5
leaq (,%r14,8), %rdx
movq %r9, 0x30(%rsp)
movq %r9, %rdi
xorl %esi, %esi
callq 0x20e0
testq %r14, %r14
jle 0x408a3
movq 0x120(%rsp), %rax
decq %rax
movq %rax, 0x98(%rsp)
leaq -0x8(%r13), %rdi
leaq -0xc(%r13), %rax
movq %rax, 0x70(%rsp)
movq $-0x1, %r9
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
movq $-0x1, 0x58(%rsp)
movq $-0x1, %r11
movsd 0x48(%rsp), %xmm4
movsd 0x88(%rsp), %xmm5
movsd 0x80(%rsp), %xmm6
movq %r13, 0xa0(%rsp)
movq %r14, 0x68(%rsp)
movq %r11, 0xb8(%rsp)
movq 0x18(%rsp), %rax
movq %rbx, %rcx
shlq $0x4, %rcx
leaq (%rcx,%r13), %r8
movl 0xc(%r13,%rcx), %r10d
movzbl %r10b, %r12d
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, 0x18(%rsp)
cmpq %rsi, %rbx
movq %rbx, 0x28(%rsp)
movq %r8, 0x40(%rsp)
movq %r12, 0x38(%rsp)
movq %rdi, 0x78(%rsp)
movq %r10, 0xa8(%rsp)
jle 0x405b3
movl 0x8(%r8), %r13d
leal 0x1(%r13), %ecx
movq 0x120(%rsp), %rdx
movq %rcx, 0xc8(%rsp)
subq %rcx, %rdx
movq %rdx, 0xc0(%rsp)
movl %r10d, %eax
shrl $0x8, %eax
movl %eax, 0x24(%rsp)
movslq %ebx, %rax
movq %rax, 0xd0(%rsp)
movq $-0x1, 0x8(%rsp)
movq %r15, %r14
xorl %ebp, %ebp
movl (%rdi), %ecx
movl %r13d, %r11d
subl %ecx, %r11d
movl $0x80000000, %eax       # imm = 0x80000000
testl %r11d, %r11d
jle 0x40526
movq 0x40(%rsp), %rdx
movl 0x4(%rdx), %r10d
movl -0x4(%rdi), %edx
movl %r10d, %esi
subl %edx, %esi
testl %esi, %esi
jle 0x40526
movl %esi, %r8d
subl %r11d, %r8d
movl %r8d, %ebx
negl %ebx
cmovsl %r8d, %ebx
cmpl $0x11, %ebx
jb 0x404b3
incl %r10d
movq 0x128(%rsp), %r8
subq %r10, %r8
movq %rdx, %r10
subq %rcx, %r10
cmpl %edx, %ecx
movl $0x0, %ecx
cmovbeq %rcx, %r10
movq 0xc8(%rsp), %rcx
addq %r8, %rcx
cmpq %r8, 0xc0(%rsp)
cmovleq 0x120(%rsp), %rcx
addq %r10, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %ebx
jg 0x40526
cmpl %r11d, %esi
movl %r11d, %r10d
cmovll %esi, %r10d
movq 0x38(%rsp), %rax
cmpl %r10d, %eax
movl %r10d, %ecx
cmovll %eax, %ecx
movl $0x1, %eax
cmpl 0x24(%rsp), %ecx
jl 0x404de
movl %ecx, %eax
xorl %edx, %edx
divl 0x24(%rsp)
cmpl %r11d, %esi
jne 0x404ea
cmpl %r10d, 0x38(%rsp)
jge 0x40526
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movapd %xmm0, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm6, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %r10d, %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 0x40531
movq %r12, %r11
jmp 0x40598
cltq
movq (%rsp), %rcx
movslq (%rcx,%r14,4), %r11
addq %rax, %r11
cmpq %r12, %r11
jle 0x40555
xorl %eax, %eax
testq %rbp, %rbp
setg %al
subq %rax, %rbp
movq %r14, 0x8(%rsp)
jmp 0x4057c
movq 0x30(%rsp), %rax
movq 0xd0(%rsp), %rcx
cmpq %rcx, (%rax,%r14,8)
jne 0x40579
cmpq 0xb0(%rsp), %rbp
jge 0x4088c
incq %rbp
movq %r12, %r11
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rax
testq %rax, %rax
js 0x40598
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq %rdx, (%rcx,%rax,8)
leaq -0x1(%r14), %rax
addq $-0x10, %rdi
movq %r11, %r12
cmpq 0x18(%rsp), %r14
movq %rax, %r14
jg 0x4041e
jmp 0x405c2
movq $-0x1, 0x8(%rsp)
movq %r12, %r11
movq %r15, %rax
testq %r9, %r9
movq 0xa0(%rsp), %r13
movq 0x68(%rsp), %r14
movq 0x118(%rsp), %r12
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %r10
js 0x405fe
movl 0x4(%r10), %ecx
movq %r9, %rdx
shlq $0x4, %rdx
movl 0x4(%r13,%rdx), %edx
subq %rdx, %rcx
cmpq %r12, %rcx
jle 0x4064a
movq $-0x1, %r9
cmpq 0x18(%rsp), %rbx
jle 0x4064a
movl 0x4(%r10), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
movq 0x70(%rsp), %rdi
movl (%rdi), %esi
movq %rcx, %r8
subq %rsi, %r8
cmpq %r12, %r8
jg 0x4064a
movq (%rsp), %rsi
movl (%rsi,%r15,4), %esi
cmpl %esi, %edx
cmovlq %r15, %r9
cmovlel %esi, %edx
leaq -0x1(%r15), %rsi
addq $-0x10, %rdi
cmpq 0x18(%rsp), %r15
movq %rsi, %r15
jg 0x4061a
testq %r9, %r9
sets %cl
cmpq %rax, %r9
setge %al
orb %cl, %al
jne 0x40784
movq %r9, %r8
shlq $0x4, %r8
movl 0x8(%r10), %ecx
movl 0x8(%r13,%r8), %edx
movl %ecx, %edi
subl %edx, %edi
movl $0x80000000, %eax       # imm = 0x80000000
testl %edi, %edi
jle 0x4076d
addq %r13, %r8
movl 0x4(%r10), %r10d
movl 0x4(%r8), %r8d
movl %r10d, %ebx
subl %r8d, %ebx
testl %ebx, %ebx
jle 0x4076d
movl %ebx, %esi
subl %edi, %esi
movl %esi, %ebp
negl %ebp
cmovsl %esi, %ebp
cmpl $0x11, %ebp
jb 0x406fb
incl %ecx
incl %r10d
movq 0x120(%rsp), %r15
movq %r15, %rsi
subq %rcx, %rsi
movq 0x128(%rsp), %r14
subq %r10, %r14
movq %r8, %r10
subq %rdx, %r10
cmpl %r8d, %edx
movl $0x0, %edx
cmovbeq %rdx, %r10
addq %r14, %rcx
cmpq %r14, %rsi
movq 0x68(%rsp), %r14
cmovleq %r15, %rcx
addq %r10, %rcx
cvtsi2sd %rcx, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %ecx
cmpl %ecx, %ebp
jg 0x4076d
cmpl %edi, %ebx
movl %edi, %r8d
cmovll %ebx, %r8d
movq 0x38(%rsp), %rax
cmpl %r8d, %eax
movl %r8d, %ecx
cmovll %eax, %ecx
movq 0xa8(%rsp), %rsi
shrl $0x8, %esi
movl $0x1, %eax
cmpl %esi, %ecx
jl 0x4072c
movl %ecx, %eax
xorl %edx, %edx
divl %esi
cmpl %edi, %ebx
jne 0x40737
cmpl %r8d, 0x38(%rsp)
jge 0x4076d
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movapd %xmm0, %xmm1
cvtsi2sd %eax, %xmm2
mulsd %xmm6, %xmm1
cvtsi2sd %r8d, %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 0x40790
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %r10
jmp 0x40789
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rdx
jmp 0x407bc
movq (%rsp), %rcx
movslq (%rcx,%r9,4), %rcx
cltq
addq %rcx, %rax
cmpq %rax, %r11
movq 0x8(%rsp), %rdx
cmovlq %r9, %rdx
cmovleq %rax, %r11
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %r10
movq (%rsp), %rcx
movl %r11d, (%rcx,%rbx,4)
movq %rdx, (%rax,%rbx,8)
testq %r9, %r9
js 0x407ef
movl 0x4(%r10), %eax
movq %r9, %rcx
shlq $0x4, %rcx
movl 0x4(%r13,%rcx), %ecx
subq %rcx, %rax
cmpq %r12, %rax
jg 0x407f2
movq (%rsp), %rax
cmpl %r11d, (%rax,%r9,4)
jge 0x407f2
movq %rbx, %r9
movslq %r11d, %rax
movq 0xb8(%rsp), %r11
cmpq %rax, %r11
jle 0x40809
movq 0x78(%rsp), %rdi
jmp 0x40874
movl 0x4(%r10), %edx
movl 0x8(%r10), %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, %r10
addq %r8, %r10
cmpq %r8, %rdi
cmovleq 0x98(%rsp), %r10
cmpl %edx, %esi
movl $0x1, %edx
cmovbeq %rdx, %rcx
addq %r10, %rcx
cmpq %rax, %r11
movq 0x78(%rsp), %rdi
jl 0x40867
cmpq 0x60(%rsp), %rcx
jge 0x40874
movq %rax, %r11
movq %rbx, 0x58(%rsp)
movq %rcx, 0x60(%rsp)
incq %rbx
addq $0x10, %rdi
addq $0x10, 0x70(%rsp)
cmpq %r14, %rbx
jne 0x4036e
jmp 0x40897
movq %r12, %r11
movq %r14, %rax
jmp 0x405c2
movq 0x30(%rsp), %r9
movq 0x58(%rsp), %rdi
jmp 0x408b5
movq $-0x1, %rdi
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r9
movq %rdi, %rax
shlq $0x4, %rax
movl 0x8(%r13,%rax), %ecx
movq 0x50(%rsp), %rsi
movl %ecx, 0x8(%rsi)
movl %ecx, 0x4(%rsi)
movl 0x4(%r13,%rax), %edx
movl %edx, 0x18(%rsi)
movl %edx, 0x14(%rsi)
movl %r11d, 0x24(%rsi)
xorl %eax, %eax
testq %rdi, %rdi
js 0x408f7
movq 0x10(%rsp), %rsi
movq %rdi, (%r9,%rax,8)
incq %rax
movq (%rsi,%rdi,8), %rdi
testq %rdi, %rdi
jns 0x408e7
movq -0x8(%r9,%rax,8), %rsi
shlq $0x4, %rsi
movl 0x8(%r13,%rsi), %edi
movq 0x50(%rsp), %r10
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 0x4097f
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 0x40962
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq