// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rax
subq -0x10(%rbp), %rax
cmpq $0x1, %rax
jg 0x2d8fc2
jmp 0x2d94c0
movb $0x1, %al
cmpq $0x0, -0x10(%rbp)
movb %al, -0x89(%rbp)
jge 0x2d8fe2
movq -0x18(%rbp), %rax
cmpq -0x28(%rbp), %rax
setl %al
movb %al, -0x89(%rbp)
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0x2d8fee
jmp 0x2d8ff0
jmp 0x2d900f
leaq 0xba9e0(%rip), %rdi     # 0x3939d7
leaq 0xb7831(%rip), %rsi     # 0x39082f
movl $0x3c34, %edx           # imm = 0x3C34
leaq 0xba9df(%rip), %rcx     # 0x3939e9
callq 0x2130
cmpq $0x0, -0x10(%rbp)
jl 0x2d9045
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
imulq $0x28, -0x10(%rbp), %rax
addq %rax, %rsi
leaq -0x40(%rbp), %rdx
leaq -0x40(%rbp), %rcx
addq $0x8, %rcx
leaq -0x60(%rbp), %r8
leaq -0x60(%rbp), %r9
addq $0x8, %r9
callq 0x2984d0
jmp 0x2d906a
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x40(%rbp), %rdx
leaq -0x40(%rbp), %rcx
addq $0x8, %rcx
leaq -0x60(%rbp), %r8
leaq -0x60(%rbp), %r9
addq $0x8, %r9
callq 0x2984d0
movq -0x18(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x2d90a3
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
imulq $0x28, -0x18(%rbp), %rax
addq %rax, %rsi
leaq -0x50(%rbp), %rdx
leaq -0x50(%rbp), %rcx
addq $0x8, %rcx
leaq -0x70(%rbp), %r8
leaq -0x70(%rbp), %r9
addq $0x8, %r9
callq 0x2984d0
jmp 0x2d90d7
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
subq $0x1, %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
leaq -0x50(%rbp), %rdx
leaq -0x50(%rbp), %rcx
addq $0x8, %rcx
leaq -0x70(%rbp), %r8
leaq -0x70(%rbp), %r9
addq $0x8, %r9
callq 0x2984d0
cmpq $0x0, -0x60(%rbp)
jl 0x2d90ed
movb $0x1, %al
cmpq $0x0, -0x58(%rbp)
movb %al, -0x8a(%rbp)
jge 0x2d9116
xorl %eax, %eax
cmpq $0x0, -0x70(%rbp)
movb %al, -0x8b(%rbp)
jl 0x2d910a
cmpq $0x0, -0x68(%rbp)
setge %al
movb %al, -0x8b(%rbp)
movb -0x8b(%rbp), %al
movb %al, -0x8a(%rbp)
movb -0x8a(%rbp), %al
testb $0x1, %al
jne 0x2d9122
jmp 0x2d9124
jmp 0x2d9143
leaq 0xba914(%rip), %rdi     # 0x393a3f
leaq 0xb76fd(%rip), %rsi     # 0x39082f
movl $0x3c43, %edx           # imm = 0x3C43
leaq 0xba8ab(%rip), %rcx     # 0x3939e9
callq 0x2130
cmpq $0x0, -0x60(%rbp)
jl 0x2d929c
cmpq $0x0, -0x58(%rbp)
jl 0x2d929c
cmpq $0x0, -0x70(%rbp)
jl 0x2d929c
cmpq $0x0, -0x68(%rbp)
jl 0x2d929c
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x2d929a
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
imulq $0x28, -0x78(%rbp), %rax
addq %rax, %rsi
leaq -0x80(%rbp), %rdx
leaq -0x88(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x2984d0
movq -0x60(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x80(%rbp), %rax
subq -0x40(%rbp), %rax
movl %eax, %edi
movq -0x50(%rbp), %rax
subq -0x40(%rbp), %rax
movl %eax, %esi
movq -0x70(%rbp), %rax
subq -0x60(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %ecx
movq -0xa0(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x88(%rbp), %rax
subq -0x38(%rbp), %rax
movl %eax, %edi
movq -0x48(%rbp), %rax
subq -0x38(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rax
subq -0x58(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rcx
addq %rcx, %rax
movslq 0x14(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x2d917b
jmp 0x2d929c
cmpq $0x0, -0x70(%rbp)
jl 0x2d92ae
cmpq $0x0, -0x68(%rbp)
jge 0x2d93ae
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x2d93ac
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
imulq $0x28, -0x78(%rbp), %rax
addq %rax, %rsi
leaq -0x80(%rbp), %rdx
leaq -0x88(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x2984d0
movq -0x60(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x80(%rbp), %rax
subq -0x40(%rbp), %rax
movl %eax, %edi
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
movl %eax, %esi
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %ecx
movq -0xa8(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x58(%rbp), %rax
movq -0x88(%rbp), %rcx
subq -0x38(%rbp), %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rcx
addq %rcx, %rax
movslq 0x14(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x2d92ba
jmp 0x2d93ae
cmpq $0x0, -0x60(%rbp)
jl 0x2d93c0
cmpq $0x0, -0x58(%rbp)
jge 0x2d94c0
movq -0x18(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x10(%rbp), %rax
jle 0x2d94be
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
imulq $0x28, -0x78(%rbp), %rax
addq %rax, %rsi
leaq -0x80(%rbp), %rdx
leaq -0x88(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x2984d0
movq -0x68(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x48(%rbp), %rax
subq -0x88(%rbp), %rax
movl %eax, %edi
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
movl %eax, %edx
callq 0xc14d0
movl %eax, %ecx
movq -0xb0(%rbp), %rax
movl %ecx, %ecx
subq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
movq -0x70(%rbp), %rax
movq -0x50(%rbp), %rcx
subq -0x80(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rcx
addq %rcx, %rax
movslq 0x14(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
imulq $0x28, -0x78(%rbp), %rcx
addq %rcx, %rax
movslq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x78(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x2d93cc
jmp 0x2d94c0
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)