// File: /workspace/github_cpp_makefile_star2_O3/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 %rcx, %r15
movq %rsi, %r12
movq 0x80(%rsp), %rsi
movq 0x78(%rsp), %rcx
xorl %eax, %eax
movl %eax, (%rsi)
movl %eax, (%rcx)
testq %r12, %r12
jle 0x1a2709
movq %r9, %rbx
movq %r8, %r14
movl 0x68(%rsp), %esi
xorl %eax, %eax
xorl %r13d, %r13d
xorl %ecx, %ecx
cmpl %esi, (%rdx,%rax,4)
setge %cl
addq %rcx, %r13
incq %rax
cmpq %rax, %r12
jne 0x1a24e9
testq %r13, %r13
je 0x1a2707
movq %r13, %rsi
shlq $0x4, %rsi
movq %rdi, 0x10(%rsp)
movq %rsi, (%rsp)
movq %rdx, 0x8(%rsp)
callq 0x1ee115
movl 0x68(%rsp), %r8d
movq 0x8(%rsp), %rdi
movq %rax, %rbp
testq %r12, %r12
jle 0x1a255a
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%rdi,%rax,4), %rdx
cmpl %r8d, %edx
jl 0x1a2552
movq %rcx, %rsi
shlq $0x4, %rsi
movq %rdx, (%rbp,%rsi)
incq %rcx
movq %rax, 0x8(%rbp,%rsi)
incq %rax
cmpq %rax, %r12
jne 0x1a2535
movq (%rsp), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x19febb
shlq $0x2, %r12
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20e0
xorl %r11d, %r11d
movl 0x68(%rsp), %r9d
movq 0x8(%rsp), %r8
decq %r13
movslq 0x60(%rsp), %r10
movq %r13, %rax
xorl %ecx, %ecx
movq %rax, %rdx
shlq $0x4, %rdx
leaq (%rdx,%rbp), %rsi
movq 0x8(%rbp,%rdx), %rdi
movq %rcx, %rdx
testq %rdi, %rdi
js 0x1a25c8
movq %rcx, %rdx
cmpl $0x0, (%rbx,%rdi,4)
jne 0x1a25cc
incq %rdx
movl $0x1, (%rbx,%rdi,4)
movq (%r15,%rdi,8), %rdi
testq %rdi, %rdi
jns 0x1a25af
movl (%rsi), %esi
jmp 0x1a25d2
movl (%rsi), %esi
subl (%r8,%rdi,4), %esi
cmpl %r9d, %esi
jl 0x1a25ef
movq %rdx, %rsi
subq %rcx, %rsi
jle 0x1a25ef
xorl %edi, %edi
cmpq %r10, %rsi
setge %dil
cmovgeq %rdx, %rcx
addl %edi, %r11d
leaq -0x1(%rax), %rdx
testq %rax, %rax
movq %rdx, %rax
jg 0x1a2594
addl 0x70(%rsp), %r11d
movslq %r11d, %rsi
shlq $0x3, %rsi
movq 0x10(%rsp), %rdi
movq %r10, 0x20(%rsp)
callq 0x1ee115
movq %rax, 0x18(%rsp)
movq $0x0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20e0
movq 0x18(%rsp), %r11
movq 0x20(%rsp), %r10
movl 0x68(%rsp), %r9d
movq 0x8(%rsp), %r8
xorl %r12d, %r12d
movq %r13, %rax
shlq $0x4, %rax
leaq (%rax,%rbp), %rcx
movq 0x8(%rbp,%rax), %rdx
movq %r12, %rax
testq %rdx, %rdx
js 0x1a267f
movq %r12, %rax
cmpl $0x0, (%rbx,%rdx,4)
jne 0x1a2683
movl %edx, (%r14,%rax,4)
incq %rax
movl $0x1, (%rbx,%rdx,4)
movq (%r15,%rdx,8), %rdx
testq %rdx, %rdx
jns 0x1a2662
movl (%rcx), %ecx
jmp 0x1a2689
movl (%rcx), %ecx
subl (%r8,%rdx,4), %ecx
cmpl %r9d, %ecx
setge %sil
movq %rax, %rdx
subq %r12, %rdx
setg %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x1a26c3
cmpq %r10, %rdx
jl 0x1a26c3
shlq $0x20, %rcx
orq %rdx, %rcx
movq (%rsp), %rsi
movslq %esi, %rdx
incl %esi
movq %rsi, (%rsp)
movq %rcx, (%r11,%rdx,8)
movq %rax, %r12
leaq -0x1(%r13), %rax
testq %r13, %r13
movq %rax, %r13
jg 0x1a2647
movq %r11, %rbx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x1ee04f
cmpq $0x7fffffff, %r12       # imm = 0x7FFFFFFF
jge 0x1a2718
movq 0x78(%rsp), %rax
movq (%rsp), %rcx
movl %ecx, (%rax)
movq 0x80(%rsp), %rax
movl %r12d, (%rax)
movq %rbx, %rax
jmp 0x1a2709
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9d52c(%rip), %rdi     # 0x23fc4b
leaq 0x9d535(%rip), %rsi     # 0x23fc5b
leaq 0x9d538(%rip), %rcx     # 0x23fc65
movl $0x2ee, %edx            # imm = 0x2EE
callq 0x2110