// File: /workspace/github_cpp_makefile_star2_O1/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 %r9, %rbx
movq %r8, 0x18(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x38(%rsp)
movq 0xc0(%rsp), %rax
movslq (%rax), %rax
testq %rax, %rax
js 0x1c8fb2
movq 0xc8(%rsp), %rcx
movq (%rcx), %rcx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl (%rcx,%rdx), %ecx
jmp 0x1c8fb9
movq $-0x1, %rcx
testq %rcx, %rcx
js 0x1c9337
movq 0x18(%rsp), %rax
movq 0x8(%rax), %r8
movq 0xd0(%rsp), %rdx
leaq -0x1(%r8), %rax
movq 0x30(%rsi), %r9
movq (%rdx), %r10
movq 0x20(%rdi), %rdx
movq %rdx, 0x78(%rsp)
leaq -0x1(%r8), %rdx
movq %rdx, 0x60(%rsp)
movq %r8, 0x68(%rsp)
leaq (%r8,%r8,4), %rdx
leaq -0x18(,%rdx,8), %rdx
movq %rdx, 0x58(%rsp)
movq %r9, 0x30(%rsp)
movq %r10, 0x8(%rsp)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %r8
addq %rcx, %r8
movl (%r10,%r8), %r11d
leaq (%r11,%r11,8), %rsi
movl (%r9,%rsi,4), %edx
movl %edx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x78(%rsp), %rcx
cmpl $0x0, (%rcx,%rdi,8)
js 0x1c9134
movq %r11, 0x80(%rsp)
movq %r8, 0x10(%rsp)
leaq (%r10,%r8), %rbp
movl 0x20(%r9,%rsi,4), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rdx,2), %edx
movq $-0x1, %r14
movq $-0x1, %rsi
cmpq %rbx, %rax
jl 0x1c9141
movq 0x18(%rsp), %rcx
movq (%rcx), %rsi
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdi
addq %rcx, %rdi
addq $0x14, %rdi
leaq (%rax,%rax,4), %r8
leaq (%rsi,%r8,8), %r8
addq $0x10, %r8
movq $-0x1, %rsi
movq %rax, %r9
cmpl -0x8(%r8), %edx
jne 0x1c911e
movl 0x10(%rbp), %r15d
movslq -0x4(%r8), %r11
cmpl %r11d, %r15d
jne 0x1c90bf
movl 0x14(%rbp), %r10d
cmpl (%r8), %r10d
je 0x1c913c
movl (%rdi), %r13d
movslq (%r8), %r10
cmpl %r10d, %r13d
cmovgel %r10d, %r13d
movl -0x4(%rdi), %ecx
movl -0x4(%r8), %r12d
cmpl %r12d, %ecx
cmovgl %ecx, %r12d
subl %r12d, %r13d
movl $0x0, %ecx
cmovlel %ecx, %r13d
movslq %r13d, %rcx
movl 0x14(%rbp), %r12d
subl %r15d, %r12d
subq %rcx, %r12
subq %r11, %r10
subq %rcx, %r10
addq %r12, %r10
cmpq $-0x1, %r14
sete %cl
cmpq %r10, %rsi
setg %r11b
orb %cl, %r11b
movq %r9, %r11
jne 0x1c9118
movq %r14, %r11
movq %rsi, %r10
movq %r11, %r14
movq %r10, %rsi
leaq -0x1(%r9), %rcx
addq $-0x28, %r8
cmpq %rbx, %r9
movq %rcx, %r9
jg 0x1c90a3
jmp 0x1c9141
movq %rax, %r14
jmp 0x1c9316
movq %r9, %r14
xorl %esi, %esi
testq %rsi, %rsi
je 0x1c9206
movq 0x60(%rsp), %r9
cmpq %rax, %r9
jle 0x1c9206
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdi
addq %rcx, %rdi
addq $0x14, %rdi
movq 0x18(%rsp), %rcx
movq (%rcx), %r8
addq 0x58(%rsp), %r8
cmpl -0x8(%r8), %edx
jne 0x1c91f1
movl 0x10(%rbp), %r15d
movslq -0x4(%r8), %r11
cmpl %r11d, %r15d
jne 0x1c9191
movl 0x14(%rbp), %ecx
cmpl (%r8), %ecx
je 0x1c9203
movl (%rdi), %ecx
movslq (%r8), %r10
cmpl %r10d, %ecx
cmovgel %r10d, %ecx
movl -0x4(%rdi), %r12d
movl -0x4(%r8), %r13d
cmpl %r13d, %r12d
cmovgl %r12d, %r13d
subl %r13d, %ecx
movl $0x0, %r12d
cmovlel %r12d, %ecx
movslq %ecx, %rcx
movl 0x14(%rbp), %r12d
subl %r15d, %r12d
subq %rcx, %r12
subq %r11, %r10
subq %rcx, %r10
addq %r12, %r10
cmpq $-0x1, %r14
sete %cl
cmpq %r10, %rsi
setg %r11b
orb %cl, %r11b
movq %r9, %r11
jne 0x1c91eb
movq %r14, %r11
movq %rsi, %r10
movq %r11, %r14
movq %r10, %rsi
decq %r9
addq $-0x28, %r8
cmpq %rax, %r9
jg 0x1c9176
jmp 0x1c9206
movq %r9, %r14
cmpq %rbx, %r14
jl 0x1c9349
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rsi
movl 0x4(%rdx,%rsi), %eax
movq 0x18(%rsp), %rcx
movq (%rcx), %r13
leaq (%r14,%r14,4), %r15
movl %eax, 0x14(%r13,%r15,8)
movl 0x8(%rdx,%rsi), %eax
movl %eax, 0x18(%r13,%r15,8)
movq 0x80(%rsp), %rax
movl %eax, 0x20(%r13,%r15,8)
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movq 0x70(%rsp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
incq %rax
movq %rax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
testq %r14, %r14
jle 0x1c9291
leaq -0x28(,%r15,8), %rsi
addq %r13, %rsi
movq 0x38(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
leaq 0x1(%r14), %rax
cmpq 0x68(%rsp), %rax
jge 0x1c92c5
leaq (%rax,%rax,4), %rax
leaq (,%rax,8), %rsi
addq %r13, %rsi
movq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rdx
leaq 0x28(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
movl 0x10(%rbp), %eax
cmpq %rax, 0x40(%rsp)
jg 0x1c9307
cmpq %rax, 0x20(%rsp)
movq 0x30(%rsp), %r9
jl 0x1c930c
movl 0x14(%rbp), %ecx
cmpq %rcx, 0x48(%rsp)
jg 0x1c930c
cmpq %rcx, 0x28(%rsp)
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %r8
jl 0x1c9316
movl %eax, 0xc(%r13,%r15,8)
movl 0x14(%r10,%r8), %eax
movl %eax, 0x10(%r13,%r15,8)
jmp 0x1c9316
movq 0x30(%rsp), %r9
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %r8
movl 0xc(%r10,%r8), %ecx
movq %r14, %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
movq $-0x1, %rdx
cmoveq %rdx, %rcx
jne 0x1c9010
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6d692(%rip), %rdi     # 0x2369e2
leaq 0x6a634(%rip), %rsi     # 0x23398b
leaq 0x6d690(%rip), %rcx     # 0x2369ee
movl $0x3be3, %edx           # imm = 0x3BE3
callq 0x2110