// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
testq %rdx, %rdx
je 0x166946
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, 0x28(%rsp)
movq 0x18(%rdi), %r12
andq $0x0, 0x8(%rcx)
leaq 0x1(%rdx), %rdi
movq (%rcx), %rax
cmpq %rdi, 0x10(%rcx)
movsd %xmm0, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
jae 0x166819
movq %rbx, %rsi
shrq %rsi
orq %rbx, %rsi
movq %rsi, %rdx
shrq $0x2, %rdx
orq %rsi, %rdx
movq %rdx, %rsi
shrq $0x4, %rsi
orq %rdx, %rsi
movq %rsi, %rdx
shrq $0x8, %rdx
orq %rsi, %rdx
movq %rdx, %rsi
shrq $0x10, %rsi
orq %rdx, %rsi
incq %rsi
movq %rsi, 0x10(%rcx)
imulq $0x28, %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq %rdi, 0x8(%rcx)
xorps %xmm2, %xmm2
movups %xmm2, 0x10(%rax)
movups %xmm2, (%rax)
andq $0x0, 0x20(%rax)
movq (%rcx), %rax
movq %rbx, 0x20(%rsp)
movl %ebx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0x80000000, %edx       # imm = 0x80000000
andl 0x4(%rax), %edx
orl %esi, %edx
movl %edx, 0x4(%rax)
movq (%rcx), %r14
orl $-0x1, 0x8(%r14)
addq $0x18, %r13
pushq $0x1
popq %r8
pushq $0x28
popq %rbp
xorl %edx, %edx
movl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
cmpq %rdi, %r8
jae 0x166932
movups %xmm2, 0x10(%r14,%rbp)
movups %xmm2, (%r14,%rbp)
andq $0x0, 0x20(%r14,%rbp)
movl -0xc(%r13), %eax
movl (%r13), %esi
shrdl $0x1f, %eax, %esi
movq (%rcx), %r14
movl %esi, 0x8(%r14,%rbp)
orl $-0x1, 0x20(%r14,%rbp)
movl -0x18(%r13), %eax
movl %eax, (%r14,%rbp)
movl (%r13), %eax
andl %r9d, %eax
movl %eax, 0x1c(%r14,%rbp)
movsd -0x14(%r13), %xmm0
movsd -0x8(%r13), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movupd %xmm1, 0xc(%r14,%rbp)
cmpq $0x2, %r8
jb 0x166922
movq %r8, (%rsp)
movq %rdx, %r15
xorl $0x1, %esi
movl -0x20(%r14,%rbp), %ebx
movl %ebx, %edx
xorl $0x1, %edx
movq %r12, %rdi
callq 0x1664de
movl %eax, -0x8(%r14,%rbp)
testl %eax, %eax
js 0x166a36
movq 0x20(%r12), %rcx
shrl %ebx
movl (%rcx,%rbx,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
addl %eax, %ecx
movq %r15, %rdx
addq %rcx, %rdx
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdi
xorps %xmm2, %xmm2
movq (%rsp), %r8
movl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
incq %r8
addq $0x1c, %r13
addq $0x28, %rbp
jmp 0x166864
xorpd %xmm0, %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x16694d
pushq $0x1
popq %rax
jmp 0x166a27
xorl %eax, %eax
jmp 0x166a27
movq 0x48(%r14), %rax
leaq 0x40(%rsp), %rsi
movq %rax, 0x20(%rsi)
movups 0x28(%r14), %xmm0
movups 0x38(%r14), %xmm1
movaps %xmm1, 0x10(%rsi)
movaps %xmm0, (%rsi)
imulq $0x28, 0x20(%rsp), %rax
movl 0x8(%r14,%rax), %r13d
movl 0xc(%r14,%rax), %ecx
movl %ecx, (%rsp)
movl 0x10(%r14,%rax), %r15d
movslq 0x18(%r14,%rax), %r14
andl $0x0, (%rsi)
movq 0x20(%r12), %rax
movl %r13d, %r12d
shrl %r12d
movl (%rax,%r12,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subq %rax, %rdx
xorl %eax, %eax
testq %rdx, %rdx
cmovleq %rax, %rdx
movq %rdx, %rbp
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %r8
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0x1665b6
testb $0x1, %r13b
jne 0x1669d5
addl %ebp, %r15d
jmp 0x1669ee
movq 0x18(%rbx), %rax
movq 0x20(%rax), %rax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl (%rax,%r12,8), %ecx
subl (%rsp), %ebp
addl %ecx, %ebp
movl %ebp, %r15d
movslq %r15d, %rax
subq 0x30(%rsp), %r14
subq 0x38(%rsp), %rax
movq %r14, %rcx
subq %rax, %rcx
cmovgq %r14, %rax
movq %rcx, %rdx
negq %rdx
cmovsq %rcx, %rdx
cvtsi2sd %rax, %xmm0
mulsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
xorl %eax, %eax
cmpq %rcx, %rdx
setle %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8be71(%rip), %rdi     # 0x1f28ae
leaq 0x8b0df(%rip), %rsi     # 0x1f1b23
leaq 0x8be81(%rip), %rcx     # 0x1f28cc
movl $0x11cd, %edx           # imm = 0x11CD
callq 0x2110