// File: /workspace/github_c_makefile_star2_O0/lh3[P]minigraph/lchain.c
// Function: mg_lchain_dp
mg128_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, mg128_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, n_iter = 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;
	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, 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);
			++n_iter;
			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;
	}
	if (mg_dbg_flag & MG_DBG_LC_PROF) fprintf(stderr, "LP\tn_iter=%ld\tmmax_f=%d\n", (long)n_iter, mmax_f);

	u = mg_chain_backtrack(km, n, f, p, v, t, min_cnt, min_sc, max_drop, 0, &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 $0xf0, %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)
movq $0x0, -0x90(%rbp)
cmpq $0x0, 0x40(%rbp)
je 0x22218
movq 0x40(%rbp), %rax
movq $0x0, (%rax)
movq 0x38(%rbp), %rax
movl $0x0, (%rax)
cmpq $0x0, 0x28(%rbp)
je 0x22226
cmpq $0x0, 0x30(%rbp)
jne 0x22240
movq 0x48(%rbp), %rdi
movq 0x30(%rbp), %rsi
callq 0x2670
movq $0x0, -0x18(%rbp)
jmp 0x228a2
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x2224e
movl -0x24(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x22262
cmpl $0x0, 0x18(%rbp)
jne 0x22262
movl -0x24(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, 0x18(%rbp)
je 0x2226f
movl $0x7fffffff, -0x60(%rbp) # imm = 0x7FFFFFFF
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2890
movq %rax, -0x68(%rbp)
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2890
movq %rax, -0x40(%rbp)
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2890
movq %rax, -0x50(%rbp)
movq 0x48(%rbp), %rdi
movq 0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x2540
movq %rax, -0x48(%rbp)
movq $0x0, -0x70(%rbp)
movq $-0x1, -0x80(%rbp)
movq -0x70(%rbp), %rax
cmpq 0x28(%rbp), %rax
jge 0x2278b
movq $-0x1, -0xa0(%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, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
movq -0x88(%rbp), %rcx
xorl %eax, %eax
cmpq -0x70(%rbp), %rcx
movb %al, -0xbd(%rbp)
jge 0x223b5
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, -0xbe(%rbp)
jne 0x223a9
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, -0xbe(%rbp)
movb -0xbe(%rbp), %al
movb %al, -0xbd(%rbp)
movb -0xbd(%rbp), %al
testb $0x1, %al
jne 0x223c1
jmp 0x223d8
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x2231a
movq -0x70(%rbp), %rax
subq -0x88(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x223fe
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 0x22543
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 0x228c0
movl %eax, -0xb4(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
cmpl $0x80000000, -0xb4(%rbp) # imm = 0x80000000
jne 0x22485
jmp 0x22532
movq -0x40(%rbp), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0xb4(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0xac(%rbp), %eax
jle 0x224db
movl -0xb4(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpl $0x0, -0xb0(%rbp)
jle 0x224d9
movl -0xb0(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0x22508
movq -0x48(%rbp), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x70(%rbp), %rcx
cmpl %ecx, %eax
jne 0x22506
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
cmpl -0x28(%rbp), %eax
jle 0x22504
jmp 0x22543
jmp 0x22506
jmp 0x22508
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x22530
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 0x22532
movq -0x78(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x2240a
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x22582
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 0x225eb
movl $0x80000000, -0xb8(%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 0x225e9
movl -0xb8(%rbp), %eax
movq -0x40(%rbp), %rcx
movq -0x78(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x225d9
movq -0x40(%rbp), %rax
movq -0x78(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xb8(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x225db
movq -0x78(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x225a0
jmp 0x225eb
cmpq $0x0, -0x80(%rbp)
jl 0x22699
movq -0x80(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jge 0x22699
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 0x228c0
movl %eax, -0xbc(%rbp)
cmpl $0x80000000, -0xbc(%rbp) # imm = 0x80000000
je 0x22697
movl -0xac(%rbp), %eax
movl -0xbc(%rbp), %ecx
movq -0x40(%rbp), %rdx
movq -0x80(%rbp), %rsi
addl (%rdx,%rsi,4), %ecx
cmpl %ecx, %eax
jge 0x22697
movl -0xbc(%rbp), %eax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movl %eax, -0xac(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x22699
movl -0xac(%rbp), %edx
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0xa0(%rbp), %rdx
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0xa0(%rbp)
jl 0x226f3
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xac(%rbp), %eax
jle 0x226f3
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xc4(%rbp)
jmp 0x226ff
movl -0xac(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %edx
movq -0x50(%rbp), %rax
movq -0x70(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
cmpq $0x0, -0x80(%rbp)
jl 0x2275c
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 0x22764
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 0x22764
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0xac(%rbp), %eax
jge 0x22778
movl -0xac(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x2277a
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x222d4
leaq 0x22ef6(%rip), %rax     # 0x45688
movl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x227c0
movq 0x22853(%rip), %rax     # 0x44ff8
movq (%rax), %rdi
movq -0x90(%rbp), %rdx
movl -0x5c(%rbp), %ecx
leaq 0x1aa68(%rip), %rsi     # 0x3d221
movb $0x0, %al
callq 0x2210
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
xorl %eax, %eax
leaq -0x54(%rbp), %r10
leaq -0x58(%rbp), %rax
movl %r14d, (%rsp)
movl %ebx, 0x8(%rsp)
movl %r11d, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x21af0
movq %rax, -0x98(%rbp)
movl -0x54(%rbp), %ecx
movq 0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x98(%rbp), %rcx
movq 0x40(%rbp), %rax
movq %rcx, (%rax)
movq 0x48(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x2670
movq 0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2670
movq 0x48(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x2670
cmpl $0x0, -0x54(%rbp)
jne 0x22880
movq 0x48(%rbp), %rdi
movq 0x30(%rbp), %rsi
callq 0x2670
movq 0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x2670
movq $0x0, -0x18(%rbp)
jmp 0x228a2
movq 0x48(%rbp), %rdi
movl -0x54(%rbp), %esi
movq -0x98(%rbp), %rdx
movl -0x58(%rbp), %ecx
movq -0x50(%rbp), %r8
movq 0x30(%rbp), %r9
callq 0x22b70
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
addq $0xf0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)