// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq $0x0, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xc0(%rbp)
movq 0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x2100
cmpq $0x0, -0x20(%rbp)
jg 0x2d4da7
jmp 0x2d532d
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
shll %ecx
shrl %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
cmpq $0x0, -0x40(%rbp)
jl 0x2d52f4
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x60(%rbp)
movq -0xc8(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2d4ea6
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
movl 0x14(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0xd8(%rbp)
jmp 0x2d4eb7
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xc8(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2d4ef1
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
movl 0x10(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0xe0(%rbp)
jmp 0x2d4f02
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xc8(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
shrl %eax
movl %eax, %eax
movq 0x10(%rbp), %rcx
movslq 0x14(%rcx), %rcx
cmpq %rcx, %rax
jge 0x2d4f3f
movq -0xc8(%rbp), %rax
movl 0x4(%rax), %ecx
shll %ecx
shrl %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %eax
movq 0x10(%rbp), %rcx
movslq 0xc(%rcx), %rcx
cmpq %rcx, %rax
jge 0x2d4f67
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %eax
movq 0x10(%rbp), %rcx
movslq 0x18(%rcx), %rcx
cmpq %rcx, %rax
jle 0x2d4f8f
movq -0xc8(%rbp), %rax
movl 0x8(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movq 0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
cmpq %rcx, %rax
jle 0x2d4fb7
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq $0x0, -0x50(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x2d52a7
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movl 0x4(%rax), %eax
shll %eax
shrl %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq -0xd0(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x80(%rbp)
movq -0xd0(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2d5045
movq -0xc0(%rbp), %rax
movq -0xd0(%rbp), %rcx
movl 0x14(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0xe8(%rbp)
jmp 0x2d5056
movq -0xd0(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0xd0(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2d5093
movq -0xc0(%rbp), %rax
movq -0xd0(%rbp), %rcx
movl 0x10(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0xf0(%rbp)
jmp 0x2d50a4
movq -0xd0(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x2d52a5
movq -0x80(%rbp), %rax
cmpq -0x60(%rbp), %rax
jg 0x2d52a5
movq -0x88(%rbp), %rax
cmpq -0x68(%rbp), %rax
jg 0x2d52a5
movq -0x90(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x2d52a5
movq -0xc8(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
movq -0xd0(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
cmpl %ecx, %eax
jne 0x2d5110
jmp 0x2d512f
leaq 0xbdd55(%rip), %rdi     # 0x392e6c
leaq 0xbb711(%rip), %rsi     # 0x39082f
movl $0x399e, %edx           # imm = 0x399E
leaq 0xbe1fb(%rip), %rcx     # 0x393325
callq 0x2130
movq -0x78(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x2d5162
movq -0x80(%rbp), %rax
cmpq -0x60(%rbp), %rax
jne 0x2d5162
movq -0x88(%rbp), %rax
cmpq -0x68(%rbp), %rax
jne 0x2d5162
movq -0x90(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x2d5162
jmp 0x2d52e3
movq -0x60(%rbp), %rax
subq -0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x70(%rbp), %rax
subq -0x88(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jle 0x2d51aa
movq -0x98(%rbp), %rax
subq -0xa0(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0x2d51bf
movq -0xa0(%rbp), %rax
subq -0x98(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x98(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jl 0x2d51ed
movq -0x98(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0x2d51fb
movq -0xa0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xb0(%rbp)
movsd -0x38(%rbp), %xmm1
cvtsi2sdq -0xb0(%rbp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x2d523f
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jg 0x2d52a3
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %esi
movq -0xd0(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %edx
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %r8d
movq -0x28(%rbp), %r9
leaq -0x50(%rbp), %rax
movq %rax, (%rsp)
callq 0x2d0050
addq -0xb8(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x2d52a5
jmp 0x2d52e1
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0xc8(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
movq -0xc8(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, %r8d
xorl %edx, %edx
leaq -0x50(%rbp), %r9
callq 0x110c0
addq -0xb8(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x2d52e3
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2d4e2f
cmpq $0x3fffffff, -0xb8(%rbp) # imm = 0x3FFFFFFF
jle 0x2d530f
movl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq %rax, -0x108(%rbp)
jmp 0x2d531d
movq -0xb8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)