// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: update_rovlp_chain_qse(ma_ug_t*, long, long, mg_lchain_t*, long, long)
void update_rovlp_chain_qse(ma_ug_t *ug, int64_t sidx, int64_t eidx, mg_lchain_t *a, int64_t a_n, int64_t qlen)
{
	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], tt; 
	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);

	if(left_q[0] >= 0 && left_q[1] >= 0 && 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].qe = cal_qext_coor(left_r[1], right_r[1], left_q[1], right_q[1], re);
			assert(a[i].qe >= 0 && a[i].qe <= qlen);
			a[i].qs = cal_qext_coor(left_r[0], (a[i].qe<=right_q[0])?re:right_r[0], 
											left_q[0], (a[i].qe<=right_q[0])?a[i].qe:right_q[0], rs);
			assert(a[i].qs >= 0 && a[i].qs <= qlen);
			if(a[i].qs > a[i].qe) {
				tt = a[i].qs; a[i].qs = a[i].qe; a[i].qe = tt;
			}
			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].qe = cal_qext_coor(left_r[1], re, left_q[1], left_q[1] + re - left_r[1], re);
			if(a[i].qe < 0) a[i].qe = 0; if(a[i].qe > qlen) a[i].qe = qlen;
			a[i].qs = cal_qext_coor(left_r[0], (a[i].qe<=left_q[1])?re:left_r[1], 
											left_q[0], (a[i].qe<=left_q[1])?a[i].qe:left_q[1], rs);
			assert(a[i].qs >= 0 && a[i].qs <= qlen);
			if(a[i].qs > a[i].qe) {
				tt = a[i].qs; a[i].qs = a[i].qe; a[i].qe = tt;
			}
			
			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);
			a[i].qe = cal_qext_coor(right_r[0], right_r[1], right_q[0], right_q[1], re);
			assert(a[i].qe >= 0 && a[i].qe <= qlen);
			a[i].qs = cal_qext_coor(rs, right_r[0], right_q[0]-(right_r[0]-rs), right_q[0], rs);
			if(a[i].qs < 0) a[i].qs = 0; if(a[i].qs > qlen) a[i].qs = qlen;
			if(a[i].qs > a[i].qe) {
				tt = a[i].qs; a[i].qs = a[i].qe; a[i].qe = tt;
			}

			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 $0xb8, %rsp
