// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, 0x58(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, 0x60(%rsp)
movq 0xb0(%rsp), %rax
movq 0x8(%r8), %rcx
movq %rcx, 0x18(%rsp)
movslq (%rax), %rax
pushq $-0x1
popq %rcx
testq %rax, %rax
js 0x186ba6
movq 0xb8(%rsp), %rcx
movq (%rcx), %rcx
imulq $0x1c, %rax, %rax
movl (%rcx,%rax), %ecx
movq 0x18(%rsp), %rax
decq %rax
testq %rcx, %rcx
js 0x186ea8
movq 0x68(%rsp), %rdx
movq 0x30(%rdx), %rsi
movq 0xc0(%rsp), %rdx
movq (%rdx), %r10
imulq $0x1c, %rcx, %r11
movl (%r10,%r11), %r15d
imulq $0x24, %r15, %rdi
movl (%rsi,%rdi), %edx
movq 0x60(%rsp), %rcx
movq 0x20(%rcx), %r8
movl %edx, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
cmpl $0x0, (%r8,%r9,8)
js 0x186e89
movq %r15, 0x70(%rsp)
leaq (%r10,%r11), %r15
movl 0x20(%rsi,%rdi), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rdx,2), %edx
movq %r10, 0x8(%rsp)
movq %r11, (%rsp)
leaq (%r10,%r11), %rcx
addq $0x14, %rcx
movq %rcx, 0x10(%rsp)
imulq $0x28, %rax, %r9
addq $0x10, %r9
pushq $-0x1
popq %r8
movq %r8, %rdi
movq %rax, %r12
cmpq %rbx, %r12
jl 0x186cdc
movq (%r14), %r13
cmpl -0x8(%r13,%r9), %edx
jne 0x186cd0
movl 0x10(%r15), %ebp
movl -0x4(%r13,%r9), %esi
cmpl %esi, %ebp
jne 0x186c6d
movl 0x14(%r15), %r11d
movl (%r13,%r9), %ecx
cmpl %ecx, %r11d
jne 0x186c7a
jmp 0x186d8d
movq 0x10(%rsp), %rcx
movl (%rcx), %r11d
movl (%r13,%r9), %ecx
cmpl %ecx, %r11d
movl %ecx, %r13d
cmovll %r11d, %r13d
cmpl %esi, %ebp
movl %esi, %r10d
cmovgl %ebp, %r10d
subl %r10d, %r13d
movl $0x0, %r10d
cmovlel %r10d, %r13d
movslq %r13d, %r13
subl %ebp, %r11d
subq %r13, %r11
subl %esi, %ecx
movslq %ecx, %r10
subq %r13, %r10
addq %r11, %r10
cmpq $-0x1, %rdi
sete %cl
cmpq %r10, %r8
setg %sil
orb %cl, %sil
movq %r12, %r11
jne 0x186cca
movq %rdi, %r11
movq %r8, %r10
movq %r11, %rdi
movq %r10, %r8
decq %r12
addq $-0x28, %r9
jmp 0x186c36
testq %r8, %r8
je 0x186d8a
movq 0x18(%rsp), %r12
imulq $0x28, %r12, %r9
addq $0x10, %r9
decq %r12
cmpq %rax, %r12
jle 0x186d8a
movq (%r14), %r13
leaq -0x28(%r9), %rbp
cmpl -0x30(%r13,%r9), %edx
movq %rbp, %r9
jne 0x186cf2
movl 0x10(%r15), %r11d
movl -0x4(%r13,%rbp), %r9d
cmpl %r9d, %r11d
jne 0x186d2d
movl 0x14(%r15), %r10d
movl (%r13,%rbp), %ebp
cmpl %ebp, %r10d
jne 0x186d3a
jmp 0x186d8d
movq 0x10(%rsp), %rcx
movl (%rcx), %r10d
movl (%r13,%rbp), %ebp
cmpl %ebp, %r10d
movl %ebp, %ecx
cmovll %r10d, %ecx
cmpl %r9d, %r11d
movl %r9d, %esi
cmovgl %r11d, %esi
subl %esi, %ecx
movl $0x0, %esi
cmovlel %esi, %ecx
movslq %ecx, %rcx
subl %r11d, %r10d
subq %rcx, %r10
subl %r9d, %ebp
movslq %ebp, %rsi
subq %rcx, %rsi
addq %r10, %rsi
cmpq $-0x1, %rdi
sete %cl
cmpq %rsi, %r8
setg %r9b
orb %cl, %r9b
cmovneq %r12, %rdi
cmovneq %rsi, %r8
jmp 0x186cea
movq %rdi, %r12
cmpq %rbx, %r12
jl 0x186eb7
movq (%r14), %r13
imulq $0x28, %r12, %rdx
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
movq 0x4(%rax,%rcx), %rax
movq %rax, 0x14(%r13,%rdx)
movq 0x70(%rsp), %rax
movl %eax, 0x20(%r13,%rdx)
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movq 0x58(%rsp), %rax
movl (%rax), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
incq %rbp
movq %rbp, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
testq %r12, %r12
jle 0x186e0f
leaq (%rdx,%r13), %rsi
addq $-0x28, %rsi
movq 0x38(%rsp), %rdi
movq %rdx, 0x10(%rsp)
leaq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq 0x10(%rsp), %rdx
leaq 0x1(%r12), %rax
cmpq 0x18(%rsp), %rax
jge 0x186e47
imulq $0x28, %rax, %rsi
addq %r13, %rsi
movq 0x38(%rsp), %rdi
movq %rdx, %rbp
leaq 0x20(%rsp), %rdx
leaq 0x28(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq %rbp, %rdx
movq 0x20(%rsp), %rbp
movl 0x10(%r15), %eax
cmpq %rax, 0x40(%rsp)
jg 0x186e7e
cmpq %rax, %rbp
jl 0x186e7e
movl 0x14(%r15), %ecx
cmpq %rcx, 0x48(%rsp)
jg 0x186e7e
cmpq %rcx, 0x28(%rsp)
movq 0x8(%rsp), %r10
movq (%rsp), %r11
jl 0x186e8c
movl %eax, 0xc(%r13,%rdx)
movl %ecx, 0x10(%r13,%rdx)
jmp 0x186e8c
movq 0x8(%rsp), %r10
movq (%rsp), %r11
jmp 0x186e8c
movq %rax, %r12
movl 0xc(%r10,%r11), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rcx
pushq $-0x1
popq %rax
cmoveq %rax, %rcx
movq %r12, %rax
jmp 0x186bae
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6dcbc(%rip), %rdi     # 0x1f4b7a
leaq 0x6ac5e(%rip), %rsi     # 0x1f1b23
leaq 0x6dcba(%rip), %rcx     # 0x1f4b86
movl $0x3be3, %edx           # imm = 0x3BE3
callq 0x2110