// File: /workspace/github_cpp_makefile_star2_O3/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, %r15d
movl %ecx, 0x50(%rsp)
movl %edx, 0x20(%rsp)
movl %edi, 0x8(%rsp)
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rcx
movq 0xd0(%rsp), %rbp
movq 0xc8(%rsp), %rdx
testq %rax, %rax
je 0x1a2789
movq $0x0, (%rax)
movl $0x0, (%rcx)
testq %rdx, %rdx
sete %al
testq %rbp, %rbp
sete %cl
orb %al, %cl
je 0x1a27a1
xorl %r15d, %r15d
jmp 0x1a2f82
movl %esi, 0x2c(%rsp)
movq %r9, 0x68(%rsp)
movq 0xe8(%rsp), %rbx
leaq (,%rdx,8), %rsi
movq %rbx, %rdi
movss %xmm0, 0x4c(%rsp)
callq 0x1ee115
movq %rax, 0x30(%rsp)
movq 0xc8(%rsp), %rax
leaq (,%rax,4), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1ee115
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1ee115
movq %rax, %r13
movl $0x4, %edx
movq %rbx, %rdi
movq 0xc8(%rsp), %rsi
callq 0x1edfb4
movq %r14, %rdx
movq 0xc8(%rsp), %rsi
movss 0x4c(%rsp), %xmm8
movq %rax, 0x38(%rsp)
testq %rsi, %rsi
movq %r14, (%rsp)
jle 0x1a2d0c
movslq 0x8(%rsp), %rbx
movslq %r15d, %rax
movq %rax, 0x70(%rsp)
leaq -0x8(%rbp), %r14
movq $-0x1, %rdi
xorl %r15d, %r15d
movss 0x9d34b(%rip), %xmm0    # 0x23fba0
movss 0x9d347(%rip), %xmm1    # 0x23fba4
movss 0x9d343(%rip), %xmm2    # 0x23fba8
movsd 0x9d2e3(%rip), %xmm3    # 0x23fb50
xorl %r9d, %r9d
movq %r13, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
movq %r15, %r12
shlq $0x4, %r12
movq 0x8(%rbp,%r12), %r11
movq %r11, %rcx
shrq $0x38, %rcx
movq %r11, %rax
shrq $0x20, %rax
movzbl %al, %eax
cmpq $0xff, %rcx
je 0x1a28c5
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm3, %xmm4
mulsd %xmm4, %xmm5
cvttsd2si %xmm5, %eax
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
movl 0x2c(%rsp), %esi
addq %rbp, %r12
cmpq %r15, %r9
jge 0x1a290e
movq (%r12), %rcx
movq %r9, %rdx
shlq $0x4, %rdx
addq %rbp, %rdx
movq (%rdx), %r8
movq %r8, %r10
xorq %rcx, %r10
shrq $0x20, %r10
sete %r10b
addq %rbx, %r8
cmpq %r8, %rcx
setbe %r8b
testb %r8b, %r10b
jne 0x1a290e
incq %r9
addq $0x10, %rdx
cmpq %r15, %r9
jl 0x1a28df
movq %r15, %r9
movq %r9, %rcx
movq %r15, %rdx
subq %r9, %rdx
movq %r15, %r9
movq 0x70(%rsp), %r8
subq %r8, %r9
cmpq %r8, %rdx
cmovleq %rcx, %r9
leaq -0x1(%r15), %r10
movq $-0x1, 0x18(%rsp)
cmpq %r9, %r15
movq %r14, 0x78(%rsp)
movq %r12, 0x40(%rsp)
jle 0x1a2add
movl (%r12), %edx
movq $-0x1, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
movq %r14, %r8
movq %r10, %rbp
movq (%r8), %r13
movl %r11d, %r12d
subl %r13d, %r12d
setle %cl
cmpl 0x8(%rsp), %r12d
setg %r14b
orb %cl, %r14b
jne 0x1a29ac
movl %edx, %r14d
subl -0x8(%r8), %r14d
testl %r14d, %r14d
setle %cl
cmpl %esi, %r14d
setg %bl
orb %cl, %bl
jne 0x1a29ac
movl %r12d, %ebx
subl %r14d, %ebx
negl %ebx
movl %r12d, %ecx
subl %r14d, %ecx
cmovbl %ebx, %ecx
cmpl 0x20(%rsp), %ecx
jle 0x1a29c7
movl %eax, %r12d
leaq -0x1(%rbp), %r13
addq $-0x10, %r8
movl %r12d, %eax
cmpq %r9, %rbp
movq %r13, %rbp
jg 0x1a2964
jmp 0x1a2ae3
cmpl %r12d, %r14d
cmovbl %r14d, %r12d
movq %r13, %rbx
shrq $0x20, %rbx
movzbl %bl, %ebx
shrq $0x38, %r13
cmpl %r12d, %ebx
cmovbl %ebx, %r12d
cmpq $0xff, %r13
je 0x1a2a16
xorps %xmm4, %xmm4
cvtsi2sd %r13d, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %r12d, %xmm5
mulsd %xmm3, %xmm4
mulsd %xmm4, %xmm5
cvttsd2si %xmm5, %r12d
cmpl $0x2, %r12d
movl $0x1, %ebx
cmovll %ebx, %r12d
testl %ecx, %ecx
je 0x1a2a76
xorps %xmm4, %xmm4
cvtsi2ss %ecx, %xmm4
xorpd %xmm5, %xmm5
cmpl $0x2, %ecx
jl 0x1a2a66
movd %xmm4, %ecx
movl %ecx, %ebx
shrl $0x17, %ebx
movzbl %bl, %ebx
addl $-0x80, %ebx
cvtsi2ss %rbx, %xmm6
andl $0x807fffff, %ecx       # imm = 0x807FFFFF
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, %r12d
movq (%rsp), %rcx
addl (%rcx,%rbp,4), %r12d
cmpl %eax, %r12d
jle 0x1a2a9b
xorl %eax, %eax
movl 0x10(%rsp), %ecx
testl %ecx, %ecx
setg %al
subl %eax, %ecx
movl %ecx, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
jmp 0x1a2abd
movq 0x38(%rsp), %rcx
cmpl %r15d, (%rcx,%rbp,4)
jne 0x1a2aba
movl 0x10(%rsp), %ecx
cmpl 0x50(%rsp), %ecx
jge 0x1a2d04
incl %ecx
movl %ecx, 0x10(%rsp)
movl %eax, %r12d
movq 0x30(%rsp), %rax
movq (%rax,%rbp,8), %rax
testq %rax, %rax
js 0x1a29af
movq 0x38(%rsp), %rcx
movl %r15d, (%rcx,%rax,4)
jmp 0x1a29af
movq %r10, %r13
movl %eax, %r12d
testq %rdi, %rdi
movq 0xd0(%rsp), %rbp
movq (%rsp), %rdx
js 0x1a2b0f
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rdi, %rcx
shlq $0x4, %rcx
subq (%rbp,%rcx), %rax
cmpq 0x60(%rsp), %rax
jle 0x1a2b3d
movq $-0x1, %rdi
cmpq %r9, %r15
jle 0x1a2ba8
movl $0x80000000, %eax       # imm = 0x80000000
movl (%rdx,%r10,4), %ecx
cmpl %ecx, %eax
cmovlq %r10, %rdi
cmovlel %ecx, %eax
leaq -0x1(%r10), %rcx
cmpq %r9, %r10
movq %rcx, %r10
jg 0x1a2b24
testq %rdi, %rdi
sets %al
cmpq %r13, %rdi
setge %cl
orb %al, %cl
jne 0x1a2ba8
movq %rdi, %rax
shlq $0x4, %rax
movq 0x8(%rbp,%rax), %r10
subl %r10d, %r11d
setle %cl
cmpl 0x8(%rsp), %r11d
setg %r8b
orb %cl, %r8b
jne 0x1a2ba4
movq 0x40(%rsp), %rcx
movl (%rcx), %ecx
subl (%rbp,%rax), %ecx
testl %ecx, %ecx
setle %al
cmpl %esi, %ecx
setg %r8b
orb %al, %r8b
jne 0x1a2ba4
movl %r11d, %r8d
subl %ecx, %r8d
negl %r8d
movl %r11d, %eax
subl %ecx, %eax
cmovbl %r8d, %eax
cmpl 0x20(%rsp), %eax
jle 0x1a2c2b
movq (%rsp), %rdx
leaq (%rdx,%r15,4), %rsi
movl %r12d, (%rdx,%r15,4)
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, (%rax,%r15,8)
movq %rcx, %rax
testq %rcx, %rcx
movq 0x58(%rsp), %r13
js 0x1a2bd7
movl (%r13,%rax,4), %eax
cmpl %r12d, %eax
cmovgl %eax, %r12d
movl %r12d, (%r13,%r15,4)
testq %rdi, %rdi
movq 0x60(%rsp), %rbx
movq 0x78(%rsp), %r14
js 0x1a2c0b
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rdi, %rcx
shlq $0x4, %rcx
subq (%rbp,%rcx), %rax
cmpq %rbx, %rax
jg 0x1a2c0e
movl (%rdx,%rdi,4), %eax
cmpl (%rsi), %eax
jge 0x1a2c0e
movq %r15, %rdi
incq %r15
addq $0x10, %r14
movq 0xc8(%rsp), %rsi
cmpq %rsi, %r15
jne 0x1a287a
jmp 0x1a2d0c
cmpl %r11d, %ecx
cmovbl %ecx, %r11d
movq %r10, %rcx
shrq $0x20, %rcx
movzbl %cl, %ecx
shrq $0x38, %r10
cmpl %r11d, %ecx
cmovbl %ecx, %r11d
cmpq $0xff, %r10
movq (%rsp), %rdx
je 0x1a2c78
cvtsi2sd %r10d, %xmm4
cvtsi2sd %r11d, %xmm5
mulsd %xmm3, %xmm4
mulsd %xmm4, %xmm5
cvttsd2si %xmm5, %r11d
cmpl $0x2, %r11d
movl $0x1, %ecx
cmovll %ecx, %r11d
testl %eax, %eax
movq 0x58(%rsp), %r13
je 0x1a2cdb
xorps %xmm4, %xmm4
cvtsi2ss %eax, %xmm4
xorpd %xmm5, %xmm5
cmpl $0x2, %eax
jl 0x1a2ccb
movd %xmm4, %eax
movl %eax, %ecx
shrl $0x17, %ecx
movzbl %cl, %ecx
addl $-0x80, %ecx
cvtsi2ss %rcx, %xmm6
andl $0x807fffff, %eax       # imm = 0x807FFFFF
orl $0x3f800000, %eax       # imm = 0x3F800000
movd %eax, %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, %eax
subl %eax, %r11d
addl (%rdx,%rdi,4), %r11d
cmpl %r11d, %r12d
jge 0x1a2ba8
leaq (%rdx,%r15,4), %rsi
movl %r11d, (%rdx,%r15,4)
movq 0x30(%rsp), %rax
movq %rdi, (%rax,%r15,8)
movl %r11d, %r12d
movq %rdi, %rax
jmp 0x1a2bcb
movq %rbp, %r13
jmp 0x1a2ae0
subq $0x8, %rsp
leaq 0x5c(%rsp), %rax
leaq 0x8c(%rsp), %rbx
movq 0xf0(%rsp), %rdi
movq 0x38(%rsp), %r15
movq %r15, %rcx
movq %r13, %r8
movq 0x40(%rsp), %r12
movq %r12, %r9
pushq %rax
pushq %rbx
pushq $0x0
movl 0xe0(%rsp), %eax
pushq %rax
pushq 0x90(%rsp)
callq 0x1a24aa
addq $0x30, %rsp
movq %rax, %rcx
movslq (%rbx), %r14
movq 0xe8(%rsp), %rbx
movq 0xd8(%rsp), %rax
movl %r14d, (%rax)
movq 0xe0(%rsp), %rax
movq %rcx, 0x8(%rsp)
movq %rcx, (%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1ee04f
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x1ee04f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1ee04f
testq %r14, %r14
je 0x1a2f2f
movslq 0x54(%rsp), %rsi
shlq $0x4, %rsi
movq %rbx, %rdi
callq 0x1ee115
movq %rax, %r15
testl %r14d, %r14d
movq 0x8(%rsp), %r12
jle 0x1a2e21
movq %r13, %rax
addq $-0x4, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%r12,%rcx,8), %rsi
testq %rsi, %rsi
jle 0x1a2e19
movslq %edx, %rdi
shlq $0x2, %rdi
leaq (%rdi,%rsi,4), %rdi
negq %rsi
addq %rax, %rdi
movq %rdx, %r8
shlq $0x4, %r8
addq %r15, %r8
xorl %r9d, %r9d
movslq (%rdi,%r9,4), %r10
shlq $0x4, %r10
movups (%rbp,%r10), %xmm0
movups %xmm0, (%r8)
decq %r9
addq $0x10, %r8
cmpq %r9, %rsi
jne 0x1a2df8
subq %r9, %rdx
incq %rcx
cmpq %r14, %rcx
jne 0x1a2dd1
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1ee04f
movq %r14, %r13
shlq $0x4, %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1ee115
movq %rax, %rbp
testl %r14d, %r14d
jle 0x1a2e7d
leaq 0x8(%rbp), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq %rdx, %rsi
shlq $0x4, %rsi
movq (%r15,%rsi), %rsi
movq %rsi, -0x8(%rax)
movq %rdx, %rsi
shlq $0x20, %rsi
orq %rcx, %rsi
movq %rsi, (%rax)
movslq (%r12,%rcx,8), %rsi
addq %rsi, %rdx
incq %rcx
addq $0x10, %rax
cmpq %rcx, %r14
jne 0x1a2e4e
addq %rbp, %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0x19febb
leaq (,%r14,8), %rsi
movq %rbx, %rdi
movq %rsi, 0x10(%rsp)
callq 0x1ee115
movq %rax, %r13
testl %r14d, %r14d
movq %rbp, 0x18(%rsp)
jle 0x1a2f37
movq %rbp, %r12
addq $0x8, %r12
movq %r15, 0x20(%rsp)
movq %r13, %r15
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %rbp, %rbx
movslq (%r12), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, (%r15,%r13,8)
movq %rbp, %rdi
shlq $0x4, %rdi
addq 0xd0(%rsp), %rdi
movl 0x4(%r12), %esi
shlq $0x4, %rsi
addq 0x20(%rsp), %rsi
movslq %eax, %rbp
movq %rbp, %rdx
shlq $0x4, %rdx
callq 0x2160
addq %rbx, %rbp
incq %r13
addq $0x10, %r12
cmpq %r13, %r14
jne 0x1a2ec5
shlq $0x4, %rbp
movq 0xe8(%rsp), %rbx
movq %r15, %r13
movq 0x20(%rsp), %r15
movq 0x8(%rsp), %r12
jmp 0x1a2f39
xorl %r15d, %r15d
movq %rbp, %rsi
jmp 0x1a2f6f
xorl %ebp, %ebp
movq %r12, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rdx
callq 0x2160
movq %r15, %rdi
movq 0xd0(%rsp), %r14
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2160
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1ee04f
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x1ee04f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1ee04f
movq %r15, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq