// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %r8, %r15
movq %rcx, %r12
movq %rsi, %r13
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
movl 0x68(%rsp), %esi
xorl %ebx, %ebx
movl %ebx, (%rax)
movl %ebx, (%rcx)
testq %r13, %r13
jle 0x19980b
xorl %eax, %eax
xorl %ebx, %ebx
xorl %ecx, %ecx
cmpl %esi, (%rdx,%rax,4)
setge %cl
addq %rcx, %rbx
incq %rax
cmpq %rax, %r13
jne 0x1997f8
testq %rbx, %rbx
je 0x1999ed
movq %rbx, %rsi
shlq $0x4, %rsi
movq %rdi, 0x10(%rsp)
movq %rsi, (%rsp)
movq %rdx, 0x8(%rsp)
callq 0x1e36fd
movl 0x68(%rsp), %r8d
movq 0x8(%rsp), %rdi
movq %rax, %rbp
testq %r13, %r13
jle 0x199869
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%rdi,%rax,4), %rdx
cmpl %r8d, %edx
jl 0x199861
movq %rcx, %rsi
shlq $0x4, %rsi
movq %rdx, (%rbp,%rsi)
incq %rcx
movq %rax, 0x8(%rbp,%rsi)
incq %rax
cmpq %rax, %r13
jne 0x199844
movq (%rsp), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x19712b
shlq $0x2, %r13
movq %r14, %rdi
xorl %esi, %esi
movq %r13, 0x20(%rsp)
movq %r13, %rdx
callq 0x20e0
movl 0x68(%rsp), %r10d
leaq -0x1(%rbx), %r13
movslq 0x60(%rsp), %r11
movl $0x0, %eax
testq %rbx, %rbx
jle 0x199917
xorl %eax, %eax
movq %r13, %rcx
xorl %edx, %edx
movq 0x8(%rsp), %r9
movq %rcx, %r8
shlq $0x4, %r8
movq 0x8(%rbp,%r8), %rdi
movq %rdx, %rsi
testq %rdi, %rdi
js 0x1998de
cmpl $0x0, (%r14,%rdi,4)
jne 0x1998de
incq %rsi
movl $0x1, (%r14,%rdi,4)
movq (%r12,%rdi,8), %rdi
jmp 0x1998c1
addq %rbp, %r8
movl (%r8), %r8d
testq %rdi, %rdi
js 0x1998ed
subl (%r9,%rdi,4), %r8d
cmpl %r10d, %r8d
jl 0x19990b
movq %rsi, %rdi
subq %rdx, %rdi
jle 0x19990b
xorl %r8d, %r8d
cmpq %r11, %rdi
setge %r8b
cmovgeq %rsi, %rdx
addl %r8d, %eax
leaq -0x1(%rcx), %rsi
testq %rcx, %rcx
movq %rsi, %rcx
jg 0x1998b2
movq %r11, 0x18(%rsp)
addl 0x70(%rsp), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %rdi
callq 0x1e36fd
movq %rax, (%rsp)
movq %r14, %rdi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
callq 0x20e0
testq %rbx, %rbx
jle 0x1999f1
xorl %r10d, %r10d
xorl %eax, %eax
movq 0x8(%rsp), %r8
movl 0x68(%rsp), %r9d
movq 0x18(%rsp), %r11
movq %r13, %rdx
shlq $0x4, %rdx
movq 0x8(%rbp,%rdx), %rcx
movq %rax, %rbx
testq %rcx, %rcx
js 0x199991
cmpl $0x0, (%r14,%rcx,4)
jne 0x199991
movl %ecx, (%r15,%rbx,4)
incq %rbx
movl $0x1, (%r14,%rcx,4)
movq (%r12,%rcx,8), %rcx
jmp 0x199970
addq %rbp, %rdx
movl (%rdx), %edx
testq %rcx, %rcx
js 0x19999f
subl (%r8,%rcx,4), %edx
cmpl %r9d, %edx
setge %sil
movq %rbx, %rcx
subq %rax, %rcx
setg %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x1999be
cmpq %r11, %rcx
jge 0x1999c3
movq %rax, %rbx
jmp 0x1999d8
shlq $0x20, %rdx
orq %rcx, %rdx
movslq %r10d, %rax
incl %r10d
movq (%rsp), %rcx
movq %rdx, (%rcx,%rax,8)
leaq -0x1(%r13), %rcx
movq %rbx, %rax
testq %r13, %r13
movq %rcx, %r13
jg 0x199961
jmp 0x1999f6
xorl %eax, %eax
jmp 0x199a25
xorl %ebx, %ebx
xorl %r10d, %r10d
movq %r10, %r14
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x1e3632
cmpq $0x7fffffff, %rbx       # imm = 0x7FFFFFFF
jge 0x199a34
movq 0x78(%rsp), %rax
movl %r14d, (%rax)
movq 0x80(%rsp), %rax
movl %ebx, (%rax)
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x99f40(%rip), %rdi     # 0x23397b
leaq 0x99f49(%rip), %rsi     # 0x23398b
leaq 0x99f4c(%rip), %rcx     # 0x233995
movl $0x2ee, %edx            # imm = 0x2EE
callq 0x2110