// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0xe0, %rsp
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movl %edi, -0x1c(%rbp)
movl %esi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movl %ecx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq $0x0, -0x88(%rbp)
movq $-0x1, -0x90(%rbp)
cmpq $0x0, 0x48(%rbp)
je 0x18f5d
movq 0x48(%rbp), %rax
movq $0x0, (%rax)
movq 0x40(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, 0x30(%rbp)
je 0x18f6b
cmpq $0x0, 0x38(%rbp)
jne 0x18f85
movq 0x50(%rbp), %rdi
movq 0x38(%rbp), %rsi
callq 0xac90
movq $0x0, -0x18(%rbp)
jmp 0x19453
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x18f93
movl -0x24(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x18fa7
cmpl $0x0, 0x18(%rbp)
jne 0x18fa7
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, 0x18(%rbp)
je 0x18fb4
movl $0x7fffffff, -0x60(%rbp) # imm = 0x7FFFFFFF
movq 0x50(%rbp), %rdi
movq 0x30(%rbp), %rsi
shlq $0x3, %rsi
callq 0xaeb0
movq %rax, -0x70(%rbp)
movq 0x50(%rbp), %rdi
movq 0x30(%rbp), %rsi
shlq $0x2, %rsi
callq 0xaeb0
movq %rax, -0x40(%rbp)
movq 0x50(%rbp), %rdi
movq 0x30(%rbp), %rsi
shlq $0x2, %rsi
callq 0xaeb0
movq %rax, -0x50(%rbp)
movq 0x50(%rbp), %rdi
movq 0x30(%rbp), %rsi
movl $0x4, %edx
callq 0xab60
movq %rax, -0x48(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x30(%rbp), %rax
jge 0x1937b
movq $-0x1, -0xa0(%rbp)
movl 0x20(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl $0x0, -0xa8(%rbp)
movq -0x88(%rbp), %rcx
xorl %eax, %eax
cmpq -0x78(%rbp), %rcx
movb %al, -0xb1(%rbp)
jge 0x1908e
movq 0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq 0x38(%rbp), %rcx
movq -0x88(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
subq %rcx, %rax
movl 0x28(%rbp), %ecx
shlq %cl, %rax
movslq -0x1c(%rbp), %rcx
cmpq %rcx, %rax
seta %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x1909a
jmp 0x190ae
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x1903d
cmpq $0x0, -0x90(%rbp)
jl 0x19155
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
jl 0x19155
movl -0x64(%rbp), %eax
movl %eax, -0xb8(%rbp)
movq 0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x38(%rbp), %rax
movq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %r8d
movss -0x34(%rbp), %xmm0
movl 0x18(%rbp), %r9d
movl 0x28(%rbp), %r10d
movl 0x20(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x19470
movl %eax, %ecx
movl -0xb8(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jle 0x19153
movl -0xac(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x19167
movl $0x0, -0x64(%rbp)
movq $-0x1, -0x90(%rbp)
movq -0x78(%rbp), %rax
subq -0x88(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x1918d
movq -0x78(%rbp), %rax
movslq -0x2c(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jl 0x192be
movq 0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq 0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %r8d
movss -0x34(%rbp), %xmm0
movl 0x18(%rbp), %r9d
movl 0x28(%rbp), %r10d
movl 0x20(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x19470
movl %eax, -0xb0(%rbp)
cmpl $0x80000000, -0xb0(%rbp) # imm = 0x80000000
jne 0x19200
jmp 0x192ad
movq -0x40(%rbp), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jle 0x19256
movl -0xb0(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpl $0x0, -0xa8(%rbp)
jle 0x19254
movl -0xa8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x19283
movq -0x48(%rbp), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x78(%rbp), %rcx
cmpl %ecx, %eax
jne 0x19281
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
cmpl -0x28(%rbp), %eax
jle 0x1927f
jmp 0x192be
jmp 0x19281
jmp 0x19283
movq -0x70(%rbp), %rax
movq -0x80(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x192ab
movq -0x78(%rbp), %rax
movl %eax, %edx
movq -0x48(%rbp), %rax
movq -0x70(%rbp), %rcx
movq -0x80(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x192ad
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x19199
movl -0xa4(%rbp), %edx
movq -0x40(%rbp), %rax
movq -0x78(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0xa0(%rbp), %rdx
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0xa0(%rbp)
jl 0x19318
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa4(%rbp), %eax
jle 0x19318
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xbc(%rbp)
jmp 0x19324
movl -0xa4(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %edx
movq -0x50(%rbp), %rax
movq -0x78(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x5c(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jge 0x19349
movl -0xa4(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x64(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jge 0x19368
movl -0xa4(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x1936a
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x19011
movq 0x50(%rbp), %rdi
movq 0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x48(%rbp), %r9
movl -0x30(%rbp), %r14d
movl 0x10(%rbp), %ebx
movl -0x60(%rbp), %r11d
leaq -0x54(%rbp), %r10
leaq -0x58(%rbp), %rax
movl %r14d, (%rsp)
movl %ebx, 0x8(%rsp)
movl %r11d, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x197c0
movq %rax, -0x98(%rbp)
movl -0x54(%rbp), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x98(%rbp), %rcx
movq 0x48(%rbp), %rax
movq %rcx, (%rax)
movq 0x50(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0xac90
movq 0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xac90
movq 0x50(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xac90
cmpl $0x0, -0x54(%rbp)
jne 0x19431
movq 0x50(%rbp), %rdi
movq 0x38(%rbp), %rsi
callq 0xac90
movq 0x50(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xac90
movq $0x0, -0x18(%rbp)
jmp 0x19453
movq 0x50(%rbp), %rdi
movl -0x54(%rbp), %esi
movq -0x98(%rbp), %rdx
movl -0x58(%rbp), %ecx
movq -0x50(%rbp), %r8
movq 0x38(%rbp), %r9
callq 0x19cb0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0xe0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)