// File: /workspace/github_c_makefile_star2_O1/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 $0xf8, %rsp
movl %ecx, 0x78(%rsp)
movl %edx, 0x14(%rsp)
movq 0x168(%rsp), %r14
movq 0x160(%rsp), %rdx
movq 0x158(%rsp), %r11
movq 0x150(%rsp), %r15
movq 0x148(%rsp), %r13
movl 0x140(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x138(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x130(%rsp), %r10d
testq %rdx, %rdx
je 0x16131
movq $0x0, (%rdx)
movl $0x0, (%r11)
testq %r13, %r13
setne %al
testq %r15, %r15
setne %cl
testb %cl, %al
jne 0x1614c
movq %r14, %rdi
movq %r15, %rsi
jmp 0x169b3
movl %r8d, 0x4(%rsp)
movss %xmm0, 0x6c(%rsp)
movss %xmm1, 0x70(%rsp)
movq %r10, 0xa8(%rsp)
movq %r11, 0xb0(%rsp)
movq %r9, 0xb8(%rsp)
movq %rdx, 0xc0(%rsp)
movl 0x14(%rsp), %ecx
cmpl %ecx, %edi
cmovlel %ecx, %edi
movl %edi, 0x30(%rsp)
cmpl %ecx, %esi
movl %ecx, %eax
cmovgl %esi, %eax
cmpl $0x0, 0xc(%rsp)
cmovnel %esi, %eax
movl %eax, 0x10(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovel %ecx, %eax
movq %rax, 0xa0(%rsp)
leaq (,%r13,8), %rsi
movq %r14, %rdi
callq 0x31c1
movq %rax, 0x28(%rsp)
leaq (,%r13,4), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x31c1
movq %rax, %rbp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x31c1
movq %rax, %r12
movl $0x4, %edx
movq %r14, 0xc8(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x3054
movq 0x28(%rsp), %rdx
testq %r13, %r13
movq %rbp, 0x20(%rsp)
movq %rax, 0x58(%rsp)
jle 0x168cd
movl 0x30(%rsp), %edi
movslq %edi, %rcx
movq %rcx, 0x60(%rsp)
movslq 0x4(%rsp), %rcx
movq %rcx, 0xe8(%rsp)
addq $-0x4, %rax
movq %rax, 0x88(%rsp)
movq %rdx, %rax
addq $-0x8, %rax
movq %rax, 0x90(%rsp)
movq %rbp, %rax
addq $-0x4, %rax
movq %rax, 0x98(%rsp)
leaq -0x8(%r15), %rax
movq %rax, 0x80(%rsp)
movq $0x0, 0x38(%rsp)
movq $-0x1, %r11
movss 0x12f63(%rip), %xmm0    # 0x291e0
movss 0x12f4f(%rip), %xmm1    # 0x291d4
movss 0x12f4b(%rip), %xmm2    # 0x291d8
movss 0x12f47(%rip), %xmm3    # 0x291dc
xorl %esi, %esi
xorl %ebx, %ebx
xorl %r8d, %r8d
movq %rdx, %rcx
movq %rbp, %rdx
movss 0x70(%rsp), %xmm7
movss 0x6c(%rsp), %xmm8
movl %edi, %ebp
movq %r15, 0xe0(%rsp)
movq %r12, 0xd8(%rsp)
movq %r13, 0xd0(%rsp)
movq %r8, 0x40(%rsp)
movq %rbx, %rax
shlq $0x4, %rax
leaq (%r15,%rax), %rdi
cmpq %rbx, %rsi
movq %rdi, 0x48(%rsp)
jge 0x16318
movq (%rdi), %r9
movq %rsi, %r10
shlq $0x4, %r10
addq %r15, %r10
movq (%r10), %rdi
movq %rdi, %r8
xorq %r9, %r8
shrq $0x20, %r8
jne 0x16309
addq 0x60(%rsp), %rdi
cmpq %rdi, %r9
jbe 0x16318
incq %rsi
addq $0x10, %r10
cmpq %rbx, %rsi
jl 0x162f0
movq %rbx, %rsi
movq %rsi, %rdi
movq 0x8(%r15,%rax), %r14
movq %r14, %rax
shrq $0x20, %rax
movq %rbx, %r8
subq %rsi, %r8
movq %rbx, %rsi
movq 0xe8(%rsp), %r9
subq %r9, %rsi
cmpq %r9, %r8
cmovleq %rdi, %rsi
movzbl %al, %eax
movl %eax, 0x8(%rsp)
leaq -0x1(%rbx), %r10
cmpq %rsi, %rbx
jle 0x165ce
movq %r14, %rax
shrq $0x30, %rax
movzbl %al, %eax
movl %eax, 0x4(%rsp)
movq $-0x1, 0x18(%rsp)
xorl %r8d, %r8d
movq 0x80(%rsp), %rcx
movq $0x0, 0x50(%rsp)
movq %r8, %r12
movq (%rcx), %r9
movq %r14, %rdi
movl %r14d, %r13d
subl %r9d, %r13d
setle %al
movl %ebp, %edx
cmpl %ebp, %r13d
setg %r8b
orb %al, %r8b
movl $0x80000000, %ebp       # imm = 0x80000000
jne 0x164e1
movq %r9, %rax
shrq $0x30, %rax
movzbl %al, %r8d
movq 0x48(%rsp), %rax
movl (%rax), %r14d
subl -0x8(%rcx), %r14d
cmpl %r8d, 0x4(%rsp)
jne 0x163dc
cmpl 0x10(%rsp), %r13d
jg 0x164e1
testl %r14d, %r14d
je 0x164e1
movl %r13d, %eax
subl %r14d, %eax
movl %eax, %r15d
negl %r15d
cmovsl %eax, %r15d
cmpl %r8d, 0x4(%rsp)
jne 0x163fe
cmpl 0x14(%rsp), %r15d
jg 0x164e1
cmpl $0x0, 0xc(%rsp)
jne 0x1641e
cmpl $0x2, 0x34(%rsp)
jl 0x1641e
cmpl %r8d, 0x4(%rsp)
jne 0x1641e
cmpl %r14d, 0x10(%rsp)
jl 0x164e1
cmpl %r14d, %r13d
movl %r14d, %ebp
cmovll %r13d, %ebp
shrq $0x20, %r9
movzbl %r9b, %eax
cmpl %ebp, %eax
movl %ebp, %r9d
cmovll %eax, %r9d
cmpl %r14d, %r13d
jne 0x16446
cmpl %ebp, %eax
jge 0x164de
xorps %xmm4, %xmm4
cmpl %r14d, %r13d
je 0x1649d
leal 0x1(%r15), %eax
xorps %xmm4, %xmm4
cvtsi2ss %eax, %xmm4
movd %xmm4, %eax
movl %r9d, 0x74(%rsp)
movl %eax, %r9d
shrl $0x17, %r9d
addl $-0x80, %r9d
cvtsi2ss %r9, %xmm5
movl 0x74(%rsp), %r9d
andl $0x7fffff, %eax         # imm = 0x7FFFFF
orl $0x3f800000, %eax       # imm = 0x3F800000
movd %eax, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %r15d, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %ebp, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0xc(%rsp)
jne 0x1659c
cmpl %r8d, 0x4(%rsp)
jne 0x1659c
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %eax
subl %eax, %r9d
movl %r9d, %ebp
movl %ebp, %eax
negl %eax
jno 0x164f4
xorl %r8d, %r8d
movl %edx, %ebp
movq %rdi, %r14
jmp 0x1657c
movq 0x98(%rsp), %rax
addl (%rax,%r12,4), %ebp
cmpl 0x8(%rsp), %ebp
jle 0x1652d
leaq (%rbx,%r12), %rax
decq %rax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
movq 0x50(%rsp), %r8
testl %r8d, %r8d
setg %al
subl %eax, %r8d
movq %r8, 0x50(%rsp)
movl %ebp, 0x8(%rsp)
jmp 0x1655a
movq 0x88(%rsp), %rax
cmpl %ebx, (%rax,%r12,4)
jne 0x1655a
movq 0x50(%rsp), %r9
leal 0x1(%r9), %eax
movb $0x1, %r8b
cmpl 0x78(%rsp), %r9d
movq %rax, 0x50(%rsp)
movl %edx, %ebp
movq %rdi, %r14
jl 0x1655f
jmp 0x1657c
movl %edx, %ebp
movq %rdi, %r14
movq 0x90(%rsp), %rax
movq (%rax,%r12,8), %rax
testq %rax, %rax
js 0x16579
movq 0x58(%rsp), %r8
movl %ebx, (%r8,%rax,4)
xorl %r8d, %r8d
testb %r8b, %r8b
jne 0x165dc
leaq -0x1(%r12), %r8
leaq (%rbx,%r12), %rax
decq %rax
addq $-0x10, %rcx
cmpq %rsi, %rax
jg 0x16381
jmp 0x165e5
cmpl %r8d, 0x4(%rsp)
je 0x165b1
movl $0x1, %ebp
testl %r14d, %r14d
je 0x164e1
cmpl %r8d, 0x4(%rsp)
jne 0x165c1
cmpl %r14d, %r13d
jge 0x164cf
minss %xmm4, %xmm5
cvttss2si %xmm5, %eax
jmp 0x164db
movq $-0x1, 0x18(%rsp)
movq %r10, %r8
jmp 0x1661d
leaq (%rbx,%r12), %r8
decq %r8
jmp 0x165eb
addq %rbx, %r8
decq %r8
movq 0x38(%rsp), %rax
subq %r12, %rax
incq %rax
movq %rax, 0x38(%rsp)
movq 0xe0(%rsp), %r15
movq 0xd8(%rsp), %r12
movq 0xd0(%rsp), %r13
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rdx
testq %r11, %r11
js 0x1663c
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %r11, %rdi
shlq $0x4, %rdi
subq (%r15,%rdi), %rax
cmpq 0x60(%rsp), %rax
jbe 0x16666
movq $-0x1, %r11
cmpq %rsi, %rbx
jle 0x16666
movl $0x80000000, %edi       # imm = 0x80000000
movl (%rdx,%r10,4), %eax
cmpl %eax, %edi
cmovlq %r10, %r11
cmovlel %eax, %edi
leaq -0x1(%r10), %rax
cmpq %rsi, %r10
movq %rax, %r10
jg 0x1664d
testq %r11, %r11
js 0x167e6
cmpq %r8, %r11
jge 0x167e6
movq %r11, %r10
shlq $0x4, %r10
movq 0x8(%r15,%r10), %r8
movl %r14d, %ecx
subl %r8d, %ecx
setle %al
cmpl %ebp, %ecx
setg %dl
orb %al, %dl
movl $0x80000000, %r9d       # imm = 0x80000000
jne 0x167d5
addq %r15, %r10
shrq $0x30, %r14
movl $0xff, %eax
andl %eax, %r14d
movq %r8, %rdx
shrq $0x30, %rdx
andl %eax, %edx
movq 0x48(%rsp), %rax
movl (%rax), %edi
subl (%r10), %edi
cmpl %edx, %r14d
jne 0x166d9
cmpl 0x10(%rsp), %ecx
jg 0x167d5
testl %edi, %edi
je 0x167d5
movl %ecx, %eax
subl %edi, %eax
movl %eax, %r10d
negl %r10d
cmovsl %eax, %r10d
cmpl %edx, %r14d
jne 0x166f7
cmpl 0x14(%rsp), %r10d
jg 0x167d5
cmpl $0x0, 0xc(%rsp)
jne 0x16714
cmpl $0x2, 0x34(%rsp)
jl 0x16714
cmpl %edx, %r14d
jne 0x16714
cmpl %edi, 0x10(%rsp)
jl 0x167d5
cmpl %edi, %ecx
movl %edi, %r9d
cmovll %ecx, %r9d
shrq $0x20, %r8
movzbl %r8b, %eax
cmpl %r9d, %eax
movl %r9d, %r8d
cmovll %eax, %r8d
cmpl %edi, %ecx
jne 0x1673c
cmpl %r9d, %eax
jge 0x167d2
xorps %xmm4, %xmm4
cmpl %edi, %ecx
je 0x16792
leal 0x1(%r10), %eax
xorps %xmm4, %xmm4
cvtsi2ss %eax, %xmm4
movd %xmm4, %eax
movq %r14, %rbp
movl %eax, %r14d
shrl $0x17, %r14d
addl $-0x80, %r14d
cvtsi2ss %r14, %xmm5
movq %rbp, %r14
movl 0x30(%rsp), %ebp
andl $0x7fffff, %eax         # imm = 0x7FFFFF
orl $0x3f800000, %eax       # imm = 0x3F800000
movd %eax, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %r10d, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %r9d, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0xc(%rsp)
jne 0x168a0
cmpl %edx, %r14d
jne 0x168a0
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %eax
subl %eax, %r8d
movl %r8d, %r9d
movl %r9d, %eax
negl %eax
jno 0x167f7
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rdx
movq 0x40(%rsp), %r8
movl 0x8(%rsp), %r10d
movq 0x18(%rsp), %rax
jmp 0x1681f
movq 0x20(%rsp), %rdx
addl (%rdx,%r11,4), %r9d
movl 0x8(%rsp), %r10d
cmpl %r9d, %r10d
movq 0x18(%rsp), %rax
cmovlq %r11, %rax
cmovlel %r9d, %r10d
movq 0x28(%rsp), %rcx
movq 0x40(%rsp), %r8
movl %r10d, (%rdx,%rbx,4)
movq %rax, (%rcx,%rbx,8)
testq %rax, %rax
js 0x16835
movl (%r12,%rax,4), %eax
cmpl %r10d, %eax
jg 0x16838
movl %r10d, %eax
movl %eax, (%r12,%rbx,4)
testq %r11, %r11
js 0x16864
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %r11, %rdi
shlq $0x4, %rdi
subq (%r15,%rdi), %rax
cmpq 0x60(%rsp), %rax
ja 0x16867
movl (%rdx,%r11,4), %eax
cmpl (%rdx,%rbx,4), %eax
jge 0x16867
movq %rbx, %r11
cmpl %r10d, %r8d
cmovlel %r10d, %r8d
incq %rbx
addq $0x4, 0x88(%rsp)
addq $0x8, 0x90(%rsp)
addq $0x4, 0x98(%rsp)
addq $0x10, 0x80(%rsp)
cmpq %r13, %rbx
jne 0x162c9
jmp 0x168df
cmpl %edx, %r14d
je 0x168b3
movl $0x1, %r9d
testl %edi, %edi
je 0x167d5
cmpl %edx, %r14d
jne 0x168c0
cmpl %edi, %ecx
jge 0x167c3
minss %xmm4, %xmm5
cvttss2si %xmm5, %eax
jmp 0x167cf
xorl %r8d, %r8d
movq $0x0, 0x38(%rsp)
movq %rdx, %rcx
movq %rbp, %rdx
leaq 0x1bd92(%rip), %rax     # 0x32678
testb $-0x80, (%rax)
jne 0x169cc
leaq 0x7c(%rsp), %rax
leaq 0xf4(%rsp), %rbx
movq 0xc8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %r8
movq 0x58(%rsp), %r9
pushq %rax
pushq %rbx
pushq $0x0
pushq 0xb8(%rsp)
pushq 0xc8(%rsp)
pushq 0xe0(%rsp)
movq %rcx, %r13
callq 0x15cbc
addq $0x30, %rsp
movl (%rbx), %ebp
movq %r14, %rbx
movq %rax, %r14
movq 0xb0(%rsp), %rax
movl %ebp, (%rax)
movq 0xc0(%rsp), %rax
movq %r14, (%rax)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x30f6
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x30f6
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
callq 0x30f6
testl %ebp, %ebp
je 0x169a2
movl 0x7c(%rsp), %ecx
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq %r12, %r8
movq %r15, %r9
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x169fb
movq %rbx, %rdi
movq %r15, %rsi
callq 0x30f6
movq %rbx, %rdi
movq %r12, %rsi
callq 0x30f6
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1b625(%rip), %rax     # 0x31ff8
movq (%rax), %rdi
leaq 0x128dc(%rip), %rsi     # 0x292b9
movq 0x38(%rsp), %rdx
movl %r8d, %ecx
xorl %eax, %eax
callq 0x2210
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
jmp 0x168ef