// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/lchain.c
// Function: mg_lchain_dp
mm128_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, float chn_pen_skip,
					  int is_cdna, int n_seg, int64_t n, mm128_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, mmax_f = 0, max_drop = bw;
	int64_t *p, i, j, max_ii, st = 0;
	uint64_t *u;

	if (_u) *_u = 0, *n_u_ = 0;
	if (n == 0 || a == 0) {
		kfree(km, a);
		return 0;
	}
	if (max_dist_x < bw) max_dist_x = bw;
	if (max_dist_y < bw && !is_cdna) max_dist_y = bw;
	if (is_cdna) max_drop = INT32_MAX;
	p = Kmalloc(km, int64_t, n);
	f = Kmalloc(km, int32_t, n);
	v = Kmalloc(km, int32_t, n);
	t = Kcalloc(km, int32_t, n);

	// fill the score and backtrack arrays
	for (i = 0, max_ii = -1; i < n; ++i) {
		int64_t max_j = -1, end_j;
		int32_t max_f = a[i].y>>32&0xff, n_skip = 0;
		while (st < i && (a[i].x>>32 != a[st].x>>32 || a[i].x > a[st].x + max_dist_x)) ++st;
		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, chn_pen_skip, is_cdna, n_seg);
			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 || a[i].x - a[max_ii].x > (int64_t)max_dist_x) {
			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;
		}
		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, chn_pen_skip, is_cdna, n_seg);
			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;
		v[i] = max_j >= 0 && v[max_j] > max_f? v[max_j] : max_f; // v[] keeps the peak score up to i; f[] is the score ending at i, not always the peak
		if (max_ii < 0 || (a[i].x - a[max_ii].x <= (int64_t)max_dist_x && f[max_ii] < f[i]))
			max_ii = i;
		if (mmax_f < max_f) mmax_f = max_f;
		//fprintf(stderr, "X1\t%ld\t%ld:%d\t%ld\t%ld:%d\t%ld\t%ld\n", (long)i, (long)(a[i].x>>32), (int32_t)a[i].x, (long)max_j, max_j<0?-1L:(long)(a[max_j].x>>32), max_j<0?-1:(int32_t)a[max_j].x, (long)max_f, (long)v[i]);
	}

	u = mg_chain_backtrack(km, n, f, p, v, t, min_cnt, min_sc, max_drop, &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;
	}
	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 $0xc8, %rsp
movss %xmm1, 0x1c(%rsp)
movss %xmm0, 0x18(%rsp)
movl %ecx, 0x40(%rsp)
movl %edx, 0xc(%rsp)
movq 0x138(%rsp), %r15
movq 0x130(%rsp), %rdx
movq 0x128(%rsp), %r10
movq 0x120(%rsp), %r14
movq 0x118(%rsp), %r12
movl 0x110(%rsp), %eax
movq %rax, 0x70(%rsp)
movl 0x108(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x100(%rsp), %r11d
testq %rdx, %rdx
je 0x13071
andq $0x0, (%rdx)
andl $0x0, (%r10)
testq %r12, %r12
setne %al
testq %r14, %r14
setne %cl
testb %cl, %al
jne 0x1308c
movq %r15, %rdi
movq %r14, %rsi
jmp 0x13506
movq %r11, 0x80(%rsp)
movq %r10, 0x88(%rsp)
movq %r9, 0x90(%rsp)
movq %rdx, 0x98(%rsp)
movl 0xc(%rsp), %eax
movl %edi, %ebp
cmpl %eax, %edi
cmovlel %eax, %ebp
cmpl %eax, %esi
cmovgl %esi, %eax
cmpl $0x0, 0x8(%rsp)
cmovnel %esi, %eax
movl %eax, 0x10(%rsp)
leaq (,%r12,8), %rsi
movq %r15, %rdi
movl %r8d, (%rsp)
callq 0x10afc
movq %rax, 0x28(%rsp)
movq %r14, 0x20(%rsp)
leaq (,%r12,4), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x10afc
movq %rax, %rbx
movq %r15, %rdi
movq %r14, %rsi
movq 0x20(%rsp), %r14
callq 0x10afc
movq %rax, %r13
pushq $0x4
popq %rdx
movq %r15, 0xa0(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x10980
movq %rax, 0x38(%rsp)
movl %ebp, 0x14(%rsp)
movslq %ebp, %r8
movslq (%rsp), %rax
movq %rax, 0xa8(%rsp)
xorl %r9d, %r9d
testq %r12, %r12
movl $0x0, %eax
movq %r12, 0x78(%rsp)
cmovgq %r12, %rax
movq %rax, 0xb0(%rsp)
leaq -0x10(%r14), %r10
pushq $-0x1
popq %rbp
xorl %r12d, %r12d
movq %r13, 0x48(%rsp)
movq %rbx, 0x68(%rsp)
movq %r8, 0x50(%rsp)
cmpq 0xb0(%rsp), %r9
je 0x13420
movq %r9, %rcx
shlq $0x4, %rcx
leaq (%r14,%rcx), %rax
movq %rax, (%rsp)
cmpq %r9, %r12
movq %r9, %rax
cmovgq %r12, %rax
movzbl 0xc(%r14,%rcx), %r15d
movq %r12, %rcx
shlq $0x4, %rcx
addq %r14, %rcx
movq %rbp, 0xb8(%rsp)
cmpq %r9, %r12
jge 0x131e8
movq (%rsp), %rdx
movq (%rdx), %rdx
movq (%rcx), %rsi
movq %rsi, %rdi
xorq %rdx, %rdi
shrq $0x20, %rdi
sete %dil
addq %r8, %rsi
cmpq %rsi, %rdx
setbe %dl
testb %dl, %dil
jne 0x131e5
incq %r12
addq $0x10, %rcx
jmp 0x131b1
movq %r12, %rax
movq %r9, %rcx
subq %rax, %rcx
movq %r9, %r12
movq 0xa8(%rsp), %rdx
subq %rdx, %r12
cmpq %rdx, %rcx
cmovleq %rax, %r12
movq %r9, 0x30(%rsp)
leaq -0x1(%r9), %r13
xorl %r14d, %r14d
movq %r10, 0x58(%rsp)
movq %r10, %rbx
movq %r13, %rbp
pushq $-0x1
popq %rax
movq %rax, 0x60(%rsp)
cmpq %r12, %rbp
jl 0x132d2
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %ecx
movl 0x14(%rsp), %r8d
movss 0x20(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
movl 0x10(%rsp), %r9d
pushq 0x78(%rsp)
callq 0x1351f
addq $0x10, %rsp
movl %eax, %ecx
negl %ecx
jno 0x1326d
movl %r15d, %eax
jmp 0x132c3
movq 0x68(%rsp), %rcx
addl (%rcx,%rbp,4), %eax
cmpl %r15d, %eax
jle 0x1328c
xorl %ecx, %ecx
testl %r14d, %r14d
setg %cl
subl %ecx, %r14d
movq %rbp, 0x60(%rsp)
jmp 0x132a8
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
cmpl %ecx, (%rax,%rbp,4)
jne 0x132a5
cmpl 0x40(%rsp), %r14d
jge 0x132d2
incl %r14d
movl %r15d, %eax
movq 0x28(%rsp), %rcx
movq (%rcx,%rbp,8), %rcx
testq %rcx, %rcx
js 0x132c3
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rsi
movl %esi, (%rdx,%rcx,4)
decq %rbp
addq $-0x10, %rbx
movl %eax, %r15d
jmp 0x13222
movq 0xb8(%rsp), %rdx
testq %rdx, %rdx
movq 0x20(%rsp), %r14
movq 0x68(%rsp), %rbx
movq 0x50(%rsp), %r8
js 0x13305
movq (%rsp), %rax
movq (%rax), %rax
movq %rdx, %rcx
shlq $0x4, %rcx
subq (%r14,%rcx), %rax
cmpq %r8, %rax
jbe 0x13324
movl $0x80000000, %eax       # imm = 0x80000000
pushq $-0x1
popq %rdx
cmpq %r12, %r13
jl 0x13324
movl (%rbx,%r13,4), %ecx
cmpl %ecx, %eax
cmovlq %r13, %rdx
cmovlel %ecx, %eax
decq %r13
jmp 0x1330d
testq %rdx, %rdx
sets %al
cmpq %rbp, %rdx
setge %cl
orb %al, %cl
movq %rdx, %rbp
jne 0x133b0
movq %rbp, %rsi
shlq $0x4, %rsi
addq %r14, %rsi
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %ecx
movl 0x14(%rsp), %r8d
movss 0x20(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
movl 0x10(%rsp), %r9d
pushq 0x78(%rsp)
callq 0x1351f
movq 0x60(%rsp), %r8
addq $0x10, %rsp
movl %eax, %ecx
negl %ecx
jo 0x133b0
addl (%rbx,%rbp,4), %eax
cmpl %eax, %r15d
jge 0x133b0
movq 0x30(%rsp), %r9
leaq (%rbx,%r9,4), %rcx
movl %eax, (%rbx,%r9,4)
movq 0x28(%rsp), %rdx
movq %rbp, (%rdx,%r9,8)
movl %eax, %r15d
movq %rbp, %rax
movq 0x48(%rsp), %r13
movq 0x58(%rsp), %r10
jmp 0x133dd
movq 0x30(%rsp), %r9
leaq (%rbx,%r9,4), %rcx
movl %r15d, (%rbx,%r9,4)
movq 0x28(%rsp), %rax
movq 0x60(%rsp), %rdx
movq %rdx, (%rax,%r9,8)
movq %rdx, %rax
testq %rdx, %rdx
movq 0x48(%rsp), %r13
movq 0x58(%rsp), %r10
js 0x133e9
movl (%r13,%rax,4), %eax
cmpl %r15d, %eax
cmovgl %eax, %r15d
movl %r15d, (%r13,%r9,4)
testq %rbp, %rbp
js 0x13411
movq (%rsp), %rax
movq (%rax), %rax
movq %rbp, %rdx
shlq $0x4, %rdx
subq (%r14,%rdx), %rax
cmpq %r8, %rax
ja 0x13414
movl (%rbx,%rbp,4), %eax
cmpl (%rcx), %eax
jge 0x13414
movq %r9, %rbp
incq %r9
addq $0x10, %r10
jmp 0x13172
cmpl $0x0, 0x8(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovel 0xc(%rsp), %eax
subq $0x8, %rsp
leaq 0x4c(%rsp), %r10
leaq 0xcc(%rsp), %rbp
movq 0xa8(%rsp), %r15
movq %r15, %rdi
movq 0x80(%rsp), %rsi
movq %rbx, %rdx
movq 0x30(%rsp), %r12
movq %r12, %rcx
movq %r13, %r8
movq 0x40(%rsp), %r14
movq %r14, %r9
pushq %r10
pushq %rbp
pushq %rax
pushq 0xa0(%rsp)
pushq 0xb8(%rsp)
callq 0x12c2c
addq $0x30, %rsp
movq %rax, %rcx
movl (%rbp), %ebp
movq 0x88(%rsp), %rax
movl %ebp, (%rax)
movq 0x98(%rsp), %rax
movq %rcx, (%rsp)
movq %rcx, (%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x10a07
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10a07
movq %r15, %rdi
movq %r14, %rsi
callq 0x10a07
testl %ebp, %ebp
je 0x134f3
movl 0x44(%rsp), %ecx
movq %r15, %rdi
movl %ebp, %esi
movq (%rsp), %rdx
movq %r13, %r8
movq 0x20(%rsp), %r9
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x13687
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x10a07
movq %r15, %rdi
movq %r13, %rsi
callq 0x10a07
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq