// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: update_exist_chain(ul_idx_t const*, ul_ov_t*, unsigned long*, long, long, long, double, mg_lchain_t*)
void update_exist_chain(const ul_idx_t *uref, ul_ov_t *ch, uint64_t *idx, int64_t idx_n, int64_t tid, int64_t bw, 
double diff_ec_ul, mg_lchain_t *res)
{
	int64_t i, j, cov_i, i_qs, i_qe, i_ts, i_te, j_qs, j_qe, j_ts, j_te, dq, dt, dd, mm, sc = 0; 
	int64_t tlen = uref->ug->g->seq[tid].len; memset(res, 0, sizeof(*res));
	ul_ov_t *li, *lj;
	if(idx_n <= 0) return;
	i = idx_n - 1; 
	res->qs = (ch[idx[i]].qs<<1)>>1; res->qe = ch[idx[i]].qe;
	res->rs = ch[idx[i]].ts; res->re = ch[idx[i]].te;
	for (; i >= 0; i--) {
		li = &(ch[idx[i]]); 
		i_qs = (li->qs<<1)>>1; i_qe = li->qe; 
		i_ts = (li->rev?(tlen-li->te):(li->ts));
		i_te = (li->rev?(tlen-li->ts):(li->te));

		if((int64_t)((li->qs<<1)>>1) < res->qs) res->qs = ((li->qs<<1)>>1); 
		if((int64_t)li->ts < res->rs) res->rs = li->ts;
		if((int64_t)li->qe > res->qe) res->qe = li->qe;
		if((int64_t)li->te > res->re) res->re = li->te;

		cov_i = 0; j = i + 1;
		if(j < idx_n) {
			lj = &(ch[idx[j]]); 
			j_qs = (lj->qs<<1)>>1; j_qe = lj->qe; 
			j_ts = (lj->rev?(tlen-lj->te):(lj->ts));
			j_te = (lj->rev?(tlen-lj->ts):(lj->te));
			if(j_qs <= i_qs && j_qe <= i_qe && j_ts <= i_ts && j_te <= i_te) {///co-linear
				assert(li->rev == lj->rev);
				if(j_qs == i_qs && j_qe == i_qe && j_ts == i_ts && j_te == i_te) continue;
				dq = i_qe - j_qs; dt = i_te - j_ts;
                dd = (dq>dt? dq-dt:dt-dq);
                mm = MAX(dq, dt); mm *= diff_ec_ul; if(mm < bw) mm = bw;
				if(dd <= mm) {///pass distance checking
                    sc += get_add_cov_score(uref, lj->ts, lj->te, li->ts, li->te, tid, &cov_i);
                }
			}
		} else {
			sc += retrieve_u_cov_region(uref, tid, 0, li->ts, li->te, &cov_i);
		}
	}
	res->score = (sc>0x3FFFFFFF?0x3FFFFFFF:sc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x50(%rsp)
movq %r9, 0x30(%rsp)
movq 0x90(%rsp), %r14
movq %rdi, 0x38(%rsp)
movq (%rdi), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq %r8, 0x40(%rsp)
movl (%rax,%r8,8), %ebp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%r14)
movupd %xmm0, (%r14)
movq $0x0, 0x20(%r14)
testq %rcx, %rcx
jle 0x1c767c
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r13
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq -0x8(%rdx,%rcx,8), %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x4(%rsi,%rcx), %eax
andl %edx, %eax
movl %eax, 0x14(%r14)
movl 0x8(%rsi,%rcx), %eax
movl %eax, 0x18(%r14)
movl 0x10(%rsi,%rcx), %eax
movl %eax, 0xc(%r14)
movl 0x14(%rsi,%rcx), %eax
movl %eax, 0x10(%r14)
movq $0x0, 0x28(%rsp)
movq %r12, %r15
movq %r12, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movq -0x8(%rbx,%r15,8), %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
leaq (%rcx,%r13), %rsi
movl 0x18(%r13,%rcx), %eax
testl %eax, %eax
js 0x1c74a5
movl 0x10(%rsi), %edx
jmp 0x1c74ae
movl 0x14(%rsi), %edi
movq %rbp, %rdx
subq %rdi, %rdx
testl %eax, %eax
js 0x1c74b7
movl 0x14(%rsi), %eax
jmp 0x1c74c0
movl 0x10(%rsi), %edi
movq %rbp, %rax
subq %rdi, %rax
movl 0x4(%r13,%rcx), %r9d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %r9d
movl 0x8(%r13,%rcx), %edi
cmpl 0x14(%r14), %r9d
jge 0x1c74dc
movl %r9d, 0x14(%r14)
movl 0x10(%rsi), %ecx
movslq 0xc(%r14), %r8
cmpq %r8, %rcx
jge 0x1c74ec
movl %ecx, 0xc(%r14)
movslq 0x18(%r14), %r8
cmpq %r8, %rdi
jle 0x1c74f9
movl %edi, 0x18(%r14)
movl 0x14(%rsi), %r8d
movslq 0x10(%r14), %r10
cmpq %r10, %r8
jle 0x1c750a
movl %r8d, 0x10(%r14)
movq $0x0, 0x48(%rsp)
cmpq %r12, %r15
jge 0x1c753d
movq (%rbx,%r15,8), %r10
leaq (%r10,%r10,8), %r11
leaq (%r11,%r11,2), %rbp
addq %r10, %rbp
movq %r13, %r10
addq %rbp, %r10
movl 0x18(%r13,%rbp), %r14d
testl %r14d, %r14d
js 0x1c755d
movl 0x10(%r10), %r11d
jmp 0x1c7569
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
xorl %edx, %edx
leaq 0x48(%rsp), %r9
callq 0x36ec9
addq %rax, 0x28(%rsp)
jmp 0x1c765e
movl 0x14(%r10), %ebx
movq 0x10(%rsp), %r11
subq %rbx, %r11
testl %r14d, %r14d
js 0x1c7574
movl 0x14(%r10), %r12d
jmp 0x1c7580
movl 0x10(%r10), %ebx
movq 0x10(%rsp), %r12
subq %rbx, %r12
movl 0x4(%r13,%rbp), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
cmpl %r9d, %ebx
ja 0x1c7647
movl 0x8(%r13,%rbp), %ebp
cmpl %edi, %ebp
ja 0x1c7647
cmpq %rdx, %r11
jg 0x1c7647
cmpq %rax, %r12
jg 0x1c7647
xorl 0x18(%rsi), %r14d
js 0x1c768b
cmpl %r9d, %ebx
movq 0x90(%rsp), %r14
jne 0x1c75d8
cmpl %edi, %ebp
jne 0x1c75d8
cmpq %rdx, %r11
jne 0x1c75d8
cmpq %rax, %r12
je 0x1c764f
subq %rbx, %rdi
subq %r11, %rax
movq %rdi, %rsi
subq %rax, %rsi
cmovgq %rdi, %rax
movq %rsi, %rdx
negq %rdx
cmovsq %rsi, %rdx
cvtsi2sd %rax, %xmm0
mulsd 0x50(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq 0x30(%rsp), %rax
jg 0x1c760e
movq 0x30(%rsp), %rax
cmpq %rax, %rdx
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rbp
jg 0x1c765e
movl 0x10(%r10), %esi
movl 0x14(%r10), %edx
leaq 0x48(%rsp), %rax
movq %rax, (%rsp)
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %r9
callq 0x1c4710
jmp 0x1c7553
movq 0x90(%rsp), %r14
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rbp
decq %r15
jg 0x1c7483
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x28(%rsp), %rcx
cmpq %rax, %rcx
cmovlq %rcx, %rax
movl %eax, 0x1c(%r14)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6e936(%rip), %rdi     # 0x235fc8
leaq 0x6c2f2(%rip), %rsi     # 0x23398b
leaq 0x6ede1(%rip), %rcx     # 0x236481
movl $0x399e, %edx           # imm = 0x399E
callq 0x2110