// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: mg_chain_backtrack(void*, long, int const*, long const*, int*, int*, int, int, int, int*, int*)
uint64_t *mg_chain_backtrack(void *km, int64_t n, const int32_t *f, const int64_t *p, int32_t *v, int32_t *t, int32_t min_cnt, int32_t min_sc, int32_t extra_u, int32_t *n_u_, int32_t *n_v_)
{
	mg128_t *z;
	uint64_t *u;
	int64_t i, k, n_z, n_v;
	int32_t n_u;
	// v[] keeps the peak score up to i; f[] is the score ending at i, not always the peak
	*n_u_ = *n_v_ = 0;
	for (i = 0, n_z = 0; i < n; ++i) // precompute n_z
		if (f[i] >= min_sc) ++n_z;
	if (n_z == 0) return 0;
	KMALLOC(km, z, n_z);
	for (i = 0, k = 0; i < n; ++i) // populate z[]
		if (f[i] >= min_sc) z[k].x = f[i], z[k++].y = i;
	radix_sort_128x(z, z + n_z);///sort by score

	memset(t, 0, n * 4);///t is a buffer
	///from the largest to the smallest
	for (k = n_z - 1, n_v = n_u = 0; k >= 0; --k) { // precompute n_u
		int64_t n_v0 = n_v;
		int32_t sc;
		///note t[i] == 0 is not used to find local alignment
		///say if we have already found a long chain, then the secondary might be able to merged to the long chain
		///t[i] == 0 is used to find those chains
		for (i = z[k].y; i >= 0 && t[i] == 0; i = p[i])
			++n_v, t[i] = 1;
		sc = i < 0? z[k].x : (int32_t)z[k].x - f[i];
		if (sc >= min_sc && n_v > n_v0 && n_v - n_v0 >= min_cnt)
			++n_u;///how many chains, including primary chains and non-primary chains
		else n_v = n_v0;
	}
	KMALLOC(km, u, n_u + extra_u);
	memset(t, 0, n * 4);
	for (k = n_z - 1, n_v = n_u = 0; k >= 0; --k) { // populate u[]
		int64_t n_v0 = n_v;
		int32_t sc;
		for (i = z[k].y; i >= 0 && t[i] == 0; i = p[i])
			v[n_v++] = i, t[i] = 1;
		sc = i < 0? z[k].x : (int32_t)z[k].x - f[i];
		if (sc >= min_sc && n_v > n_v0 && n_v - n_v0 >= min_cnt)
			u[n_u++] = (uint64_t)sc << 32 | (n_v - n_v0);
		else n_v = n_v0;
	}
	kfree(km, z);
	assert(n_v < INT32_MAX);
	*n_u_ = n_u, *n_v_ = n_v;
	return u;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %rbx
movq %rcx, %r15
movq %rdx, %r13
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
andl $0x0, (%rax)
andl $0x0, (%rcx)
movl 0x68(%rsp), %edx
xorl %r14d, %r14d
testq %rsi, %rsi
movl $0x0, %r12d
cmovgq %rsi, %r12
xorl %eax, %eax
cmpq %rax, %r12
je 0x15b06f
xorl %ecx, %ecx
cmpl %edx, (%r13,%rax,4)
setge %cl
addq %rcx, %r14
incq %rax
jmp 0x15b058
testq %r14, %r14
je 0x15b257
movq %rsi, 0x8(%rsp)
movq %r8, 0x20(%rsp)
movq %r14, %rsi
shlq $0x4, %rsi
movq %rdi, 0x10(%rsp)
movq %rsi, (%rsp)
callq 0x1a39c4
movl 0x68(%rsp), %edi
movq %rax, %rbp
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x15b0c9
movslq (%r13,%rcx,4), %rdx
cmpl %edi, %edx
jl 0x15b0c4
movq %rax, %rsi
shlq $0x4, %rsi
movq %rdx, (%rbp,%rsi)
incq %rax
movq %rcx, 0x8(%rbp,%rsi)
incq %rcx
jmp 0x15b0a2
movq (%rsp), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x158af1
movq 0x8(%rsp), %rdx
shlq $0x2, %rdx
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movq %rdx, 0x8(%rsp)
callq 0x20e0
movl 0x68(%rsp), %r8d
decq %r14
movslq 0x60(%rsp), %r9
xorl %eax, %eax
movq %r14, %rcx
testq %rcx, %rcx
js 0x15b16a
movq %rcx, %rdx
shlq $0x4, %rdx
leaq (%rdx,%rbp), %rsi
leaq (%rdx,%rbp), %rdi
addq $0x8, %rdi
xorl %edx, %edx
movq (%rdi), %rdi
testq %rdi, %rdi
js 0x15b13d
cmpl $0x0, (%rbx,%rdi,4)
jne 0x15b141
movl $0x1, (%rbx,%rdi,4)
leaq (%r15,%rdi,8), %rdi
incq %rdx
jmp 0x15b11f
movl (%rsi), %esi
jmp 0x15b148
movl (%rsi), %esi
subl (%r13,%rdi,4), %esi
cmpl %r8d, %esi
jl 0x15b165
leaq (%r12,%rdx), %rsi
cmpq %r12, %rsi
jle 0x15b165
xorl %edi, %edi
cmpq %r9, %rdx
setge %dil
cmovgeq %rsi, %r12
addl %edi, %eax
decq %rcx
jmp 0x15b105
addl 0x70(%rsp), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %rdi
movq %r9, 0x18(%rsp)
callq 0x1a39c4
movq %rax, (%rsp)
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x20e0
movq 0x18(%rsp), %r9
movl 0x68(%rsp), %r8d
xorl %r10d, %r10d
movq 0x20(%rsp), %rdi
testq %r14, %r14
js 0x15b226
movq %r14, %rax
shlq $0x4, %rax
leaq (%rax,%rbp), %rcx
leaq (%rax,%rbp), %rsi
addq $0x8, %rsi
leaq (%rdi,%r12,4), %rdx
xorl %eax, %eax
movq (%rsi), %rsi
testq %rsi, %rsi
js 0x15b1eb
cmpl $0x0, (%rbx,%rsi,4)
jne 0x15b1ef
movl %esi, (%rdx,%rax,4)
movl $0x1, (%rbx,%rsi,4)
leaq (%r15,%rsi,8), %rsi
incq %rax
jmp 0x15b1ca
movl (%rcx), %ecx
jmp 0x15b1f6
movl (%rcx), %ecx
subl (%r13,%rsi,4), %ecx
cmpl %r8d, %ecx
jl 0x15b221
leaq (%r12,%rax), %rdx
cmpq %r12, %rdx
jle 0x15b221
cmpq %r9, %rax
jl 0x15b221
shlq $0x20, %rcx
orq %rax, %rcx
movslq %r10d, %rax
incl %r10d
movq (%rsp), %rsi
movq %rcx, (%rsi,%rax,8)
movq %rdx, %r12
decq %r14
jmp 0x15b1ac
movl %r10d, %ebx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x1a38cf
cmpq $0x7fffffff, %r12       # imm = 0x7FFFFFFF
jge 0x15b268
movq 0x78(%rsp), %rax
movl %ebx, (%rax)
movq 0x80(%rsp), %rax
movl %r12d, (%rax)
movq (%rsp), %rax
jmp 0x15b259
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x968a4(%rip), %rdi     # 0x1f1b13
leaq 0x968ad(%rip), %rsi     # 0x1f1b23
leaq 0x968b0(%rip), %rcx     # 0x1f1b2d
movl $0x2ee, %edx            # imm = 0x2EE
callq 0x2110