// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: mg_lchain_dp(int, int, int, int, int, int, int, float, long, mg128_t*, int*, unsigned long**, void*)
mg128_t *mg_lchain_dp(int max_dist_x, int max_dist_y, int bw, int max_skip, int max_iter, int min_cnt, int min_sc, float chn_pen_gap, 
					  int64_t n, mg128_t *a, int *n_u_, uint64_t **_u, void *km)
{ // TODO: make sure this works when n has more than 32 bits
	int32_t *f, *t, *v, n_u, n_v;
	int64_t *p, i, j, max_ii, st = 0;
	uint64_t *u;

	if (_u) *_u = 0, *n_u_ = 0;
	if (n == 0 || a == 0) return 0;
	KMALLOC(km, p, n);///id of last cell
	KMALLOC(km, f, n);///f[] is the score ending at i, not always the peak
	KMALLOC(km, v, n);///v[] keeps the peak score up to i;
	KCALLOC(km, t, n);///t doesn't matter too much; it is mainly used to accelrate the iteration

	// a[].x: ref_id(31)rev(1)r_pos(32)
 	// a[].y: weight(8)query_id(8)flag(8)span(8)q_pos(32)
	// fill the score and backtrack arrays
	for (i = st = 0, max_ii = -1; i < n; ++i) {
		int64_t max_j = -1, end_j;
        ///max_f -> score of minimizer
		int32_t max_f = normal_sc(a[i].y>>MG_SEED_WT_SHIFT, a[i].y>>32&0xff), n_skip = 0;
		///until we are at the same rid, same direction, and the coordinates are close enough
		while (st < i && (a[i].x>>32 != a[st].x>>32 || a[i].x > a[st].x + max_dist_x)) ++st;
		///max_iter = 10000 in default, which means dp can go back to up to 10000 cells
		if (i - st > max_iter) st = i - max_iter; 
		for (j = i - 1; j >= st; --j) {
			int32_t sc;
			sc = comput_sc(&a[i], &a[j], max_dist_x, max_dist_y, bw, chn_pen_gap);
			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) {///note we scan j backwards; we don't need to update t[] for each i
				if (++n_skip > max_skip)
					break;
			}
			if (p[j] >= 0) t[p[j]] = i;//p[]: prefix idx; means there is a chain longer than 2
		}
		end_j = j;///end_j might be > 0; just the end idx of backwards
		///if not close enough, select a new max
		///max_ii is just used to rescue best-score in case best-score appears before end_j
		if (max_ii < 0 || (int64_t)(a[i].x - a[max_ii].x) > (int64_t)max_dist_x) {///select a new max
			int32_t max = INT32_MIN;
			max_ii = -1;
			for (j = i - 1; j >= st; --j)
				if (max < f[j]) max = f[j], max_ii = j;
		}
		///note: it will happen when `max_ii` < `end_j`; 
		///iteration is terminated at `end_j` mostly because of `max_skip` and `max_iter`
		///max_ii is just used to rescue best-score in case best-score appears before end_j
		if (max_ii >= 0 && max_ii < end_j) {
			int32_t tmp;
			tmp = comput_sc(&a[i], &a[max_ii], max_dist_x, max_dist_y, bw, chn_pen_gap);
			if (tmp != INT32_MIN && max_f < tmp + f[max_ii])
				max_f = tmp + f[max_ii], max_j = max_ii;
		}
		// v[] keeps the peak score up to i (as score might decerase); f[] is the score ending at i, not always the peak
		f[i] = max_f, p[i] = max_j;//p[]: prefix idx
		v[i] = max_j >= 0 && v[max_j] > max_f? v[max_j] : max_f; 
		if (max_ii < 0 || ((int64_t)(a[i].x - a[max_ii].x) <= (int64_t)max_dist_x && f[max_ii] < f[i]))
			max_ii = i;
	}
	///after mg_chain_backtrack, the results are saved in u and v;
	u = mg_chain_backtrack(km, n, f, p, v, t, min_cnt, min_sc, 0, &n_u, &n_v);
	*n_u_ = n_u, *_u = u; // NB: note that u[] may not be sorted by score here
	kfree(km, p); kfree(km, f); kfree(km, t);
	if (n_u == 0) {
		kfree(km, a); kfree(km, v);
		return 0;
	}
	//u[]: sc|occ of chains; chain is mostly sorted by the score; at least the first chain has the largest score
	//v[]: idx of each element
	return compact_a(km, n_u, u, n_v, v, a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r8d, %ebp
movl %ecx, 0x60(%rsp)
movl %edx, 0x30(%rsp)
movl %edi, 0x20(%rsp)
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rcx
movq 0xc8(%rsp), %rdx
testq %rax, %rax
je 0x199a9d
movq $0x0, (%rax)
movl $0x0, (%rcx)
testq %rdx, %rdx
sete %al
cmpq $0x0, 0xd0(%rsp)
sete %cl
orb %al, %cl
je 0x199abb
xorl %r12d, %r12d
jmp 0x19a303
movl %esi, 0x34(%rsp)
movq %r9, 0x70(%rsp)
movq 0xe8(%rsp), %rbx
leaq (,%rdx,8), %rsi
movq %rbx, %rdi
movss %xmm0, 0x5c(%rsp)
callq 0x1e36fd
movq %rax, 0x28(%rsp)
movq 0xc8(%rsp), %rax
leaq (,%rax,4), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1e36fd
movq %rax, %r15
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1e36fd
movq %rax, 0x10(%rsp)
movl $0x4, %edx
movq %rbx, %rdi
movq 0xc8(%rsp), %rsi
callq 0x1e3590
movq 0xc8(%rsp), %rsi
movss 0x5c(%rsp), %xmm8
movq 0xd0(%rsp), %r11
movq %rax, 0x48(%rsp)
testq %rsi, %rsi
jle 0x19a06f
movslq 0x20(%rsp), %rbx
movslq %ebp, %rax
movq %rax, 0x78(%rsp)
leaq -0x8(%r11), %rax
movq %rax, 0x68(%rsp)
movq $-0x1, %rdi
xorl %ebp, %ebp
movss 0x99d5a(%rip), %xmm0    # 0x2338d0
movss 0x99d56(%rip), %xmm1    # 0x2338d4
movss 0x99d52(%rip), %xmm2    # 0x2338d8
movsd 0x99cf2(%rip), %xmm3    # 0x233880
xorl %r9d, %r9d
movq %rbx, 0x38(%rsp)
movq %rbp, %rsi
shlq $0x4, %rsi
movq 0x8(%r11,%rsi), %r8
movq %r8, %rax
shrq $0x38, %rax
movq %r8, %rcx
shrq $0x20, %rcx
movzbl %cl, %ecx
cmpq $0xff, %rax
je 0x199be0
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %ecx, %xmm5
mulsd %xmm3, %xmm4
mulsd %xmm4, %xmm5
cvttsd2si %xmm5, %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovll %eax, %ecx
movl %ecx, 0x8(%rsp)
addq %r11, %rsi
cmpq %rbp, %r9
movq %rsi, 0x18(%rsp)
jge 0x199c29
movq (%rsi), %rax
movq %r9, %rcx
shlq $0x4, %rcx
addq %r11, %rcx
movq (%rcx), %rdx
movq %rdx, %r10
xorq %rax, %r10
shrq $0x20, %r10
jne 0x199c15
addq %rbx, %rdx
cmpq %rdx, %rax
jbe 0x199c24
incq %r9
addq $0x10, %rcx
cmpq %rbp, %r9
jl 0x199bfe
movq %rbp, %r9
movq 0x18(%rsp), %rsi
movq %r9, %rax
movq %rbp, %rcx
subq %r9, %rcx
movq %rbp, %r9
movq 0x78(%rsp), %rdx
subq %rdx, %r9
cmpq %rdx, %rcx
cmovleq %rax, %r9
leaq -0x1(%rbp), %rbx
cmpq %r9, %rbp
jle 0x199e0f
movl (%rsi), %eax
movl %eax, 0x50(%rsp)
movq $-0x1, 0x40(%rsp)
xorl %eax, %eax
movq 0x68(%rsp), %rdx
movq %rbx, %rcx
movq (%rdx), %r10
movl %r8d, %r11d
subl %r10d, %r11d
setle %r14b
cmpl 0x20(%rsp), %r11d
setg %r12b
orb %r14b, %r12b
movl $0x80000000, %r13d      # imm = 0x80000000
jne 0x199d95
movq %r15, %rsi
movl 0x50(%rsp), %r12d
subl -0x8(%rdx), %r12d
testl %r12d, %r12d
setle %r14b
cmpl 0x34(%rsp), %r12d
setg %r15b
orb %r14b, %r15b
je 0x199cb8
movq %rsi, %r15
jmp 0x199d95
movl %r12d, %r15d
subl %r11d, %r15d
movl %r15d, %r14d
negl %r14d
cmovsl %r15d, %r14d
cmpl 0x30(%rsp), %r14d
movq %rsi, %r15
jg 0x199d95
cmpl %r11d, %r12d
movl %r11d, %r13d
cmovll %r12d, %r13d
movq %r10, %r15
shrq $0x20, %r15
movzbl %r15b, %r15d
shrq $0x38, %r10
cmpl %r13d, %r15d
cmovll %r15d, %r13d
cmpq $0xff, %r10
je 0x199d2a
xorps %xmm4, %xmm4
cvtsi2sd %r10d, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %r13d, %xmm5
mulsd %xmm3, %xmm4
mulsd %xmm4, %xmm5
cvttsd2si %xmm5, %r13d
cmpl $0x2, %r13d
movl $0x1, %r10d
cmovll %r10d, %r13d
cmpl %r11d, %r12d
movq %rsi, %r15
je 0x199d95
xorps %xmm4, %xmm4
cvtsi2ss %r14d, %xmm4
xorpd %xmm5, %xmm5
cmpl $0x2, %r14d
jb 0x199d84
movd %xmm4, %r10d
movl %r10d, %r11d
shrl $0x17, %r11d
addl $-0x80, %r11d
cvtsi2ss %r11, %xmm6
andl $0x7fffff, %r10d        # imm = 0x7FFFFF
orl $0x3f800000, %r10d      # imm = 0x3F800000
movd %r10d, %xmm7
movdqa %xmm7, %xmm5
mulss %xmm0, %xmm5
addss %xmm1, %xmm5
mulss %xmm7, %xmm5
addss %xmm2, %xmm5
addss %xmm6, %xmm5
mulss %xmm8, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %r10d
subl %r10d, %r13d
movl %r13d, %r10d
negl %r10d
jo 0x199df1
addl (%r15,%rcx,4), %r13d
cmpl 0x8(%rsp), %r13d
jle 0x199dc0
xorl %r10d, %r10d
testl %eax, %eax
setg %r10b
subl %r10d, %eax
movq %rcx, 0x40(%rsp)
movl %r13d, 0x8(%rsp)
jmp 0x199dda
movq 0x48(%rsp), %rsi
cmpl %ebp, (%rsi,%rcx,4)
jne 0x199dda
leal 0x1(%rax), %r11d
movb $0x1, %r10b
cmpl 0x60(%rsp), %eax
movl %r11d, %eax
jge 0x199df4
movq 0x28(%rsp), %r10
movq (%r10,%rcx,8), %r10
testq %r10, %r10
js 0x199df1
movq 0x48(%rsp), %rsi
movl %ebp, (%rsi,%r10,4)
xorl %r10d, %r10d
testb %r10b, %r10b
jne 0x199e1d
leaq -0x1(%rcx), %r11
addq $-0x10, %rdx
cmpq %r9, %rcx
movq %r11, %rcx
jg 0x199c6a
jmp 0x199e20
movq $-0x1, 0x40(%rsp)
movq %rbx, %r11
jmp 0x199e20
movq %rcx, %r11
testq %rdi, %rdi
movq 0x18(%rsp), %r10
js 0x199e47
movq (%r10), %rax
movq %rdi, %rcx
shlq $0x4, %rcx
movq 0xd0(%rsp), %rdx
subq (%rdx,%rcx), %rax
cmpq 0x38(%rsp), %rax
jle 0x199e71
movq $-0x1, %rdi
cmpq %r9, %rbp
jle 0x199e71
movl $0x80000000, %eax       # imm = 0x80000000
movl (%r15,%rbx,4), %ecx
cmpl %ecx, %eax
cmovlq %rbx, %rdi
cmovlel %ecx, %eax
leaq -0x1(%rbx), %rcx
cmpq %r9, %rbx
movq %rcx, %rbx
jg 0x199e58
testq %rdi, %rdi
sets %al
cmpq %r11, %rdi
setge %cl
orb %al, %cl
movq 0x40(%rsp), %r14
jne 0x199ee5
movq %rdi, %r10
shlq $0x4, %r10
movq 0xd0(%rsp), %r11
movq 0x8(%r11,%r10), %rdx
subl %edx, %r8d
setle %al
cmpl 0x20(%rsp), %r8d
setg %cl
orb %al, %cl
movl $0x80000000, %eax       # imm = 0x80000000
movq 0x38(%rsp), %rbx
jne 0x199fd8
movq 0x18(%rsp), %rcx
movl (%rcx), %ecx
subl (%r11,%r10), %ecx
testl %ecx, %ecx
setle %r10b
cmpl 0x34(%rsp), %ecx
setg %r11b
orb %r10b, %r11b
je 0x199f04
movq 0xd0(%rsp), %r11
jmp 0x199fd8
movq 0xd0(%rsp), %r11
movq 0x10(%rsp), %rcx
movq 0xc8(%rsp), %rsi
movq 0x38(%rsp), %rbx
jmp 0x199ff0
movl %ecx, %r11d
subl %r8d, %r11d
movl %r11d, %r10d
negl %r10d
cmovsl %r11d, %r10d
cmpl 0x30(%rsp), %r10d
movq 0xd0(%rsp), %r11
jg 0x199fd8
cmpl %r8d, %ecx
movl %r8d, %eax
cmovll %ecx, %eax
movq %rdx, %r11
shrq $0x20, %r11
movzbl %r11b, %r11d
shrq $0x38, %rdx
cmpl %eax, %r11d
cmovll %r11d, %eax
cmpq $0xff, %rdx
je 0x199f71
xorps %xmm4, %xmm4
cvtsi2sd %edx, %xmm4
cvtsi2sd %eax, %xmm5
mulsd %xmm3, %xmm4
mulsd %xmm4, %xmm5
cvttsd2si %xmm5, %eax
cmpl $0x2, %eax
movl $0x1, %edx
cmovll %edx, %eax
cmpl %r8d, %ecx
movq 0xd0(%rsp), %r11
je 0x199fd8
xorps %xmm4, %xmm4
cvtsi2ss %r10d, %xmm4
xorpd %xmm5, %xmm5
cmpl $0x2, %r10d
jb 0x199fc9
movd %xmm4, %ecx
movl %ecx, %edx
shrl $0x17, %edx
addl $-0x80, %edx
cvtsi2ss %rdx, %xmm6
andl $0x7fffff, %ecx         # imm = 0x7FFFFF
orl $0x3f800000, %ecx       # imm = 0x3F800000
movd %ecx, %xmm7
movdqa %xmm7, %xmm5
mulss %xmm0, %xmm5
addss %xmm1, %xmm5
mulss %xmm7, %xmm5
addss %xmm2, %xmm5
addss %xmm6, %xmm5
mulss %xmm8, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %ecx
subl %ecx, %eax
movl %eax, %ecx
negl %ecx
jno 0x199ff6
movq 0x10(%rsp), %rcx
movq 0xc8(%rsp), %rsi
movq 0x18(%rsp), %r10
movl 0x8(%rsp), %edx
jmp 0x19a019
addl (%r15,%rdi,4), %eax
movl 0x8(%rsp), %edx
cmpl %eax, %edx
cmovlq %rdi, %r14
cmovlel %eax, %edx
movq 0x10(%rsp), %rcx
movq 0xc8(%rsp), %rsi
movq 0x18(%rsp), %r10
movl %edx, (%r15,%rbp,4)
movq 0x28(%rsp), %rax
movq %r14, (%rax,%rbp,8)
testq %r14, %r14
js 0x19a033
movl (%rcx,%r14,4), %eax
cmpl %edx, %eax
jg 0x19a035
movl %edx, %eax
movl %eax, (%rcx,%rbp,4)
testq %rdi, %rdi
js 0x19a05a
movq (%r10), %rax
movq %rdi, %rcx
shlq $0x4, %rcx
subq (%r11,%rcx), %rax
cmpq %rbx, %rax
jg 0x19a05d
movl (%r15,%rdi,4), %eax
cmpl (%r15,%rbp,4), %eax
jge 0x19a05d
movq %rbp, %rdi
incq %rbp
addq $0x10, 0x68(%rsp)
cmpq %rsi, %rbp
jne 0x199b96
subq $0x8, %rsp
leaq 0x6c(%rsp), %rax
leaq 0x8c(%rsp), %rbx
movq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
movq %r15, %r13
movq 0x30(%rsp), %r15
movq %r15, %rcx
movq 0x18(%rsp), %r8
movq 0x50(%rsp), %rbp
movq %rbp, %r9
pushq %rax
pushq %rbx
pushq $0x0
movl 0xe0(%rsp), %eax
pushq %rax
pushq 0x98(%rsp)
callq 0x1997be
addq $0x30, %rsp
movq %rax, %rcx
movslq (%rbx), %r14
movq %r12, %rbx
movq 0xd8(%rsp), %rax
movl %r14d, (%rax)
movq 0xe0(%rsp), %rax
movq %rcx, 0x20(%rsp)
movq %rcx, (%rax)
movq %r12, %rdi
movq %r15, %rsi
callq 0x1e3632
movq %r12, %rdi
movq %r13, %rsi
callq 0x1e3632
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1e3632
testq %r14, %r14
je 0x19a2a5
movslq 0x64(%rsp), %rsi
shlq $0x4, %rsi
movq %rbx, %rdi
callq 0x1e36fd
movq %rax, %r12
testl %r14d, %r14d
movq 0xd0(%rsp), %r11
movq 0x20(%rsp), %r15
jle 0x19a192
movq 0x10(%rsp), %rax
addq $-0x4, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%r15,%rcx,8), %rsi
testq %rsi, %rsi
jle 0x19a18a
movslq %edx, %rdi
shlq $0x2, %rdi
leaq (%rdi,%rsi,4), %rdi
negq %rsi
addq %rax, %rdi
movq %rdx, %r8
shlq $0x4, %r8
addq %r12, %r8
xorl %r9d, %r9d
movslq (%rdi,%r9,4), %r10
shlq $0x4, %r10
movups (%r11,%r10), %xmm0
movups %xmm0, (%r8)
decq %r9
addq $0x10, %r8
cmpq %r9, %rsi
jne 0x19a16a
subq %r9, %rdx
incq %rcx
cmpq %r14, %rcx
jne 0x19a143
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x1e3632
movq %r14, %r13
shlq $0x4, %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1e36fd
movq %rax, %rbp
testl %r14d, %r14d
jle 0x19a1f0
leaq 0x8(%rbp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq %rdx, %rsi
shlq $0x4, %rsi
movq (%r12,%rsi), %rsi
movq %rsi, -0x8(%rax)
movq %rdx, %rsi
shlq $0x20, %rsi
orq %rcx, %rsi
movq %rsi, (%rax)
movslq (%r15,%rcx,8), %rsi
addq %rsi, %rdx
incq %rcx
addq $0x10, %rax
cmpq %rcx, %r14
jne 0x19a1c1
addq %rbp, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x19712b
leaq (,%r14,8), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1e36fd
movq %rax, %rcx
testl %r14d, %r14d
movq %rbp, 0x28(%rsp)
jle 0x19a2b7
movq %r13, 0x50(%rsp)
movq %r12, 0x8(%rsp)
movq %rbp, %r12
addq $0x8, %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %rbp, %rbx
movslq (%r12), %rax
movq (%r15,%rax,8), %rax
movq %rax, (%rcx,%r13,8)
movq %rbp, %rdi
shlq $0x4, %rdi
addq 0xd0(%rsp), %rdi
movl 0x4(%r12), %esi
shlq $0x4, %rsi
addq 0x8(%rsp), %rsi
movslq %eax, %rbp
movq %rbp, %rdx
shlq $0x4, %rdx
movq %rcx, %r15
callq 0x2160
movq %r15, %rcx
movq 0x20(%rsp), %r15
addq %rbx, %rbp
incq %r13
addq $0x10, %r12
cmpq %r13, %r14
jne 0x19a238
shlq $0x4, %rbp
movq 0xe8(%rsp), %rbx
movq 0x8(%rsp), %r12
movq 0x50(%rsp), %r13
jmp 0x19a2b9
xorl %r12d, %r12d
movq 0xd0(%rsp), %rsi
movq 0x10(%rsp), %r15
jmp 0x19a2f0
xorl %ebp, %ebp
movq %r15, %rdi
movq %rcx, %rsi
movq %r13, %rdx
movq %rcx, %r15
callq 0x2160
movq %r12, %rdi
movq 0xd0(%rsp), %r14
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2160
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1e3632
movq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x1e3632
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1e3632
movq %r12, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq