// File: chhylp123[P]hifiasm/inter.cpp
// Function: gen_gchain_track(void*, mg_lchain_t*, long, asg_t const*, st_mt_t*, vec_sp_node_t*, vec_mg_pathv_t*)
uint32_t gen_gchain_track(void *km, mg_lchain_t *a, int64_t a_n, const asg_t *g, 
st_mt_t *dst_done, vec_sp_node_t *out, vec_mg_pathv_t *res)
{
	int64_t k, p_n/**, trav_occ = 0**/; mg_lchain_t *l0, *l1; mg_path_dst_t dst; uint64_t dst_group; mg_pathv_t *p;
	res->n = 0; kv_pushp(mg_pathv_t, *res, &p); p->v = (uint32_t)-1; p->d = 0; p->pre = 0;
	for (k = 1; k < a_n; k++) {
		l0 = a + k - 1; l1 = a + k;
		assert(!l1->inner_pre); assert(l1->dist_pre >= 0);
		memset(&dst, 0, sizeof(dst));
		dst.v = l0->v^1; 
		assert(l1->dist_pre >= 0);
		dst.target_dist = l1->dist_pre;
		dst.target_hash = l1->hash_pre;
		dst.check_hash = 1; p_n = res->n;
		if((dst.target_hash != (__ac_Wang_hash((l1->v^1))+__ac_Wang_hash(dst.v))) || 
													(g_adjacent_dis(g, l1->v^1, dst.v) != dst.target_dist)) {
			hc_shortest_k(km, g, l1->v^1, 1, &dst, dst.target_dist*(1+SEC_LEN_DIF), MG_MAX_SHORT_K, dst_done, &dst_group, out, res, 1, 1, SEC_LEN_DIF);
			// debug_gchain2(g, res->a + p_n, res->n - p_n);
			// fprintf(stderr, "[M::%s::n->%ld]\tutg%.6dl(%c)\t->\tutg%.6dl(%c)\n", __func__, res->n - p_n, 
			// (int32_t)(l0->v>>1)+1, "+-"[l0->v&1], (int32_t)(l1->v>>1)+1, "+-"[l1->v&1]);
			
			// fprintf(stderr, "\n-[M::%s::res->n->%u::p_n->%ld] utg%.6dl(v:%u) -> utg%.6dl(v:%u)\n", 
			// 			__func__, (uint32_t)res->n, p_n, (int32_t)(l1->v>>1)+1, l1->v, (int32_t)(l0->v>>1)+1, l0->v);
			// dbg_print(res->a + p_n, res->n - p_n);
			assert(res->n - p_n > 1); assert(dst.target_hash == dst.hash); 
			res->a[p_n-1].d = res->a[res->n-1].d - res->a[res->n-2].d; res->n--; 
			reverse_track(res->a + p_n, res->n - p_n); res->n--;///reomve l1 from res
			// trav_occ++;
		} else {
			res->a[p_n-1].d = dst.target_dist;
		}

		kv_pushp(mg_pathv_t, *res, &p); p->v = (uint32_t)-1; p->pre = k; p->d = 0;
		
	}
	// fprintf(stderr, "[M::%s::]\ta_n:%ld\ttrav_occ:%ld\n", __func__, a_n, trav_occ);
	return res->n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xe8, %rsp
movq 0x10(%rbp), %rax
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 0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x2a9951
movq 0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2a9915
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x2a9923
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x2a9923
movq -0xa0(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rbp), %rax
imulq $0xc, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x8(%rdx)
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x98(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x98(%rbp), %rax
movl $0x0, 0x8(%rax)
movq $0x1, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x2a9d8e
movq -0x18(%rbp), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
addq $-0x28, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
imulq $0x28, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl 0x4(%rax), %eax
sarl $0x1f, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2a99f4
jmp 0x2a99f6
jmp 0x2a9a15
leaq 0xe9863(%rip), %rdi     # 0x393260
leaq 0xe6e2b(%rip), %rsi     # 0x39082f
movl $0x3955, %edx           # imm = 0x3955
leaq 0xe985f(%rip), %rcx     # 0x39326f
callq 0x2130
movq -0x58(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jl 0x2a9a21
jmp 0x2a9a40
leaq 0xe71e6(%rip), %rdi     # 0x390c0e
leaq 0xe6e00(%rip), %rsi     # 0x39082f
movl $0x3955, %edx           # imm = 0x3955
leaq 0xe9834(%rip), %rcx     # 0x39326f
callq 0x2130
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x2100
movq -0x50(%rbp), %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x88(%rbp)
movq -0x58(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jl 0x2a9a6f
jmp 0x2a9a8e
leaq 0xe7198(%rip), %rdi     # 0x390c0e
leaq 0xe6db2(%rip), %rsi     # 0x39082f
movl $0x3958, %edx           # imm = 0x3958
leaq 0xe97e6(%rip), %rcx     # 0x39326f
callq 0x2130
movq -0x58(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x58(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x80(%rbp)
movl -0x7c(%rbp), %eax
andl $0xbfffffff, %eax       # imm = 0xBFFFFFFF
orl $0x40000000, %eax       # imm = 0x40000000
movl %eax, -0x7c(%rbp)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0x58(%rbp), %rax
movl 0x8(%rax), %edi
xorl $0x1, %edi
callq 0x286f50
movl %eax, -0xa8(%rbp)
movl -0x88(%rbp), %edi
callq 0x286f50
movl -0xa8(%rbp), %ecx
movl %eax, %edx
movl -0xa4(%rbp), %eax
addl %edx, %ecx
cmpl %ecx, %eax
jne 0x2a9b27
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movl -0x88(%rbp), %edx
callq 0x298410
movslq -0x84(%rbp), %rcx
cmpq %rcx, %rax
je 0x2a9c9c
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x58(%rbp), %rax
movl 0x8(%rax), %edx
xorl $0x1, %edx
cvtsi2sdl -0x84(%rbp), %xmm0
movsd 0xe6bf7(%rip), %xmm1    # 0x390740
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r9d
movq -0x30(%rbp), %rbx
movq -0x38(%rbp), %r10
movq 0x10(%rbp), %rax
movl $0x1, %ecx
leaq -0x88(%rbp), %r8
leaq -0x90(%rbp), %r11
movss 0xe6c1b(%rip), %xmm0    # 0x390794
movl $0xf, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq $0x1, 0x28(%rsp)
movq $0x1, 0x30(%rsp)
callq 0x29be70
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x48(%rbp), %rax
cmpq $0x1, %rax
jbe 0x2a9bbf
jmp 0x2a9bde
leaq 0xe9720(%rip), %rdi     # 0x3932e6
leaq 0xe6c62(%rip), %rsi     # 0x39082f
movl $0x3966, %edx           # imm = 0x3966
leaq 0xe9696(%rip), %rcx     # 0x39326f
callq 0x2130
movl -0x80(%rbp), %eax
cmpl -0x64(%rbp), %eax
jne 0x2a9be8
jmp 0x2a9c07
leaq 0xe7103(%rip), %rdi     # 0x390cf2
leaq 0xe6c39(%rip), %rsi     # 0x39082f
movl $0x3966, %edx           # imm = 0x3966
leaq 0xe966d(%rip), %rcx     # 0x39326f
callq 0x2130
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq $0x1, %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rdx
movq 0x8(%rdx), %rdx
subq $0x2, %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
subl 0x4(%rax), %ecx
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rdx
subq $0x1, %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rdi
imulq $0xc, -0x48(%rbp), %rax
addq %rax, %rdi
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rsi
subq -0x48(%rbp), %rsi
callq 0x2d4940
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
jmp 0x2a9cbb
movl -0x84(%rbp), %ecx
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rdx
subq $0x1, %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
jmp 0x2a9cbd
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x2a9d2a
movq 0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2a9cee
movq 0x10(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x2a9cfc
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x2a9cfc
movq -0xb0(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rdi
movq 0x10(%rbp), %rax
imulq $0xc, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x8(%rdx)
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x98(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2a99aa
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq $0xe8, %rsp
popq %rbx
popq %rbp
retq