// File: /workspace/github_c_makefile_star2_O1/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, (%rsp)
movl %r8d, %ebp
movl %ecx, 0x5c(%rsp)
movl %edx, 0x8(%rsp)
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rcx
movq 0xe8(%rsp), %r14
movq 0xe0(%rsp), %r13
testq %rax, %rax
je 0xdb2f
movq $0x0, (%rax)
movl $0x0, (%rcx)
movq 0x100(%rsp), %rbx
testq %r13, %r13
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
je 0xdb4f
xorl %r15d, %r15d
jmp 0xe317
movl %r9d, 0x54(%rsp)
movl 0x8(%rsp), %ecx
cmpl %ecx, %edi
cmovlel %ecx, %edi
movl %edi, 0x18(%rsp)
cmpl %ecx, %esi
movl %ecx, %eax
cmovgl %esi, %eax
cmpl $0x0, 0xc8(%rsp)
cmovnel %esi, %eax
movl %eax, 0x28(%rsp)
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovel %ecx, %eax
movl %eax, 0x44(%rsp)
leaq (,%r13,8), %rsi
movq %rbx, %rdi
callq 0x80a1
movq %rax, 0x30(%rsp)
leaq (,%r13,4), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x80a1
movq %rax, %r15
movq %rbx, %rdi
movq %r12, 0x60(%rsp)
movq %r12, %rsi
callq 0x80a1
movq %rax, 0x20(%rsp)
movl $0x4, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7f34
movq %rax, 0x10(%rsp)
testq %r13, %r13
movq %r15, 0x38(%rsp)
jle 0xde28
movl 0xd8(%rsp), %eax
movl %eax, %eax
movq %rax, 0x70(%rsp)
movslq 0x18(%rsp), %rax
movq %rax, 0x68(%rsp)
movslq %ebp, %rax
movq %rax, 0x78(%rsp)
xorl %r12d, %r12d
movq $-0x1, %r13
xorl %ebx, %ebx
xorl %r8d, %r8d
cmpq %rbx, %r12
jge 0xdc49
movl 0x4(%r14,%rbx,8), %eax
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %rdi
movl 0x4(%r14,%r12,8), %esi
movq %rax, %rdx
subq %rsi, %rdx
shlq %cl, %rdx
cmpq %rdi, %rdx
jbe 0xdc49
incq %r12
cmpq %rbx, %r12
jl 0xdc29
movq %rbx, %rsi
jmp 0xdc4c
movq %r12, %rsi
xorl %r15d, %r15d
movq $-0x1, %rcx
testq %r13, %r13
js 0xdce6
movl $0x0, %r12d
movq $-0x1, 0x48(%rsp)
movl 0xd0(%rsp), %eax
movl %eax, %ebp
cmpq %rsi, %r13
jl 0xdcfb
movq (%r14,%rbx,8), %rdi
movq %rsi, %rbp
movq (%r14,%r13,8), %rsi
movl 0x18(%rsp), %edx
movl 0x28(%rsp), %ecx
movl %r8d, %r12d
movl 0x8(%rsp), %r8d
movss (%rsp), %xmm0
movl 0xc8(%rsp), %r9d
movl 0xd0(%rsp), %eax
pushq %rax
movl 0xe0(%rsp), %eax
pushq %rax
callq 0xe356
movq %rbp, %rsi
addq $0x10, %rsp
movl %eax, %ebp
addl %r12d, %ebp
movl 0xd0(%rsp), %ecx
cmpl %ecx, %ebp
movq $-0x1, %rax
cmovgq %r13, %rax
movq %rax, 0x48(%rsp)
cmovlel %ecx, %ebp
movq %r13, %rcx
jmp 0xdcfb
xorl %r12d, %r12d
movq $-0x1, 0x48(%rsp)
movl 0xd0(%rsp), %eax
movl %eax, %ebp
movl %r12d, 0x58(%rsp)
movq %rcx, 0x80(%rsp)
movq %rbx, %rax
subq %rsi, %rax
movq %rbx, %r12
movq 0x78(%rsp), %rcx
subq %rcx, %r12
cmpq %rcx, %rax
cmovleq %rsi, %r12
movq %rbx, %r13
cmpq %r12, %r13
jle 0xddcf
movq (%r14,%rbx,8), %rdi
movq -0x8(%r14,%r13,8), %rsi
movl 0x18(%rsp), %edx
movl 0x28(%rsp), %ecx
movl 0x8(%rsp), %r8d
movss (%rsp), %xmm0
movl 0xc8(%rsp), %r9d
movl 0xd0(%rsp), %eax
pushq %rax
movl 0xe0(%rsp), %eax
pushq %rax
callq 0xe356
addq $0x10, %rsp
movl %eax, %ecx
negl %ecx
jo 0xddc2
movq 0x38(%rsp), %rcx
addl -0x4(%rcx,%r13,4), %eax
cmpl %ebp, %eax
movq 0x10(%rsp), %rdx
jle 0xdd99
leaq -0x1(%r13), %rcx
movq %rcx, 0x48(%rsp)
xorl %ecx, %ecx
testl %r15d, %r15d
setg %cl
subl %ecx, %r15d
movl %eax, %ebp
jmp 0xddb0
cmpl %ebx, -0x4(%rdx,%r13,4)
jne 0xddb0
leal 0x1(%r15), %ecx
movb $0x1, %al
cmpl 0x5c(%rsp), %r15d
movl %ecx, %r15d
jge 0xddc4
movq 0x30(%rsp), %rax
movq -0x8(%rax,%r13,8), %rax
testq %rax, %rax
js 0xddc2
movl %ebx, (%rdx,%rax,4)
xorl %eax, %eax
decq %r13
testb %al, %al
je 0xdd23
movq 0x38(%rsp), %r15
movl %ebp, (%r15,%rbx,4)
movq 0x30(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, (%rax,%rbx,8)
movq %rcx, %rax
testq %rcx, %rcx
movq 0x20(%rsp), %rcx
js 0xddfa
movl (%rcx,%rax,4), %eax
cmpl %ebp, %eax
jg 0xddfc
movl %ebp, %eax
movl %eax, (%rcx,%rbx,4)
movl 0x58(%rsp), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
movq 0x80(%rsp), %r13
cmovlq %rbx, %r13
incq %rbx
movl %ebp, %r8d
cmpq 0xe0(%rsp), %rbx
jne 0xdc15
movl 0xc0(%rsp), %r13d
movq $0x0, 0x18(%rsp)
movq 0xe0(%rsp), %r12
testq %r12, %r12
jle 0xde60
xorl %eax, %eax
xorl %ebp, %ebp
xorl %ecx, %ecx
cmpl %r13d, (%r15,%rax,4)
setge %cl
addq %rcx, %rbp
incq %rax
cmpq %rax, %r12
jne 0xde4a
jmp 0xde62
xorl %ebp, %ebp
testq %rbp, %rbp
je 0xdfc1
movq %rbp, %rbx
shlq $0x4, %rbx
movq 0x100(%rsp), %rdi
movq %rbx, %rsi
callq 0x80a1
movq %rax, %rdi
testq %r12, %r12
jle 0xdeb2
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%r15,%rax,4), %rdx
cmpl %r13d, %edx
jl 0xdeaa
movq %rcx, %rsi
shlq $0x4, %rsi
movq %rdx, (%rdi,%rsi)
incq %rcx
movq %rax, 0x8(%rdi,%rsi)
incq %rax
cmpq %rax, %r12
jne 0xde8e
addq %rdi, %rbx
movq %rdi, (%rsp)
movq %rbx, %rsi
callq 0x4928
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movq 0x60(%rsp), %rdx
callq 0x2160
leaq -0x1(%rbp), %r15
movslq 0x54(%rsp), %rax
movq %rax, 0x28(%rsp)
testq %rbp, %rbp
jle 0xdfe3
xorl %r13d, %r13d
movq %r15, %rbx
movl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r8
movq (%rsp), %r9
movq %rbx, %r12
shlq $0x4, %r12
movq 0x8(%r9,%r12), %rax
cmpl $0x0, (%r8,%rax,4)
jne 0xdfa6
addq %r9, %r12
movl 0x44(%rsp), %edi
movq %r9, %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rbx, %r9
callq 0xe52f
movq 0x8(%r12), %rdx
movq %r13, %rcx
cmpq %rax, %rdx
jne 0xdf50
movq 0x10(%rsp), %r8
jmp 0xdf6e
movq 0x30(%rsp), %rsi
movq 0x10(%rsp), %r8
incq %rcx
movl $0x1, (%r8,%rdx,4)
movq (%rsi,%rdx,8), %rdx
cmpq %rax, %rdx
jne 0xdf5a
movl (%r12), %eax
testq %rdx, %rdx
movq (%rsp), %r9
js 0xdf83
movq 0x38(%rsp), %rsi
subl (%rsi,%rdx,4), %eax
cmpl 0xc0(%rsp), %eax
jl 0xdfa6
movq %rcx, %rax
subq %r13, %rax
jle 0xdfa6
xorl %edx, %edx
cmpq 0x28(%rsp), %rax
setge %dl
cmovgeq %rcx, %r13
addl %edx, 0x8(%rsp)
leaq -0x1(%rbx), %rax
testq %rbx, %rbx
movq %rax, %rbx
jg 0xdf09
movslq 0x8(%rsp), %rsi
shlq $0x3, %rsi
jmp 0xdfe5
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x100(%rsp), %rbx
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rcx
jmp 0xe13c
xorl %esi, %esi
movq 0x100(%rsp), %rbx
movq %rbx, %rdi
callq 0x80a1
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movq 0x60(%rsp), %rdx
callq 0x2160
testq %rbp, %rbp
jle 0xe105
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %r8
movq (%rsp), %rsi
movq %r15, %r12
shlq $0x4, %r12
movq 0x8(%rsi,%r12), %rax
cmpl $0x0, (%r8,%rax,4)
jne 0xe0cc
addq %rsi, %r12
movl 0x44(%rsp), %edi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %r15, %r9
callq 0xe52f
movq 0x8(%r12), %rcx
movq %r13, %rbp
cmpq %rax, %rcx
jne 0xe06d
movq 0x10(%rsp), %r8
jmp 0xe093
movq 0x20(%rsp), %rdx
movq 0x30(%rsp), %rsi
movq 0x10(%rsp), %r8
movl %ecx, (%rdx,%rbp,4)
incq %rbp
movl $0x1, (%r8,%rcx,4)
movq (%rsi,%rcx,8), %rcx
cmpq %rax, %rcx
jne 0xe07c
movl (%r12), %eax
testq %rcx, %rcx
movq (%rsp), %rsi
js 0xe0a8
movq 0x38(%rsp), %rdx
subl (%rdx,%rcx,4), %eax
cmpl 0xc0(%rsp), %eax
setge %dl
movq %rbp, %rcx
subq %r13, %rcx
setg %dil
andb %dl, %dil
cmpb $0x1, %dil
jne 0xe0cc
cmpq 0x28(%rsp), %rcx
jge 0xe0e4
movq %r13, %rbp
leaq -0x1(%r15), %rax
movq %rbp, %r13
testq %r15, %r15
movq %rax, %r15
jg 0xe029
jmp 0xe10b
shlq $0x20, %rax
orq %rcx, %rax
movq 0x18(%rsp), %rdx
movslq %edx, %rcx
incl %edx
movq %rdx, 0x18(%rsp)
movq 0x8(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
jmp 0xe0cf
xorl %ebp, %ebp
movq (%rsp), %rsi
movq %rbx, %rdi
callq 0x7fd6
cmpq $0x7fffffff, %rbp       # imm = 0x7FFFFFFF
jge 0xe337
movslq %ebp, %r15
shlq $0x3, %r15
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rcx
movq 0x8(%rsp), %rbp
movq 0x18(%rsp), %r12
movl %r12d, (%rcx)
movq %rbp, (%rax)
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0x7fd6
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x7fd6
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x7fd6
testl %r12d, %r12d
je 0xe2c7
movq %rbx, %rdi
movq %r15, %rsi
callq 0x80a1
movq %rax, %r15
movslq %r12d, %r9
testl %r12d, %r12d
jle 0xe1c7
movq 0x20(%rsp), %rax
addq $-0x4, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%rbp,%rcx,8), %rsi
testq %rsi, %rsi
jle 0xe1bf
movslq %edx, %rdi
leaq (%rax,%rdi,4), %rdi
movslq (%rdi,%rsi,4), %r8
movq (%r14,%r8,8), %r8
movq %r8, (%r15,%rdx,8)
incq %rdx
decq %rsi
jne 0xe1ab
incq %rcx
cmpq %r9, %rcx
jne 0xe19a
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r9, (%rsp)
callq 0x7fd6
movq (%rsp), %r13
shlq $0x4, %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x80a1
movq (%rsp), %r8
movq %rax, %rdi
testl %r12d, %r12d
jle 0xe229
leaq 0x8(%rdi), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x4(%r15,%rdx,8), %esi
movq %rsi, -0x8(%rax)
movq %rdx, %rsi
shlq $0x20, %rsi
orq %rcx, %rsi
movq %rsi, (%rax)
movslq (%rbp,%rcx,8), %rsi
addq %rsi, %rdx
incq %rcx
addq $0x10, %rax
cmpq %rcx, %r8
jne 0xe1ff
addq %rdi, %r13
movq %r13, %rsi
movq %r8, %r13
movq %rdi, 0x28(%rsp)
callq 0x4928
leaq (,%r13,8), %rsi
movq %rbx, %rdi
callq 0x80a1
movq %rax, 0x20(%rsp)
testl %r12d, %r12d
jle 0xe2d4
movq 0x28(%rsp), %r12
addq $0x8, %r12
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %rbp, 0x8(%rsp)
movq %r13, %rbp
movslq (%r12), %rax
movq 0x8(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq 0x20(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
leaq (%r14,%r13,8), %rdi
movl 0x4(%r12), %ecx
leaq (%r15,%rcx,8), %rsi
movslq %eax, %r13
leaq (,%r13,8), %rdx
callq 0x21e0
addq %rbp, %r13
incq %rbx
addq $0x10, %r12
cmpq %rbx, (%rsp)
jne 0xe269
shlq $0x3, %r13
movq 0x100(%rsp), %rbx
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rbp
jmp 0xe2d7
xorl %r15d, %r15d
movq %r14, %rsi
movq 0x20(%rsp), %r12
jmp 0xe30c
xorl %r13d, %r13d
shll $0x3, %r12d
movslq %r12d, %rdx
movq %rbp, %rdi
movq 0x20(%rsp), %r12
movq %r12, %rsi
callq 0x21e0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x21e0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7fd6
movq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x7fd6
movq %r12, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7fd6
movq %r15, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5d6e(%rip), %rdi      # 0x140ac
leaq 0x5d77(%rip), %rsi      # 0x140bc
leaq 0x5d78(%rip), %rcx      # 0x140c4
movl $0x48, %edx
callq 0x2150