// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
testq %rdx, %rdx
je 0x1af517
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r14
leaq 0x1(%rdx), %rbx
movq 0x18(%rdi), %r8
movq $0x0, 0x8(%rcx)
movq (%rcx), %rax
cmpq %rbx, 0x10(%rcx)
movq %rcx, 0x10(%rsp)
movsd %xmm0, 0x20(%rsp)
jae 0x1af3d3
movq %r15, %rsi
shrq %rsi
orq %r15, %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
leaq 0x1(%rsi), %rdx
movq %rdx, 0x10(%rcx)
leaq 0x8(,%rsi,8), %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rax, %rdi
movq %r8, %r12
callq 0x2370
movq 0x10(%rsp), %rcx
movq %r12, %r8
movq %rax, (%rcx)
movq %r14, 0x8(%rsp)
movq %rbx, 0x8(%rcx)
xorps %xmm2, %xmm2
movups %xmm2, 0x10(%rax)
movups %xmm2, (%rax)
movq $0x0, 0x20(%rax)
movq (%rcx), %rax
movl %r15d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0x80000000, %edx       # imm = 0x80000000
andl 0x4(%rax), %edx
orl %esi, %edx
movl %edx, 0x4(%rax)
movq (%rcx), %rbp
movl $0xffffffff, 0x8(%rbp)  # imm = 0xFFFFFFFF
cmpq $0x2, %rbx
movq %r15, 0x18(%rsp)
jb 0x1af51e
addq $0x18, %r13
movq %r15, %rdx
negq %rdx
movl $0x1, %r15d
movl $0x28, %ebx
xorl %r9d, %r9d
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
movq %rdx, 0x28(%rsp)
movups %xmm2, 0x10(%rbp,%rbx)
movups %xmm2, (%rbp,%rbx)
movq $0x0, 0x20(%rbp,%rbx)
movl -0xc(%r13), %eax
movl (%r13), %esi
shrdl $0x1f, %eax, %esi
movq (%rcx), %rbp
movl %esi, 0x8(%rbp,%rbx)
movl $0xffffffff, 0x20(%rbp,%rbx) # imm = 0xFFFFFFFF
movl -0x18(%r13), %eax
movl %eax, (%rbp,%rbx)
movl (%r13), %eax
andl %edi, %eax
movl %eax, 0x1c(%rbp,%rbx)
movsd -0x14(%r13), %xmm0
movsd -0x8(%r13), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movupd %xmm1, 0xc(%rbp,%rbx)
cmpq $0x2, %r15
jb 0x1af4f9
movq %r9, 0x30(%rsp)
xorl $0x1, %esi
movl -0x20(%rbp,%rbx), %r12d
movl %r12d, %edx
xorl $0x1, %edx
movq %r8, %rdi
movq %r8, %r14
callq 0x1af06e
movl %eax, -0x8(%rbp,%rbx)
testl %eax, %eax
js 0x1af613
movq 0x20(%r14), %rcx
shrl %r12d
movl (%rcx,%r12,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
addl %eax, %ecx
movq 0x30(%rsp), %r9
addq %rcx, %r9
movq %r14, %r8
movq 0x10(%rsp), %rcx
xorps %xmm2, %xmm2
movq 0x28(%rsp), %rdx
movl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
addq $0x28, %rbx
addq $0x1c, %r13
leaq (%rdx,%r15), %rax
incq %rax
incq %r15
cmpq $0x1, %rax
jne 0x1af442
jmp 0x1af521
xorl %eax, %eax
jmp 0x1af604
xorl %r9d, %r9d
movl $0x1, %eax
xorpd %xmm0, %xmm0
movsd 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
ja 0x1af604
movapd %xmm1, %xmm0
movl 0x30(%rbp), %edi
movq 0x18(%rsp), %rax
leaq (%rax,%rax,4), %rax
movl 0x8(%rbp,%rax,8), %esi
movq 0x20(%r8), %rcx
movl %esi, %edx
shrl %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subq %rcx, %r9
xorl %ecx, %ecx
testq %r9, %r9
cmovgq %r9, %rcx
testb $0x1, %dil
jne 0x1af57c
movl 0x34(%rbp), %r9d
movq 0x8(%rsp), %r10
jmp 0x1af599
movq 0x8(%rsp), %r10
movq 0x18(%r10), %r8
movq 0x20(%r8), %r8
shrl %edi
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl (%r8,%rdi,8), %r9d
subl 0x38(%rbp), %r9d
movslq 0x3c(%rbp), %r8
movslq 0x18(%rbp,%rax,8), %rdi
movslq %r9d, %r9
movapd %xmm0, %xmm1
testb $0x1, %sil
jne 0x1af5b5
addl 0x10(%rbp,%rax,8), %ecx
jmp 0x1af5ce
movq 0x18(%r10), %rsi
movq 0x20(%rsi), %rsi
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl (%rsi,%rdx,8), %r10d
subl 0xc(%rbp,%rax,8), %ecx
addl %r10d, %ecx
movslq %ecx, %rax
subq %r9, %rax
subq %r8, %rdi
movq %rdi, %rcx
subq %rax, %rcx
cmovleq %rax, %rdi
movq %rcx, %rdx
negq %rdx
cmovsq %rcx, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
xorl %eax, %eax
cmpq %rcx, %rdx
setle %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x913cc(%rip), %rdi     # 0x2409e6
leaq 0x9063a(%rip), %rsi     # 0x23fc5b
leaq 0x913dc(%rip), %rcx     # 0x240a04
movl $0x11cd, %edx           # imm = 0x11CD
callq 0x2110