// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movsd %xmm0, 0x8(%rsp)
testq %rdx, %rdx
je 0x1a62b6
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
leaq 0x1(%rdx), %rbx
movq 0x18(%rdi), %r8
movq $0x0, 0x8(%rcx)
cmpq %rbx, 0x10(%rcx)
jae 0x1a6152
movq %r12, %rax
shrq %rax
orq %r12, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%r15)
movq (%r15), %rdi
leaq 0x8(,%rax,8), %rax
leaq (%rax,%rax,4), %rsi
movq %r8, %rbp
callq 0x2370
movq %rbp, %r8
movq %rax, (%r15)
movq %r14, (%rsp)
movq %rbx, 0x8(%r15)
movq (%r15), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rax)
movupd %xmm0, (%rax)
movq $0x0, 0x20(%rax)
movq (%r15), %rax
movl %r12d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x80000000, %edx       # imm = 0x80000000
andl 0x4(%rax), %edx
orl %ecx, %edx
movl %edx, 0x4(%rax)
movq (%r15), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
cmpq $0x2, %rbx
movq %r12, 0x10(%rsp)
jb 0x1a62bd
addq $0x18, %r13
movq %r12, %rcx
negq %rcx
movl $0x1, %ebx
movl $0x28, %r12d
xorl %r9d, %r9d
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq %r15, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq (%r15), %rax
movupd %xmm0, 0x10(%rax,%r12)
movupd %xmm0, (%rax,%r12)
movq $0x0, 0x20(%rax,%r12)
movl -0xc(%r13), %eax
movl (%r13), %esi
shrdl $0x1f, %eax, %esi
movq (%r15), %rbp
movl %esi, 0x8(%rbp,%r12)
movl $0xffffffff, 0x20(%rbp,%r12) # imm = 0xFFFFFFFF
movl -0x18(%r13), %eax
movl %eax, (%rbp,%r12)
movl (%r13), %eax
andl %edx, %eax
movl %eax, 0x1c(%rbp,%r12)
movl -0x14(%r13), %eax
movl %eax, 0x14(%rbp,%r12)
movl -0x10(%r13), %eax
movl %eax, 0x18(%rbp,%r12)
movl -0x8(%r13), %eax
movl %eax, 0xc(%rbp,%r12)
movl -0x4(%r13), %eax
movl %eax, 0x10(%rbp,%r12)
cmpq $0x2, %rbx
jb 0x1a6298
movq %r9, %r15
xorl $0x1, %esi
movl -0x20(%rbp,%r12), %edx
xorl $0x1, %edx
movq %r8, %rdi
movq %r8, %r14
callq 0x1a5df0
movl %eax, -0x8(%rbp,%r12)
testl %eax, %eax
js 0x1a63ad
movq 0x20(%r14), %rcx
movl -0x20(%rbp,%r12), %edx
shrl %edx
movl (%rcx,%rdx,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
addl %eax, %ecx
movq %r15, %r9
addq %rcx, %r9
movq %r14, %r8
movq 0x20(%rsp), %r15
xorpd %xmm0, %xmm0
movq 0x18(%rsp), %rcx
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
addq $0x28, %r12
addq $0x1c, %r13
leaq (%rcx,%rbx), %rax
incq %rax
incq %rbx
cmpq $0x1, %rax
jne 0x1a61cb
jmp 0x1a62c0
xorl %eax, %eax
jmp 0x1a639e
xorl %r9d, %r9d
movl $0x1, %eax
xorpd %xmm0, %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x1a639e
movq (%r15), %rax
movl 0x30(%rax), %edi
movq 0x10(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0x8(%rax,%rcx,8), %edx
movq 0x20(%r8), %rsi
movl %edx, %r8d
shrl %r8d
movl (%rsi,%r8,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
subq %rsi, %r9
xorl %esi, %esi
testq %r9, %r9
cmovgq %r9, %rsi
testb $0x1, %dil
jne 0x1a6318
movl 0x34(%rax), %r9d
movq (%rsp), %r10
jmp 0x1a6334
movq (%rsp), %r10
movq 0x18(%r10), %r8
movq 0x20(%r8), %r8
shrl %edi
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl (%r8,%rdi,8), %r9d
subl 0x38(%rax), %r9d
movslq 0x3c(%rax), %r8
movslq 0x18(%rax,%rcx,8), %rdi
movslq %r9d, %r9
testb $0x1, %dl
jne 0x1a634b
addl 0x10(%rax,%rcx,8), %esi
jmp 0x1a6366
movq 0x18(%r10), %r10
movq 0x20(%r10), %r10
shrl %edx
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
andl (%r10,%rdx,8), %r11d
subl 0xc(%rax,%rcx,8), %esi
addl %r11d, %esi
movslq %esi, %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 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
xorl %eax, %eax
cmpq %rcx, %rdx
setle %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8e362(%rip), %rdi     # 0x234716
leaq 0x8d5d0(%rip), %rsi     # 0x23398b
leaq 0x8e372(%rip), %rcx     # 0x234734
movl $0x11cd, %edx           # imm = 0x11CD
callq 0x2110