// File: /workspace/github_cpp_makefile_star2_O1/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 0x1c99b4
movq %r8, %r12
movq %rcx, %r15
movq %rdx, %rbx
testq %rsi, %rsi
setns %al
cmpq %r8, %rdx
setl %cl
orb %al, %cl
je 0x1c99c6
movq %rdi, %r14
xorl %eax, %eax
testq %rsi, %rsi
movq %rsi, 0x8(%rsp)
cmovnsq %rsi, %rax
leaq (%rax,%rax,4), %rax
leaq (%r15,%rax,8), %rsi
leaq 0x58(%rsp), %rcx
leaq 0x18(%rsp), %r9
leaq 0x50(%rsp), %rdx
leaq 0x10(%rsp), %r13
movq %r13, %r8
callq 0x1a5e4b
cmpq %r12, %rbx
leaq (%r12,%r12,4), %rax
leaq -0x28(%r15,%rax,8), %rax
movq %rbx, 0x28(%rsp)
leaq (%rbx,%rbx,4), %rcx
movq %r15, 0x70(%rsp)
movq %rcx, 0x80(%rsp)
leaq (%r15,%rcx,8), %rsi
cmovgeq %rax, %rsi
leaq 0x68(%rsp), %rcx
leaq 0x48(%rsp), %r9
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %r8
movq %r14, %rbx
movq %r14, %rdi
callq 0x1a5e4b
movq 0x8(%r13), %rax
orq (%r13), %rax
setns %al
jns 0x1c9652
cmpq $0x0, 0x40(%rsp)
js 0x1c99e5
cmpq $0x0, 0x48(%rsp)
js 0x1c99e5
movq 0x40(%rsp), %r14
movq 0x48(%rsp), %r15
testq %r14, %r14
setns %cl
testq %r15, %r15
setns %dl
andb %cl, %dl
andb %dl, %al
cmpb $0x1, %al
movq %rbx, 0x78(%rsp)
jne 0x1c97cf
movq 0x8(%rsp), %rax
incq %rax
cmpq 0x28(%rsp), %rax
jge 0x1c97cf
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rdx
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %rbp
movq 0x60(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rsp), %rax
movq %rax, %rsi
notq %rsi
addq 0x28(%rsp), %rsi
leaq (%rax,%rax,4), %rax
movq 0x70(%rsp), %rcx
leaq (%rcx,%rax,8), %r13
addq $0x28, %r13
movq %rdx, 0x98(%rsp)
movq %rsi, 0xa0(%rsp)
movq 0x78(%rsp), %rdi
movq %r13, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
movq 0x38(%rsp), %rax
movl %eax, %ecx
subl %ebx, %ecx
movq 0x90(%rsp), %rdx
subl %ebx, %edx
movl %r14d, %esi
subl %r12d, %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 %r12d, %edx
movl %edx, 0x14(%r13)
movq 0x30(%rsp), %rcx
movl %ecx, %esi
subl %ebp, %esi
movq 0x88(%rsp), %rdi
subl %ebp, %edi
movl %r15d, %r8d
movq 0x98(%rsp), %r11
subl %r11d, %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 %r11d, %esi
movl %esi, 0x18(%r13)
movslq %edx, %r12
movq %r12, 0x10(%rsp)
movslq %esi, %rdx
movq 0xa0(%rsp), %rsi
movq %rdx, 0x18(%rsp)
movq %rax, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
addq $0x28, %r13
movq %rcx, %rbp
movq %rax, %rbx
decq %rsi
jne 0x1c96db
orq %r15, %r14
jns 0x1c98bd
movq 0x8(%rsp), %rax
incq %rax
cmpq 0x28(%rsp), %rax
jge 0x1c98bd
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r15
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %rbp
movq 0x8(%rsp), %rax
movq %rax, %r14
notq %r14
addq 0x28(%rsp), %r14
leaq (%rax,%rax,4), %rax
movq 0x70(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
addq $0x28, %r12
movq 0x78(%rsp), %rdi
movq %r12, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
movq 0x38(%rsp), %rax
movl %eax, %ecx
subl %ebx, %ecx
movl %ebp, %edx
subl %ebx, %edx
movl %r15d, %esi
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
subl %r13d, %esi
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
addl %r13d, %ecx
movl %ecx, 0x14(%r12)
movq 0x30(%rsp), %rdx
movq %rdx, %rsi
subq %rbp, %rsi
addq %r15, %rsi
movl %esi, 0x18(%r12)
movslq %ecx, %r13
movq %r13, 0x10(%rsp)
movslq %esi, %r15
movq %r15, 0x18(%rsp)
movq %rax, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
addq $0x28, %r12
movq %rdx, %rbp
movq %rax, %rbx
decq %r14
jne 0x1c9820
cmpq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rbx
movq 0x8(%rsp), %rdx
js 0x1c98db
cmpq $0x0, 0x18(%rsp)
jns 0x1c99b4
decq %rbx
cmpq %rdx, %rbx
jle 0x1c99b4
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %r14
movq 0x60(%rsp), %r13
movq 0x68(%rsp), %rbp
movq 0x70(%rsp), %rax
movq 0x80(%rsp), %rcx
leaq (%rax,%rcx,8), %r12
addq $-0x28, %r12
movq 0x78(%rsp), %rdi
movq %r12, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
movq 0x30(%rsp), %rax
movl %ebp, %ecx
subl %eax, %ecx
subl %r13d, %ebp
movl %r14d, %edx
subl %r15d, %edx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
subl %ecx, %r14d
movl %r14d, 0x18(%r12)
movq 0x38(%rsp), %rcx
movq %rcx, %rdx
subq %r13, %rdx
addq %r15, %rdx
movl %edx, 0x14(%r12)
movslq %edx, %r15
movq 0x8(%rsp), %rdx
movq %r15, 0x40(%rsp)
movslq %r14d, %r14
movq %r14, 0x48(%rsp)
movq %rcx, 0x60(%rsp)
movq %rax, 0x68(%rsp)
decq %rbx
addq $-0x28, %r12
movq %rax, %rbp
movq %rcx, %r13
cmpq %rdx, %rbx
jg 0x1c9910
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6d166(%rip), %rdi     # 0x236b33
leaq 0x69fb7(%rip), %rsi     # 0x23398b
leaq 0x6d16a(%rip), %rcx     # 0x236b45
movl $0x3c34, %edx           # imm = 0x3C34
callq 0x2110
leaq 0x6d1af(%rip), %rdi     # 0x236b9b
leaq 0x69f98(%rip), %rsi     # 0x23398b
leaq 0x6d14b(%rip), %rcx     # 0x236b45
movl $0x3c43, %edx           # imm = 0x3C43
callq 0x2110