// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xd8, %rsp
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movl %edi, -0x14(%rbp)
movl %esi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movss %xmm0, -0x2c(%rbp)
movq $0x0, -0x78(%rbp)
cmpq $0x0, 0x30(%rbp)
je 0x283ef0
movq 0x30(%rbp), %rax
movq $0x0, (%rax)
movq 0x28(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, 0x18(%rbp)
je 0x283efe
cmpq $0x0, 0x20(%rbp)
jne 0x283f0b
movq $0x0, -0x10(%rbp)
jmp 0x2844ba
movq 0x38(%rbp), %rdi
movq 0x18(%rbp), %rsi
shlq $0x3, %rsi
callq 0x30e4f0
movq %rax, -0x58(%rbp)
movq 0x38(%rbp), %rdi
movq 0x18(%rbp), %rsi
shlq $0x2, %rsi
callq 0x30e4f0
movq %rax, -0x38(%rbp)
movq 0x38(%rbp), %rdi
movq 0x18(%rbp), %rsi
shlq $0x2, %rsi
callq 0x30e4f0
movq %rax, -0x48(%rbp)
movq 0x38(%rbp), %rdi
movq 0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x30e1a0
movq %rax, -0x40(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x60(%rbp)
movq $-0x1, -0x70(%rbp)
movq -0x60(%rbp), %rax
cmpq 0x18(%rbp), %rax
jge 0x2843ea
movq $-0x1, -0x88(%rbp)
movq 0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
shrq $0x38, %rdi
movq 0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xff, %rax
movl %eax, %esi
callq 0x30cad0
movl %eax, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movq -0x78(%rbp), %rcx
xorl %eax, %eax
cmpq -0x60(%rbp), %rcx
movb %al, -0xa5(%rbp)
jge 0x28406a
movq 0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
shrq $0x20, %rcx
movq 0x20(%rbp), %rax
movq -0x78(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rax), %rdx
shrq $0x20, %rdx
movb $0x1, %al
cmpq %rdx, %rcx
movb %al, -0xa6(%rbp)
jne 0x28405e
movq 0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x20(%rbp), %rcx
movq -0x78(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0x14(%rbp), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
seta %al
movb %al, -0xa6(%rbp)
movb -0xa6(%rbp), %al
movb %al, -0xa5(%rbp)
movb -0xa5(%rbp), %al
testb $0x1, %al
jne 0x284076
jmp 0x284087
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x283fdc
movq -0x60(%rbp), %rax
subq -0x78(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq %rcx, %rax
jle 0x2840a7
movq -0x60(%rbp), %rax
movslq -0x24(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jl 0x2841c8
movq 0x20(%rbp), %rdi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x20(%rbp), %rsi
movq -0x68(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %r8d
movss -0x2c(%rbp), %xmm0
callq 0x2844d0
movl %eax, -0x9c(%rbp)
cmpl $0x80000000, -0x9c(%rbp) # imm = 0x80000000
jne 0x28410a
jmp 0x2841b7
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x9c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x94(%rbp), %eax
jle 0x284160
movl -0x9c(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpl $0x0, -0x98(%rbp)
jle 0x28415e
movl -0x98(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x28418d
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x60(%rbp), %rcx
cmpl %ecx, %eax
jne 0x28418b
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
cmpl -0x20(%rbp), %eax
jle 0x284189
jmp 0x2841c8
jmp 0x28418b
jmp 0x28418d
movq -0x58(%rbp), %rax
movq -0x68(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x2841b5
movq -0x60(%rbp), %rax
movl %eax, %edx
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x2841b7
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x2840b3
movq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x70(%rbp)
jl 0x284207
movq 0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x20(%rbp), %rcx
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movslq -0x14(%rbp), %rcx
cmpq %rcx, %rax
jle 0x28426d
movl $0x80000000, -0xa0(%rbp) # imm = 0x80000000
movq $-0x1, -0x70(%rbp)
movq -0x60(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jl 0x28426b
movl -0xa0(%rbp), %eax
movq -0x38(%rbp), %rcx
movq -0x68(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x28425b
movq -0x38(%rbp), %rax
movq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x28425d
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x284225
jmp 0x28426d
cmpq $0x0, -0x70(%rbp)
jl 0x28430c
movq -0x70(%rbp), %rax
cmpq -0x90(%rbp), %rax
jge 0x28430c
movq 0x20(%rbp), %rdi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x20(%rbp), %rsi
movq -0x70(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
movl -0x1c(%rbp), %r8d
movss -0x2c(%rbp), %xmm0
callq 0x2844d0
movl %eax, -0xa4(%rbp)
cmpl $0x80000000, -0xa4(%rbp) # imm = 0x80000000
je 0x28430a
movl -0x94(%rbp), %eax
movl -0xa4(%rbp), %ecx
movq -0x38(%rbp), %rdx
movq -0x70(%rbp), %rsi
addl (%rdx,%rsi,4), %ecx
cmpl %ecx, %eax
jge 0x28430a
movl -0xa4(%rbp), %eax
movq -0x38(%rbp), %rcx
movq -0x70(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movl %eax, -0x94(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x28430c
movl -0x94(%rbp), %edx
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x88(%rbp), %rdx
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0x88(%rbp)
jl 0x284366
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x94(%rbp), %eax
jle 0x284366
movq -0x48(%rbp), %rax
movq -0x88(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xac(%rbp)
jmp 0x284372
movl -0x94(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %edx
movq -0x48(%rbp), %rax
movq -0x60(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpq $0x0, -0x70(%rbp)
jl 0x2843cf
movq 0x20(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x20(%rbp), %rcx
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movslq -0x14(%rbp), %rcx
cmpq %rcx, %rax
jg 0x2843d7
movq -0x38(%rbp), %rax
movq -0x70(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x38(%rbp), %rcx
movq -0x60(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x2843d7
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x2843d9
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x283f78
movq 0x38(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %r9
movl -0x28(%rbp), %ebx
movl 0x10(%rbp), %r11d
xorl %eax, %eax
leaq -0x4c(%rbp), %r10
leaq -0x50(%rbp), %rax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x2839e0
movq %rax, -0x80(%rbp)
movl -0x4c(%rbp), %ecx
movq 0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x80(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, (%rax)
movq 0x38(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x30e2d0
movq 0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x30e2d0
movq 0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x30e2d0
cmpl $0x0, -0x4c(%rbp)
jne 0x28449b
movq 0x38(%rbp), %rdi
movq 0x20(%rbp), %rsi
callq 0x30e2d0
movq 0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x30e2d0
movq $0x0, -0x10(%rbp)
jmp 0x2844ba
movq 0x38(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq -0x80(%rbp), %rdx
movl -0x50(%rbp), %ecx
movq -0x48(%rbp), %r8
movq 0x20(%rbp), %r9
callq 0x284650
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax,%rax)