// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: update_rovlp_chain_qse_back(ma_ug_t*, long, long, mg_lchain_t*, long)
void update_rovlp_chain_qse_back(ma_ug_t *ug, int64_t sidx, int64_t eidx, mg_lchain_t *a, int64_t a_n)
{
	if(eidx - sidx <= 1) return;
	assert(sidx>=0||eidx<a_n);
	// fprintf(stderr, "******[M::%s::] sidx:%ld, eidx:%ld\n", __func__, sidx, eidx);
	int64_t left_r[2], right_r[2], left_q[2], right_q[2]; 
	int64_t i, rs, re;//qs or qe might be -1, while rs and re should >= 0
	if(sidx >= 0) {
		get_r_offset(ug, &(a[sidx]), &left_r[0], &left_r[1], &left_q[0], &left_q[1]);
	} else {
		get_r_offset(ug, &(a[0]), &left_r[0], &left_r[1], &left_q[0], &left_q[1]);
	}

	if(eidx < a_n) {
		get_r_offset(ug, &(a[eidx]), &right_r[0], &right_r[1], &right_q[0], &right_q[1]);
	} else {
		get_r_offset(ug, &(a[a_n-1]), &right_r[0], &right_r[1], &right_q[0], &right_q[1]);
	}
	assert((left_q[0] >= 0 && left_q[1] >= 0) || (right_q[0] >= 0 && right_q[1] >= 0)); ///assert(re >= rs);
	// fprintf(stderr, "##[M::%s::] right_q[0]:%ld, right_q[1]:%ld, left_q[0]:%ld, left_q[1]:%ld\n", 
	// __func__, right_q[0], right_q[1], left_q[0], left_q[1]);
	// fprintf(stderr, "##[M::%s::] right_r[0]:%ld, right_r[1]:%ld, left_r[0]:%ld, left_r[1]:%ld\n", 
	// __func__, right_r[0], right_r[1], left_r[0], left_r[1]);
	// if(left_q[0] >= 0 && left_q[1] >= 0 && right_q[0] >= 0 && right_q[1] >= 0) {
	// 	rlen[0] = (right_r[0] - left_r[0]); rlen[1] = (right_r[1] - left_r[1]); 
	// 	qlen[0] = (right_q[0] - left_q[0]); qlen[1] = (right_q[1] - left_q[1]); 
	// 	for (i = sidx+1; i < eidx; i++) {
	// 		get_r_offset(ug, &(a[i]), &rs, &re, NULL, NULL);
	// 		a[i].qs = left_q[0] + get_offset_adjust((rs - left_r[0]), rlen[0], qlen[0]);
	// 		a[i].qe = left_q[1] + get_offset_adjust((re - left_r[1]), rlen[1], qlen[1]);
	// 	}
	// }

	if(left_q[0] >= 0 && left_q[1] >= 0 && right_q[0] >= 0 && right_q[1] >= 0) {
		// fprintf(stderr, "+++sidx:%ld+++ left_qs:%ld, left_qe:%ld, left_rs:%ld, left_re:%ld\n", 
		// sidx, left_q[0], left_q[1], left_r[0], left_r[1]);
		// fprintf(stderr, "---eidx:%ld--- right_qs:%ld, right_qe:%ld, right_rs:%ld, right_re:%ld\n", 
		// eidx, right_q[0], right_q[1], right_r[0], right_r[1]);

		for (i = sidx+1; i < eidx; i++) {
			get_r_offset(ug, &(a[i]), &rs, &re, NULL, NULL);
			// a[i].qs = left_q[0] + get_offset_adjust((rs - left_r[0]), rlen[0], qlen[0]);
			///a[i].qs>=left_q[0] && a[i].qs<left_q[0]
			// a[i].qs = left_q[0] + get_offset_adjust(rs - left_r[0], left_r[1]-left_r[0], left_q[1]-left_q[0]);
			a[i].qs = left_q[0] + get_offset_adjust(rs - left_r[0], right_r[0]-left_r[0], right_q[0]-left_q[0]);
			// a[i].qe = left_q[1] + get_offset_adjust((re - left_r[1]), rlen[1], qlen[1]);
			a[i].qe = left_q[1] + get_offset_adjust((re - left_r[1]), right_r[1] - left_r[1], right_q[1] - left_q[1]);
			
			// fprintf(stderr, ">>>i:%ld<<< a[i].qs:%u, a[i].qe:%u, rs:%ld, re:%ld\n", i, a[i].qs, a[i].qe, rs, re);

			left_q[0] = a[i].qs; left_q[1] = a[i].qe;
			left_r[0] = rs; left_r[1] = re;
		}
	}

	if(right_q[0] < 0 || right_q[1] < 0) {
		for (i = sidx+1; i < eidx; i++) {
			get_r_offset(ug, &(a[i]), &rs, &re, NULL, NULL);
			///a[i].qs>=left_q[0] && a[i].qs<left_q[0]
			a[i].qs = left_q[0] + get_offset_adjust(rs - left_r[0], left_r[1]-left_r[0], left_q[1]-left_q[0]);
			///a[i].qe>=left_q[1]
			a[i].qe = left_q[1] + (re - left_r[1]);
			left_q[0] = a[i].qs; left_q[1] = a[i].qe;
			left_r[0] = rs; left_r[1] = re;
		}
	}

	if(left_q[0] < 0 || left_q[1] < 0) {
		for (i = eidx-1; i > sidx; i--) {
			get_r_offset(ug, &(a[i]), &rs, &re, NULL, NULL);
			a[i].qe = right_q[1] - get_offset_adjust(right_r[1]-re, right_r[1]-right_r[0], right_q[1]-right_q[0]);
			a[i].qs = right_q[0] - (right_r[0]-rs);
			right_q[0] = a[i].qs; right_q[1] = a[i].qe;
			right_r[0] = rs; right_r[1] = re;
		}
	}
	// if(left_q[0] < 0) left_q[0] = right_q[0] - (right_r[0] - left_r[0]);
	// if(left_q[1] < 0) left_q[1] = right_q[1] - (right_r[1] - left_r[1]);
	// if(right_q[0] < 0 || right_q[1] < 0) {
	// 	right_q[0] = left_q[0] + (right_r[0] - left_r[0]);
	// 	right_q[1] = left_q[1] + (right_r[1] - left_r[1]);
	// }	
	
	// fprintf(stderr, "******[M::%s::] right_q[0]:%ld, right_q[1]:%ld\n", __func__, right_q[0], right_q[1]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %rax
subq %rsi, %rax
cmpq $0x2, %rax
jl 0x1d3872
movq %r8, %r12
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %rbp
testq %rsi, %rsi
setns %al
cmpq %r8, %rdx
setl %cl
orb %al, %cl
je 0x1d3884
movq %rdi, %r14
xorl %eax, %eax
testq %rbp, %rbp
cmovnsq %rbp, %rax
leaq (%rax,%rax,4), %rax
leaq (%r15,%rax,8), %rsi
leaq 0x28(%rsp), %rcx
leaq 0x58(%rsp), %r9
leaq 0x20(%rsp), %rdx
leaq 0x50(%rsp), %r13
movq %r13, %r8
callq 0x1af0c9
cmpq %r12, %rbx
leaq (%r12,%r12,4), %rax
leaq -0x28(%r15,%rax,8), %rax
movq %rbx, 0x40(%rsp)
leaq (%rbx,%rbx,4), %rbx
leaq (%r15,%rbx,8), %rsi
cmovgeq %rax, %rsi
leaq 0x38(%rsp), %rcx
leaq 0x78(%rsp), %r9
leaq 0x30(%rsp), %rdx
leaq 0x70(%rsp), %r12
movq %r14, 0x8(%rsp)
movq %r14, %rdi
movq %r12, %r8
callq 0x1af0c9
movq (%r13), %r14
movq 0x8(%r13), %rsi
movq (%r12), %r9
movq 0x8(%r12), %r10
movq %r9, %rax
orq %r10, %rax
setns %al
movq %rsi, %rcx
orq %r14, %rcx
movq %rbp, 0x48(%rsp)
movq %r15, 0x60(%rsp)
movq %rbx, 0x80(%rsp)
movq %r10, 0x68(%rsp)
js 0x1d3679
testb %al, %al
je 0x1d3681
leaq 0x1(%rbp), %rax
movq 0x40(%rsp), %rcx
cmpq %rcx, %rax
jge 0x1d3681
movq 0x20(%rsp), %r12
movq 0x28(%rsp), %rbx
movq 0x30(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x90(%rsp)
movq %rbp, %rdx
notq %rdx
addq %rcx, %rdx
leaq (,%rbp,4), %rax
addq %rbp, %rax
leaq (%r15,%rax,8), %r13
addq $0x28, %r13
movq %rdx, 0xa0(%rsp)
movq 0x8(%rsp), %rdi
movq %rsi, %rbp
movq %r13, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
movq %r9, %r15
xorl %r9d, %r9d
callq 0x1af0c9
movq %r15, %r9
movq 0x18(%rsp), %rax
movl %eax, %ecx
subl %r12d, %ecx
movq 0x98(%rsp), %rdx
subl %r12d, %edx
movl %r9d, %esi
subl %r14d, %esi
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rdx
addl %r14d, %edx
movq 0x68(%rsp), %r10
movl %edx, 0x14(%r13)
movq 0x10(%rsp), %rcx
movl %ecx, %esi
subl %ebx, %esi
movq 0x90(%rsp), %rdi
subl %ebx, %edi
movl %r10d, %r8d
subl %ebp, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r8, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rsi
addl %ebp, %esi
movl %esi, 0x18(%r13)
movslq %edx, %r14
movq 0xa0(%rsp), %rdx
movq %r14, 0x50(%rsp)
movslq %esi, %rsi
movq %rsi, 0x58(%rsp)
movq %rax, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
addq $0x28, %r13
movq %rcx, %rbx
movq %rax, %r12
decq %rdx
jne 0x1d3584
jmp 0x1d3681
testb %al, %al
je 0x1d38a3
movq %r10, %rax
orq %r9, %rax
movq %r9, 0x88(%rsp)
jns 0x1d3778
movq 0x48(%rsp), %rdx
leaq 0x1(%rdx), %rax
movq 0x40(%rsp), %rcx
cmpq %rcx, %rax
jge 0x1d3778
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rbx
movq %rdx, %r13
notq %r13
addq %rcx, %r13
leaq (%rdx,%rdx,4), %rax
movq 0x60(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
addq $0x28, %r12
movq 0x8(%rsp), %rdi
movq %rsi, %rbp
movq %r12, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1af0c9
movq 0x18(%rsp), %rax
movl %eax, %ecx
subl %r15d, %ecx
movl %ebx, %edx
subl %r15d, %edx
movl %ebp, %esi
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
subl %r14d, %esi
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
addl %r14d, %ecx
movl %ecx, 0x14(%r12)
movq 0x10(%rsp), %rdx
movq %rdx, %rsi
subq %rbx, %rsi
addq %rbp, %rsi
movl %esi, 0x18(%r12)
movslq %ecx, %r14
movq %r14, 0x50(%rsp)
movslq %esi, %rsi
movq %rsi, 0x58(%rsp)
movq %rax, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
addq $0x28, %r12
movq %rdx, %rbx
movq 0x8(%rsp), %rdi
movq %rax, %r15
decq %r13
jne 0x1d36d5
jmp 0x1d377d
movq 0x8(%rsp), %rdi
testq %r14, %r14
movq 0x40(%rsp), %rbp
movq 0x48(%rsp), %rdx
movq 0x88(%rsp), %r13
movq 0x68(%rsp), %r14
js 0x1d37a2
testq %rsi, %rsi
jns 0x1d3872
decq %rbp
cmpq %rdx, %rbp
jle 0x1d3872
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %r15
movq 0x60(%rsp), %rax
movq 0x80(%rsp), %rcx
leaq (%rax,%rcx,8), %r12
addq $-0x28, %r12
movq %r12, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1af0c9
movq 0x10(%rsp), %rax
movl %r15d, %ecx
subl %eax, %ecx
subl %ebx, %r15d
movl %r14d, %edx
subl %r13d, %edx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
subl %ecx, %r14d
movl %r14d, 0x18(%r12)
movq 0x18(%rsp), %rcx
movq %rcx, %rdx
subq %rbx, %rdx
addq %r13, %rdx
movl %edx, 0x14(%r12)
movslq %edx, %r13
movq 0x48(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq %r13, 0x70(%rsp)
movslq %r14d, %r14
movq %r14, 0x78(%rsp)
movq %rcx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
decq %rbp
addq $-0x28, %r12
movq %rax, %r15
movq %rcx, %rbx
cmpq %rdx, %rbp
jg 0x1d37cd
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6f578(%rip), %rdi     # 0x242e03
leaq 0x6c3c9(%rip), %rsi     # 0x23fc5b
leaq 0x6f57c(%rip), %rcx     # 0x242e15
movl $0x3c34, %edx           # imm = 0x3C34
callq 0x2110
leaq 0x6f5c1(%rip), %rdi     # 0x242e6b
leaq 0x6c3aa(%rip), %rsi     # 0x23fc5b
leaq 0x6f55d(%rip), %rcx     # 0x242e15
movl $0x3c43, %edx           # imm = 0x3C43
callq 0x2110