// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/lchain.c
// Function: mg_lchain_rmq
mm128_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, mm128_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;
	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 = 0;
	if (max_dist_inner > max_dist) max_dist_inner = max_dist;
	p = Kmalloc(km, int64_t, n);
	f = Kmalloc(km, int32_t, n);
	t = Kcalloc(km, int32_t, n);
	v = Kmalloc(km, int32_t, 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;
		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;
					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;
						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;
					}
				}
			}
		}
		// 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);
	}
	km_destroy(mem_mp);

	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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x398, %rsp            # imm = 0x398
movss %xmm1, 0x3c(%rsp)
movss %xmm0, 0x1c(%rsp)
movl %r8d, %ebp
movl %ecx, 0x94(%rsp)
movq %rdx, 0x50(%rsp)
movq 0x3f0(%rsp), %rax
movq 0x3e8(%rsp), %rcx
movq 0x3e0(%rsp), %rbx
movq 0x3d8(%rsp), %r15
andq $0x0, 0x78(%rsp)
andq $0x0, 0x70(%rsp)
testq %rax, %rax
je 0x1440d
andq $0x0, (%rax)
andl $0x0, (%rcx)
movq 0x3f8(%rsp), %r14
testq %r15, %r15
setne %al
testq %rbx, %rbx
setne %cl
testb %cl, %al
jne 0x14430
movq %r14, %rdi
movq %rbx, %rsi
jmp 0x14c5a
movq %r9, 0xb8(%rsp)
movq 0x50(%rsp), %rax
movl %edi, %r12d
cmpl %eax, %edi
cmovlel %eax, %r12d
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
cmpl %r12d, %esi
cmovgel %r12d, %esi
movq %rsi, 0x60(%rsp)
leaq (,%r15,8), %rsi
movq %r14, %rdi
callq 0x10afc
movq %rax, 0x40(%rsp)
movq %rbx, %r13
leaq (,%r15,4), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10afc
movq %rax, 0x28(%rsp)
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x10980
movq %rax, 0x80(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10afc
movq %rax, 0x30(%rsp)
movl $0x10000, %esi          # imm = 0x10000
movq %r14, %rdi
callq 0x1093c
movq %rax, %rbx
pushq $0x1
popq %rsi
pushq $0x28
popq %rdx
movq %rax, %rdi
callq 0x10980
xorl %esi, %esi
movq %rax, %rdi
cvtss2sd 0x1c(%rsp), %xmm0
mulsd 0x13bd8(%rip), %xmm0    # 0x280b8
movsd %xmm0, 0xc8(%rsp)
movq %rbx, 0xb0(%rsp)
movq %rbx, 0x20(%rax)
movl %r12d, 0x98(%rsp)
movslq %r12d, %rax
movq %rax, 0xd0(%rsp)
testq %r15, %r15
movl $0x0, %eax
cmovgq %r15, %rax
movq %rax, 0xc0(%rsp)
leaq 0x8(%r13), %rax
movq %rax, 0x88(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movq $0x0, 0x68(%rsp)
movl $0x0, 0x38(%rsp)
movl %ebp, 0x18(%rsp)
movq %rdi, 0x58(%rsp)
cmpq 0xc0(%rsp), %rsi
je 0x14b69
movq %rsi, %rax
shlq $0x4, %rax
leaq (%rax,%r13), %rbx
movzbl 0xc(%r13,%rax), %eax
movl %eax, 0xc(%rsp)
cmpq %rsi, %r12
movq %rsi, 0x10(%rsp)
jge 0x14591
movq %r12, %rbp
shlq $0x4, %rbp
movq (%r13,%rbp), %rax
cmpq (%rbx), %rax
jne 0x145a3
movq %r12, 0x48(%rsp)
movl 0x18(%rsp), %ebp
jmp 0x14596
movq %r12, 0x48(%rsp)
leaq 0xe0(%rsp), %r13
jmp 0x14685
movq %r15, 0x20(%rsp)
addq 0x88(%rsp), %rbp
leaq 0xe0(%rsp), %r13
cmpq %rsi, %r12
jge 0x14677
movq %rbx, %r15
movq %rdi, %r13
callq 0x14cb1
movq %rax, %r14
movl (%rbp), %eax
movl %eax, (%r14)
movq %r12, 0x8(%r14)
movq 0x28(%rsp), %rcx
cvtsi2sdl (%rcx,%r12,4), %xmm0
addl -0x8(%rbp), %eax
cvtsi2sd %eax, %xmm1
mulsd 0xc8(%rsp), %xmm1
addsd %xmm0, %xmm1
xorpd 0x14df0(%rip), %xmm1    # 0x293f0
movlpd %xmm1, 0x10(%r14)
leaq 0x78(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x13b82
cmpl $0x0, 0x60(%rsp)
jle 0x14658
movq %r13, %rdi
callq 0x14cb1
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 0x70(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x13b82
incq %r12
addq $0x10, %rbp
movq 0x10(%rsp), %rsi
movq %r13, %rdi
leaq 0xe0(%rsp), %r13
movq %r15, %rbx
jmp 0x145b8
movq %rsi, 0x48(%rsp)
movl 0x18(%rsp), %ebp
movq 0x20(%rsp), %r15
movq %r15, %r14
shlq $0x4, %r14
addq 0x88(%rsp), %r14
cmpq %rsi, %r15
jge 0x14720
movq (%rbx), %rax
movq -0x8(%r14), %rcx
movq %rcx, %rdx
xorq %rax, %rdx
shrq $0x20, %rdx
setne %dl
addq 0xd0(%rsp), %rcx
cmpq %rcx, %rax
seta %al
orb %dl, %al
movq 0x78(%rsp), %rdi
jne 0x146d2
testq %rdi, %rdi
je 0x14720
cmpl %ebp, 0x34(%rdi)
jbe 0x14720
movl (%r14), %eax
movl %eax, 0xe0(%rsp)
movq %r15, 0xe8(%rsp)
movq %r13, %rsi
xorl %edx, %edx
callq 0x13878
testq %rax, %rax
je 0x1470f
leaq 0x78(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x13f11
movq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x14cde
incq %r15
addq $0x10, %r14
movq 0x10(%rsp), %rsi
jmp 0x14694
cmpl $0x0, 0x60(%rsp)
jle 0x147d1
movq 0x68(%rsp), %rax
movq %rax, %r14
shlq $0x4, %r14
addq 0x88(%rsp), %r14
movq %rax, 0x68(%rsp)
cmpq %rsi, %rax
jge 0x147d1
movq (%rbx), %rax
movq -0x8(%r14), %rcx
movq %rcx, %rdx
xorq %rax, %rdx
shrq $0x20, %rdx
movq 0x70(%rsp), %rdi
jne 0x14779
addq 0x60(%rsp), %rcx
cmpq %rcx, %rax
ja 0x14779
testq %rdi, %rdi
je 0x147d1
cmpl %ebp, 0x34(%rdi)
jbe 0x147d1
movl (%r14), %eax
movl %eax, 0xe0(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq %r13, %rsi
xorl %edx, %edx
callq 0x13878
testq %rax, %rax
je 0x147bb
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x13f11
movq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x14cde
movq 0x68(%rsp), %rax
incq %rax
addq $0x10, %r14
movq 0x10(%rsp), %rsi
jmp 0x1473f
movq $0x7fffffff, 0x160(%rsp) # imm = 0x7FFFFFFF
movl 0x8(%rbx), %r14d
movl %r14d, %ebp
andq $0x0, 0x128(%rsp)
subl 0x98(%rsp), %ebp
movl %ebp, 0x158(%rsp)
movl %r14d, 0x120(%rsp)
movq 0x78(%rsp), %r12
movq %r12, %rdi
leaq 0x158(%rsp), %rsi
leaq 0x120(%rsp), %rdx
callq 0x1397f
testq %rax, %rax
je 0x14a7c
movl (%rax), %ecx
cmpl %ebp, %ecx
jl 0x14c92
cmpl %r14d, %ecx
jg 0x14c92
movq 0x8(%rax), %rbp
movq 0x28(%rsp), %rax
movl (%rax,%rbp,4), %r13d
movq %rbp, %rsi
shlq $0x4, %rsi
addq 0x3e0(%rsp), %rsi
movq %rbx, %rdi
movss 0x1c(%rsp), %xmm0
movss 0x3c(%rsp), %xmm1
leaq 0xa4(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
callq 0x14d40
movl %eax, %r8d
addl %r13d, %r8d
movq 0x50(%rsp), %rax
cmpl %eax, 0xa0(%rsp)
setle %al
movl 0xc(%rsp), %edx
cmpl %edx, %r8d
setg %cl
testb %cl, %al
pushq $-0x1
popq %rax
cmoveq %rax, %rbp
cmovel %edx, %r8d
cmpl $0x0, 0xa4(%rsp)
jne 0x14ab4
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
movq 0x3e0(%rsp), %r13
movq 0x10(%rsp), %rsi
je 0x14ac1
testl %r14d, %r14d
jle 0x14ac1
movl %r8d, 0xc(%rsp)
decl %r14d
movl %r14d, 0xe0(%rsp)
movq 0x3d8(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0xe0(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
movq %rdi, %r14
leaq 0x118(%rsp), %rcx
callq 0x138f5
movq 0xd8(%rsp), %rsi
testq %rsi, %rsi
je 0x14aa8
movq %rbp, 0xa8(%rsp)
movq %r14, %rdi
leaq 0x190(%rsp), %rbp
movq %rbp, %rdx
callq 0x142bf
movl $0x0, 0x20(%rsp)
movl 0xc(%rsp), %edx
movq 0x390(%rsp), %rax
cmpq %rbp, %rax
jb 0x14a93
movq (%rax), %rax
testq %rax, %rax
je 0x14a93
movl 0x8(%rbx), %ecx
subl 0x60(%rsp), %ecx
cmpl %ecx, (%rax)
jl 0x14a93
movl %edx, %ebp
movq %r13, %rcx
movq 0x8(%rax), %r13
movq 0x28(%rsp), %rax
movl (%rax,%r13,4), %eax
movl %eax, 0xc(%rsp)
movq %r13, %rsi
shlq $0x4, %rsi
addq %rcx, %rsi
movq %rbx, %rdi
movss 0x1c(%rsp), %xmm0
movss 0x3c(%rsp), %xmm1
xorl %edx, %edx
leaq 0x9c(%rsp), %rcx
callq 0x14d40
movl %eax, %r14d
movq 0x50(%rsp), %rax
cmpl %eax, 0x9c(%rsp)
jle 0x149e5
movl %ebp, %r14d
movq 0x3e0(%rsp), %r13
leaq 0x190(%rsp), %rbp
jmp 0x14a65
addl 0xc(%rsp), %r14d
cmpl %ebp, %r14d
jle 0x14a0f
xorl %eax, %eax
movl 0x20(%rsp), %ecx
testl %ecx, %ecx
setg %al
subl %eax, %ecx
movl %ecx, 0x20(%rsp)
movq %r13, 0xa8(%rsp)
movq 0x10(%rsp), %rdx
jmp 0x14a3c
movq 0x80(%rsp), %rax
movq 0x10(%rsp), %rdx
cmpl %edx, (%rax,%r13,4)
jne 0x14a39
movl 0x20(%rsp), %eax
cmpl 0x94(%rsp), %eax
jge 0x14b59
incl %eax
movl %eax, 0x20(%rsp)
movl %ebp, %r14d
movq 0x40(%rsp), %rax
movq (%rax,%r13,8), %rax
testq %rax, %rax
leaq 0x190(%rsp), %rbp
js 0x14a5d
movq 0x80(%rsp), %rcx
movl %edx, (%rcx,%rax,4)
movq 0x3e0(%rsp), %r13
movq %rbp, %rdi
xorl %esi, %esi
callq 0x1432d
movl %r14d, %edx
testl %eax, %eax
jne 0x14952
jmp 0x14a96
pushq $-0x1
popq %rbp
movq 0x10(%rsp), %rsi
movq 0x3e0(%rsp), %r13
movl 0xc(%rsp), %r8d
jmp 0x14b11
movl %edx, %r14d
movl %r14d, %r8d
movq 0x10(%rsp), %rsi
movq 0xa8(%rsp), %rbp
jmp 0x14ac1
movq 0x10(%rsp), %rsi
movl 0xc(%rsp), %r8d
jmp 0x14ac1
movq 0x3e0(%rsp), %r13
movq 0x10(%rsp), %rsi
testq %rbp, %rbp
js 0x14b11
movq %rbp, %rax
shlq $0x4, %rax
movq (%r13,%rax), %rcx
cmpq (%rbx), %rcx
jae 0x14c73
addq %r13, %rax
movl 0x8(%rax), %eax
cmpl 0x8(%rbx), %eax
jge 0x14c73
movq 0x28(%rsp), %rax
movl %r8d, (%rax,%rsi,4)
movq 0x40(%rsp), %rax
movq %rbp, (%rax,%rsi,8)
movq 0x30(%rsp), %rcx
movl (%rcx,%rbp,4), %eax
cmpl %r8d, %eax
cmovgl %eax, %r8d
movl 0x18(%rsp), %ebp
jmp 0x14b2c
movq 0x28(%rsp), %rax
movl %r8d, (%rax,%rsi,4)
movq 0x40(%rsp), %rax
movq %rbp, (%rax,%rsi,8)
movl 0x18(%rsp), %ebp
movq 0x30(%rsp), %rcx
movl %r8d, (%rcx,%rsi,4)
testq %r12, %r12
je 0x14b47
movl 0x34(%r12), %eax
movl 0x38(%rsp), %ecx
cmpl %eax, %ecx
cmovbel %eax, %ecx
movl %ecx, 0x38(%rsp)
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %r12
incq %rsi
jmp 0x14548
movl %ebp, %r14d
movq 0x3e0(%rsp), %r13
jmp 0x14a96
movq 0xb0(%rsp), %rdi
callq 0x109cc
subq $0x8, %rsp
movq 0x400(%rsp), %r13
movq %r13, %rdi
movq 0x3e0(%rsp), %rsi
movq 0x30(%rsp), %r15
movq %r15, %rdx
movq 0x48(%rsp), %r12
movq %r12, %rcx
movq 0x38(%rsp), %r8
movq 0x88(%rsp), %r14
movq %r14, %r9
leaq 0xe8(%rsp), %rax
pushq %rax
leaq 0x1a0(%rsp), %rax
pushq %rax
pushq 0x68(%rsp)
movl 0x3f0(%rsp), %eax
pushq %rax
pushq 0xe0(%rsp)
callq 0x12c2c
addq $0x30, %rsp
movq %rax, %rbx
movl 0x190(%rsp), %ebp
movq 0x3e8(%rsp), %rax
movl %ebp, (%rax)
movq 0x3f0(%rsp), %rax
movq %rbx, (%rax)
movq %r13, %rdi
movq %r12, %rsi
callq 0x10a07
movq %r13, %rdi
movq %r15, %rsi
callq 0x10a07
movq %r13, %rdi
movq %r14, %rsi
callq 0x10a07
testl %ebp, %ebp
je 0x14c42
movl 0xe0(%rsp), %ecx
movq %r13, %rdi
movl %ebp, %esi
movq %rbx, %rdx
movq 0x30(%rsp), %r8
movq 0x3e0(%rsp), %r9
callq 0x13687
jmp 0x14c61
movq %r13, %rdi
movq 0x3e0(%rsp), %rsi
callq 0x10a07
movq %r13, %rdi
movq 0x30(%rsp), %rsi
callq 0x10a07
xorl %eax, %eax
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x148cb(%rip), %rdi     # 0x29545
leaq 0x1478f(%rip), %rsi     # 0x29410
leaq 0x14845(%rip), %rcx     # 0x294cd
movl $0x160, %edx            # imm = 0x160
callq 0x21a0
leaq 0x14817(%rip), %rdi     # 0x294b0
leaq 0x14770(%rip), %rsi     # 0x29410
leaq 0x14826(%rip), %rcx     # 0x294cd
movl $0x140, %edx            # imm = 0x140
callq 0x21a0