// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movsd %xmm1, -0x40(%rbp)
movsd %xmm2, -0x48(%rbp)
movq $0x0, -0xe8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x226f0
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq 0x20(%rbp), %rax
cmpq 0x28(%rbp), %rax
jge 0x23cff
movq 0x20(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x23d0a
movq 0x28(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movsd -0x48(%rbp), %xmm1
cvtsi2sdq -0xa0(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0xa0(%rbp)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0x90(%rbp)
movq $0x7fffffff, -0xb8(%rbp) # imm = 0x7FFFFFFF
cmpq $0x0, 0x30(%rbp)
je 0x23dc3
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rdx
movsd -0x48(%rbp), %xmm0
callq 0x23300
cltq
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xe0(%rbp)
jle 0x23dc1
movq -0xe0(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0xc0(%rbp), %rax
movq -0x98(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x90(%rbp)
jmp 0x2433d
jmp 0x23dc3
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2100
movq $0x0, -0x70(%rbp)
movq $0x0, -0xd0(%rbp)
movq $-0x1, -0xa8(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0x10(%rbp), %rax
jge 0x2433b
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
movl %eax, %eax
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $-0x1, -0x80(%rbp)
movq $-0x1, -0x78(%rbp)
movq -0xd0(%rbp), %rax
subq -0x70(%rbp), %rax
cmpq 0x10(%rbp), %rax
jle 0x23e5d
movq -0xd0(%rbp), %rax
subq 0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xd0(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
cmpq -0x70(%rbp), %rax
jl 0x23fb2
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x8(%rbp), %rsi
movq -0xd8(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movsd -0x48(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
movq 0x20(%rbp), %rdx
movq 0x28(%rbp), %rcx
callq 0x2b100
cltq
movq %rax, -0x88(%rbp)
cmpq $-0x80000000, -0x88(%rbp) # imm = 0x80000000
jne 0x23edb
jmp 0x23f9b
movq -0xc0(%rbp), %rax
movq -0xd8(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
addq -0x88(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x60(%rbp), %rax
jle 0x23f33
movq -0x88(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x68(%rbp)
jle 0x23f31
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x23f69
movq -0x58(%rbp), %rax
movq -0xd8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0xd0(%rbp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x23f67
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
cmpq -0x30(%rbp), %rax
jle 0x23f65
jmp 0x23fb2
jmp 0x23f67
jmp 0x23f69
movq -0x50(%rbp), %rax
movq -0xd8(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x23f99
movq -0xd0(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0xd8(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x23f9b
movq -0xd8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x23e6f
movq -0xd8(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0xa8(%rbp)
jl 0x23ffe
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x8(%rbp), %rcx
movq -0xa8(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
cmpq 0x18(%rbp), %rax
jle 0x240d9
movl $0x80000000, -0xc4(%rbp) # imm = 0x80000000
movq $-0x1, -0xa8(%rbp)
movq -0xd0(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rcx
xorl %eax, %eax
cmpq -0x70(%rbp), %rcx
movb %al, -0xf1(%rbp)
jl 0x24074
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x8(%rbp), %rcx
movq -0xd8(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
cmpq 0x18(%rbp), %rax
setle %al
movb %al, -0xf1(%rbp)
movb -0xf1(%rbp), %al
testb $0x1, %al
jne 0x24080
jmp 0x240d7
movl -0xc4(%rbp), %eax
movq -0xc0(%rbp), %rcx
movq -0xd8(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x240be
movq -0xc0(%rbp), %rax
movq -0xd8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xc4(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x240c0
movq -0xd8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x24025
jmp 0x240d9
cmpq $0x0, -0xa8(%rbp)
jl 0x24197
movq -0xa8(%rbp), %rax
cmpq -0x80(%rbp), %rax
jge 0x24197
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x8(%rbp), %rsi
movq -0xa8(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movsd -0x48(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
movq 0x20(%rbp), %rdx
movq 0x28(%rbp), %rcx
callq 0x2b100
movl %eax, -0xc8(%rbp)
cmpl $0x80000000, -0xc8(%rbp) # imm = 0x80000000
je 0x24195
movq -0x60(%rbp), %rax
movl -0xc8(%rbp), %ecx
movq -0xc0(%rbp), %rdx
movq -0xa8(%rbp), %rsi
addl (%rdx,%rsi,4), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x24195
movl -0xc8(%rbp), %eax
movq -0xc0(%rbp), %rcx
movq -0xa8(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
cltq
movq %rax, -0x60(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x24197
movq -0x60(%rbp), %rax
movl %eax, %edx
movq -0xc0(%rbp), %rax
movq -0xd0(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x78(%rbp), %rdx
movq -0x50(%rbp), %rax
movq -0xd0(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0xa8(%rbp)
jl 0x24222
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x8(%rbp), %rcx
movq -0xa8(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
cmpq 0x18(%rbp), %rax
jg 0x24230
movq -0xc0(%rbp), %rax
movq -0xa8(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0xc0(%rbp), %rcx
movq -0xd0(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x24230
movq -0xd0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xc0(%rbp), %rax
movq -0xd0(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
cmpq -0x90(%rbp), %rax
jl 0x24322
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, %edi
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, %esi
movq 0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %r8
shlq $0x4, %r8
addq %r8, %rax
movl 0x4(%rax), %eax
movl %eax, %r8d
movq 0x28(%rbp), %r9
callq 0x21580
movq %rax, -0xb0(%rbp)
movq -0xc0(%rbp), %rax
movq -0xd0(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
cmpq -0x90(%rbp), %rax
jg 0x242eb
movq -0xb0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jge 0x24320
movq -0xc0(%rbp), %rax
movq -0xd0(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x90(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x24322
jmp 0x24324
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x23df4
jmp 0x2433d
movq -0x98(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x90(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x24(%rax)
movq $0x0, -0xe8(%rbp)
cmpq $0x0, -0xd0(%rbp)
jl 0x24400
movq -0xd0(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0xe8(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xe8(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0xd0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x50(%rbp), %rax
movq -0xd0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd0(%rbp)
jmp 0x243ac
movq -0x8(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0xe8(%rbp), %rdx
subq $0x1, %rdx
movq (%rcx,%rdx,8), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0xe8(%rbp), %rdx
subq $0x1, %rdx
movq (%rcx,%rdx,8), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edi
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %esi
movq 0x20(%rbp), %rdx
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, %r8d
movq 0x28(%rbp), %r9
callq 0x21580
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x20(%rax)
movq $0x0, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0xe8(%rbp), %rax
jge 0x244ff
movq -0x8(%rbp), %rcx
movq -0x58(%rbp), %rax
movq -0xe8(%rbp), %rdx
subq -0xd0(%rbp), %rdx
subq $0x1, %rdx
movq (%rax,%rdx,8), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x18(%rbp), %rax
movq -0xd0(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x24496
movq -0xe8(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nop