// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r8d, %r15d
movl %ecx, 0x68(%rsp)
movl %edx, 0x20(%rsp)
movq 0x148(%rsp), %rbx
movq 0x140(%rsp), %rdx
movq 0x138(%rsp), %r10
movq 0x130(%rsp), %rbp
movq 0x128(%rsp), %r13
movl 0x120(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x118(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x110(%rsp), %r8d
testq %rdx, %rdx
je 0x14ef4
movq $0x0, (%rdx)
movl $0x0, (%r10)
testq %r13, %r13
setne %al
testq %rbp, %rbp
setne %cl
testb %cl, %al
jne 0x14f0f
movq %rbx, %rdi
movq %rbp, %rsi
jmp 0x15692
movq %r8, 0x90(%rsp)
movq %r10, 0x98(%rsp)
movq %r9, 0xa0(%rsp)
movq %rdx, 0xa8(%rsp)
movl 0x20(%rsp), %eax
cmpl %eax, %edi
cmovlel %eax, %edi
movl %edi, 0x24(%rsp)
cmpl %eax, %esi
movl %eax, %ecx
cmovgl %esi, %ecx
cmpl $0x0, 0x8(%rsp)
cmovnel %esi, %ecx
movl %ecx, 0x1c(%rsp)
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmovel %eax, %ecx
movq %rcx, 0x88(%rsp)
leaq (,%r13,8), %rsi
movq %rbx, %rdi
movss %xmm1, 0x60(%rsp)
movss %xmm0, 0x5c(%rsp)
callq 0x126e9
movq %rax, 0x50(%rsp)
leaq (,%r13,4), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x126e9
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x126e9
movq %rax, %r12
movl $0x4, %edx
movq %rbx, 0xb0(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1257c
movss 0x5c(%rsp), %xmm8
movss 0x60(%rsp), %xmm7
movq %rax, 0x38(%rsp)
testq %r13, %r13
movq %r14, 0x40(%rsp)
jle 0x155bf
movslq 0x24(%rsp), %rax
movq %rax, 0x48(%rsp)
movslq %r15d, %rax
movq %rax, 0xb8(%rsp)
leaq -0x8(%rbp), %rax
movq %rax, 0x78(%rsp)
movq $-0x1, %rsi
xorl %r9d, %r9d
movss 0x183bc(%rip), %xmm0    # 0x2d3c8
movss 0x183a8(%rip), %xmm1    # 0x2d3bc
movss 0x183a4(%rip), %xmm2    # 0x2d3c0
movss 0x183a0(%rip), %xmm3    # 0x2d3c4
xorl %r8d, %r8d
movq %r13, 0x70(%rsp)
movq %rbp, 0xc8(%rsp)
movq %r12, 0xc0(%rsp)
movq %r8, %rax
shlq $0x4, %rax
leaq (%rax,%rbp), %rcx
cmpq %r8, %r9
movq %rcx, 0x80(%rsp)
jge 0x1508c
movq (%rcx), %rcx
movq %r9, %rdx
shlq $0x4, %rdx
addq %rbp, %rdx
movq 0x48(%rsp), %r11
movq (%rdx), %rdi
movq %rdi, %r10
xorq %rcx, %r10
shrq $0x20, %r10
jne 0x1507d
addq %r11, %rdi
cmpq %rdi, %rcx
jbe 0x1508c
incq %r9
addq $0x10, %rdx
cmpq %r8, %r9
jl 0x15066
movq %r8, %r9
movq %r9, %rcx
movq 0x8(%rbp,%rax), %r15
movq %r15, %rax
shrq $0x20, %rax
movq %r8, %rdx
subq %r9, %rdx
movq %r8, %r9
movq 0xb8(%rsp), %rdi
subq %rdi, %r9
cmpq %rdi, %rdx
cmovleq %rcx, %r9
movzbl %al, %eax
movl %eax, 0xc(%rsp)
leaq -0x1(%r8), %r13
cmpq %r9, %r8
jle 0x15309
movq %r15, %rax
shrq $0x30, %rax
movzbl %al, %r14d
movq $-0x1, 0x28(%rsp)
xorl %ebp, %ebp
movq 0x78(%rsp), %rdx
movq %r13, %rdi
movq %r15, 0x10(%rsp)
movq (%rdx), %rax
movl %r15d, %r12d
subl %eax, %r12d
setle %cl
cmpl 0x24(%rsp), %r12d
setg %r10b
orb %cl, %r10b
movl $0x80000000, %r11d      # imm = 0x80000000
jne 0x15256
movq %rax, %rcx
shrq $0x30, %rcx
movzbl %cl, %ebx
movq 0x80(%rsp), %rcx
movl (%rcx), %r10d
subl -0x8(%rdx), %r10d
cmpl %ebx, %r14d
jne 0x15143
cmpl 0x1c(%rsp), %r12d
jg 0x15256
testl %r10d, %r10d
je 0x15256
movl %r12d, %ecx
subl %r10d, %ecx
movl %ecx, %r15d
negl %r15d
cmovsl %ecx, %r15d
cmpl %ebx, %r14d
jne 0x15163
cmpl 0x20(%rsp), %r15d
jg 0x15256
cmpl $0x0, 0x8(%rsp)
jne 0x15181
cmpl $0x2, 0x34(%rsp)
jl 0x15181
cmpl %ebx, %r14d
jne 0x15181
cmpl %r10d, 0x1c(%rsp)
jl 0x15256
cmpl %r10d, %r12d
movl %r10d, %r11d
cmovll %r12d, %r11d
shrq $0x20, %rax
movzbl %al, %ecx
cmpl %r11d, %ecx
movl %r11d, %eax
cmovll %ecx, %eax
cmpl %r10d, %r12d
jne 0x151ad
cmpl %r11d, %ecx
jl 0x151ad
movl %eax, %r11d
jmp 0x15256
xorps %xmm4, %xmm4
cmpl %r10d, %r12d
je 0x15201
leal 0x1(%r15), %ecx
xorps %xmm4, %xmm4
cvtsi2ss %ecx, %xmm4
movd %xmm4, %ecx
movl %eax, 0x64(%rsp)
movl %ecx, %eax
shrl $0x17, %eax
addl $-0x80, %eax
cvtsi2ss %rax, %xmm5
movl 0x64(%rsp), %eax
andl $0x7fffff, %ecx         # imm = 0x7FFFFF
orl $0x3f800000, %ecx       # imm = 0x3F800000
movd %ecx, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %r15d, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %r11d, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0x8(%rsp)
jne 0x1523e
cmpl %ebx, %r14d
jne 0x1523e
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
movl %eax, %r11d
cvttss2si %xmm4, %eax
subl %eax, %r11d
jmp 0x15256
cmpl %ebx, %r14d
je 0x152e2
movl $0x1, %r11d
testl %r10d, %r10d
jne 0x152e2
movl %r11d, %eax
negl %eax
jno 0x15266
xorl %eax, %eax
movq 0x10(%rsp), %r15
jmp 0x152c8
movq 0x40(%rsp), %rax
addl (%rax,%rdi,4), %r11d
cmpl 0xc(%rsp), %r11d
jle 0x1528b
xorl %eax, %eax
testl %ebp, %ebp
setg %al
subl %eax, %ebp
movq %rdi, 0x28(%rsp)
movl %r11d, 0xc(%rsp)
jmp 0x152aa
movq 0x38(%rsp), %rax
cmpl %r8d, (%rax,%rdi,4)
jne 0x152aa
leal 0x1(%rbp), %ecx
movb $0x1, %al
cmpl 0x68(%rsp), %ebp
movl %ecx, %ebp
movq 0x10(%rsp), %r15
jl 0x152af
jmp 0x152c8
movq 0x10(%rsp), %r15
movq 0x50(%rsp), %rax
movq (%rax,%rdi,8), %rax
testq %rax, %rax
js 0x152c6
movq 0x38(%rsp), %rcx
movl %r8d, (%rcx,%rax,4)
xorl %eax, %eax
testb %al, %al
jne 0x15317
leaq -0x1(%rdi), %rax
addq $-0x10, %rdx
cmpq %r9, %rdi
movq %rax, %rdi
jg 0x150ed
jmp 0x1531a
movl %eax, %r11d
cmpl %ebx, %r14d
jne 0x152fc
cmpl %r10d, %r12d
jl 0x152fc
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
jmp 0x15235
minss %xmm4, %xmm5
cvttss2si %xmm5, %eax
jmp 0x15239
movq $-0x1, 0x28(%rsp)
movq %r13, %rax
jmp 0x1531a
movq %rdi, %rax
testq %rsi, %rsi
movq 0xc8(%rsp), %rbp
movq 0x40(%rsp), %r14
movq 0x80(%rsp), %r10
js 0x1534a
movq (%r10), %rcx
movq %rsi, %rdx
shlq $0x4, %rdx
subq (%rbp,%rdx), %rcx
cmpq 0x48(%rsp), %rcx
jbe 0x15374
movq $-0x1, %rsi
cmpq %r9, %r8
jle 0x15374
movl $0x80000000, %edx       # imm = 0x80000000
movl (%r14,%r13,4), %ecx
cmpl %ecx, %edx
cmovlq %r13, %rsi
cmovlel %ecx, %edx
leaq -0x1(%r13), %rcx
cmpq %r9, %r13
movq %rcx, %r13
jg 0x1535b
testq %rsi, %rsi
sets %cl
cmpq %rax, %rsi
setge %al
orb %cl, %al
movq 0xc0(%rsp), %r12
jne 0x153c0
movq %rsi, %rbx
shlq $0x4, %rbx
movq 0x8(%rbp,%rbx), %rax
movl %r15d, %edx
subl %eax, %edx
setle %cl
cmpl 0x24(%rsp), %edx
setg %dil
orb %cl, %dil
movl $0x80000000, %r11d      # imm = 0x80000000
movq 0x70(%rsp), %r13
je 0x153ca
movq %r10, %r15
jmp 0x15502
movq 0x70(%rsp), %r13
jmp 0x1550c
addq %rbp, %rbx
shrq $0x30, %r15
movl $0xff, %ecx
andl %ecx, %r15d
movq %rax, %rdi
shrq $0x30, %rdi
andl %ecx, %edi
movq %r15, %rcx
movq %r10, %r15
movl (%r10), %r10d
subl (%rbx), %r10d
movq %rcx, 0x10(%rsp)
cmpl %edi, %ecx
jne 0x1540a
cmpl 0x1c(%rsp), %edx
jg 0x15502
testl %r10d, %r10d
je 0x15502
movl %edx, %ecx
subl %r10d, %ecx
movl %ecx, %ebx
negl %ebx
cmovsl %ecx, %ebx
cmpl %edi, 0x10(%rsp)
jne 0x15426
cmpl 0x20(%rsp), %ebx
jg 0x15502
cmpl $0x0, 0x8(%rsp)
jne 0x15445
cmpl $0x2, 0x34(%rsp)
jl 0x15445
cmpl %edi, 0x10(%rsp)
jne 0x15445
cmpl %r10d, 0x1c(%rsp)
jl 0x15502
cmpl %r10d, %edx
movl %r10d, %r11d
cmovll %edx, %r11d
shrq $0x20, %rax
movzbl %al, %ecx
cmpl %r11d, %ecx
movl %r11d, %eax
cmovll %ecx, %eax
cmpl %r10d, %edx
jne 0x1546d
cmpl %r11d, %ecx
jge 0x154ff
xorps %xmm4, %xmm4
cmpl %r10d, %edx
je 0x154c0
leal 0x1(%rbx), %ecx
xorps %xmm4, %xmm4
cvtsi2ss %ecx, %xmm4
movd %xmm4, %ecx
movl %ecx, %r14d
shrl $0x17, %r14d
addl $-0x80, %r14d
cvtsi2ss %r14, %xmm5
movq 0x40(%rsp), %r14
andl $0x7fffff, %ecx         # imm = 0x7FFFFF
orl $0x3f800000, %ecx       # imm = 0x3F800000
movd %ecx, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %ebx, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %r11d, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0x8(%rsp)
jne 0x1558e
cmpl %edi, 0x10(%rsp)
jne 0x1558e
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %ecx
subl %ecx, %eax
movl %eax, %r11d
movl %r11d, %eax
negl %eax
jno 0x15517
movq %r15, %r10
movl 0xc(%rsp), %ecx
movq 0x28(%rsp), %rdx
jmp 0x15532
addl (%r14,%rsi,4), %r11d
movl 0xc(%rsp), %ecx
cmpl %r11d, %ecx
movq 0x28(%rsp), %rdx
cmovlq %rsi, %rdx
cmovlel %r11d, %ecx
movq %r15, %r10
movl %ecx, (%r14,%r8,4)
movq 0x50(%rsp), %rax
movq %rdx, (%rax,%r8,8)
testq %rdx, %rdx
js 0x1554c
movl (%r12,%rdx,4), %eax
cmpl %ecx, %eax
jg 0x1554e
movl %ecx, %eax
movl %eax, (%r12,%r8,4)
testq %rsi, %rsi
js 0x15577
movq (%r10), %rax
movq %rsi, %rcx
shlq $0x4, %rcx
subq (%rbp,%rcx), %rax
cmpq 0x48(%rsp), %rax
ja 0x1557a
movl (%r14,%rsi,4), %eax
cmpl (%r14,%r8,4), %eax
jge 0x1557a
movq %r8, %rsi
incq %r8
addq $0x10, 0x78(%rsp)
cmpq %r13, %r8
jne 0x1503c
jmp 0x155bf
cmpl %edi, 0x10(%rsp)
je 0x155a3
movl $0x1, %r11d
testl %r10d, %r10d
je 0x15502
cmpl %edi, 0x10(%rsp)
jne 0x155b2
cmpl %r10d, %edx
jge 0x154f1
minss %xmm4, %xmm5
cvttss2si %xmm5, %ecx
jmp 0x154fd
subq $0x8, %rsp
leaq 0x74(%rsp), %rax
movq %r14, %rdx
leaq 0xdc(%rsp), %r14
movq 0xb8(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq 0x58(%rsp), %rbx
movq %rbx, %rcx
movq %r12, %r8
movq 0x40(%rsp), %r9
pushq %rax
pushq %r14
pushq 0xa0(%rsp)
pushq 0xb0(%rsp)
pushq 0xc8(%rsp)
callq 0x14a8c
addq $0x30, %rsp
movl (%r14), %r13d
movq %r15, %r14
movq %rax, %r15
movq 0x98(%rsp), %rax
movl %r13d, (%rax)
movq 0xa8(%rsp), %rax
movq %r15, (%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1261e
movq %r14, %rdi
movq 0x40(%rsp), %rsi
callq 0x1261e
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x1261e
testl %r13d, %r13d
je 0x15681
movl 0x6c(%rsp), %ecx
movq %r14, %rdi
movl %r13d, %esi
movq %r15, %rdx
movq %r12, %r8
movq %rbp, %r9
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x156ab
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1261e
movq %r14, %rdi
movq %r12, %rsi
callq 0x1261e
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq