// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movsd %xmm0, 0x60(%rsp)
movq %r9, 0x28(%rsp)
movq 0xa0(%rsp), %rbx
movq %rdi, 0x48(%rsp)
movq (%rdi), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq %r8, 0x50(%rsp)
movl (%rax,%r8,8), %eax
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rbx)
movupd %xmm0, (%rbx)
andq $0x0, 0x20(%rbx)
testq %rcx, %rcx
jle 0x185575
movq %rcx, %r12
movq %rdx, %rbp
movq %rsi, %r13
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x40(%rsp)
leaq -0x1(%rcx), %r10
imulq $0x1c, -0x8(%rdx,%rcx,8), %rax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x4(%rsi,%rax), %ecx
andl %edx, %ecx
movl %ecx, 0x14(%rbx)
movl 0x8(%rsi,%rax), %ecx
movl %ecx, 0x18(%rbx)
movq 0x10(%rsi,%rax), %rax
movq %rax, 0xc(%rbx)
movq $0x0, 0x20(%rsp)
movq %r12, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
testq %r10, %r10
js 0x185561
imulq $0x1c, (%rbp,%r10,8), %rcx
leaq (%rcx,%r13), %rdx
cmpl $0x0, 0x18(%r13,%rcx)
js 0x1853b4
movl 0x10(%rdx), %r8d
movl 0x14(%rdx), %r11d
movq %r8, %rsi
movq %r11, %rax
jmp 0x1853ca
movl 0x10(%rdx), %r8d
movl 0x14(%rdx), %r11d
movq 0x40(%rsp), %rax
movq %rax, %rsi
subq %r11, %rsi
subq %r8, %rax
movl 0x4(%r13,%rcx), %r9d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %r9d
movl 0x8(%r13,%rcx), %edi
cmpl 0x14(%rbx), %r9d
jge 0x1853e6
movl %r9d, 0x14(%rbx)
movl %r8d, %ecx
movslq 0xc(%rbx), %r14
cmpq %r14, %rcx
jge 0x1853f6
movl %r8d, 0xc(%rbx)
movslq 0x18(%rbx), %r8
cmpq %r8, %rdi
jle 0x185402
movl %edi, 0x18(%rbx)
movl %r11d, %r8d
movslq 0x10(%rbx), %r14
cmpq %r14, %r8
jle 0x185412
movl %r11d, 0x10(%rbx)
andq $0x0, 0x58(%rsp)
leaq 0x1(%r10), %r15
cmpq %r12, %r15
jge 0x185448
imulq $0x1c, 0x8(%rbp,%r10,8), %r14
leaq (%r14,%r13), %r10
movl 0x18(%r13,%r14), %ebp
testl %ebp, %ebp
js 0x185468
movl 0x10(%r10), %r11d
movl 0x14(%r10), %r10d
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
jmp 0x185488
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
xorl %edx, %edx
leaq 0x58(%rsp), %r9
callq 0xa240
addq %rax, 0x20(%rsp)
jmp 0x1854e8
movl 0x10(%r10), %ebx
movl 0x14(%r10), %r12d
movq 0x40(%rsp), %r10
movq %r10, %r11
movq %r12, 0x38(%rsp)
subq %r12, %r11
movq %rbx, 0x30(%rsp)
subq %rbx, %r10
movl 0x4(%r13,%r14), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpl %r9d, %r12d
ja 0x1854d6
movl 0x8(%r13,%r14), %r14d
cmpl %edi, %r14d
ja 0x1854d6
cmpq %rsi, %r11
jg 0x1854d6
cmpq %rax, %r10
jg 0x1854d6
xorl 0x18(%rdx), %ebp
js 0x185584
cmpl %r9d, %r12d
movq 0x18(%rsp), %rbp
jne 0x1854f4
cmpl %edi, %r14d
jne 0x1854f4
cmpq %rsi, %r11
jne 0x1854f4
cmpq %rax, %r10
jne 0x1854f4
movq 0x10(%rsp), %r12
jmp 0x1854e0
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rbp
movq 0xa0(%rsp), %rbx
addq $-0x2, %r15
movq %r15, %r10
jmp 0x185389
subq %r12, %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 0x60(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq 0x28(%rsp), %rax
movq 0xa0(%rsp), %rbx
jg 0x185532
movq 0x28(%rsp), %rax
cmpq %rax, %rdx
movq 0x10(%rsp), %r12
jg 0x1854e8
movl 0x30(%rsp), %esi
movl 0x38(%rsp), %edx
leaq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %r9
callq 0x182953
jmp 0x18545e
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x20(%rsp), %rcx
cmpq %rax, %rcx
cmovlq %rcx, %rax
movl %eax, 0x1c(%rbx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6ebd5(%rip), %rdi     # 0x1f4160
leaq 0x6c591(%rip), %rsi     # 0x1f1b23
leaq 0x6f080(%rip), %rcx     # 0x1f4619
movl $0x399e, %edx           # imm = 0x399E
callq 0x2110