// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %rdi, 0x38(%rsp)
movq %rdx, %rax
subq %rsi, %rax
cmpq $0x2, %rax
jl 0x1873d4
movq %r8, %r12
movq %rcx, %r14
movq %rdx, %rbp
testq %rsi, %rsi
setns %al
cmpq %r8, %rdx
setl %cl
orb %al, %cl
je 0x1874b6
xorl %eax, %eax
testq %rsi, %rsi
movq %rsi, 0x58(%rsp)
cmovnsq %rsi, %rax
imulq $0x28, %rax, %rsi
addq %r14, %rsi
leaq 0x18(%rsp), %rcx
leaq 0x78(%rsp), %r9
leaq 0x10(%rsp), %rdx
leaq 0x70(%rsp), %r13
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %r8
callq 0x166527
imulq $0x28, %r12, %rax
imulq $0x28, %rbp, %rcx
movq %rcx, 0x80(%rsp)
addq %r14, %rcx
cmpq %r12, %rbp
movq %r14, 0x50(%rsp)
leaq -0x28(%r14,%rax), %rsi
cmovlq %rcx, %rsi
leaq 0x48(%rsp), %rcx
leaq 0x68(%rsp), %r9
leaq 0x40(%rsp), %rdx
leaq 0x60(%rsp), %r12
movq %rbx, %rdi
movq %r12, %r8
callq 0x166527
movq (%r13), %rbx
movq 0x8(%r13), %r15
movq %rbx, %rax
orq %r15, %rax
setns %al
movq (%r12), %rdx
js 0x1871a2
movq 0x68(%rsp), %rsi
jmp 0x1871b9
testq %rdx, %rdx
js 0x1874d5
movq 0x68(%rsp), %rsi
testq %rsi, %rsi
js 0x1874d5
testq %rdx, %rdx
setns %cl
andb %cl, %al
testq %rsi, %rsi
setns %cl
andb %al, %cl
cmpb $0x1, %cl
movq %rbp, 0x20(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
jne 0x1872fb
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r14
movq 0x40(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x58(%rsp), %rax
leaq 0x1(%rax), %r12
imulq $0x28, %rax, %rax
movq 0x50(%rsp), %rcx
leaq (%rax,%rcx), %r13
addq $0x28, %r13
cmpq 0x20(%rsp), %r12
jge 0x1872ec
movq 0x38(%rsp), %rdi
movq %r13, %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq 0x8(%rsp), %rax
movl %eax, %ecx
subl %ebp, %ecx
movq 0x90(%rsp), %rdx
subl %ebp, %edx
movq 0x30(%rsp), %rsi
subl %ebx, %esi
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rdx, %xmm1
cvtsi2sd %rsi, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rdx
addl %ebx, %edx
movl %edx, 0x14(%r13)
movq (%rsp), %rcx
movl %ecx, %esi
subl %r14d, %esi
movq 0x88(%rsp), %rdi
subl %r14d, %edi
movq 0x28(%rsp), %r8
subl %r15d, %r8d
cvtsi2sd %rsi, %xmm0
cvtsi2sd %rdi, %xmm1
cvtsi2sd %r8, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rsi
addl %r15d, %esi
movl %esi, 0x18(%r13)
movslq %edx, %rbx
movq %rbx, 0x70(%rsp)
movslq %esi, %r15
movq %r15, 0x78(%rsp)
movq %rax, 0x10(%rsp)
movq %rcx, 0x18(%rsp)
incq %r12
addq $0x28, %r13
movq %rcx, %r14
movq %rax, %rbp
jmp 0x18721f
movq 0x20(%rsp), %rbp
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rsi
movq %rdx, %rax
orq %rsi, %rax
jns 0x1873ca
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %r14
movq 0x58(%rsp), %rax
leaq 0x1(%rax), %r13
imulq $0x28, %rax, %rax
movq 0x50(%rsp), %rcx
leaq (%rax,%rcx), %r12
addq $0x28, %r12
cmpq 0x20(%rsp), %r13
jge 0x1873c5
movq 0x38(%rsp), %rdi
movq %r12, %rsi
leaq 0x8(%rsp), %rdx
movq %rsp, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq 0x8(%rsp), %rax
movl %eax, %ecx
subl %ebp, %ecx
movl %r14d, %edx
subl %ebp, %edx
movl %r15d, %esi
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rdx, %xmm1
subl %ebx, %esi
divsd %xmm1, %xmm0
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
addl %ebx, %ecx
movl %ecx, 0x14(%r12)
movq (%rsp), %rdx
movq %rdx, %rsi
subq %r14, %rsi
addq %r15, %rsi
movl %esi, 0x18(%r12)
movslq %ecx, %rbx
movq %rbx, 0x70(%rsp)
movslq %esi, %r15
movq %r15, 0x78(%rsp)
movq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
incq %r13
addq $0x28, %r12
movq %rdx, %r14
movq %rax, %rbp
jmp 0x18732b
movq 0x20(%rsp), %rbp
testq %rbx, %rbx
js 0x1873e6
testq %r15, %r15
js 0x1873e6
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
decq %rbp
movq 0x50(%rsp), %rax
movq 0x80(%rsp), %rcx
leaq (%rcx,%rax), %r12
addq $-0x28, %r12
leaq 0x8(%rsp), %r15
movq %rsp, %r13
cmpq 0x58(%rsp), %rbp
jle 0x1873d4
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq (%rsp), %rax
movl %r14d, %ecx
subl %eax, %ecx
subl %ebx, %r14d
movq 0x28(%rsp), %rdi
movl %edi, %edx
cvtsi2sd %rcx, %xmm0
cvtsi2sd %r14, %xmm1
movq 0x30(%rsp), %rsi
subl %esi, %edx
divsd %xmm1, %xmm0
cvtsi2sd %rdx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
subl %ecx, %edi
movl %edi, 0x18(%r12)
movq 0x8(%rsp), %rcx
movq %rcx, %rdx
subq %rbx, %rdx
addq %rsi, %rdx
movl %edx, 0x14(%r12)
movslq %edx, %rdx
movq %rdx, 0x30(%rsp)
movq %rdx, 0x60(%rsp)
movslq %edi, %rdi
movq %rdi, 0x28(%rsp)
movq %rdi, 0x68(%rsp)
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
decq %rbp
addq $-0x28, %r12
movq %rax, %r14
movq %rcx, %rbx
jmp 0x187410
leaq 0x6d80e(%rip), %rdi     # 0x1f4ccb
leaq 0x6a65f(%rip), %rsi     # 0x1f1b23
leaq 0x6d812(%rip), %rcx     # 0x1f4cdd
movl $0x3c34, %edx           # imm = 0x3C34
callq 0x2110
leaq 0x6d857(%rip), %rdi     # 0x1f4d33
leaq 0x6a640(%rip), %rsi     # 0x1f1b23
leaq 0x6d7f3(%rip), %rcx     # 0x1f4cdd
movl $0x3c43, %edx           # imm = 0x3C43
callq 0x2110