movq %rdi, 0x70(%rsp)
movq %rdx, %rax
subq %rsi, %rax
cmpq $0x2, %rax
jl 0x1c9f0d
movq %r8, %rbx
movq %rcx, %r13
movq %rdx, %r15
testq %rsi, %rsi
setns %al
cmpq %r8, %rdx
setl %cl
orb %al, %cl
je 0x1c9f9b
movq %r9, 0x78(%rsp)
xorl %eax, %eax
testq %rsi, %rsi
movq %rsi, 0x8(%rsp)
cmovnsq %rsi, %rax
leaq (%rax,%rax,4), %rax
leaq (%r13,%rax,8), %rsi
leaq 0x58(%rsp), %rcx
leaq 0x18(%rsp), %r9
leaq 0x50(%rsp), %rdx
leaq 0x10(%rsp), %r14
movq 0x70(%rsp), %r12
movq %r12, %rdi
movq %r14, %r8
callq 0x1a5e4b
cmpq %rbx, %r15
leaq (%rbx,%rbx,4), %rax
leaq -0x28(%r13,%rax,8), %rax
movq %r15, (%rsp)
leaq (%r15,%r15,4), %rcx
movq %r13, 0x98(%rsp)
movq %rcx, 0xa0(%rsp)
leaq (%r13,%rcx,8), %rsi
cmovgeq %rax, %rsi
leaq 0x88(%rsp), %rcx
leaq 0x48(%rsp), %r9
leaq 0x80(%rsp), %rdx
leaq 0x40(%rsp), %r8
movq %r12, %rdi
callq 0x1a5e4b
movq 0x8(%r14), %rax
orq (%r14), %rax
setns %al
jns 0x1c9afa
cmpq $0x0, 0x40(%rsp)
js 0x1c9fba
cmpq $0x0, 0x48(%rsp)
js 0x1c9fba
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rdx
movq %rcx, 0x60(%rsp)
testq %rcx, %rcx
setns %cl
movq %rdx, 0x68(%rsp)
testq %rdx, %rdx
setns %dl
andb %cl, %dl
andb %dl, %al
cmpb $0x1, %al
jne 0x1c9c7d
movq 0x8(%rsp), %rax
incq %rax
cmpq (%rsp), %rax
jge 0x1c9c7d
movq 0x10(%rsp), %rbp
movq 0x18(%rsp), %rbx
movq 0x50(%rsp), %r13
movq 0x58(%rsp), %r12
movq 0x80(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x8(%rsp), %rax
movq %rax, %rdx
notq %rdx
addq (%rsp), %rdx
leaq (%rax,%rax,4), %rax
movq 0x98(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
addq $0x28, %r15
movq %rdx, 0x28(%rsp)
movq %r12, %r14
movq 0x70(%rsp), %rdi
movq %r15, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
movq 0x30(%rsp), %r12
movq %r14, %rdi
movq 0xb0(%rsp), %rsi
movq %rbx, %rdx
movq 0x68(%rsp), %rcx
movq %r12, %r8
callq 0x1a6ef6
movq %rax, %rbx
movl %ebx, 0x18(%r15)
testl %ebx, %ebx
js 0x1c9f1f
movl %ebx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq 0x78(%rsp), %rcx
jg 0x1c9f1f
movq 0x60(%rsp), %rax
cmpq %rax, %rcx
movq %r12, %rsi
cmovgq 0xa8(%rsp), %rsi
cmovgeq %rax, %rcx
movq 0x38(%rsp), %r14
movq %r13, %rdi
movq %rbp, %rdx
movq %r14, %r8
callq 0x1a6ef6
movl %eax, 0x14(%r15)
testl %eax, %eax
js 0x1c9f7c
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq 0x78(%rsp), %rcx
jg 0x1c9f7c
cmpl %eax, %ebx
movq 0x28(%rsp), %rdx
jge 0x1c9c51
movl %ebx, 0x14(%r15)
movl %eax, 0x18(%r15)
movslq 0x14(%r15), %rbp
movq %rbp, 0x10(%rsp)
movslq 0x18(%r15), %rbx
movq %rbx, 0x18(%rsp)
movq %r14, 0x50(%rsp)
movq %r12, 0x58(%rsp)
addq $0x28, %r15
movq %r14, %r13
decq %rdx
jne 0x1c9b8f
movq 0x68(%rsp), %rax
orq %rax, 0x60(%rsp)
jns 0x1c9dc6
movq 0x8(%rsp), %rax
incq %rax
cmpq (%rsp), %rax
jge 0x1c9dc6
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rbp
movq 0x50(%rsp), %r12
movq 0x58(%rsp), %r14
movq 0x8(%rsp), %rax
movq %rax, %rdx
notq %rdx
addq (%rsp), %rdx
leaq (%rax,%rax,4), %rax
movq 0x98(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
addq $0x28, %r15
movq 0x78(%rsp), %rbx
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %r12, 0x28(%rsp)
movq %r14, %r12
movq 0x70(%rsp), %rdi
movq %r15, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
movq 0x30(%rsp), %r14
movq %r14, %rcx
subq %r12, %rcx
addq %rbp, %rcx
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r14, %r8
callq 0x1a6ef6
movq %rax, %r13
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
cmpq %rbx, %r13
cmovgl %ebx, %r13d
movslq %r13d, %rcx
cmpq %rcx, %rbp
cmovgeq %r14, %r12
movl %r13d, 0x18(%r15)
cmovlq %rbp, %rcx
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movq %rax, %r12
movq 0x68(%rsp), %rdx
movq %rax, %r8
callq 0x1a6ef6
movl %eax, 0x14(%r15)
testl %eax, %eax
js 0x1c9f3e
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rbx, %rcx
jg 0x1c9f3e
cmpl %eax, %r13d
movq 0x60(%rsp), %rdx
jge 0x1c9d9d
movl %r13d, 0x14(%r15)
movl %eax, 0x18(%r15)
movslq 0x14(%r15), %rsi
movq %rsi, 0x10(%rsp)
movslq 0x18(%r15), %rbp
movq %rbp, 0x18(%rsp)
movq %r12, 0x50(%rsp)
movq %r14, 0x58(%rsp)
addq $0x28, %r15
decq %rdx
jne 0x1c9cdb
cmpq $0x0, 0x10(%rsp)
js 0x1c9dda
cmpq $0x0, 0x18(%rsp)
jns 0x1c9f0d
movq (%rsp), %rax
decq %rax
movq %rax, (%rsp)
cmpq 0x8(%rsp), %rax
jle 0x1c9f0d
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %r12
movq 0x80(%rsp), %r14
movq 0x88(%rsp), %rsi
movq 0x98(%rsp), %rax
movq 0xa0(%rsp), %rcx
leaq (%rax,%rcx,8), %r15
addq $-0x28, %r15
movq 0x78(%rsp), %rbp
movq %rsi, %rbx
movq 0x70(%rsp), %rdi
movq %r15, %rsi
leaq 0x38(%rsp), %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5e4b
movq 0x30(%rsp), %r8
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %r8, 0x28(%rsp)
callq 0x1a6ef6
movq %rax, %r12
movl %r12d, 0x18(%r15)
testl %r12d, %r12d
js 0x1c9f5d
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rbp, %rax
jg 0x1c9f5d
movq 0x38(%rsp), %rbx
movq %r13, %rdx
subq %r14, %rdx
addq %rbx, %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
movq %rbx, %r8
callq 0x1a6ef6
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
movl %ebp, %ecx
cmpq %rbp, %rax
jg 0x1c9eb5
movl %eax, %ecx
movl %ecx, 0x14(%r15)
cmpl %r12d, %ecx
movq 0x8(%rsp), %rdx
movq 0x28(%rsp), %rsi
jle 0x1c9ed0
movl %r12d, 0x14(%r15)
movl %ecx, 0x18(%r15)
movslq 0x14(%r15), %r13
movq %r13, 0x40(%rsp)
movslq 0x18(%r15), %r12
movq %r12, 0x48(%rsp)
movq %rbx, 0x80(%rsp)
movq %rsi, 0x88(%rsp)
movq (%rsp), %rax
decq %rax
addq $-0x28, %r15
movq %rbx, %r14
movq %rax, (%rsp)
cmpq %rdx, %rax
jg 0x1c9e27
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6cd1a(%rip), %rdi     # 0x236c40
leaq 0x69a5e(%rip), %rsi     # 0x23398b
leaq 0x6ccb2(%rip), %rcx     # 0x236be6
movl $0x3c9f, %edx           # imm = 0x3C9F
callq 0x2110
leaq 0x6cd1b(%rip), %rdi     # 0x236c60
leaq 0x69a3f(%rip), %rsi     # 0x23398b
leaq 0x6cc93(%rip), %rcx     # 0x236be6
movl $0x3cb2, %edx           # imm = 0x3CB2
callq 0x2110
leaq 0x6ccdc(%rip), %rdi     # 0x236c40
leaq 0x69a20(%rip), %rsi     # 0x23398b
leaq 0x6cc74(%rip), %rcx     # 0x236be6
movl $0x3cc2, %edx           # imm = 0x3CC2
callq 0x2110
leaq 0x6ccdd(%rip), %rdi     # 0x236c60
leaq 0x69a01(%rip), %rsi     # 0x23398b
leaq 0x6cc55(%rip), %rcx     # 0x236be6
movl $0x3ca2, %edx           # imm = 0x3CA2
callq 0x2110
leaq 0x6cb91(%rip), %rdi     # 0x236b33
leaq 0x699e2(%rip), %rsi     # 0x23398b
leaq 0x6cc36(%rip), %rcx     # 0x236be6
movl $0x3c8a, %edx           # imm = 0x3C8A
callq 0x2110
leaq 0x6cbda(%rip), %rdi     # 0x236b9b
leaq 0x699c3(%rip), %rsi     # 0x23398b
leaq 0x6cc17(%rip), %rcx     # 0x236be6
movl $0x3c99, %edx           # imm = 0x3C99
callq 0x2110