// File: chhylp123[P]hifiasm/inter.cpp
// Function: l2g_res_chain(ma_ug_t*, ul_ov_t*, unsigned long, vec_mg_lchain_t*, double)
int64_t l2g_res_chain(ma_ug_t *ug, ul_ov_t *a, uint64_t a_n, vec_mg_lchain_t *gchains, double diff_rate)
{
	// fprintf(stderr, "[M::%s::] a_n::%lu\n", __func__, a_n);
	if(a_n <= 0) return 0;
	uint64_t k, m; int64_t l, rs, re, qs, qe, dq, dr, dif, mm; 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]);
	}

	if(diff_rate < 0) return 1;

	mg_lchain_t s = gchains->a[1], e = gchains->a[a_n-1];
	s.off = 0; 
	l -= (int64_t)g->seq[gchains->a[a_n-1].v>>1].len;
	if(l < 0) l = 0; 
	e.off = l;

	get_u_offset(ug, &s, &rs, NULL, &qs, NULL); get_u_offset(ug, &e, NULL, &re, NULL, &qe);
	dq = qe - qs; dr = re - rs;
	dif = (dq>dr? dq-dr:dr-dq);
	mm = MAX(dq, dr); mm *= diff_rate; 
	if(dif <= mm) return 1;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
ja 0x298a44
movq $0x0, -0x8(%rbp)
jmp 0x298fb3
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x20(%rbp), %rax
jae 0x298b3f
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 0x298b41
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, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x298dfd
movq -0x28(%rbp), %rax
movq (%rax), %rdi
imulq $0x28, -0x38(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2100
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
shll %ecx
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rdx
addq %rdx, %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
orl %eax, %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movq -0x18(%rbp), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
cmpq $0x1, -0x38(%rbp)
jbe 0x298dde
movq -0x90(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x28, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x38(%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 -0x38(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
subq $0x1, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x20(%rax)
jl 0x298d6c
jmp 0x298d8b
leaq 0xf8847(%rip), %rdi     # 0x3915ba
leaq 0xf7ab5(%rip), %rsi     # 0x39082f
movl $0x11cd, %edx           # imm = 0x11CD
leaq 0xf8852(%rip), %rcx     # 0x3915d8
callq 0x2130
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%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 -0x38(%rbp), %rdx
subq $0x1, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
addl 0x20(%rcx), %eax
cltq
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x298de0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x298bab
xorps %xmm0, %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x298e14
movq $0x1, -0x8(%rbp)
jmp 0x298fb3
movq -0x28(%rbp), %rax
movq (%rax), %rsi
addq $0x28, %rsi
leaq -0xb8(%rbp), %rdi
movl $0x28, %edx
callq 0x2180
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rax
subq $0x1, %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
leaq -0xe0(%rbp), %rdi
movl $0x28, %edx
callq 0x2180
movl $0x0, -0xb8(%rbp)
movq -0x90(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq -0x20(%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
movl %eax, %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jge 0x298eaf
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl %eax, -0xe0(%rbp)
movq -0x10(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
leaq -0x50(%rbp), %rdx
xorl %eax, %eax
movl %eax, %r9d
leaq -0x60(%rbp), %r8
movq %r9, %rcx
callq 0x2985f0
movq -0x10(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %r8d
leaq -0x58(%rbp), %rcx
leaq -0x68(%rbp), %r9
movq %r8, %rdx
callq 0x2985f0
movq -0x68(%rbp), %rax
subq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x58(%rbp), %rax
subq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x298f2c
movq -0x70(%rbp), %rax
subq -0x78(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x298f3b
movq -0x78(%rbp), %rax
subq -0x70(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
jl 0x298f5d
movq -0x70(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x298f68
movq -0x78(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x88(%rbp)
movsd -0x30(%rbp), %xmm1
cvtsi2sdq -0x88(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jg 0x298fab
movq $0x1, -0x8(%rbp)
jmp 0x298fb3
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq