// File: /workspace/github_c_makefile_star2_O2/lh3[P]miniprot/chain.c
// Function: mp_chain
uint64_t *mp_chain(int32_t max_dist_x, int32_t max_dist_y, int32_t bw, int32_t max_skip, int32_t max_iter, int32_t min_cnt, int32_t min_sc, float chn_ceof_log,
				   int32_t is_spliced, int32_t kmer, int32_t bbit, int64_t n, uint64_t *a, int32_t *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, hf = 0;
	int64_t *p, i, j, st = 0, hi = -1;
	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_spliced) max_dist_y = bw;
	if (is_spliced) 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; i < n; ++i) {
		int64_t max_j = -1;
		int32_t max_f = kmer, n_skip = 0;
		while (st < i && ((a[i]>>32) - (a[st]>>32)) << bbit > max_dist_x) ++st;
		if (hi >= 0 && hi >= st) { // a heuristic to rescue some missing optimal chains due to max_skip
			int32_t sc;
			sc = hf + comput_sc(a[i], a[hi], max_dist_x, max_dist_y, bw, chn_ceof_log, is_spliced, bbit, kmer);
			if (sc > max_f) max_f = sc, max_j = hi;
		} else hf = 0, hi = -1;
		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_ceof_log, is_spliced, bbit, kmer);
			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;
		}
		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 (mmax_f < max_f) mmax_f = max_f;
		if (hf < max_f) hf = max_f, hi = i;
	}

	u = mp_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 $0x88, %rsp
movss %xmm0, 0x1c(%rsp)
movl %ecx, 0x60(%rsp)
movl %edx, (%rsp)
movl %edi, %r15d
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rcx
movq 0xe8(%rsp), %rbx
movq 0xe0(%rsp), %r14
testq %rax, %rax
je 0xc44d
andq $0x0, (%rax)
andl $0x0, (%rcx)
movq 0x100(%rsp), %r13
testq %r14, %r14
sete %al
testq %rbx, %rbx
sete %cl
orb %al, %cl
je 0xc473
movq $0x0, 0x8(%rsp)
jmp 0xcc2e
movl %r9d, 0x5c(%rsp)
movl (%rsp), %eax
cmpl %eax, %r15d
cmovlel %eax, %r15d
cmpl %eax, %esi
movq %r13, %r12
movl %eax, %r13d
cmovgl %esi, %r13d
cmpl $0x0, 0xc8(%rsp)
cmovnel %esi, %r13d
leaq (,%r14,8), %rsi
movq %r12, %rdi
movl %r8d, 0x8(%rsp)
callq 0x7460
movq %rax, 0x40(%rsp)
leaq (,%r14,4), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7460
movq %rax, 0x20(%rsp)
movq %r12, %rdi
movq %rbp, 0x68(%rsp)
movq %rbp, %rsi
callq 0x7460
movq %rax, 0x38(%rsp)
pushq $0x4
popq %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x72e4
movl 0xd0(%rsp), %r10d
movq %rax, 0x28(%rsp)
movl 0xd8(%rsp), %eax
movl %eax, %eax
movq %rax, 0x10(%rsp)
movslq %r15d, %rax
movq %rax, 0x50(%rsp)
movslq 0x8(%rsp), %rax
movq %rax, 0x70(%rsp)
testq %r14, %r14
movl $0x0, %eax
movq %rax, 0x8(%rsp)
movl $0x0, %eax
cmovleq %rax, %r14
pushq $-0x1
popq %r8
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r14, 0x78(%rsp)
cmpq %r14, %r12
je 0xc741
cmpq %r12, %rbp
movq %r12, %r14
cmovgq %rbp, %r14
cmpq %r12, %rbp
jge 0xc57b
movl 0x4(%rbx,%r12,8), %eax
movl 0x4(%rbx,%rbp,8), %ecx
subq %rcx, %rax
movq 0x10(%rsp), %rcx
shlq %cl, %rax
cmpq 0x50(%rsp), %rax
jbe 0xc578
incq %rbp
jmp 0xc553
movq %rbp, %r14
movl $0x0, 0x34(%rsp)
movl $0x0, %ecx
pushq $-0x1
popq %rbp
movq %rbp, %rdx
movq %rbp, %rsi
movl %r10d, %eax
testq %r8, %r8
js 0xc601
movq %rbp, %rdx
movq %rbp, %rsi
movl %r10d, %eax
cmpq %r14, %r8
jl 0xc601
movq (%rbx,%r12,8), %rdi
movq (%rbx,%r8,8), %rsi
movl %r15d, %edx
movl %r13d, %ecx
movq %r8, 0x48(%rsp)
movl (%rsp), %r8d
movss 0x1c(%rsp), %xmm0
movl 0xc8(%rsp), %r9d
pushq %r10
movl 0xe0(%rsp), %eax
pushq %rax
callq 0xcc6d
movl 0xe0(%rsp), %r10d
addq $0x10, %rsp
movq 0x8(%rsp), %rcx
addl %ecx, %eax
cmpl %r10d, %eax
movq %rbp, %rsi
movq 0x48(%rsp), %rdx
cmovgq %rdx, %rsi
cmovlel %r10d, %eax
movq %rsi, 0x48(%rsp)
movl %eax, 0x8(%rsp)
movl %ecx, 0x64(%rsp)
movq %rdx, 0x80(%rsp)
movq %r12, %rax
subq %r14, %rax
movq %r12, %rbp
movq 0x70(%rsp), %rcx
subq %rcx, %rbp
cmpq %rcx, %rax
cmovleq %r14, %rbp
movq %r12, %r14
cmpq %rbp, %r14
jle 0xc6e3
movq (%rbx,%r12,8), %rdi
movq -0x8(%rbx,%r14,8), %rsi
decq %r14
movl %r15d, %edx
movl %r13d, %ecx
movl (%rsp), %r8d
movss 0x1c(%rsp), %xmm0
movl 0xc8(%rsp), %r9d
pushq %r10
movl 0xe0(%rsp), %eax
pushq %rax
callq 0xcc6d
movl 0xe0(%rsp), %r10d
addq $0x10, %rsp
movl %eax, %ecx
negl %ecx
jo 0xc631
movq 0x20(%rsp), %rcx
addl (%rcx,%r14,4), %eax
cmpl 0x8(%rsp), %eax
jle 0xc6aa
xorl %ecx, %ecx
movl 0x34(%rsp), %edx
testl %edx, %edx
setg %cl
subl %ecx, %edx
movl %edx, 0x34(%rsp)
movq %r14, 0x48(%rsp)
movl %eax, 0x8(%rsp)
jmp 0xc6c3
movq 0x28(%rsp), %rax
cmpl %r12d, (%rax,%r14,4)
jne 0xc6c3
movl 0x34(%rsp), %eax
cmpl 0x60(%rsp), %eax
jge 0xc6e3
incl 0x34(%rsp)
movq 0x40(%rsp), %rax
movq (%rax,%r14,8), %rax
testq %rax, %rax
js 0xc631
movq 0x28(%rsp), %rcx
movl %r12d, (%rcx,%rax,4)
jmp 0xc631
movq 0x20(%rsp), %rax
movl 0x8(%rsp), %esi
movl %esi, (%rax,%r12,4)
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rdx
movq %rdx, (%rax,%r12,8)
testq %rdx, %rdx
movq 0x38(%rsp), %rcx
js 0xc70f
movl (%rcx,%rdx,4), %eax
cmpl %esi, %eax
jg 0xc711
movl %esi, %eax
movl 0x64(%rsp), %edx
cmpl %esi, %edx
cmovgl %edx, %esi
movq 0x80(%rsp), %rdx
cmovlq %r12, %rdx
movl %eax, (%rcx,%r12,4)
incq %r12
movl %esi, %eax
movq %rax, 0x8(%rsp)
movq %rdx, %r8
movq 0x78(%rsp), %r14
jmp 0xc540
xorl %r15d, %r15d
xorl %eax, %eax
movq 0x100(%rsp), %r13
movq 0x28(%rsp), %rbp
movl 0xc0(%rsp), %r12d
movq 0x20(%rsp), %rdx
cmpq %rax, %r14
je 0xc776
xorl %ecx, %ecx
cmpl %r12d, (%rdx,%rax,4)
setge %cl
addq %rcx, %r15
incq %rax
jmp 0xc760
cmpl $0x0, 0xc8(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovel (%rsp), %eax
movl %eax, 0x1c(%rsp)
testq %r15, %r15
je 0xca28
movq %r15, %rsi
shlq $0x4, %rsi
movq %r13, %rdi
movq %rsi, (%rsp)
callq 0x7460
movq %rax, %r13
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0xc7da
movq 0x20(%rsp), %rdx
movslq (%rdx,%rcx,4), %rdx
cmpl %r12d, %edx
jl 0xc7d5
movq %rax, %rsi
shlq $0x4, %rsi
movq %rdx, (%r13,%rsi)
incq %rax
movq %rcx, 0x8(%r13,%rsi)
incq %rcx
jmp 0xc7ae
movq (%rsp), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x3cab
movq $0x0, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movq 0x68(%rsp), %rdx
callq 0x2160
decq %r15
movslq 0x5c(%rsp), %rax
movq %rax, 0x50(%rsp)
xorl %esi, %esi
movq %r15, %r14
testq %r14, %r14
js 0xc8d3
movq %r14, %rax
shlq $0x4, %rax
movq 0x8(%r13,%rax), %rcx
cmpl $0x0, (%rbp,%rcx,4)
jne 0xc8cb
movq %rsi, (%rsp)
movq %rbp, %r8
leaq (%rax,%r13), %rbp
leaq (%rax,%r13), %r12
addq $0x8, %r12
movl 0x1c(%rsp), %edi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq %r14, %r9
callq 0xce4d
xorl %ecx, %ecx
movq (%r12), %rdx
cmpq %rax, %rdx
je 0xc884
movq 0x28(%rsp), %rsi
movl $0x1, (%rsi,%rdx,4)
movq 0x40(%rsp), %rsi
leaq (%rsi,%rdx,8), %r12
incq %rcx
jmp 0xc861
movl (%rbp), %edx
testq %rax, %rax
js 0xc894
movq 0x20(%rsp), %rsi
subl (%rsi,%rax,4), %edx
movq 0x28(%rsp), %rbp
movl 0xc0(%rsp), %eax
movq (%rsp), %rsi
cmpl %eax, %edx
jl 0xc8cb
leaq (%rsi,%rcx), %rax
cmpq %rsi, %rax
jle 0xc8cb
xorl %edx, %edx
cmpq 0x50(%rsp), %rcx
setge %dl
cmovgeq %rax, %rsi
movq 0x10(%rsp), %rax
addl %edx, %eax
movq %rax, 0x10(%rsp)
decq %r14
jmp 0xc813
movslq 0x10(%rsp), %rsi
shlq $0x3, %rsi
movq 0x100(%rsp), %rdi
callq 0x7460
movq %rax, (%rsp)
movq $0x0, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movq 0x68(%rsp), %rdx
callq 0x2160
xorl %r12d, %r12d
testq %r15, %r15
js 0xc9e9
movq %r15, %rax
shlq $0x4, %rax
movq 0x8(%r13,%rax), %rcx
cmpl $0x0, (%rbp,%rcx,4)
jne 0xc9bb
movq %rbp, %r8
leaq (%rax,%r13), %rbp
leaq (%rax,%r13), %r14
addq $0x8, %r14
movl 0x1c(%rsp), %edi
movq %r13, %rsi
movq 0x20(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq %r15, %r9
callq 0xce4d
movq 0x38(%rsp), %rcx
leaq (%rcx,%r12,4), %rdx
xorl %ecx, %ecx
movq (%r14), %rsi
cmpq %rax, %rsi
je 0xc980
movl %esi, (%rdx,%rcx,4)
movq 0x28(%rsp), %rdi
movl $0x1, (%rdi,%rsi,4)
movq 0x40(%rsp), %rdi
leaq (%rdi,%rsi,8), %r14
incq %rcx
jmp 0xc95b
movl (%rbp), %edx
testq %rax, %rax
js 0xc990
movq 0x20(%rsp), %rsi
subl (%rsi,%rax,4), %edx
movq 0x28(%rsp), %rbp
leaq (%r12,%rcx), %rax
cmpl 0xc0(%rsp), %edx
setge %sil
cmpq %r12, %rax
setg %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0xc9bb
cmpq 0x50(%rsp), %rcx
jge 0xc9c9
movq %r12, %rax
decq %r15
movq %rax, %r12
jmp 0xc908
shlq $0x20, %rdx
orq %rcx, %rdx
movq 0x10(%rsp), %rsi
movslq %esi, %rcx
incl %esi
movq %rsi, 0x10(%rsp)
movq (%rsp), %rsi
movq %rdx, (%rsi,%rcx,8)
jmp 0xc9be
movq 0x100(%rsp), %rdi
movq %r13, %rsi
callq 0x736b
cmpq $0x7fffffff, %r12       # imm = 0x7FFFFFFF
jge 0xcc50
movslq %r12d, %r15
shlq $0x3, %r15
movq 0x100(%rsp), %r13
movq 0xf8(%rsp), %rax
movq 0x10(%rsp), %r12
movq (%rsp), %rdx
jmp 0xca38
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %edx, %edx
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rcx
movl %r12d, (%rcx)
movq %rdx, (%rsp)
movq %rdx, (%rax)
movq %r13, %rdi
movq 0x40(%rsp), %rsi
callq 0x736b
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x736b
movq %r13, %rdi
movq %rbp, %rsi
callq 0x736b
testl %r12d, %r12d
je 0xcc12
movq %r13, %rdi
movq %r15, %rsi
callq 0x7460
movslq %r12d, %r15
xorl %ecx, %ecx
testq %r15, %r15
movl $0x0, %ebp
cmovgq %r15, %rbp
movq %rax, %r14
movq 0x38(%rsp), %rsi
movq %rsi, %rax
addq $-0x4, %rax
xorl %edx, %edx
cmpq %rbp, %rdx
movq (%rsp), %rdi
je 0xcaf8
movslq (%rdi,%rdx,8), %r8
movslq %ecx, %r9
shlq $0x2, %r9
testq %r8, %r8
movl $0x0, %r11d
cmovgq %r8, %r11
leaq (%r14,%rcx,8), %rdi
leaq (%r9,%r8,4), %r8
addq %rax, %r8
negq %r11
xorl %r9d, %r9d
cmpq %r9, %r11
je 0xcaf0
movslq (%r8,%r9,4), %r10
movq (%rbx,%r10,8), %r10
movq %r10, (%rdi)
decq %r9
addq $0x8, %rdi
jmp 0xcad7
incq %rdx
subq %r9, %rcx
jmp 0xcaa5
movq %r12, 0x10(%rsp)
movq %r13, %rdi
callq 0x736b
movq %r13, %rdi
movq %r15, %r13
shlq $0x4, %r13
movq %r13, %rsi
callq 0x7460
movq (%rsp), %rdi
movq %rax, %r12
addq $0x8, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rdx, %rbp
je 0xcb51
movl 0x4(%r14,%rcx,8), %esi
movq %rsi, -0x8(%rax)
movq %rcx, %rsi
shlq $0x20, %rsi
orq %rdx, %rsi
movq %rsi, (%rax)
movslq (%rdi,%rdx,8), %rsi
addq %rsi, %rcx
incq %rdx
addq $0x10, %rax
jmp 0xcb26
movq %r12, %rsi
addq %r13, %rsi
movq %r12, %rdi
callq 0x3cab
shlq $0x3, %r15
movq 0x100(%rsp), %rdi
movq %r15, %rsi
callq 0x7460
movq %rax, 0x38(%rsp)
shlq $0x3, %rbp
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r14, 0x8(%rsp)
cmpq %r13, %rbp
movq (%rsp), %rdi
je 0xcbcd
movslq 0x8(%r12,%r13,2), %rax
movq (%rdi,%rax,8), %rax
movq 0x38(%rsp), %rcx
movq %rax, (%rcx,%r13)
leaq (%rbx,%r15,8), %rdi
movl 0xc(%r12,%r13,2), %ecx
leaq (%r14,%rcx,8), %rsi
movslq %eax, %r14
leaq (,%r14,8), %rdx
callq 0x21f0
addq %r14, %r15
movq 0x8(%rsp), %r14
addq $0x8, %r13
jmp 0xcb87
movq 0x10(%rsp), %rax
shll $0x3, %eax
movslq %eax, %rdx
movq %r14, %r13
movq 0x38(%rsp), %r14
movq %r14, %rsi
callq 0x21f0
shlq $0x3, %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x21f0
movq 0x100(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x736b
movq %r12, %rbx
jmp 0xcc20
movq $0x0, 0x8(%rsp)
movq 0x38(%rsp), %r14
movq %r13, %rdi
movq %rbx, %rsi
callq 0x736b
movq %r14, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x736b
movq 0x8(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x64a5(%rip), %rdi      # 0x130fc
leaq 0x64ae(%rip), %rsi      # 0x1310c
leaq 0x64af(%rip), %rcx      # 0x13114
pushq $0x48
popq %rdx
callq 0x2150