// File: /workspace/github_c_makefile_star2_O2/lh3[P]minigraph/lchain.c
// Function: mg_lchain_rmq
mg128_t *mg_lchain_rmq(int max_dist, int max_dist_inner, int bw, int max_chn_skip, int cap_rmq_size, int min_cnt, int min_sc, float chn_pen_gap, float chn_pen_skip,
					   int64_t n, mg128_t *a, int *n_u_, uint64_t **_u, void *km)
{
	int32_t *f,*t, *v, n_u, n_v, mmax_f = 0, max_rmq_size = 0, max_drop = bw;
	int64_t *p, i, i0, st = 0, st_inner = 0, n_iter = 0;
	uint64_t *u;
	lc_elem_t *root = 0, *root_inner = 0;
	void *mem_mp = 0;
	kmp_rmq_t *mp;

	if (_u) *_u = 0, *n_u_ = 0;
	if (n == 0 || a == 0) {
		kfree(km, a);
		return 0;
	}
	if (max_dist < bw) max_dist = bw;
	if (max_dist_inner <= 0 || max_dist_inner >= max_dist) max_dist_inner = 0;
	KMALLOC(km, p, n);
	KMALLOC(km, f, n);
	KCALLOC(km, t, n);
	KMALLOC(km, v, n);
	mem_mp = km_init2(km, 0x10000);
	mp = kmp_init_rmq(mem_mp);

	// fill the score and backtrack arrays
	for (i = i0 = 0; i < n; ++i) {
		int64_t max_j = -1;
		int32_t q_span = a[i].y>>32&0xff, max_f = q_span;
		lc_elem_t s, *q, *r, lo, hi;
		// add in-range anchors
		if (i0 < i && a[i0].x != a[i].x) {
			int64_t j;
			for (j = i0; j < i; ++j) {
				q = kmp_alloc_rmq(mp);
				q->y = (int32_t)a[j].y, q->i = j, q->pri = -(f[j] + 0.5 * chn_pen_gap * ((int32_t)a[j].x + (int32_t)a[j].y));
				krmq_insert(lc_elem, &root, q, 0);
				if (max_dist_inner > 0) {
					r = kmp_alloc_rmq(mp);
					*r = *q;
					krmq_insert(lc_elem, &root_inner, r, 0);
				}
			}
			i0 = i;
		}
		// get rid of active chains out of range
		while (st < i && (a[i].x>>32 != a[st].x>>32 || a[i].x > a[st].x + max_dist || krmq_size(head, root) > cap_rmq_size)) {
			s.y = (int32_t)a[st].y, s.i = st;
			if ((q = krmq_find(lc_elem, root, &s, 0)) != 0) {
				q = krmq_erase(lc_elem, &root, q, 0);
				kmp_free_rmq(mp, q);
			}
			++st;
		}
		if (max_dist_inner > 0)  { // similar to the block above, but applied to the inner tree
			while (st_inner < i && (a[i].x>>32 != a[st_inner].x>>32 || a[i].x > a[st_inner].x + max_dist_inner || krmq_size(head, root_inner) > cap_rmq_size)) {
				s.y = (int32_t)a[st_inner].y, s.i = st_inner;
				if ((q = krmq_find(lc_elem, root_inner, &s, 0)) != 0) {
					q = krmq_erase(lc_elem, &root_inner, q, 0);
					kmp_free_rmq(mp, q);
				}
				++st_inner;
			}
		}
		// RMQ
		lo.i = INT32_MAX, lo.y = (int32_t)a[i].y - max_dist;
		hi.i = 0, hi.y = (int32_t)a[i].y - 1;
		if ((q = krmq_rmq(lc_elem, root, &lo, &hi)) != 0) {
			int32_t sc, exact, width, n_skip = 0;
			int64_t j = q->i;
			assert(q->y >= lo.y && q->y <= hi.y);
			sc = f[j] + comput_sc_simple(&a[i], &a[j], chn_pen_gap, chn_pen_skip, &exact, &width);
			if (width <= bw && sc > max_f) max_f = sc, max_j = j;
			if (!exact && root_inner && (int32_t)a[i].y > 0) {
				lc_elem_t *lo, *hi;
				s.y = (int32_t)a[i].y - 1, s.i = n;
				krmq_interval(lc_elem, root_inner, &s, &lo, &hi);
				if (lo) {
					const lc_elem_t *q;
					int32_t width, n_rmq_iter = 0;
					krmq_itr_t(lc_elem) itr;
					krmq_itr_find(lc_elem, root_inner, lo, &itr);
					while ((q = krmq_at(&itr)) != 0) {
						if (q->y < (int32_t)a[i].y - max_dist_inner) break;
						++n_rmq_iter;
						j = q->i;
						sc = f[j] + comput_sc_simple(&a[i], &a[j], chn_pen_gap, chn_pen_skip, 0, &width);
						if (width <= bw) {
							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_chn_skip)
									break;
							}
							if (p[j] >= 0) t[p[j]] = i;
						}
						if (!krmq_itr_prev(lc_elem, &itr)) break;
					}
					n_iter += n_rmq_iter;
				}
			}
		}
		// set max
		assert(max_j < 0 || (a[max_j].x < a[i].x && (int32_t)a[max_j].y < (int32_t)a[i].y));
		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 (max_rmq_size < krmq_size(head, root)) max_rmq_size = krmq_size(head, root);
	}
	if (mg_dbg_flag & MG_DBG_LC_PROF) fprintf(stderr, "LP\tn_iter=%ld\tmmax_f=%d\trmq_size=%d\tmp_max=%ld\n", (long)n_iter, mmax_f, max_rmq_size, mp->max);
	km_destroy(mem_mp);

	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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movss %xmm1, 0x54(%rsp)
