// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq 0x30(%rbp), %rax
movl $0x0, (%rax)
movq 0x28(%rbp), %rax
movl $0x0, (%rax)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x283a6e
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl 0x18(%rbp), %eax
jl 0x283a5e
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x283a60
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x283a38
cmpq $0x0, -0x60(%rbp)
jne 0x283a82
movq $0x0, -0x8(%rbp)
jmp 0x283e80
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
shlq $0x4, %rsi
callq 0x30e4f0
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x283b11
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl 0x18(%rbp), %eax
jl 0x283b01
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movslq (%rax,%rcx,4), %rcx
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x58(%rbp)
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x283b03
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x283aa7
movq -0x40(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x27ea40
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2100
movq -0x60(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x6c(%rbp)
movq $0x0, -0x68(%rbp)
cmpq $0x0, -0x58(%rbp)
jl 0x283c79
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x50(%rbp)
movb %al, -0x8d(%rbp)
jl 0x283ba5
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
sete %al
movb %al, -0x8d(%rbp)
movb -0x8d(%rbp), %al
testb $0x1, %al
jne 0x283bb1
jmp 0x283bde
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
jmp 0x283b81
cmpq $0x0, -0x50(%rbp)
jge 0x283c00
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0x98(%rbp)
jmp 0x283c26
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movq -0x50(%rbp), %rdx
subl (%rcx,%rdx,4), %eax
cltq
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl 0x18(%rbp), %eax
jl 0x283c5e
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x283c5e
movq -0x68(%rbp), %rax
subq -0x78(%rbp), %rax
movslq 0x10(%rbp), %rcx
cmpq %rcx, %rax
jl 0x283c5e
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x283c66
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x283c68
movq -0x58(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x283b57
movq -0x10(%rbp), %rdi
movl -0x6c(%rbp), %eax
addl 0x20(%rbp), %eax
movslq %eax, %rsi
shlq $0x3, %rsi
callq 0x30e4f0
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2100
movq -0x60(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x6c(%rbp)
movq $0x0, -0x68(%rbp)
cmpq $0x0, -0x58(%rbp)
jl 0x283e2b
movq -0x68(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x50(%rbp)
movb %al, -0x99(%rbp)
jl 0x283d12
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
sete %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x283d1e
jmp 0x283d5b
movq -0x50(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x68(%rbp)
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
jmp 0x283cee
cmpq $0x0, -0x50(%rbp)
jge 0x283d7d
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
jmp 0x283da3
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movq -0x50(%rbp), %rdx
subl (%rcx,%rdx,4), %eax
cltq
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl 0x18(%rbp), %eax
jl 0x283e0d
movq -0x68(%rbp), %rax
cmpq -0x88(%rbp), %rax
jle 0x283e0d
movq -0x68(%rbp), %rax
subq -0x88(%rbp), %rax
movslq 0x10(%rbp), %rcx
cmpq %rcx, %rax
jl 0x283e0d
movslq -0x8c(%rbp), %rdx
shlq $0x20, %rdx
movq -0x68(%rbp), %rax
subq -0x88(%rbp), %rax
orq %rax, %rdx
movq -0x48(%rbp), %rax
movl -0x6c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x6c(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x283e18
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x283e1a
movq -0x58(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x283cc1
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x30e2d0
cmpq $0x7fffffff, -0x68(%rbp) # imm = 0x7FFFFFFF
jge 0x283e44
jmp 0x283e63
leaq 0x10c9d4(%rip), %rdi    # 0x39081f
leaq 0x10c9dd(%rip), %rsi    # 0x39082f
movl $0x2ee, %edx            # imm = 0x2EE
leaq 0x10c9db(%rip), %rcx    # 0x390839
callq 0x2130
movl -0x6c(%rbp), %ecx
movq 0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq 0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)