// File: /workspace/github_c_makefile_star2_O3/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 %ecx, 0x78(%rsp)
movl %edx, 0x3c(%rsp)
movl %edi, %ebp
movq 0x148(%rsp), %rbx
movq 0x140(%rsp), %rdx
movq 0x138(%rsp), %rdi
movq 0x130(%rsp), %r15
movq 0x128(%rsp), %r13
movl 0x120(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0x118(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x110(%rsp), %r10d
testq %rdx, %rdx
je 0x14ec8
movq $0x0, (%rdx)
movl $0x0, (%rdi)
testq %r13, %r13
setne %al
testq %r15, %r15
setne %cl
testb %cl, %al
jne 0x14ee3
movq %rbx, %rdi
movq %r15, %rsi
jmp 0x156d7
movl %r8d, 0xc(%rsp)
movq %r10, 0x98(%rsp)
movq %rdi, 0xa0(%rsp)
movq %r9, 0xa8(%rsp)
movq %rdx, 0xb0(%rsp)
movl 0x3c(%rsp), %eax
cmpl %eax, %ebp
cmovlel %eax, %ebp
cmpl %eax, %esi
movl %eax, %ecx
cmovgl %esi, %ecx
cmpl $0x0, 0x1c(%rsp)
cmovnel %esi, %ecx
movl %ecx, 0x38(%rsp)
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmovel %eax, %ecx
movq %rcx, 0x90(%rsp)
leaq (,%r13,8), %rsi
movq %rbx, %rdi
movss %xmm1, 0x74(%rsp)
movss %xmm0, 0x70(%rsp)
callq 0x1264d
movq %rax, 0x58(%rsp)
leaq (,%r13,4), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1264d
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1264d
movq %rax, %r12
movl $0x4, %edx
movq %rbx, 0xb8(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x124ec
movq %r14, %rdx
movss 0x70(%rsp), %xmm8
movss 0x74(%rsp), %xmm7
movq %rax, 0x68(%rsp)
testq %r13, %r13
movq %r15, 0x60(%rsp)
movq %r14, 0x28(%rsp)
jle 0x155ff
movslq %ebp, %rbx
movslq 0xc(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq -0x8(%r15), %rax
movq %rax, 0x80(%rsp)
movq $-0x1, %rsi
xorl %r9d, %r9d
movss 0x193fd(%rip), %xmm0    # 0x2e3e4
movss 0x193e9(%rip), %xmm1    # 0x2e3d8
movss 0x193e5(%rip), %xmm2    # 0x2e3dc
movss 0x193e1(%rip), %xmm3    # 0x2e3e0
xorl %r8d, %r8d
movq %r12, 0x50(%rsp)
movl %ebp, 0x30(%rsp)
movq %r13, 0xc8(%rsp)
movq %rbx, 0x88(%rsp)
movq %r8, %rax
shlq $0x4, %rax
leaq (%r15,%rax), %rcx
cmpq %r8, %r9
movq %rcx, 0x10(%rsp)
movq %r8, 0x40(%rsp)
jge 0x15071
movq (%rcx), %rcx
movq %r9, %r11
shlq $0x4, %r11
addq %r15, %r11
movq (%r11), %rdi
movq %rdi, %r10
xorq %rcx, %r10
shrq $0x20, %r10
sete %r10b
addq %rbx, %rdi
cmpq %rdi, %rcx
setbe %dil
testb %dil, %r10b
jne 0x15071
incq %r9
addq $0x10, %r11
cmpq %r8, %r9
jl 0x15042
movq %r8, %r9
movq %r9, %rcx
movq 0x8(%r15,%rax), %r14
movq %r14, %rax
shrq $0x20, %rax
movq 0x40(%rsp), %r8
movq %r8, %rdx
subq %r9, %rdx
movq %r8, %r9
movq 0xc0(%rsp), %rdi
subq %rdi, %r9
cmpq %rdi, %rdx
cmovleq %rcx, %r9
movzbl %al, %ecx
leaq -0x1(%r8), %r12
cmpq %r9, %r8
jle 0x152f4
movq %r14, %rax
shrq $0x30, %rax
movzbl %al, %eax
movl %eax, 0xc(%rsp)
movq $-0x1, 0x20(%rsp)
movl $0x0, 0x34(%rsp)
movq 0x80(%rsp), %rdx
movq %r12, %r11
movq (%rdx), %r13
movl %r14d, %eax
subl %r13d, %eax
setle %dil
cmpl %ebp, %eax
setg %r10b
orb %dil, %r10b
je 0x150f9
movl %ecx, %ebx
jmp 0x152bf
movq %r14, %r8
movq %r13, %rdi
shrq $0x30, %rdi
movzbl %dil, %r14d
movq 0x10(%rsp), %rdi
movl (%rdi), %r15d
subl -0x8(%rdx), %r15d
cmpl %r14d, 0xc(%rsp)
jne 0x15165
cmpl 0x38(%rsp), %eax
jg 0x15230
testl %r15d, %r15d
je 0x15230
movl %eax, %edi
subl %r15d, %edi
movl %edi, %ebx
negl %ebx
cmovsl %edi, %ebx
cmpl 0x3c(%rsp), %ebx
jg 0x15230
cmpl $0x0, 0x1c(%rsp)
jne 0x15171
cmpl $0x2, 0x4c(%rsp)
jl 0x15171
cmpl %r14d, 0xc(%rsp)
jne 0x15171
cmpl %r15d, 0x38(%rsp)
jl 0x15230
jmp 0x15171
movl %eax, %edi
subl %r15d, %edi
movl %edi, %ebx
negl %ebx
cmovsl %edi, %ebx
cmpl %r15d, %eax
movl %r15d, %r10d
cmovll %eax, %r10d
shrq $0x20, %r13
movzbl %r13b, %edi
cmpl %r10d, %edi
movl %r10d, %r13d
cmovll %edi, %r13d
testl %ebx, %ebx
jne 0x1519a
cmpl %r10d, %edi
jge 0x15226
testl %ebx, %ebx
je 0x151ea
leal 0x1(%rbx), %edi
xorps %xmm4, %xmm4
cvtsi2ss %edi, %xmm4
movd %xmm4, %edi
movl %edi, %ebp
shrl $0x17, %ebp
addl $-0x80, %ebp
xorps %xmm5, %xmm5
cvtsi2ss %rbp, %xmm5
movl 0x30(%rsp), %ebp
andl $0x7fffff, %edi         # imm = 0x7FFFFF
orl $0x3f800000, %edi       # imm = 0x3F800000
movd %edi, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
jmp 0x151ed
xorps %xmm4, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %ebx, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %r10d, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0x1c(%rsp)
jne 0x1523a
cmpl %r14d, 0xc(%rsp)
jne 0x1523a
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %eax
subl %eax, %r13d
movl %r13d, %eax
negl %eax
movl %r13d, %ebx
jno 0x15253
movl %ecx, %ebx
movq %r8, %r14
jmp 0x152bf
cmpl %r14d, 0xc(%rsp)
je 0x152d7
movl $0x1, %ebx
testl %r15d, %r15d
jne 0x152d7
movq 0x28(%rsp), %rax
addl (%rax,%r11,4), %ebx
cmpl %ecx, %ebx
jle 0x1527b
xorl %eax, %eax
movl 0x34(%rsp), %ecx
testl %ecx, %ecx
setg %al
subl %eax, %ecx
movl %ecx, 0x34(%rsp)
movq %r11, 0x20(%rsp)
movq %r8, %r14
jmp 0x152a4
movq 0x68(%rsp), %rax
movq 0x40(%rsp), %rdi
cmpl %edi, (%rax,%r11,4)
movq %r8, %r14
jne 0x152a2
movl 0x34(%rsp), %eax
cmpl 0x78(%rsp), %eax
jge 0x15530
incl %eax
movl %eax, 0x34(%rsp)
movl %ecx, %ebx
movq 0x58(%rsp), %rax
movq (%rax,%r11,8), %rax
testq %rax, %rax
js 0x152bf
movq 0x68(%rsp), %rcx
movq 0x40(%rsp), %rdi
movl %edi, (%rcx,%rax,4)
leaq -0x1(%r11), %rax
addq $-0x10, %rdx
movl %ebx, %ecx
cmpq %r9, %r11
movq %rax, %r11
jg 0x150da
jmp 0x15302
cmpl %r14d, 0xc(%rsp)
jne 0x152e7
cmpl %r15d, %eax
jge 0x15217
minss %xmm4, %xmm5
cvttss2si %xmm5, %eax
jmp 0x15223
movq $-0x1, 0x20(%rsp)
movq %r12, %rax
movl %ecx, %ebx
testq %rsi, %rsi
movq 0x60(%rsp), %r15
movq 0xc8(%rsp), %r13
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %r10
movq 0x20(%rsp), %r11
js 0x1533b
movq (%r10), %rcx
movq %rsi, %rdi
shlq $0x4, %rdi
subq (%r15,%rdi), %rcx
cmpq 0x88(%rsp), %rcx
jbe 0x15368
movq $-0x1, %rsi
cmpq %r9, 0x40(%rsp)
jle 0x153a9
movl $0x80000000, %ecx       # imm = 0x80000000
movl (%rdx,%r12,4), %edi
cmpl %edi, %ecx
cmovlq %r12, %rsi
cmovlel %edi, %ecx
leaq -0x1(%r12), %rdi
cmpq %r9, %r12
movq %rdi, %r12
jg 0x1534e
testq %rsi, %rsi
sets %cl
cmpq %rax, %rsi
setge %al
orb %cl, %al
jne 0x153a9
movq %rsi, %r11
shlq $0x4, %r11
movq 0x8(%r15,%r11), %r10
movl %r14d, %ecx
subl %r10d, %ecx
setle %al
cmpl %ebp, %ecx
setg %dl
orb %al, %dl
je 0x1541a
movq 0x28(%rsp), %rdx
movq 0x10(%rsp), %r10
movq 0x20(%rsp), %r11
movq 0x40(%rsp), %r8
leaq (%rdx,%r8,4), %rax
movl %ebx, (%rdx,%r8,4)
movq 0x58(%rsp), %rcx
movq %r11, (%rcx,%r8,8)
testq %r11, %r11
movq 0x50(%rsp), %r12
js 0x153d2
movl (%r12,%r11,4), %ecx
cmpl %ebx, %ecx
cmovgl %ecx, %ebx
movl %ebx, (%r12,%r8,4)
testq %rsi, %rsi
movq 0x88(%rsp), %rbx
js 0x153fd
movq (%r10), %rcx
movq %rsi, %rdi
shlq $0x4, %rdi
subq (%r15,%rdi), %rcx
cmpq %rbx, %rcx
ja 0x15400
movl (%rdx,%rsi,4), %ecx
cmpl (%rax), %ecx
jge 0x15400
movq %r8, %rsi
incq %r8
addq $0x10, 0x80(%rsp)
cmpq %r13, %r8
jne 0x1501b
jmp 0x155ff
addq %r15, %r11
shrq $0x30, %r14
movl $0xff, %edx
andl %edx, %r14d
movq %r10, %rax
shrq $0x30, %rax
andl %edx, %eax
movq 0x10(%rsp), %rdx
movl (%rdx), %r8d
subl (%r11), %r8d
cmpl %eax, %r14d
jne 0x15499
cmpl 0x38(%rsp), %ecx
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %r11
jg 0x15526
testl %r8d, %r8d
je 0x15526
movl %ecx, %edi
subl %r8d, %edi
movl %edi, %r11d
negl %r11d
cmovsl %edi, %r11d
cmpl 0x3c(%rsp), %r11d
jg 0x1539a
cmpl $0x0, 0x1c(%rsp)
jne 0x154a8
cmpl $0x2, 0x4c(%rsp)
jl 0x154a8
cmpl %eax, %r14d
jne 0x154a8
cmpl %r8d, 0x38(%rsp)
jl 0x1539a
jmp 0x154a8
movl %ecx, %edi
subl %r8d, %edi
movl %edi, %r11d
negl %r11d
cmovsl %edi, %r11d
cmpl %r8d, %ecx
movl %r8d, %ebp
cmovll %ecx, %ebp
shrq $0x20, %r10
movzbl %r10b, %edi
cmpl %ebp, %edi
movl %ebp, %r10d
cmovll %edi, %r10d
testl %r11d, %r11d
jne 0x154d5
cmpl %ebp, %edi
jl 0x154d5
movq 0x50(%rsp), %r12
jmp 0x15577
testl %r11d, %r11d
je 0x15538
leal 0x1(%r11), %edi
cvtsi2ss %edi, %xmm4
movd %xmm4, %edi
movq %r14, %rdx
movl %edi, %r14d
shrl $0x17, %r14d
addl $-0x80, %r14d
cvtsi2ss %r14, %xmm5
movq %rdx, %r14
andl $0x7fffff, %edi         # imm = 0x7FFFFF
orl $0x3f800000, %edi       # imm = 0x3F800000
movd %edi, %xmm6
movdqa %xmm6, %xmm4
mulss %xmm1, %xmm4
addss %xmm2, %xmm4
mulss %xmm6, %xmm4
addss %xmm3, %xmm4
addss %xmm5, %xmm4
jmp 0x1553b
movq 0x10(%rsp), %r10
jmp 0x153a9
movq %r11, %rax
jmp 0x15300
xorps %xmm4, %xmm4
movq 0x50(%rsp), %r12
xorps %xmm5, %xmm5
cvtsi2ss %r11d, %xmm5
xorps %xmm6, %xmm6
cvtsi2ss %ebp, %xmm6
mulss %xmm7, %xmm6
mulss %xmm8, %xmm5
addss %xmm6, %xmm5
cmpl $0x0, 0x1c(%rsp)
jne 0x1558b
cmpl %eax, %r14d
jne 0x1558b
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %eax
subl %eax, %r10d
movl 0x30(%rsp), %ebp
movl %r10d, %eax
negl %eax
movl %r10d, %r11d
jo 0x1539a
jmp 0x1559f
cmpl %eax, %r14d
movl 0x30(%rsp), %ebp
je 0x155d7
movl $0x1, %r11d
testl %r8d, %r8d
jne 0x155d7
movq 0x28(%rsp), %rdx
addl (%rdx,%rsi,4), %r11d
cmpl %r11d, %ebx
jge 0x1539f
movq 0x40(%rsp), %r8
leaq (%rdx,%r8,4), %rax
movl %r11d, (%rdx,%r8,4)
movq 0x58(%rsp), %rcx
movq %rsi, (%rcx,%r8,8)
movl %r11d, %ebx
movq %rsi, %r11
movq 0x10(%rsp), %r10
jmp 0x153c9
cmpl %eax, %r14d
jne 0x155ef
cmpl %r8d, %ecx
jl 0x155ef
mulss %xmm0, %xmm4
addss %xmm5, %xmm4
cvttss2si %xmm4, %eax
jmp 0x155f7
minss %xmm4, %xmm5
cvttss2si %xmm5, %eax
subl %eax, %r10d
jmp 0x1557b
subq $0x8, %rsp
leaq 0x84(%rsp), %rax
leaq 0xdc(%rsp), %r14
movq 0xc0(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq 0x60(%rsp), %rbp
movq %rbp, %rcx
movq %r12, %r8
movq 0x70(%rsp), %rbx
movq %rbx, %r9
pushq %rax
pushq %r14
pushq 0xa8(%rsp)
pushq 0xb8(%rsp)
pushq 0xd0(%rsp)
callq 0x14ab0
addq $0x30, %rsp
movl (%r14), %r13d
movq %r15, %r14
movq %rax, %r15
movq 0xa0(%rsp), %rax
movl %r13d, (%rax)
movq 0xb0(%rsp), %rax
movq %r15, (%rax)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x12587
movq %r14, %rdi
movq 0x28(%rsp), %rsi
callq 0x12587
movq %r14, %rdi
movq %rbx, %rsi
callq 0x12587
testl %r13d, %r13d
je 0x156c4
movl 0x7c(%rsp), %ecx
movq %r14, %rdi
movl %r13d, %esi
movq %r15, %rdx
movq %r12, %r8
movq 0x60(%rsp), %r9
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x156f0
movq %r14, %rdi
movq 0x60(%rsp), %rsi
callq 0x12587
movq %r14, %rdi
movq %r12, %rsi
callq 0x12587
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq