// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movss %xmm0, 0x8(%rsp)
movl %ecx, 0x60(%rsp)
movl %edx, (%rsp)
movq 0x108(%rsp), %r15
movq 0x100(%rsp), %rax
movq 0xf8(%rsp), %rbp
testq %r15, %r15
je 0xdda8
movq $0x0, (%r15)
movl $0x0, (%rax)
movq 0x110(%rsp), %rbx
movq 0xf0(%rsp), %r14
testq %r14, %r14
sete %al
testq %rbp, %rbp
sete %cl
orb %al, %cl
je 0xddd0
xorl %r15d, %r15d
jmp 0xe644
movl %r8d, 0x50(%rsp)
movl %r9d, 0x5c(%rsp)
movl (%rsp), %ecx
cmpl %ecx, %edi
cmovlel %ecx, %edi
movl %edi, 0x30(%rsp)
cmpl %ecx, %esi
movl %ecx, %eax
cmovgl %esi, %eax
cmpl $0x0, 0xd8(%rsp)
cmovnel %esi, %eax
movl %eax, 0x28(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovel %ecx, %eax
movl %eax, 0x44(%rsp)
leaq (,%r14,8), %rsi
movq %rbx, %rdi
callq 0x84a5
movq %rax, 0x18(%rsp)
movq %rbx, %r12
leaq (,%r14,4), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x84a5
movq %rax, %r13
movq %r12, %rdi
movq %rbx, %rsi
callq 0x84a5
movq %rax, %rbp
movl $0x4, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x8344
movq %rax, 0x10(%rsp)
testq %r14, %r14
jle 0xe401
movq %rbx, 0x68(%rsp)
movq %r13, 0x20(%rsp)
movl 0xe8(%rsp), %eax
movl %eax, %eax
movq %rax, 0x80(%rsp)
movl 0x30(%rsp), %edx
movslq %edx, %rax
movq %rax, 0x78(%rsp)
movslq 0x50(%rsp), %rax
movq %rax, 0x88(%rsp)
xorl %r13d, %r13d
movq $-0x1, 0x70(%rsp)
movq $-0x1, %rbx
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0xf8(%rsp), %rsi
movq %rbp, 0x38(%rsp)
cmpq %r15, %r13
jge 0xdef3
movl 0x4(%rsi,%r15,8), %eax
movq 0x80(%rsp), %rcx
movq 0x78(%rsp), %r9
movl 0x4(%rsi,%r13,8), %r8d
movq %rax, %rdi
subq %r8, %rdi
shlq %cl, %rdi
cmpq %r9, %rdi
jbe 0xdef3
incq %r13
cmpq %r15, %r13
jl 0xded3
movq %r15, %rbp
jmp 0xdef6
movq %r13, %rbp
xorl %edi, %edi
movq $-0x1, %rcx
testq %rbx, %rbx
js 0xdf8b
movq $-0x1, 0x48(%rsp)
movl 0xe0(%rsp), %eax
movl %eax, %r12d
cmpq %rbp, %rbx
jl 0xdf9e
movq (%rsi,%r15,8), %rdi
movq (%rsi,%rbx,8), %rsi
movl 0x28(%rsp), %ecx
movl (%rsp), %r8d
movss 0x8(%rsp), %xmm0
movl 0xd8(%rsp), %r9d
movl 0xe0(%rsp), %eax
movq %rax, %r13
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
callq 0xe683
movl 0x40(%rsp), %edx
movq 0x108(%rsp), %rsi
addq $0x10, %rsp
movl %eax, %r12d
addl %r14d, %r12d
cmpl %r13d, %r12d
movq $-0x1, %rax
cmovgq %rbx, %rax
movq %rax, 0x48(%rsp)
cmovlel %r13d, %r12d
movl %r14d, %edi
movq %rbx, %rcx
jmp 0xdf9e
movq $-0x1, 0x48(%rsp)
movl 0xe0(%rsp), %eax
movl %eax, %r12d
movl %edi, 0x64(%rsp)
movq %rcx, 0x90(%rsp)
movq %r15, %rax
subq %rbp, %rax
movq %r15, %r13
movq 0x88(%rsp), %rcx
subq %rcx, %r13
cmpq %rcx, %rax
cmovleq %rbp, %r13
cmpq %r13, %r15
jle 0xe0a6
movq (%rsi,%r15,8), %rax
movq %rax, 0x50(%rsp)
xorl %ebx, %ebx
movq 0x70(%rsp), %rbp
movq (%rsi,%rbp,8), %rsi
movq 0x50(%rsp), %rdi
movl 0x28(%rsp), %ecx
movl (%rsp), %r8d
movss 0x8(%rsp), %xmm0
movl 0xd8(%rsp), %r9d
movl 0xe0(%rsp), %eax
pushq %rax
movl 0xf0(%rsp), %eax
pushq %rax
callq 0xe683
addq $0x10, %rsp
movl %eax, %r14d
negl %eax
jno 0xe02e
movl %r12d, %r14d
movq 0xf8(%rsp), %rsi
movl 0x30(%rsp), %edx
jmp 0xe091
movq 0x20(%rsp), %rax
addl (%rax,%rbp,4), %r14d
cmpl %r12d, %r14d
jle 0xe058
xorl %eax, %eax
testl %ebx, %ebx
setg %al
subl %eax, %ebx
movq %rbp, 0x48(%rsp)
movq 0xf8(%rsp), %rsi
movl 0x30(%rsp), %edx
jmp 0xe07a
movq 0x10(%rsp), %rax
cmpl %r15d, (%rax,%rbp,4)
movq 0xf8(%rsp), %rsi
movl 0x30(%rsp), %edx
jne 0xe077
cmpl 0x60(%rsp), %ebx
jge 0xe0a6
incl %ebx
movl %r12d, %r14d
movq 0x18(%rsp), %rax
movq (%rax,%rbp,8), %rax
testq %rax, %rax
js 0xe091
movq 0x10(%rsp), %rcx
movl %r15d, (%rcx,%rax,4)
leaq -0x1(%rbp), %rax
movl %r14d, %r12d
cmpq %r13, %rbp
movq %rax, %rbp
jg 0xdfde
jmp 0xe0a9
movl %r12d, %r14d
movq 0x20(%rsp), %rax
movl %r14d, (%rax,%r15,4)
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, (%rax,%r15,8)
testq %rcx, %rcx
movq 0x38(%rsp), %rbp
js 0xe0d3
movl (%rbp,%rcx,4), %eax
cmpl %r14d, %eax
jg 0xe0d6
movl %r14d, %eax
movl %eax, (%rbp,%r15,4)
movl 0x64(%rsp), %eax
cmpl %r14d, %eax
cmovgl %eax, %r14d
movq 0x90(%rsp), %rbx
cmovlq %r15, %rbx
incq %r15
incq 0x70(%rsp)
cmpq 0xf0(%rsp), %r15
jne 0xdebc
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
movq 0x108(%rsp), %r15
movq 0x100(%rsp), %r12
jle 0xe426
xorl %eax, %eax
xorl %r14d, %r14d
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %r13
movl 0xd0(%rsp), %edx
xorl %ecx, %ecx
cmpl %edx, (%r13,%rax,4)
setge %cl
addq %rcx, %r14
incq %rax
cmpq %rax, %rdi
jne 0xe13f
testq %r14, %r14
je 0xe448
movq %r14, %rbx
shlq $0x4, %rbx
movq 0x110(%rsp), %rdi
movq %rbx, %rsi
callq 0x84a5
movq %rax, %r12
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%r13,%rax,4), %rdx
cmpl 0xd0(%rsp), %edx
jl 0xe19c
movq %rcx, %rsi
shlq $0x4, %rsi
movq %rdx, (%r12,%rsi)
incq %rcx
movq %rax, 0x8(%r12,%rsi)
incq %rax
cmpq %rax, 0xf0(%rsp)
jne 0xe17b
addq %r12, %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4ca8
xorl %r13d, %r13d
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
movq 0x68(%rsp), %rdx
callq 0x2160
decq %r14
movslq 0x5c(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r14, %rbx
movl $0x0, (%rsp)
movq %rbx, %r15
shlq $0x4, %r15
movq 0x8(%r12,%r15), %rax
cmpl $0x0, (%rbp,%rax,4)
jne 0xe276
addq %r12, %r15
movl 0x44(%rsp), %edi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rbp, %r8
movq %rbx, %r9
callq 0xe86d
movq 0x18(%rsp), %rsi
movq 0x8(%r15), %rdx
movq %r13, %rcx
cmpq %rax, %rdx
je 0xe23f
movq %r13, %rcx
incq %rcx
movl $0x1, (%rbp,%rdx,4)
movq (%rsi,%rdx,8), %rdx
cmpq %rax, %rdx
jne 0xe22b
movl (%r15), %edx
testq %rax, %rax
js 0xe24f
movq 0x20(%rsp), %rsi
subl (%rsi,%rax,4), %edx
movq 0x10(%rsp), %rbp
cmpl 0xd0(%rsp), %edx
jl 0xe276
movq %rcx, %rax
subq %r13, %rax
jle 0xe276
xorl %edx, %edx
cmpq 0x30(%rsp), %rax
setge %dl
cmovgeq %rcx, %r13
addl %edx, (%rsp)
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0xe1e5
movslq (%rsp), %rsi
shlq $0x3, %rsi
movq 0x110(%rsp), %rbx
movq %rbx, %rdi
callq 0x84a5
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movq 0x68(%rsp), %rdx
callq 0x2160
movq 0x10(%rsp), %rsi
movq $0x0, 0x28(%rsp)
movq 0x38(%rsp), %rbp
movq %r14, %r15
shlq $0x4, %r15
movq 0x8(%r12,%r15), %rax
cmpl $0x0, (%rsi,%rax,4)
jne 0xe3a5
movq %rbx, %r13
addq %r12, %r15
movl 0x44(%rsp), %edi
movq %r12, %rsi
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movq %r14, %r9
callq 0xe86d
movq 0x10(%rsp), %rsi
movq 0x8(%r15), %rdx
movq 0x8(%rsp), %rdi
movq %rdi, %rcx
cmpq %rax, %rdx
je 0xe33c
movq %rdi, %rcx
movl %edx, (%rbp,%rcx,4)
incq %rcx
movl $0x1, (%rsi,%rdx,4)
movq (%rbx,%rdx,8), %rdx
cmpq %rax, %rdx
jne 0xe325
movl (%r15), %edx
testq %rax, %rax
js 0xe34c
movq 0x20(%rsp), %rsi
subl (%rsi,%rax,4), %edx
movl 0xd0(%rsp), %eax
movq %r13, %rbx
movq (%rsp), %r8
cmpl %eax, %edx
setge %sil
movq %rcx, %rax
subq %rdi, %rax
setg %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0xe3a0
cmpq 0x30(%rsp), %rax
movq 0x10(%rsp), %rsi
jl 0xe3a5
shlq $0x20, %rdx
orq %rax, %rdx
movq 0x28(%rsp), %rdi
movslq %edi, %rax
incl %edi
movq %rdi, 0x28(%rsp)
movq %rdx, (%r8,%rax,8)
movq %rcx, 0x8(%rsp)
jmp 0xe3a5
movq 0x10(%rsp), %rsi
leaq -0x1(%r14), %rax
testq %r14, %r14
movq %rax, %r14
jg 0xe2cf
movq %rbx, %rdi
movq %r12, %rsi
callq 0x83df
movq 0x8(%rsp), %rax
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
jge 0xe664
cltq
shlq $0x3, %rax
movq %rax, 0x8(%rsp)
movq 0x108(%rsp), %r15
movq 0x100(%rsp), %r12
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r14
movq (%rsp), %rax
jmp 0xe45e
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movq 0x18(%rsp), %rsi
movq 0x110(%rsp), %rbx
xorl %r14d, %r14d
movq 0x100(%rsp), %r12
jmp 0xe45e
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movq 0x110(%rsp), %rbx
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %r13
jmp 0xe45e
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movq 0x110(%rsp), %rbx
movl %r14d, (%r12)
movq %rax, (%rsp)
movq %rax, (%r15)
movq %rbx, %rdi
callq 0x83df
movq %rbx, %rdi
movq %r13, %rsi
callq 0x83df
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x83df
testl %r14d, %r14d
je 0xe5f3
movq %r14, %r12
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x84a5
movq %rax, %r15
movslq %r12d, %r13
testl %r12d, %r12d
movq 0xf8(%rsp), %r9
movq (%rsp), %r10
jle 0xe4f0
movq %rbp, %rax
addq $-0x4, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%r10,%rcx,8), %rsi
testq %rsi, %rsi
jle 0xe4e8
movslq %edx, %rdi
leaq (%rax,%rdi,4), %rdi
movslq (%rdi,%rsi,4), %r8
movq (%r9,%r8,8), %r8
movq %r8, (%r15,%rdx,8)
incq %rdx
decq %rsi
jne 0xe4d4
incq %rcx
cmpq %r13, %rcx
jne 0xe4c4
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x83df
movq %r13, %r14
shlq $0x4, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x84a5
movq (%rsp), %rdi
testl %r12d, %r12d
jle 0xe547
leaq 0x8(%rax), %r8
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x4(%r15,%rdx,8), %esi
movq %rsi, -0x8(%r8)
movq %rdx, %rsi
shlq $0x20, %rsi
orq %rcx, %rsi
movq %rsi, (%r8)
movslq (%rdi,%rcx,8), %rsi
addq %rsi, %rdx
incq %rcx
addq $0x10, %r8
cmpq %rcx, %r13
jne 0xe51e
addq %rax, %r14
movq %rax, %rdi
movq %r14, %rsi
movq %rax, %r14
callq 0x4ca8
leaq (,%r13,8), %rsi
movq %rbx, %rdi
callq 0x84a5
movq %rax, %rbp
testl %r12d, %r12d
movq %r14, 0x8(%rsp)
jle 0xe600
movq %r12, 0x28(%rsp)
movq %r14, %r12
addq $0x8, %r12
movq %rbp, 0x38(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
movq %r14, %rbx
movslq (%r12), %rax
movq (%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq 0x38(%rsp), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0xf8(%rsp), %rcx
leaq (%rcx,%r14,8), %rdi
movl 0x4(%r12), %ecx
leaq (%r15,%rcx,8), %rsi
movslq %eax, %r14
leaq (,%r14,8), %rdx
callq 0x21e0
addq %rbx, %r14
incq %rbp
addq $0x10, %r12
cmpq %rbp, %r13
jne 0xe58f
shlq $0x3, %r14
movq 0x110(%rsp), %rbx
movq 0x38(%rsp), %rbp
movq 0x28(%rsp), %r12
jmp 0xe603
xorl %r15d, %r15d
movq 0xf8(%rsp), %rsi
jmp 0xe63c
xorl %r14d, %r14d
movq (%rsp), %rdi
shll $0x3, %r12d
movslq %r12d, %rdx
movq %rbp, %rsi
callq 0x21e0
movq %r15, %rdi
movq 0xf8(%rsp), %r12
movq %r12, %rsi
movq %r14, %rdx
callq 0x21e0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x83df
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x83df
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x83df
movq %r15, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6a91(%rip), %rdi      # 0x150fc
leaq 0x6a9a(%rip), %rsi      # 0x1510c
leaq 0x6a9b(%rip), %rcx      # 0x15114
movl $0x48, %edx
callq 0x2150