// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: update_existing_anchors(asg_t const*, ul_vec_t*, ma_ug_t*, ma_utg_t*, vec_mg_lchain_t*, long, mg_lchain_t*, kv_ul_ov_t*, kv_ul_ov_t*)
void update_existing_anchors(const asg_t *rg, ul_vec_t *rch, ma_ug_t *ug, ma_utg_t *u, vec_mg_lchain_t *res, int64_t res_n0, 
mg_lchain_t *uo, kv_ul_ov_t *raw_idx, kv_ul_ov_t *raw_chn)
{
	int64_t z = -1, m = res->n-1, midx, mdif, ovlp, novlp, mbeg, left[2], right[2]; 
	uint64_t zv;
	if(uo->off >= 0) z = raw_idx->a[uo->off].qn;///if there are some base-level alignments

	for (; z >= 0;) {
		zv = ((rch->bb.a[raw_chn->a[z].qn].hid<<1)+rch->bb.a[raw_chn->a[z].qn].rev);
		if(!(rg->seq[zv>>1].del)) {
			for (midx = mdif = -1, mbeg = m; m >= res_n0; m--) {
				if(zv==res->a[m].v) {
					if(((int32_t)raw_chn->a[z].ts == res->a[m].rs && (int32_t)raw_chn->a[z].te == res->a[m].re)) {
						midx = m; mdif = 0; 
						break;
					} else {
						ovlp = ((MIN((int32_t)raw_chn->a[z].te, res->a[m].re) > MAX((int32_t)raw_chn->a[z].ts, res->a[m].rs))? 
									(MIN((int32_t)raw_chn->a[z].te, res->a[m].re) - MAX((int32_t)raw_chn->a[z].ts, res->a[m].rs)):0);
						novlp = (raw_chn->a[z].te - raw_chn->a[z].ts - ovlp) + (res->a[m].re - res->a[m].rs - ovlp);
						if(midx==-1 || mdif>novlp) {
							midx = m; mdif = novlp;
						}
					}
				}
			}
			if(mdif != 0) {
				for (m = res->n-1; m > mbeg; m--) {
					if(zv == res->a[m].v) {
						if(((int32_t)raw_chn->a[z].ts == res->a[m].rs && (int32_t)raw_chn->a[z].te == res->a[m].re)) {
							midx = m; mdif = 0; 
							break;
						} else {
							ovlp = ((MIN((int32_t)raw_chn->a[z].te, res->a[m].re) > MAX((int32_t)raw_chn->a[z].ts, res->a[m].rs))? 
										(MIN((int32_t)raw_chn->a[z].te, res->a[m].re) - MAX((int32_t)raw_chn->a[z].ts, res->a[m].rs)):0);
							novlp = (raw_chn->a[z].te - raw_chn->a[z].ts - ovlp) + (res->a[m].re - res->a[m].rs - ovlp);
							if(midx==-1 || mdif>novlp) {
								midx = m; mdif = novlp;
							}
						}
					}
				}
			}
			m = midx; 
			// if(m < 0) fprintf(stderr, ">>>>>>[M::%s::] z->%ld\n", __func__, z);
			assert(m >= res_n0);
			res->a[m].qs = raw_chn->a[z].qs; res->a[m].qe = raw_chn->a[z].qe; 
			res->a[m].dist_pre = raw_chn->a[z].qn;///the idx of this chain at rch 
			// fprintf(stderr, "%c, res_n0->%ld, m->%ld, raw_idx->%u, qs->%d, qe->%d, ts->%d, te->%d, mdif->%ld\n", 
			// "+-"[(uo->v&1)], res_n0, m, raw_chn->a[z].qn, raw_chn->a[z].qs, raw_chn->a[z].qe, raw_chn->a[z].ts, raw_chn->a[z].te, mdif);
			


			left[0] = left[1] = -1; right[0] = right[1] = u->len+1;
			if(m > 0) get_r_offset(ug, &(res->a[m-1]), &left[0], &left[1], NULL, NULL);
			if(m + 1 < (int64_t)res->n) get_r_offset(ug, &(res->a[m+1]), &right[0], &right[1], NULL, NULL);

			// if(!(uo->v&1)) {///forward
			// 	if(m > 0) {
			// 		left[0] = a[m-1].rs; left[1] = a[m-1].re;
			// 	}
			// 	if(m + 1 < (int64_t)a_n) {
			// 		right[0] = a[m+1].rs; right[1] = a[m+1].re;
			// 	}
			// } else {//reverse
			// 	if(m > 0) {
			// 		right[0] = a[m-1].rs; right[1] = a[m-1].re;
			// 	}
			// 	if(m + 1 < (int64_t)a_n) { 
			// 		left[0] = a[m+1].rs; left[1] = a[m+1].re;
			// 	}
			// }
			///otherwise a[m] is not co-linear with a[m-1] and a[m+1]
			if(raw_chn->a[z].ts>=left[0]&&raw_chn->a[z].ts<=right[0]
							&&raw_chn->a[z].te>=left[1]&&raw_chn->a[z].te<=right[1]) {
				res->a[m].rs = raw_chn->a[z].ts; res->a[m].re = raw_chn->a[z].te; 
			}
		}
		if(raw_chn->a[z].tn == (uint32_t)-1) z = -1;
        else z = raw_chn->a[z].tn;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x38(%rsp)
movq 0xc0(%rsp), %rax
movslq (%rax), %rax
testq %rax, %rax
js 0x1d31e1
movq %r9, %rbx
movq 0xd0(%rsp), %rcx
movq 0xc8(%rsp), %rdx
movq 0x18(%rsp), %r8
movq 0x8(%r8), %r9
leaq -0x1(%r9), %r10
movq (%rdx), %rdx
leaq (%rax,%rax,8), %r8
leaq (%r8,%r8,2), %r8
addq %rax, %r8
movl (%rdx,%r8), %eax
movq 0x30(%rsi), %rdx
movq (%rcx), %r11
movq 0x20(%rdi), %rcx
movq %rcx, 0x78(%rsp)
movq %r9, 0x68(%rsp)
leaq (%r9,%r9,4), %rcx
movq %rdx, %r9
leaq -0x18(,%rcx,8), %rcx
movq %rcx, 0x50(%rsp)
movq %r10, %rcx
movq %r10, 0x58(%rsp)
movq %rdx, 0x60(%rsp)
movq %r11, 0x8(%rsp)
movl %eax, %eax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %r10
addq %rax, %r10
movl (%r11,%r10), %r14d
leaq (%r14,%r14,8), %rsi
movl (%r9,%rsi,4), %edx
movl %edx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x78(%rsp), %rax
cmpl $0x0, (%rax,%rdi,8)
js 0x1d30bf
movq %r14, 0x80(%rsp)
movq %r10, 0x10(%rsp)
leaq (%r11,%r10), %r14
movl 0x20(%r9,%rsi,4), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rdx,2), %edx
movq $-0x1, %rsi
movq $-0x1, %r15
cmpq %rbx, %rcx
jl 0x1d3003
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdi
addq %rax, %rdi
addq $0x14, %rdi
leaq (%rcx,%rcx,4), %r8
leaq (%rsi,%r8,8), %r8
addq $0x10, %r8
movq $-0x1, %rsi
movq %rcx, %r9
cmpl -0x8(%r8), %edx
jne 0x1d2fe6
movl 0x10(%r14), %ebp
movl -0x4(%r8), %eax
cmpl %eax, %ebp
jne 0x1d2f89
movl 0x14(%r14), %r11d
movl (%r8), %r12d
cmpl %r12d, %r11d
jne 0x1d2f8f
jmp 0x1d30c7
movl (%rdi), %r11d
movl (%r8), %r12d
cmpl %r12d, %r11d
movl %r12d, %r13d
cmovll %r11d, %r13d
cmpl %eax, %ebp
movl %eax, %r10d
cmovgl %ebp, %r10d
subl %r10d, %r13d
movl $0x0, %r10d
cmovlel %r10d, %r13d
movslq %r13d, %r13
subl %ebp, %r11d
subq %r13, %r11
subl %eax, %r12d
movslq %r12d, %r10
subq %r13, %r10
addq %r11, %r10
cmpq $-0x1, %r15
sete %al
cmpq %r10, %rsi
setg %r11b
orb %al, %r11b
movq %r9, %r11
jne 0x1d2fe0
movq %r15, %r11
movq %rsi, %r10
movq %r11, %r15
movq %r10, %rsi
leaq -0x1(%r9), %rax
addq $-0x28, %r8
cmpq %rbx, %r9
movq %rax, %r9
jg 0x1d2f66
testq %rsi, %rsi
je 0x1d30ca
movq 0x58(%rsp), %r9
cmpq %rcx, %r9
jle 0x1d30ca
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdi
addq %rax, %rdi
addq $0x14, %rdi
movq 0x18(%rsp), %rax
movq (%rax), %r8
addq 0x50(%rsp), %r8
cmpl -0x8(%r8), %edx
jne 0x1d30ad
movl 0x10(%r14), %ebp
movl -0x4(%r8), %r10d
cmpl %r10d, %ebp
jne 0x1d3050
movl 0x14(%r14), %r11d
movl (%r8), %r12d
cmpl %r12d, %r11d
jne 0x1d3056
jmp 0x1d30c7
movl (%rdi), %r11d
movl (%r8), %r12d
cmpl %r12d, %r11d
movl %r12d, %eax
cmovll %r11d, %eax
cmpl %r10d, %ebp
movl %r10d, %r13d
cmovgl %ebp, %r13d
subl %r13d, %eax
movl $0x0, %r13d
cmovlel %r13d, %eax
cltq
subl %ebp, %r11d
subq %rax, %r11
subl %r10d, %r12d
movslq %r12d, %r10
subq %rax, %r10
addq %r11, %r10
cmpq $-0x1, %r15
sete %al
cmpq %r10, %rsi
setg %r11b
orb %al, %r11b
movq %r9, %r11
jne 0x1d30a7
movq %r15, %r11
movq %rsi, %r10
movq %r11, %r15
movq %r10, %rsi
decq %r9
addq $-0x28, %r8
cmpq %rcx, %r9
jg 0x1d302f
jmp 0x1d30ca
movq %rcx, %r15
jmp 0x1d31d0
movq %r9, %r15
cmpq %rbx, %r15
jl 0x1d31f3
movq 0x18(%rsp), %rax
movq (%rax), %rbp
leaq (%r15,%r15,4), %r13
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x4(%rax,%rcx), %rax
movq %rax, 0x14(%rbp,%r13,8)
movq 0x80(%rsp), %rax
movl %eax, 0x20(%rbp,%r13,8)
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movq 0x70(%rsp), %rax
movl (%rax), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
incq %r12
movq %r12, 0x28(%rsp)
movq %r12, 0x20(%rsp)
testq %r15, %r15
jle 0x1d3150
leaq -0x28(,%r13,8), %rsi
addq %rbp, %rsi
movq 0x38(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1af0c9
leaq 0x1(%r15), %rax
cmpq 0x68(%rsp), %rax
jge 0x1d3189
leaq (%rax,%rax,4), %rax
leaq (,%rax,8), %rsi
addq %rbp, %rsi
movq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x28(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1af0c9
movq 0x20(%rsp), %r12
movl 0x10(%r14), %eax
cmpq %rax, 0x40(%rsp)
movq 0x60(%rsp), %r9
jg 0x1d31c6
cmpq %rax, %r12
jl 0x1d31c6
movl 0x14(%r14), %ecx
cmpq %rcx, 0x48(%rsp)
jg 0x1d31c6
cmpq %rcx, 0x28(%rsp)
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r10
jl 0x1d31d0
movl %eax, 0xc(%rbp,%r13,8)
movl %ecx, 0x10(%rbp,%r13,8)
jmp 0x1d31d0
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r10
movl 0xc(%r11,%r10), %eax
movq %r15, %rcx
cmpl $-0x1, %eax
jne 0x1d2ed1
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6fab8(%rip), %rdi     # 0x242cb2
leaq 0x6ca5a(%rip), %rsi     # 0x23fc5b
leaq 0x6fab6(%rip), %rcx     # 0x242cbe
movl $0x3be3, %edx           # imm = 0x3BE3
callq 0x2110