movss %xmm0, 0x24(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, 0xa0(%rsp)
movq %rdx, 0x60(%rsp)
movq 0x400(%rsp), %rax
movq 0x3f8(%rsp), %rcx
movq 0x3f0(%rsp), %r14
movq 0x3e8(%rsp), %r13
andq $0x0, 0x80(%rsp)
andq $0x0, 0x78(%rsp)
testq %rax, %rax
je 0x149d1
andq $0x0, (%rax)
andl $0x0, (%rcx)
movq 0x408(%rsp), %r12
testq %r13, %r13
setne %al
testq %r14, %r14
setne %cl
testb %cl, %al
jne 0x149f4
movq %r12, %rdi
movq %r14, %rsi
jmp 0x1523f
movq %r9, 0xd0(%rsp)
movq 0x60(%rsp), %rax
movl %edi, %ebp
cmpl %eax, %edi
cmovlel %eax, %ebp
xorl %r15d, %r15d
testl %esi, %esi
movl $0x0, %eax
cmovgl %esi, %eax
cmpl %esi, %ebp
cmovlel %r15d, %eax
movq %rax, 0x68(%rsp)
leaq (,%r13,8), %rsi
movq %r12, %rdi
callq 0x266c
movq %rax, 0x58(%rsp)
leaq (,%r13,4), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x266c
movq %rax, 0x28(%rsp)
pushq $0x4
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x24f0
movq %rax, 0x88(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x266c
movq %rax, 0x38(%rsp)
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdi
callq 0x24ac
movq %rax, %rbx
pushq $0x1
popq %rsi
pushq $0x28
popq %rdx
movq %rax, %rdi
callq 0x24f0
movq %rax, %rdx
movq %rbx, 0xc8(%rsp)
movq %rbx, 0x20(%rax)
cvtss2sd 0x24(%rsp), %xmm0
mulsd 0xf79a(%rip), %xmm0     # 0x24248
movsd %xmm0, 0xe0(%rsp)
movl %ebp, 0xa8(%rsp)
movslq %ebp, %rbp
testq %r13, %r13
movl $0x0, %eax
cmovgq %r13, %rax
movq %rax, 0xd8(%rsp)
leaq 0x8(%r14), %rax
movq %rax, 0x98(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movq $0x0, 0x70(%rsp)
movq $0x0, 0xb8(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x20(%rsp)
movl 0xc(%rsp), %r12d
movq %rdx, 0x48(%rsp)
movq %rbp, 0xc0(%rsp)
cmpq 0xd8(%rsp), %r15
je 0x15145
movq %r15, %rax
shlq $0x4, %rax
movq 0x3f0(%rsp), %rcx
leaq (%rcx,%rax), %rsi
movzbl 0xc(%rcx,%rax), %eax
movl %eax, 0x8(%rsp)
cmpq %r15, %r14
movq %rsi, 0x30(%rsp)
jge 0x14b71
movq %r14, %r12
shlq $0x4, %r12
movq (%rcx,%r12), %rax
cmpq (%rsi), %rax
jne 0x14b7b
movq %r14, 0x10(%rsp)
movl 0xc(%rsp), %r12d
jmp 0x14c5b
movq %r14, 0x10(%rsp)
jmp 0x14c5b
movq %r13, 0x40(%rsp)
addq 0x98(%rsp), %r12
cmpq %r15, %r14
jge 0x14c4c
movq %r15, %rbx
movq %rdx, %rdi
movq %rdx, %r15
callq 0x152c8
movq %r14, %rdx
movq %rax, %r14
movl (%r12), %eax
movl %eax, (%r14)
movq %rdx, 0x8(%r14)
movq 0x28(%rsp), %rcx
movq %rdx, %r13
cvtsi2sdl (%rcx,%rdx,4), %xmm0
addl -0x8(%r12), %eax
cvtsi2sd %eax, %xmm1
mulsd 0xe0(%rsp), %xmm1
addsd %xmm0, %xmm1
xorpd 0xf675(%rip), %xmm1     # 0x24250
movlpd %xmm1, 0x10(%r14)
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x14141
cmpq $0x0, 0x68(%rsp)
je 0x14c37
movq %r15, %rdi
callq 0x152c8
movupd (%r14), %xmm0
movupd 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movupd %xmm0, (%rax)
movupd %xmm1, 0x10(%rax)
movups %xmm2, 0x20(%rax)
movq 0x30(%r14), %rcx
movq %rcx, 0x30(%rax)
leaq 0x78(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x14141
movq %r13, %r14
incq %r14
addq $0x10, %r12
movq %r15, %rdx
movq %rbx, %r15
jmp 0x14b88
movq %r15, 0x10(%rsp)
movl 0xc(%rsp), %r12d
movq 0x40(%rsp), %r13
movq %r13, %r14
shlq $0x4, %r14
addq 0x98(%rsp), %r14
cmpq %r15, %r13
jge 0x14cfd
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq -0x8(%r14), %rcx
movq %rcx, %rdx
xorq %rax, %rdx
shrq $0x20, %rdx
setne %dl
addq %rbp, %rcx
cmpq %rcx, %rax
seta %al
orb %dl, %al
movq 0x80(%rsp), %rdi
jne 0x14cac
testq %rdi, %rdi
je 0x14cfd
cmpl %r12d, 0x34(%rdi)
jbe 0x14cfd
movl (%r14), %eax
movl %eax, 0xf0(%rsp)
movq %r13, 0xf8(%rsp)
leaq 0xf0(%rsp), %rsi
xorl %edx, %edx
callq 0x13e37
testq %rax, %rax
je 0x14cf1
leaq 0x80(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x144d0
movq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x152f5
incq %r13
addq $0x10, %r14
jmp 0x14c6a
cmpq $0x0, 0x68(%rsp)
je 0x14db5
movq 0x70(%rsp), %rax
movq %rax, %r14
shlq $0x4, %r14
addq 0x98(%rsp), %r14
movq %rax, 0x70(%rsp)
cmpq %r15, %rax
jge 0x14db5
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq -0x8(%r14), %rcx
movq %rcx, %rdx
xorq %rax, %rdx
shrq $0x20, %rdx
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rdx
jne 0x14d62
addq 0x68(%rsp), %rcx
cmpq %rcx, %rax
ja 0x14d62
testq %rdi, %rdi
je 0x14db5
cmpl %r12d, 0x34(%rdi)
jbe 0x14db5
movl (%r14), %eax
movl %eax, 0xf0(%rsp)
movq %rdx, 0xf8(%rsp)
leaq 0xf0(%rsp), %rsi
xorl %edx, %edx
callq 0x13e37
testq %rax, %rax
je 0x14da4
leaq 0x78(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x144d0
movq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x152f5
movq 0x70(%rsp), %rax
incq %rax
addq $0x10, %r14
jmp 0x14d1d
movq $0x7fffffff, 0x170(%rsp) # imm = 0x7FFFFFFF
movq 0x30(%rsp), %rax
movl 0x8(%rax), %r14d
movl %r14d, %r12d
subl 0xa8(%rsp), %r12d
andq $0x0, 0x138(%rsp)
movl %r12d, 0x168(%rsp)
leal -0x1(%r14), %ebx
movl %ebx, 0x130(%rsp)
movq 0x80(%rsp), %rdi
movq %rdi, 0x40(%rsp)
leaq 0x168(%rsp), %rsi
leaq 0x130(%rsp), %rdx
callq 0x13f3e
testq %rax, %rax
je 0x15047
movl %ebx, 0xa4(%rsp)
movl (%rax), %ecx
cmpl %r12d, %ecx
jl 0x152a9
cmpl %r14d, %ecx
jge 0x152a9
movq 0x8(%rax), %rbp
movq 0x28(%rsp), %rax
movl (%rax,%rbp,4), %r12d
movq %rbp, %rsi
shlq $0x4, %rsi
addq 0x3f0(%rsp), %rsi
movq 0x30(%rsp), %rdi
movss 0x24(%rsp), %xmm0
movss 0x54(%rsp), %xmm1
leaq 0xb4(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
callq 0x15357
movl %eax, %ebx
addl %r12d, %ebx
movq 0x60(%rsp), %rax
cmpl %eax, 0xb0(%rsp)
setle %al
movl 0x8(%rsp), %edx
cmpl %edx, %ebx
setg %cl
testb %cl, %al
pushq $-0x1
popq %rax
cmoveq %rax, %rbp
movq %rbp, %rax
cmovel %edx, %ebx
cmpl $0x0, 0xb4(%rsp)
jne 0x1507f
movq 0x78(%rsp), %r12
testq %r12, %r12
je 0x1507f
testl %r14d, %r14d
jle 0x1507f
movq %rax, 0x90(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0xf0(%rsp)
movq 0x3e8(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq %r12, %rdi
leaq 0xf0(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
leaq 0x128(%rsp), %rcx
callq 0x13eb4
movq 0xe8(%rsp), %rsi
testq %rsi, %rsi
je 0x15077
movq %r12, %rdi
leaq 0x1a0(%rsp), %r14
movq %r14, %rdx
callq 0x1487e
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x3a0(%rsp), %rax
cmpq %r14, %rax
jb 0x15066
movq (%rax), %rax
testq %rax, %rax
je 0x15066
movq 0x30(%rsp), %rdi
movl 0x8(%rdi), %ecx
subl 0x68(%rsp), %ecx
cmpl %ecx, (%rax)
jl 0x15066
movq 0x8(%rax), %r14
movq 0x28(%rsp), %rax
movl (%rax,%r14,4), %r12d
movq %r14, %rsi
shlq $0x4, %rsi
addq 0x3f0(%rsp), %rsi
movss 0x24(%rsp), %xmm0
movss 0x54(%rsp), %xmm1
xorl %edx, %edx
leaq 0xac(%rsp), %rcx
callq 0x15357
movl %eax, %ecx
movq 0x60(%rsp), %rax
cmpl %eax, 0xac(%rsp)
jle 0x14fc3
movl %ebx, %r12d
leaq 0x1a0(%rsp), %r14
jmp 0x1502e
addl %r12d, %ecx
cmpl %ebx, %ecx
jle 0x14fe8
movl %ecx, %r12d
xorl %eax, %eax
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
setg %al
subl %eax, %ecx
movl %ecx, 0x8(%rsp)
movq %r14, 0x90(%rsp)
jmp 0x1500c
movq 0x88(%rsp), %rax
cmpl %r15d, (%rax,%r14,4)
jne 0x15009
movl 0x8(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jge 0x15064
incl %eax
movl %eax, 0x8(%rsp)
movl %ebx, %r12d
movq 0x58(%rsp), %rax
movq (%rax,%r14,8), %rax
testq %rax, %rax
leaq 0x1a0(%rsp), %r14
js 0x1502e
movq 0x88(%rsp), %rcx
movl %r15d, (%rcx,%rax,4)
movq %r14, %rdi
xorl %esi, %esi
callq 0x148ec
incl %ebp
movl %r12d, %ebx
testl %eax, %eax
jne 0x14f3e
jmp 0x15069
pushq $-0x1
popq %rsi
movl 0x8(%rsp), %ecx
movl %ecx, %ebx
movq 0x48(%rsp), %rdx
movq 0x10(%rsp), %r14
movq 0x40(%rsp), %rbp
jmp 0x150f3
incl %ebp
movl %ebx, %r12d
movslq %ebp, %rax
addq %rax, 0xb8(%rsp)
movl %r12d, %ebx
movq 0x90(%rsp), %rax
movq 0x10(%rsp), %r14
testq %rax, %rax
movq 0x30(%rsp), %rsi
js 0x150e4
movq %rax, %rdi
shlq $0x4, %rax
movq 0x3f0(%rsp), %rdx
movq (%rdx,%rax), %rcx
cmpq (%rsi), %rcx
jae 0x1528a
addq %rdx, %rax
movl 0x8(%rax), %eax
cmpl 0x8(%rsi), %eax
jge 0x1528a
movq 0x28(%rsp), %rax
movl %ebx, (%rax,%r15,4)
movq 0x58(%rsp), %rax
movq %rdi, (%rax,%r15,8)
movq 0x38(%rsp), %rax
movl (%rax,%rdi,4), %ecx
cmpl %ebx, %ecx
cmovlel %ebx, %ecx
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rbp
jmp 0x1510a
movl %ebx, %ecx
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rbp
movq %rax, %rsi
movq 0x28(%rsp), %rax
movl %ecx, (%rax,%r15,4)
movq 0x58(%rsp), %rax
movq %rsi, (%rax,%r15,8)
movq 0x38(%rsp), %rax
movl %ecx, (%rax,%r15,4)
movl 0x20(%rsp), %eax
cmpl %ebx, %eax
cmovlel %ebx, %eax
movl %eax, 0x20(%rsp)
testq %rbp, %rbp
je 0x15130
movl 0x34(%rbp), %eax
movl 0x1c(%rsp), %ecx
cmpl %eax, %ecx
cmovbel %eax, %ecx
movl %ecx, 0x1c(%rsp)
movl 0xc(%rsp), %r12d
movq 0xc0(%rsp), %rbp
incq %r15
jmp 0x14b1e
leaq 0x1952c(%rip), %rax     # 0x2e678
testb $-0x80, (%rax)
jne 0x15258
movq 0xc8(%rsp), %rdi
callq 0x253c
movq 0x408(%rsp), %r14
movq %r14, %rdi
movq 0x3e8(%rsp), %rsi
movq 0x28(%rsp), %r13
movq %r13, %rdx
movq 0x58(%rsp), %r12
movq %r12, %rcx
movq 0x38(%rsp), %r8
movq 0x88(%rsp), %r15
movq %r15, %r9
leaq 0xf0(%rsp), %rax
pushq %rax
leaq 0x1a8(%rsp), %rax
pushq %rax
pushq $0x0
pushq 0x78(%rsp)
movl 0x400(%rsp), %eax
pushq %rax
pushq 0xf8(%rsp)
callq 0x13168
addq $0x30, %rsp
movq %rax, %rbx
movl 0x1a0(%rsp), %ebp
movq 0x3f8(%rsp), %rax
movl %ebp, (%rax)
movq 0x400(%rsp), %rax
movq %rbx, (%rax)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2577
movq %r14, %rdi
movq %r13, %rsi
callq 0x2577
movq %r14, %rdi
movq %r15, %rsi
callq 0x2577
testl %ebp, %ebp
movq 0x3f0(%rsp), %r9
je 0x1522c
movl 0xf0(%rsp), %ecx
movq %r14, %rdi
movl %ebp, %esi
movq %rbx, %rdx
movq 0x38(%rsp), %r8
callq 0x13c46
jmp 0x15246
movq %r14, %rdi
movq %r9, %rsi
callq 0x2577
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x2577
xorl %eax, %eax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18d99(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
movq 0x10(%rdx), %r9
leaq 0xf1a6(%rip), %rsi      # 0x24413
movq 0xb8(%rsp), %rdx
movl 0x20(%rsp), %ecx
movl 0x1c(%rsp), %r8d
xorl %eax, %eax
callq 0x2210
jmp 0x15155
leaq 0xf136(%rip), %rdi      # 0x243c7
leaq 0xefd8(%rip), %rsi      # 0x24270
leaq 0xf0b0(%rip), %rcx      # 0x2434f
movl $0x163, %edx            # imm = 0x163
callq 0x2160
leaq 0xf082(%rip), %rdi      # 0x24332
leaq 0xefb9(%rip), %rsi      # 0x24270
leaq 0xf091(%rip), %rcx      # 0x2434f
movl $0x141, %edx            # imm = 0x141
callq 0x2160