// File: /workspace/github_c_makefile_star2_O3/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 $0xe8, %rsp
movl %r8d, %r12d
movl %ecx, 0x84(%rsp)
movl %edx, 0x2c(%rsp)
movl %edi, %ebx
movq 0x158(%rsp), %rdi
movq 0x150(%rsp), %r8
movq 0x148(%rsp), %r10
movq 0x140(%rsp), %rdx
movq 0x138(%rsp), %r15
movl 0x130(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0x128(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x120(%rsp), %r11d
testq %r8, %r8
je 0x16378
movq $0x0, (%r8)
movl $0x0, (%r10)
testq %r15, %r15
setne %al
testq %rdx, %rdx
setne %cl
testb %cl, %al
jne 0x16390
movq %rdx, %rsi
jmp 0x16be8
movss %xmm0, 0x7c(%rsp)
movss %xmm1, 0x80(%rsp)
movq %r11, 0xb0(%rsp)
movq %r10, 0xb8(%rsp)
movq %r9, 0xc0(%rsp)
movq %r8, 0xc8(%rsp)
movq %rdx, 0x40(%rsp)
movl 0x2c(%rsp), %ecx
movl %ebx, %ebp
cmpl %ecx, %ebx
cmovlel %ecx, %ebp
cmpl %ecx, %esi
movl %ecx, %eax
cmovgl %esi, %eax
cmpl $0x0, 0xc(%rsp)
cmovnel %esi, %eax
movl %eax, 0x1c(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovel %ecx, %eax
movq %rax, 0xa8(%rsp)
leaq (,%r15,8), %rsi
movq %rdi, %r14
callq 0x30e5
movq %rax, 0x58(%rsp)
leaq (,%r15,4), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x30e5
movq %rax, 0x50(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x30e5
movq %rax, 0x38(%rsp)
movl $0x4, %edx
movq %r14, 0xd0(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2f84
movq %rax, 0x68(%rsp)
movq %r15, 0x90(%rsp)
testq %r15, %r15
jle 0x16ae2
movslq %ebp, %rbx
movslq %r12d, %rax
movq %rax, 0xd8(%rsp)
movq 0x40(%rsp), %r14
leaq -0x8(%r14), %rax
movq %rax, 0x98(%rsp)
xorl %edx, %edx
movq $-0x1, %r8
movss 0x12da3(%rip), %xmm0    # 0x29230
movss 0x12d8f(%rip), %xmm1    # 0x29224
movss 0x12d8b(%rip), %xmm2    # 0x29228
movss 0x12d87(%rip), %xmm3    # 0x2922c
xorl %r11d, %r11d
xorl %ecx, %ecx
movq 0x50(%rsp), %r13
movss 0x80(%rsp), %xmm7
movss 0x7c(%rsp), %xmm8
movl %ebp, %r15d
xorl %ebp, %ebp
movq %rbx, 0x30(%rsp)
movq %rcx, 0xe0(%rsp)
movq %rbp, %rax
shlq $0x4, %rax
leaq (%r14,%rax), %r12
cmpq %rbp, %r11
jge 0x1651e
movq (%r12), %rcx
movq %r11, %rsi
shlq $0x4, %rsi
addq %r14, %rsi
movq (%rsi), %rdi
movq %rdi, %r9
xorq %rcx, %r9
shrq $0x20, %r9
sete %r9b
addq %rbx, %rdi
cmpq %rdi, %rcx
setbe %dil
testb %dil, %r9b
jne 0x1651e
incq %r11
addq $0x10, %rsi
cmpq %rbp, %r11
jl 0x164ef
movq %rbp, %r11
movq %r12, 0xa0(%rsp)
movq %r11, %rcx
movq 0x8(%r14,%rax), %r9
movq %r9, %rax
shrq $0x20, %rax
movq %rbp, %rsi
subq %r11, %rsi
movq %rbp, %r11
movq 0xd8(%rsp), %rdi
subq %rdi, %r11
cmpq %rdi, %rsi
cmovleq %rcx, %r11
movzbl %al, %eax
movl %eax, 0x8(%rsp)
leaq -0x1(%rbp), %rax
cmpq %r11, %rbp
movq %rbp, 0x20(%rsp)
jle 0x167d7
movq %r9, %rcx
shrq $0x30, %rcx
movzbl %cl, %ecx
movl %ecx, 0x10(%rsp)
movq $-0x1, 0x70(%rsp)
movl $0x0, 0x18(%rsp)
movq 0x98(%rsp), %rbx
movq %rax, %r14
movq (%rbx), %rsi
movq %r9, %r10
subl %esi, %r9d
setle %cl
cmpl %r15d, %r9d
setg %dil
orb %cl, %dil
je 0x165c6
incq %rdx
movq %r10, %r9
leaq -0x1(%r14), %rsi
addq $-0x10, %rbx
cmpq %r11, %r14
movq %rsi, %r14
jg 0x16593
jmp 0x167e3
movq %rsi, %rcx
shrq $0x30, %rcx
movzbl %cl, %edi
movq 0xa0(%rsp), %rcx
movl (%rcx), %ecx
subl -0x8(%rbx), %ecx
cmpl %edi, 0x10(%rsp)
jne 0x16625
cmpl 0x1c(%rsp), %r9d
jg 0x165ab
testl %ecx, %ecx
je 0x165ab
movl %r9d, %ebp
subl %ecx, %ebp
movl %ebp, %r12d
negl %r12d
cmovsl %ebp, %r12d
movq 0x20(%rsp), %rbp
cmpl 0x2c(%rsp), %r12d
jg 0x165ab
cmpl $0x0, 0xc(%rsp)
jne 0x16634
cmpl $0x2, 0x4c(%rsp)
jl 0x16634
cmpl %edi, 0x10(%rsp)
jne 0x16634
cmpl %ecx, 0x1c(%rsp)
jl 0x165ab
jmp 0x16634
movl %r9d, %ebp
subl %ecx, %ebp
movl %ebp, %r12d
negl %r12d
cmovsl %ebp, %r12d
movq %rdx, 0x60(%rsp)
movl %r15d, %edx
cmpl %ecx, %r9d
movl %ecx, %r15d
cmovll %r9d, %r15d
shrq $0x20, %rsi
movzbl %sil, %ebp
cmpl %r15d, %ebp
movl %r15d, %esi
cmovll %ebp, %esi
testl %r12d, %r12d
jne 0x16673
cmpl %r15d, %ebp
jl 0x16673
movl %edx, %r15d
movq 0x60(%rsp), %rdx
movq 0x20(%rsp), %rbp
jmp 0x1675f
testl %r12d, %r12d
je 0x166ca
leal 0x1(%r12), %ebp
xorps %xmm4, %xmm4
cvtsi2ss %ebp, %xmm4
movd %xmm4, %ebp
movl %ebp, %r13d
shrl $0x17, %r13d
addl $-0x80, %r13d
xorps %xmm5, %xmm5
cvtsi2ss %r13, %xmm5
movq 0x50(%rsp), %r13
andl $0x7fffff, %ebp         # imm = 0x7FFFFF
orl $0x3f800000, %ebp       # imm = 0x3F800000
movd %ebp, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
jmp 0x166cd
xorps %xmm4, %xmm4
movq 0x20(%rsp), %rbp
xorps %xmm5, %xmm5
cvtsi2ss %r12d, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %r15d, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0xc(%rsp)
jne 0x16714
cmpl %edi, 0x10(%rsp)
jne 0x16714
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %ecx
subl %ecx, %esi
movl %edx, %r15d
movq 0x60(%rsp), %rdx
jmp 0x1675f
cmpl %edi, 0x10(%rsp)
movl %edx, %r15d
je 0x16737
testl %ecx, %ecx
jne 0x16737
movq 0x60(%rsp), %rdx
incq %rdx
movl $0x1, %esi
movl 0x8(%rsp), %edi
movq %r10, %r9
jmp 0x16773
cmpl %edi, 0x10(%rsp)
movq 0x60(%rsp), %rdx
jne 0x16755
cmpl %ecx, %r9d
jl 0x16755
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %ecx
jmp 0x1675d
minss %xmm4, %xmm5
cvttss2si %xmm5, %ecx
subl %ecx, %esi
movl 0x8(%rsp), %edi
incq %rdx
movl %esi, %ecx
negl %ecx
movq %r10, %r9
jo 0x165b1
addl (%r13,%r14,4), %esi
cmpl %edi, %esi
jle 0x16794
xorl %ecx, %ecx
movl 0x18(%rsp), %edi
testl %edi, %edi
setg %cl
subl %ecx, %edi
movl %edi, 0x18(%rsp)
movq %r14, 0x70(%rsp)
jmp 0x167b8
movq 0x68(%rsp), %rcx
cmpl %ebp, (%rcx,%r14,4)
jne 0x167b6
movl 0x18(%rsp), %ecx
cmpl 0x84(%rsp), %ecx
jge 0x16a18
incl %ecx
movl %ecx, 0x18(%rsp)
movl %edi, %esi
movq 0x58(%rsp), %rcx
movq (%rcx,%r14,8), %rcx
testq %rcx, %rcx
js 0x167ce
movq 0x68(%rsp), %rdi
movl %ebp, (%rdi,%rcx,4)
movl %esi, 0x8(%rsp)
jmp 0x165b1
movq $-0x1, 0x70(%rsp)
movq %rax, %rsi
testq %r8, %r8
movq 0x40(%rsp), %r14
movq 0x30(%rsp), %rbx
movq 0xa0(%rsp), %r12
js 0x1680e
movq (%r12), %rcx
movq %r8, %rdi
shlq $0x4, %rdi
subq (%r14,%rdi), %rcx
cmpq %rbx, %rcx
jbe 0x16839
movq $-0x1, %r8
cmpq %r11, %rbp
jle 0x1686f
movl $0x80000000, %ecx       # imm = 0x80000000
movl (%r13,%rax,4), %edi
cmpl %edi, %ecx
cmovlq %rax, %r8
cmovlel %edi, %ecx
leaq -0x1(%rax), %rdi
cmpq %r11, %rax
movq %rdi, %rax
jg 0x1681f
testq %r8, %r8
sets %al
cmpq %rsi, %r8
setge %cl
orb %al, %cl
jne 0x1686f
movq %r8, %rcx
shlq $0x4, %rcx
movq 0x8(%r14,%rcx), %r10
movl %r9d, %eax
subl %r10d, %eax
setle %sil
cmpl %r15d, %eax
setg %dil
orb %sil, %dil
je 0x168fa
leaq (,%rbp,4), %rax
addq %r13, %rax
movl 0x8(%rsp), %edi
movl %edi, (%r13,%rbp,4)
movq 0x70(%rsp), %r9
movq 0x58(%rsp), %rcx
movq %r9, (%rcx,%rbp,8)
movl %edi, %ecx
testq %r9, %r9
movq 0x38(%rsp), %rsi
js 0x168a6
movl (%rsi,%r9,4), %ecx
cmpl %edi, %ecx
cmovlel %edi, %ecx
movl %ecx, (%rsi,%rbp,4)
testq %r8, %r8
js 0x168cb
movq (%r12), %rcx
movq %r8, %rsi
shlq $0x4, %rsi
subq (%r14,%rsi), %rcx
cmpq %rbx, %rcx
ja 0x168ce
movl (%r13,%r8,4), %ecx
cmpl (%rax), %ecx
jge 0x168ce
movq %rbp, %r8
movq 0xe0(%rsp), %rcx
cmpl %edi, %ecx
cmovlel %edi, %ecx
incq %rbp
addq $0x10, 0x98(%rsp)
cmpq 0x90(%rsp), %rbp
jne 0x164c9
jmp 0x16aeb
addq %r14, %rcx
shrq $0x30, %r9
movl $0xff, %edi
andl %edi, %r9d
movq %r10, %rsi
shrq $0x30, %rsi
andl %edi, %esi
movl (%r12), %edi
subl (%rcx), %edi
movq %r9, 0x10(%rsp)
cmpl %esi, %r9d
jne 0x16953
cmpl 0x1c(%rsp), %eax
jg 0x1686f
testl %edi, %edi
je 0x1686f
movl %eax, %ecx
subl %edi, %ecx
movl %ecx, %ebx
negl %ebx
cmovsl %ecx, %ebx
cmpl 0x2c(%rsp), %ebx
jle 0x169df
movq 0x30(%rsp), %rbx
jmp 0x1686f
movl %eax, %ecx
subl %edi, %ecx
movl %ecx, %ebx
negl %ebx
cmovsl %ecx, %ebx
cmpl %edi, %eax
movl %edi, %ecx
cmovll %eax, %ecx
shrq $0x20, %r10
movzbl %r10b, %ebp
cmpl %ecx, %ebp
movl %ecx, %r9d
cmovll %ebp, %r9d
testl %ebx, %ebx
jne 0x1698d
cmpl %ecx, %ebp
jl 0x1698d
movq 0x30(%rsp), %rbx
movq 0x20(%rsp), %rbp
jmp 0x16a9d
testl %ebx, %ebx
je 0x16a20
leal 0x1(%rbx), %ebp
cvtsi2ss %ebp, %xmm4
movd %xmm4, %ebp
movl %ebp, %r14d
shrl $0x17, %r14d
addl $-0x80, %r14d
cvtsi2ss %r14, %xmm5
movq 0x40(%rsp), %r14
andl $0x7fffff, %ebp         # imm = 0x7FFFFF
orl $0x3f800000, %ebp       # imm = 0x3F800000
movd %ebp, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
jmp 0x16a23
cmpl $0x0, 0xc(%rsp)
jne 0x1695e
cmpl $0x2, 0x4c(%rsp)
jl 0x1695e
cmpl %esi, 0x10(%rsp)
jne 0x1695e
cmpl %edi, 0x1c(%rsp)
jge 0x1695e
movq 0x30(%rsp), %rbx
movq 0x20(%rsp), %rbp
jmp 0x1686f
movq %r14, %rsi
jmp 0x167e3
xorps %xmm4, %xmm4
movq 0x20(%rsp), %rbp
xorps %xmm5, %xmm5
cvtsi2ss %ebx, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %ecx, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0xc(%rsp)
jne 0x16a66
cmpl %esi, 0x10(%rsp)
jne 0x16a66
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %eax
subl %eax, %r9d
movq 0x30(%rsp), %rbx
jmp 0x16a9d
cmpl %esi, 0x10(%rsp)
movq 0x30(%rsp), %rbx
je 0x16a7a
movl $0x1, %ecx
testl %edi, %edi
je 0x16aab
cmpl %esi, 0x10(%rsp)
jne 0x16a92
cmpl %edi, %eax
jl 0x16a92
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %eax
jmp 0x16a9a
minss %xmm4, %xmm5
cvttss2si %xmm5, %eax
subl %eax, %r9d
movl %r9d, %eax
negl %eax
movl %r9d, %ecx
jo 0x1686f
addl (%r13,%r8,4), %ecx
cmpl %ecx, 0x8(%rsp)
jge 0x1686f
leaq (,%rbp,4), %rax
addq %r13, %rax
movl %ecx, (%r13,%rbp,4)
movq 0x58(%rsp), %rsi
movq %r8, (%rsi,%rbp,8)
movl %ecx, %edi
movq %r8, %r9
movq 0x38(%rsp), %rsi
jmp 0x1689d
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x50(%rsp), %r13
leaq 0x1bb86(%rip), %rax     # 0x32678
testb $-0x80, (%rax)
jne 0x16c01
leaq 0x88(%rsp), %rax
leaq 0x8c(%rsp), %r10
movq 0xd0(%rsp), %r14
movq %r14, %rdi
movq 0x90(%rsp), %rsi
movq %r13, %rdx
movq 0x58(%rsp), %r15
movq %r15, %rcx
movq 0x38(%rsp), %r8
movq 0x68(%rsp), %rbx
movq %rbx, %r9
pushq %rax
pushq %r10
pushq $0x0
pushq 0xc0(%rsp)
pushq 0xd0(%rsp)
pushq 0xe8(%rsp)
movq %r13, %rbp
callq 0x15f50
addq $0x30, %rsp
movq %rax, %r12
leaq 0x8c(%rsp), %rax
movl (%rax), %r13d
movq 0xb8(%rsp), %rax
movl %r13d, (%rax)
movq 0xc8(%rsp), %rax
movq %r12, (%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x301f
movq %r14, %rdi
movq %rbp, %rsi
callq 0x301f
movq %r14, %rdi
movq %rbx, %rsi
callq 0x301f
testl %r13d, %r13d
je 0x16bd3
movl 0x88(%rsp), %ecx
movq %r14, %rdi
movl %r13d, %esi
movq %r12, %rdx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16c23
movq %r14, %rdi
movq 0x40(%rsp), %rsi
callq 0x301f
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x301f
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1b3f0(%rip), %rax     # 0x31ff8
movq (%rax), %rdi
leaq 0x126f7(%rip), %rsi     # 0x29309
xorl %eax, %eax
callq 0x2210
movq 0x50(%rsp), %r13
jmp 0x16afb