// File: /workspace/github_cpp_makefile_star2_O3/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, 0x50(%rsp)
movsd %xmm1, 0x88(%rsp)
movsd %xmm0, 0x80(%rsp)
movq %r9, 0xb0(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, %r12
movq %rdx, 0x90(%rsp)
movq %rsi, %r13
movq %rdi, %rbp
movq %rcx, %rdi
xorl %edx, %edx
callq 0x3ecb0
movq 0x28(%r12), %rbx
movq (%r12), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%r12), %r15
cmpq $0x0, 0x130(%rsp)
je 0x3fa30
movq %rbp, %rdi
movl %r13d, %esi
movq %r12, %rdx
movsd 0x50(%rsp), %xmm0
callq 0x3f595
testl %eax, %eax
jle 0x3fa30
movl %eax, %edi
movq 0x30(%rsp), %rax
movl -0x4(%rax,%rdi,4), %r11d
decq %rdi
jmp 0x4009a
leaq (,%r13,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
testq %r13, %r13
jle 0x4008d
movq 0x120(%rsp), %rax
decq %rax
movq %rax, 0x98(%rsp)
leaq -0x8(%rbp), %rdi
leaq -0xc(%rbp), %rax
movq %rax, 0x70(%rsp)
movq $-0x1, 0x40(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x68(%rsp)
xorl %esi, %esi
xorl %ecx, %ecx
movq $-0x1, 0x60(%rsp)
movq $-0x1, %r11
movsd 0x50(%rsp), %xmm4
movsd 0x88(%rsp), %xmm5
movsd 0x80(%rsp), %xmm6
movq %rbp, 0xa0(%rsp)
movq %rbx, 0x18(%rsp)
movq %r15, 0x28(%rsp)
movq %r13, 0x38(%rsp)
movq %rsi, 0x10(%rsp)
movq %r11, 0xb8(%rsp)
movq %rcx, %rax
movq %rsi, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rbp), %r9
movl 0xc(%rbp,%rcx), %r8d
movzbl %r8b, %r13d
movq %rsi, %rdx
subq %rax, %rdx
movq %rsi, %rcx
movq 0x110(%rsp), %rsi
subq %rsi, %rcx
cmpq %rsi, %rdx
movq 0x10(%rsp), %rdx
cmovleq %rax, %rcx
leaq -0x1(%rdx), %r15
cmpq %rcx, %rdx
movq %r9, 0x8(%rsp)
movq %rdi, 0x78(%rsp)
movq %r8, 0xa8(%rsp)
jle 0x3fd05
movl 0x8(%r9), %r11d
leal 0x1(%r11), %esi
movq 0x120(%rsp), %r10
movq %rsi, 0xd0(%rsp)
subq %rsi, %r10
movq %r10, 0xc8(%rsp)
movl %r8d, %eax
shrl $0x8, %eax
movl %eax, 0x4c(%rsp)
movslq %edx, %rax
movq %rax, 0xc0(%rsp)
movq $-0x1, 0x20(%rsp)
movq %rdi, %rbx
movq %r15, %rsi
movq %r13, %rdx
xorl %ebp, %ebp
movl (%rbx), %r12d
movl %r11d, %r10d
subl %r12d, %r10d
testl %r10d, %r10d
jle 0x3fc0c
movl 0x4(%r9), %r8d
movl -0x4(%rbx), %edi
movl %r8d, %eax
subl %edi, %eax
testl %eax, %eax
jle 0x3fc0c
movl %r10d, %r14d
subl %eax, %r14d
negl %r14d
movl %r10d, %r9d
subl %eax, %r9d
cmovbl %r14d, %r9d
cmpl $0x11, %r9d
jl 0x3fc14
incl %r8d
movq 0x128(%rsp), %r14
subq %r8, %r14
movq %rdi, %r8
subq %r12, %r8
cmpl %edi, %r12d
movl $0x0, %edi
cmovbeq %rdi, %r8
movq 0xd0(%rsp), %rdi
addq %r14, %rdi
cmpq %r14, 0xc8(%rsp)
cmovleq 0x120(%rsp), %rdi
addq %r8, %rdi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %edi
cmpl %edi, %r9d
jle 0x3fc14
movq %rdx, %r14
movq 0x8(%rsp), %r9
jmp 0x3fcec
movq %rdx, %r14
jmp 0x3fcec
cmpl %r10d, %eax
cmovbl %eax, %r10d
cmpl %r10d, %r13d
movl %r10d, %edi
cmovbl %r13d, %edi
movl $0x1, %eax
cmpl 0x4c(%rsp), %edi
jb 0x3fc3b
movzbl %dil, %eax
divb 0x4c(%rsp)
movzbl %al, %eax
cmpl %r10d, %r13d
jb 0x3fc45
testl %r9d, %r9d
je 0x3fc7f
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
movapd %xmm0, %xmm1
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, %edi
subl %edi, %eax
movq 0x8(%rsp), %r9
cltq
movq 0x30(%rsp), %rdi
movslq (%rdi,%rsi,4), %r14
addq %rax, %r14
cmpq %rdx, %r14
jle 0x3fca9
xorl %eax, %eax
testq %rbp, %rbp
setg %al
subq %rax, %rbp
movq %rsi, 0x20(%rsp)
jmp 0x3fcd0
movq 0x18(%rsp), %rax
movq 0xc0(%rsp), %rdi
cmpq %rdi, (%rax,%rsi,8)
jne 0x3fccd
cmpq 0xb0(%rsp), %rbp
jge 0x4007b
incq %rbp
movq %rdx, %r14
movq 0x28(%rsp), %rax
movq (%rax,%rsi,8), %rax
testq %rax, %rax
js 0x3fcec
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rdi
movq %rdi, (%rdx,%rax,8)
leaq -0x1(%rsi), %rax
addq $-0x10, %rbx
movq %r14, %rdx
cmpq %rcx, %rsi
movq %rax, %rsi
jg 0x3fb70
jmp 0x3fd14
movq $-0x1, 0x20(%rsp)
movq %r13, %r14
movq %r15, %rax
movq 0x40(%rsp), %r11
testq %r11, %r11
movq 0xa0(%rsp), %rbp
movq 0x118(%rsp), %r10
js 0x3fd49
movq 0x8(%rsp), %rdx
movl 0x4(%rdx), %edx
movq %r11, %rsi
shlq $0x4, %rsi
movl 0x4(%rbp,%rsi), %esi
subq %rsi, %rdx
cmpq %r10, %rdx
jle 0x3fda0
movq 0x10(%rsp), %rsi
cmpq %rcx, %rsi
jle 0x3fe73
movq 0x8(%rsp), %rdx
movl 0x4(%rdx), %edx
movq $-0x1, %r11
movl $0x80000000, %esi       # imm = 0x80000000
movq 0x70(%rsp), %r9
movl (%r9), %edi
movq %rdx, %r8
subq %rdi, %r8
cmpq %r10, %r8
jg 0x3fda0
movq 0x30(%rsp), %rdi
movl (%rdi,%r15,4), %edi
cmpl %edi, %esi
cmovlq %r15, %r11
cmovlel %edi, %esi
leaq -0x1(%r15), %rdi
addq $-0x10, %r9
cmpq %rcx, %r15
movq %rdi, %r15
jg 0x3fd70
testq %r11, %r11
sets %dl
cmpq %rax, %r11
setge %al
orb %dl, %al
jne 0x3fea3
movq %r11, %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %rdi
movl 0x8(%rdi), %eax
movl 0x8(%rbp,%rsi), %r10d
movl %eax, %edx
subl %r10d, %edx
testl %edx, %edx
jle 0x3fee1
addq %rbp, %rsi
movl 0x4(%rdi), %r15d
movl 0x4(%rsi), %ebx
movl %r15d, %r9d
subl %ebx, %r9d
testl %r9d, %r9d
jle 0x3fe55
movl %edx, %edi
subl %r9d, %edi
negl %edi
movl %edx, %esi
subl %r9d, %esi
cmovbl %edi, %esi
cmpl $0x11, %esi
jl 0x3feff
incl %eax
incl %r15d
movq 0x120(%rsp), %r12
movq %r12, %rdi
subq %rax, %rdi
movq 0x128(%rsp), %r8
subq %r15, %r8
movq %rbx, %r15
subq %r10, %r15
cmpl %ebx, %r10d
movl $0x0, %r10d
cmovbeq %r10, %r15
addq %r8, %rax
cmpq %r8, %rdi
cmovleq %r12, %rax
addq %r15, %rax
cvtsi2sd %rax, %xmm0
mulsd %xmm4, %xmm0
cvttsd2si %xmm0, %eax
cmpl %eax, %esi
jle 0x3feff
movq 0x18(%rsp), %rbx
movq 0x28(%rsp), %r15
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
jmp 0x3fef5
movq 0x30(%rsp), %rax
movl %r14d, (%rax,%rsi,4)
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rax
movq %rax, (%r15,%rsi,8)
movq %rsi, 0x40(%rsp)
movq 0x18(%rsp), %rbx
movq 0x38(%rsp), %r13
movq 0x8(%rsp), %rdi
jmp 0x3ffde
movq 0x30(%rsp), %rax
movq 0x10(%rsp), %rsi
movl %r14d, (%rax,%rsi,4)
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rax
movq %rax, (%r15,%rsi,8)
movq %rsi, 0x40(%rsp)
testq %r11, %r11
movq 0x18(%rsp), %rbx
movq 0x38(%rsp), %r13
movq 0x8(%rsp), %rdi
jns 0x3ffb1
jmp 0x3ffde
movq 0x18(%rsp), %rbx
movq 0x28(%rsp), %r15
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %r8
jmp 0x3ffa4
cmpl %edx, %r9d
cmovbl %r9d, %edx
cmpl %edx, %r13d
movl %edx, %edi
cmovbl %r13d, %edi
movq 0xa8(%rsp), %r8
shrl $0x8, %r8d
movl $0x1, %eax
cmpl %r8d, %edi
jb 0x3ff2f
movzbl %dil, %eax
divb %r8b
movzbl %al, %eax
cmpl %edx, %r13d
movq 0x20(%rsp), %r8
jb 0x3ff3d
testl %esi, %esi
je 0x3ff72
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movapd %xmm0, %xmm1
cvtsi2sd %eax, %xmm2
mulsd %xmm6, %xmm1
cvtsi2sd %edx, %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 0x18(%rsp), %rbx
movq 0x28(%rsp), %r15
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rdx
movslq (%rdx,%r11,4), %rdx
cltq
addq %rdx, %rax
cmpq %rax, %r14
cmovlq %r11, %r8
cmovleq %rax, %r14
movq 0x30(%rsp), %rax
movl %r14d, (%rax,%rsi,4)
movq %r8, (%r15,%rsi,8)
movl 0x4(%rdi), %eax
movq %r11, %rdx
shlq $0x4, %rdx
movl 0x4(%rbp,%rdx), %edx
subq %rdx, %rax
cmpq 0x118(%rsp), %rax
jg 0x3ffd9
movq 0x30(%rsp), %rax
cmpl %r14d, (%rax,%r11,4)
cmovlq %rsi, %r11
movq %r11, 0x40(%rsp)
movslq %r14d, %rax
movq 0xb8(%rsp), %r11
cmpq %rax, %r11
jle 0x3fff5
movq 0x78(%rsp), %rdi
jmp 0x40063
movl 0x4(%rdi), %esi
movl 0x8(%rdi), %edi
movq %rsi, %rdx
subq %rdi, %rdx
incq %rdx
movq %rdi, %r8
notq %r8
addq 0x120(%rsp), %r8
movq %rsi, %r9
notq %r9
addq 0x128(%rsp), %r9
movq %rdi, %r10
addq %r9, %r10
cmpq %r9, %r8
cmovleq 0x98(%rsp), %r10
cmpl %esi, %edi
movl $0x1, %esi
cmovbeq %rsi, %rdx
movq 0x10(%rsp), %rsi
addq %r10, %rdx
cmpq %rax, %r11
movq 0x78(%rsp), %rdi
jl 0x40056
cmpq 0x68(%rsp), %rdx
jge 0x40063
movq %rax, %r11
movq %rsi, 0x60(%rsp)
movq %rdx, 0x68(%rsp)
incq %rsi
addq $0x10, %rdi
addq $0x10, 0x70(%rsp)
cmpq %r13, %rsi
jne 0x3fac1
jmp 0x40086
movq %rdx, %r14
movq %rsi, %rax
jmp 0x3fd14
movq 0x60(%rsp), %rdi
jmp 0x4009a
movq $-0x1, %rdi
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq %rdi, %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 %rdi, %rdi
js 0x400cc
movq %rdi, (%rbx,%rax,8)
incq %rax
movq (%r15,%rdi,8), %rdi
jmp 0x400ba
movq -0x8(%rbx,%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 0x120(%rsp), %r10
addq %r10, %r8
notq %rdx
addq 0x128(%rsp), %rdx
decl %r10d
addl %edx, %ecx
subl %edi, %esi
leal 0x1(%rsi), %esi
movl $0x1, %edi
cmovbl %esi, %edi
cmpq %rdx, %r8
cmovlel %r10d, %ecx
addl %edi, %ecx
movl %ecx, 0x20(%r9)
testq %rax, %rax
movq 0x90(%rsp), %rsi
je 0x40151
movq %rax, %rcx
movq -0x8(%rbx,%rcx,8), %rdx
shlq $0x4, %rdx
movupd (%rbp,%rdx), %xmm0
movupd %xmm0, (%rsi)
addq $0x10, %rsi
decq %rcx
jne 0x40135
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq