// File: /workspace/github_c_makefile_star2_O2/lh3[P]minigraph/lchain.c
// Function: mg_lchain_dp
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, float chn_pen_skip,
					  int is_cdna, int n_seg, 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, mmax_f = 0, max_drop = bw;
	int64_t *p, i, j, max_ii, st = 0, n_iter = 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;
	KMALLOC(km, p, n);
	KMALLOC(km, f, n);
	KMALLOC(km, v, n);
	KCALLOC(km, 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);
			++n_iter;
			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;
	}
	if (mg_dbg_flag & MG_DBG_LC_PROF) fprintf(stderr, "LP\tn_iter=%ld\tmmax_f=%d\n", (long)n_iter, mmax_f);

	u = mg_chain_backtrack(km, n, f, p, v, t, min_cnt, min_sc, max_drop, 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;
	}
	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 $0xd8, %rsp
movss %xmm1, 0x24(%rsp)
movss %xmm0, 0x20(%rsp)
movl %ecx, 0x5c(%rsp)
movl %edx, 0x4(%rsp)
movq 0x148(%rsp), %r14
movq 0x140(%rsp), %rdx
movq 0x138(%rsp), %r10
movq 0x130(%rsp), %r15
movq 0x128(%rsp), %r13
movl 0x120(%rsp), %eax
movq %rax, 0x80(%rsp)
movl 0x118(%rsp), %eax
movl %eax, (%rsp)
movl 0x110(%rsp), %r11d
testq %rdx, %rdx
je 0x135b9
andq $0x0, (%rdx)
andl $0x0, (%r10)
testq %r13, %r13
setne %al
testq %r15, %r15
setne %cl
testb %cl, %al
jne 0x135d4
movq %r14, %rdi
movq %r15, %rsi
jmp 0x13aa3
movq %r11, 0x90(%rsp)
movq %r10, 0x98(%rsp)
movq %r9, 0xa0(%rsp)
movq %rdx, 0xa8(%rsp)
movl 0x4(%rsp), %eax
movl %edi, %ebx
cmpl %eax, %edi
cmovlel %eax, %ebx
cmpl %eax, %esi
cmovgl %esi, %eax
cmpl $0x0, (%rsp)
cmovnel %esi, %eax
movl %eax, 0x18(%rsp)
leaq (,%r13,8), %rsi
movq %r14, %rdi
movl %r8d, 0x10(%rsp)
callq 0x266c
movq %rax, 0x28(%rsp)
movq %r15, 0x68(%rsp)
leaq (,%r13,4), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x266c
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq 0x68(%rsp), %r12
callq 0x266c
movq %rax, %rbp
pushq $0x4
popq %rdx
movq %r14, 0xb0(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x24f0
movq %rax, 0x30(%rsp)
movl %ebx, 0x1c(%rsp)
movslq %ebx, %r8
movslq 0x10(%rsp), %rax
movq %rax, 0xb8(%rsp)
xorl %ecx, %ecx
testq %r13, %r13
movl $0x0, %eax
movq %r13, 0x88(%rsp)
cmovgq %r13, %rax
movq %rax, 0xc0(%rsp)
leaq -0x10(%r12), %r9
pushq $-0x1
popq %rbx
xorl %r10d, %r10d
xorl %r13d, %r13d
movq $0x0, 0x50(%rsp)
movq %rbp, 0x40(%rsp)
movq %r8, 0x48(%rsp)
cmpq 0xc0(%rsp), %r10
je 0x139b0
movq %rcx, 0xd0(%rsp)
movq %r10, %rcx
shlq $0x4, %rcx
leaq (%r12,%rcx), %rax
movq %rax, 0x10(%rsp)
cmpq %r10, %r13
movq %r10, %rax
cmovgq %r13, %rax
movzbl 0xc(%r12,%rcx), %r15d
movq %r13, %rcx
shlq $0x4, %rcx
addq %r12, %rcx
movq %rbx, 0xc8(%rsp)
cmpq %r10, %r13
jge 0x13747
movq 0x10(%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 0x13744
incq %r13
addq $0x10, %rcx
jmp 0x1370f
movq %r13, %rax
movq %r10, %rcx
subq %rax, %rcx
movq %r10, %r13
movq 0xb8(%rsp), %rdx
subq %rdx, %r13
cmpq %rdx, %rcx
cmovleq %rax, %r13
movq %r10, 0x38(%rsp)
leaq -0x1(%r10), %rbx
xorl %ebp, %ebp
movq %r9, 0x70(%rsp)
movq %r9, %r12
movq %rbx, %r14
pushq $-0x1
popq %rax
movq %rax, 0x78(%rsp)
cmpq %r13, %r14
jl 0x1383b
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %ecx
movl 0xc(%rsp), %r8d
movss 0x28(%rsp), %xmm0
movss 0x2c(%rsp), %xmm1
movl 0x8(%rsp), %r9d
pushq 0x88(%rsp)
callq 0x13ade
addq $0x10, %rsp
movl %eax, %ecx
negl %ecx
jno 0x137ce
movl %r15d, %eax
jmp 0x13822
movq 0x8(%rsp), %rcx
addl (%rcx,%r14,4), %eax
cmpl %r15d, %eax
jle 0x137ec
xorl %ecx, %ecx
testl %ebp, %ebp
setg %cl
subl %ecx, %ebp
movq %r14, 0x78(%rsp)
jmp 0x13807
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
cmpl %ecx, (%rax,%r14,4)
jne 0x13804
cmpl 0x5c(%rsp), %ebp
jge 0x13836
incl %ebp
movl %r15d, %eax
movq 0x28(%rsp), %rcx
movq (%rcx,%r14,8), %rcx
testq %rcx, %rcx
js 0x13822
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rsi
movl %esi, (%rdx,%rcx,4)
decq %r14
addq $-0x10, %r12
incq 0x50(%rsp)
movl %eax, %r15d
jmp 0x13780
incq 0x50(%rsp)
movq 0xc8(%rsp), %rdx
testq %rdx, %rdx
movq 0x68(%rsp), %r12
movq 0x40(%rsp), %rbp
js 0x1386c
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rdx, %rcx
shlq $0x4, %rcx
subq (%r12,%rcx), %rax
cmpq 0x48(%rsp), %rax
jbe 0x1388f
movl $0x80000000, %eax       # imm = 0x80000000
pushq $-0x1
popq %rdx
cmpq %r13, %rbx
jl 0x1388f
movq 0x8(%rsp), %rcx
movl (%rcx,%rbx,4), %ecx
cmpl %ecx, %eax
cmovlq %rbx, %rdx
cmovlel %ecx, %eax
decq %rbx
jmp 0x13874
testq %rdx, %rdx
sets %al
cmpq %r14, %rdx
setge %cl
orb %al, %cl
movq %rdx, %rbx
jne 0x13927
movq %rbx, %rsi
shlq $0x4, %rsi
addq %r12, %rsi
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %ecx
movl 0xc(%rsp), %r8d
movss 0x28(%rsp), %xmm0
movss 0x2c(%rsp), %xmm1
movl 0x8(%rsp), %r9d
pushq 0x88(%rsp)
callq 0x13ade
addq $0x10, %rsp
movl %eax, %ecx
negl %ecx
jo 0x13927
movq 0x8(%rsp), %rcx
addl (%rcx,%rbx,4), %eax
cmpl %eax, %r15d
jge 0x13927
movq 0x8(%rsp), %rdx
movq 0x38(%rsp), %r10
leaq (%rdx,%r10,4), %rcx
movl %eax, (%rdx,%r10,4)
movq 0x28(%rsp), %rdx
movq %rbx, (%rdx,%r10,8)
movl %eax, %r15d
movq %rbx, %rdx
movq 0x48(%rsp), %r8
movq 0x70(%rsp), %r9
jmp 0x13959
movq 0x8(%rsp), %rax
movq 0x38(%rsp), %r10
leaq (%rax,%r10,4), %rcx
movl %r15d, (%rax,%r10,4)
movq 0x28(%rsp), %rax
movq 0x78(%rsp), %rdx
movq %rdx, (%rax,%r10,8)
movl %r15d, %eax
testq %rdx, %rdx
movq 0x48(%rsp), %r8
movq 0x70(%rsp), %r9
js 0x13964
movl (%rbp,%rdx,4), %eax
cmpl %r15d, %eax
cmovlel %r15d, %eax
movl %eax, (%rbp,%r10,4)
testq %rbx, %rbx
js 0x13992
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rbx, %rdx
shlq $0x4, %rdx
subq (%r12,%rdx), %rax
cmpq %r8, %rax
ja 0x13995
movq 0x8(%rsp), %rax
movl (%rax,%rbx,4), %eax
cmpl (%rcx), %eax
jge 0x13995
movq %r10, %rbx
movq 0xd0(%rsp), %rcx
cmpl %r15d, %ecx
cmovlel %r15d, %ecx
incq %r10
addq $0x10, %r9
jmp 0x136c7
cmpl $0x0, (%rsp)
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
cmovel 0x4(%rsp), %ebx
leaq 0x1acb3(%rip), %rax     # 0x2e678
testb $-0x80, (%rax)
jne 0x13abc
leaq 0x60(%rsp), %rax
leaq 0x64(%rsp), %r10
movq 0xb0(%rsp), %r15
movq %r15, %rdi
movq 0x88(%rsp), %rsi
movq 0x8(%rsp), %r13
movq %r13, %rdx
movq 0x28(%rsp), %r14
movq %r14, %rcx
movq %rbp, %r8
movq 0x30(%rsp), %r9
pushq %rax
pushq %r10
pushq $0x0
pushq %rbx
pushq 0xb0(%rsp)
pushq 0xc8(%rsp)
callq 0x13168
addq $0x30, %rsp
movq %rax, %rbx
leaq 0x64(%rsp), %rax
movl (%rax), %ebp
movq 0x98(%rsp), %rax
movl %ebp, (%rax)
movq 0xa8(%rsp), %rax
movq %rbx, (%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2577
movq %r15, %rdi
movq %r13, %rsi
callq 0x2577
movq %r15, %rdi
movq 0x30(%rsp), %rsi
callq 0x2577
testl %ebp, %ebp
je 0x13a90
movl 0x60(%rsp), %ecx
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rdx
movq 0x40(%rsp), %r8
movq %r12, %r9
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x13c46
movq %r15, %rdi
movq %r12, %rsi
callq 0x2577
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x2577
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1a535(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
leaq 0x1084c(%rip), %rsi     # 0x24319
movq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x2210
jmp 0x139ce