// File: chhylp123[P]hifiasm/inter.cpp
// Function: l2g_res_chain_sc(ma_ug_t*, ul_ov_t*, unsigned long, vec_mg_lchain_t*)
int64_t l2g_res_chain_sc(ma_ug_t *ug, ul_ov_t *a, uint64_t a_n, vec_mg_lchain_t *gchains)
{
	// fprintf(stderr, "[M::%s::] a_n::%lu\n", __func__, a_n);
	if(a_n <= 0) return 0;
	uint64_t k, m; int64_t l; a_n++; asg_t *g = ug->g;
	gchains->n = 0; kv_resize(mg_lchain_t, *gchains, a_n); gchains->n = a_n;
	memset(&(gchains->a[0]), 0, sizeof(gchains->a[0])); 
	gchains->a[0].cnt = a_n - 1; gchains->a[0].v = (uint32_t)-1;
	for (k = 1, m = 0, l = 0; k < a_n; k++, m++) {
		memset(&(gchains->a[k]), 0, sizeof(gchains->a[k]));
		gchains->a[k].v = (a[m].tn<<1)|(a[m].rev); gchains->a[k].dist_pre = -1;
		gchains->a[k].off = a[m].qn; gchains->a[k].score = a[m].sec;
		gchains->a[k].qs = a[m].qs; gchains->a[k].qe = a[m].qe;
		gchains->a[k].rs = a[m].ts; gchains->a[k].re = a[m].te; 
		if(k > 1) {
			gchains->a[k-1].dist_pre = g_adjacent_dis(g, gchains->a[k].v^1, gchains->a[k-1].v^1);
			assert(gchains->a[k-1].dist_pre >= 0);
			l += g->seq[gchains->a[k-1].v>>1].len + gchains->a[k-1].dist_pre;
		}
		// fprintf(stderr, "[M::%s::k->%lu] utg%.6dl(%c)\n", __func__, k, (int32_t)(gchains->a[k].v>>1)+1, "+-"[gchains->a[k].v&1]);
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
ja 0x298fec
movq $0x0, -0x8(%rbp)
jmp 0x2993a4
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x20(%rbp), %rax
jae 0x2990e4
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
shrq %rcx
movq -0x28(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x2, %rcx
movq -0x28(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x4, %rcx
movq -0x28(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x8, %rcx
movq -0x28(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x10, %rcx
movq -0x28(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rax
imulq $0x28, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2990e6
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2100
movq -0x20(%rbp), %rax
subq $0x1, %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %edx, %ecx
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movq $0x1, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x29939c
movq -0x28(%rbp), %rax
movq (%rax), %rdi
imulq $0x28, -0x30(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2100
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
shll %ecx
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rdx
addq %rdx, %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
orl %eax, %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rcx
addq %rcx, %rax
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
cmpq $0x1, -0x30(%rbp)
jbe 0x29937d
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x30(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
xorl $0x1, %edx
callq 0x298410
movl %eax, %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x20(%rax)
jl 0x29930e
jmp 0x29932d
leaq 0xf82a5(%rip), %rdi     # 0x3915ba
leaq 0xf7513(%rip), %rsi     # 0x39082f
movl $0x11f4, %edx           # imm = 0x11F4
leaq 0xf8301(%rip), %rcx     # 0x391629
callq 0x2130
movq -0x48(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq -0x30(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
addl 0x20(%rcx), %eax
cltq
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x29937f
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x299150
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nop