// File: /workspace/github_c_makefile_star2_O0/lh3[P]minimap2/lchain.c
// Function: mg_lchain_dp
mm128_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, mm128_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;
	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;
	p = Kmalloc(km, int64_t, n);
	f = Kmalloc(km, int32_t, n);
	v = Kmalloc(km, int32_t, n);
	t = Kcalloc(km, int32_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);
			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;
		//fprintf(stderr, "X1\t%ld\t%ld:%d\t%ld\t%ld:%d\t%ld\t%ld\n", (long)i, (long)(a[i].x>>32), (int32_t)a[i].x, (long)max_j, max_j<0?-1L:(long)(a[max_j].x>>32), max_j<0?-1:(int32_t)a[max_j].x, (long)max_f, (long)v[i]);
	}

	u = mg_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 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
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)
movss %xmm1, -0x38(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x60(%rbp)
movq $0x0, -0x88(%rbp)
cmpq $0x0, 0x40(%rbp)
je 0x1ee3d
movq 0x40(%rbp), %rax
movq $0x0, (%rax)
movq 0x38(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, 0x28(%rbp)
je 0x1ee4b
cmpq $0x0, 0x30(%rbp)
jne 0x1ee65
movq 0x48(%rbp), %rdi
movq 0x30(%rbp), %rsi
callq 0x1a8c0
movq $0x0, -0x18(%rbp)
jmp 0x1f476
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1ee73
movl -0x24(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1ee87
cmpl $0x0, 0x18(%rbp)
jne 0x1ee87
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, 0x18(%rbp)
je 0x1ee94
movl $0x7fffffff, -0x60(%rbp) # imm = 0x7FFFFFFF
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
shlq $0x3, %rsi
callq 0x1aae0
movq %rax, -0x68(%rbp)
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
shlq $0x2, %rsi
callq 0x1aae0
movq %rax, -0x40(%rbp)
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
shlq $0x2, %rsi
callq 0x1aae0
movq %rax, -0x50(%rbp)
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x1a790
movq %rax, -0x48(%rbp)
movq $0x0, -0x70(%rbp)
movq $-0x1, -0x80(%rbp)
movq -0x70(%rbp), %rax
cmpq 0x28(%rbp), %rax
jge 0x1f39e
movq $-0x1, -0x98(%rbp)
movq 0x30(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
andq $0xff, %rax
movl %eax, -0xa4(%rbp)
movl $0x0, -0xa8(%rbp)
movq -0x88(%rbp), %rcx
xorl %eax, %eax
cmpq -0x70(%rbp), %rcx
movb %al, -0xb5(%rbp)
jge 0x1efda
movq 0x30(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
shrq $0x20, %rcx
movq 0x30(%rbp), %rax
movq -0x88(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rax), %rdx
shrq $0x20, %rdx
movb $0x1, %al
cmpq %rdx, %rcx
movb %al, -0xb6(%rbp)
jne 0x1efce
movq 0x30(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
movq -0x88(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
movslq -0x1c(%rbp), %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
seta %al
movb %al, -0xb6(%rbp)
movb -0xb6(%rbp), %al
movb %al, -0xb5(%rbp)
movb -0xb5(%rbp), %al
testb $0x1, %al
jne 0x1efe6
jmp 0x1effd
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x1ef3f
movq -0x70(%rbp), %rax
subq -0x88(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x1f023
movq -0x70(%rbp), %rax
movslq -0x2c(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x70(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x88(%rbp), %rax
jl 0x1f156
movq 0x30(%rbp), %rdi
movq -0x70(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x30(%rbp), %rsi
movq -0x78(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %r8d
movss -0x34(%rbp), %xmm0
movss -0x38(%rbp), %xmm1
movl 0x18(%rbp), %r9d
movl 0x20(%rbp), %eax
movl %eax, (%rsp)
callq 0x1f490
movl %eax, -0xac(%rbp)
cmpl $0x80000000, -0xac(%rbp) # imm = 0x80000000
jne 0x1f098
jmp 0x1f145
movq -0x40(%rbp), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0xac(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jle 0x1f0ee
movl -0xac(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpl $0x0, -0xa8(%rbp)
jle 0x1f0ec
movl -0xa8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x1f11b
movq -0x48(%rbp), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x70(%rbp), %rcx
cmpl %ecx, %eax
jne 0x1f119
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
cmpl -0x28(%rbp), %eax
jle 0x1f117
jmp 0x1f156
jmp 0x1f119
jmp 0x1f11b
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x1f143
movq -0x70(%rbp), %rax
movl %eax, %edx
movq -0x48(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x78(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1f145
movq -0x78(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x1f02f
movq -0x78(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x1f195
movq 0x30(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
movq -0x80(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movslq -0x1c(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x1f1fe
movl $0x80000000, -0xb0(%rbp) # imm = 0x80000000
movq $-0x1, -0x80(%rbp)
movq -0x70(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x88(%rbp), %rax
jl 0x1f1fc
movl -0xb0(%rbp), %eax
movq -0x40(%rbp), %rcx
movq -0x78(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x1f1ec
movq -0x40(%rbp), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xb0(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x1f1ee
movq -0x78(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x1f1b3
jmp 0x1f1fe
cmpq $0x0, -0x80(%rbp)
jl 0x1f2ac
movq -0x80(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jge 0x1f2ac
movq 0x30(%rbp), %rdi
movq -0x70(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x30(%rbp), %rsi
movq -0x80(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
movl -0x24(%rbp), %r8d
movss -0x34(%rbp), %xmm0
movss -0x38(%rbp), %xmm1
movl 0x18(%rbp), %r9d
movl 0x20(%rbp), %eax
movl %eax, (%rsp)
callq 0x1f490
movl %eax, -0xb4(%rbp)
cmpl $0x80000000, -0xb4(%rbp) # imm = 0x80000000
je 0x1f2aa
movl -0xa4(%rbp), %eax
movl -0xb4(%rbp), %ecx
movq -0x40(%rbp), %rdx
movq -0x80(%rbp), %rsi
addl (%rdx,%rsi,4), %ecx
cmpl %ecx, %eax
jge 0x1f2aa
movl -0xb4(%rbp), %eax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movl %eax, -0xa4(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x1f2ac
movl -0xa4(%rbp), %edx
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x98(%rbp), %rdx
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0x98(%rbp)
jl 0x1f306
movq -0x50(%rbp), %rax
movq -0x98(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa4(%rbp), %eax
jle 0x1f306
movq -0x50(%rbp), %rax
movq -0x98(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xbc(%rbp)
jmp 0x1f312
movl -0xa4(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %edx
movq -0x50(%rbp), %rax
movq -0x70(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpq $0x0, -0x80(%rbp)
jl 0x1f36f
movq 0x30(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
movq -0x80(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movslq -0x1c(%rbp), %rcx
cmpq %rcx, %rax
ja 0x1f377
movq -0x40(%rbp), %rax
movq -0x80(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x40(%rbp), %rcx
movq -0x70(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x1f377
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jge 0x1f38b
movl -0xa4(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x1f38d
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1eef9
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x68(%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 0x1e730
movq %rax, -0x90(%rbp)
movl -0x54(%rbp), %ecx
movq 0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x90(%rbp), %rcx
movq 0x40(%rbp), %rax
movq %rcx, (%rax)
movq 0x48(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x1a8c0
movq 0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x1a8c0
movq 0x48(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x1a8c0
cmpl $0x0, -0x54(%rbp)
jne 0x1f454
movq 0x48(%rbp), %rdi
movq 0x30(%rbp), %rsi
callq 0x1a8c0
movq 0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x1a8c0
movq $0x0, -0x18(%rbp)
jmp 0x1f476
movq 0x48(%rbp), %rdi
movl -0x54(%rbp), %esi
movq -0x90(%rbp), %rdx
movl -0x58(%rbp), %ecx
movq -0x50(%rbp), %r8
movq 0x30(%rbp), %r9
callq 0x1f740
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0xe0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)