// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, 0x48(%rsp)
movq %rdi, 0x68(%rsp)
movq %rdx, %rax
subq %rsi, %rax
cmpq $0x2, %rax
jl 0x18791a
movq %r8, %rbx
movq %r9, 0x18(%rsp)
testq %rsi, %rsi
setns %al
cmpq %r8, %rdx
setl %cl
orb %al, %cl
je 0x187ac0
movq %rdx, %r15
xorl %eax, %eax
testq %rsi, %rsi
movq %rsi, 0x80(%rsp)
cmovnsq %rsi, %rax
imulq $0x28, %rax, %rsi
movq 0x48(%rsp), %rbp
addq %rbp, %rsi
leaq 0x38(%rsp), %rcx
leaq 0xa8(%rsp), %r9
leaq 0x30(%rsp), %rdx
leaq 0xa0(%rsp), %r14
movq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r14, %r8
callq 0x166527
imulq $0x28, %rbx, %rax
imulq $0x28, %r15, %rcx
movq %rcx, 0xb0(%rsp)
addq %rbp, %rcx
movq %r15, 0x8(%rsp)
cmpq %rbx, %r15
leaq -0x28(%rbp,%rax), %rsi
cmovlq %rcx, %rsi
leaq 0x78(%rsp), %rcx
leaq 0x98(%rsp), %r9
leaq 0x70(%rsp), %rdx
leaq 0x90(%rsp), %r15
movq %r13, %rdi
movq %r15, %r8
callq 0x166527
movq (%r14), %r12
movq 0x8(%r14), %rcx
movq %r12, %rax
movq %rcx, (%rsp)
orq %rcx, %rax
setns %al
movq (%r15), %rcx
movq %rcx, 0x50(%rsp)
js 0x1875f6
movq 0x98(%rsp), %rdx
jmp 0x187610
testq %rcx, %rcx
js 0x187adf
movq 0x98(%rsp), %rdx
testq %rdx, %rdx
js 0x187adf
cmpq $0x0, 0x50(%rsp)
setns %cl
andb %cl, %al
testq %rdx, %rdx
setns %cl
andb %al, %cl
cmpb $0x1, %cl
movq 0x18(%rsp), %r13
movq %rdx, 0x60(%rsp)
jne 0x187793
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %rbp
movq 0x70(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x80(%rsp), %rax
leaq 0x1(%rax), %rdx
imulq $0x28, %rax, %rax
movq 0x48(%rsp), %rcx
leaq (%rax,%rcx), %r14
addq $0x28, %r14
movq 0x8(%rsp), %rcx
cmpq %rcx, %rdx
jge 0x18779a
movq %rdx, 0x10(%rsp)
movq %rbp, %r15
movq 0x68(%rsp), %rdi
movq %r14, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x20(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq 0x20(%rsp), %rbp
movq %r15, %rdi
movq 0x88(%rsp), %rsi
movq (%rsp), %rdx
movq 0x60(%rsp), %rcx
movq %rbp, %r8
callq 0x16749b
movq %rax, %r15
movl %r15d, 0x18(%r14)
testl %r15d, %r15d
js 0x187a44
movl %r15d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %r13, %rcx
jg 0x187a44
movq 0x50(%rsp), %rax
cmpq %rax, %rcx
movq %rbp, %rsi
cmovgq 0x58(%rsp), %rsi
cmovgeq %rax, %rcx
movq 0x28(%rsp), %r13
movq %rbx, %rdi
movq %r12, %rdx
movq %r13, %r8
callq 0x16749b
movl %eax, 0x14(%r14)
testl %eax, %eax
js 0x187a82
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq 0x18(%rsp), %rcx
ja 0x187a82
cmpl %eax, %r15d
jae 0x187743
movl %r15d, 0x14(%r14)
movl %eax, 0x18(%r14)
movq %rax, %rdx
jmp 0x187749
movq %r15, %rdx
movq %rax, %r15
movq 0x8(%rsp), %rcx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r15
movq %r15, 0xa0(%rsp)
andq %rax, %rdx
movq %rdx, (%rsp)
movq %rdx, 0xa8(%rsp)
movq %r13, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
movq 0x10(%rsp), %rdx
incq %rdx
addq $0x28, %r14
movq %r15, %r12
movq %r13, %rbx
movq 0x18(%rsp), %r13
jmp 0x187679
movq 0x8(%rsp), %rcx
jmp 0x18779f
movq 0x60(%rsp), %rdx
movq 0x50(%rsp), %rax
orq %rdx, %rax
jns 0x187909
movq %r12, 0x10(%rsp)
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r15
movq 0x80(%rsp), %rax
leaq 0x1(%rax), %rdi
imulq $0x28, %rax, %rax
movq 0x48(%rsp), %rdx
leaq (%rax,%rdx), %rsi
addq $0x28, %rsi
movq (%rsp), %r12
cmpq %rcx, %rdi
jge 0x187900
movq %rdi, 0x58(%rsp)
movq 0x68(%rsp), %rdi
movq %rsi, (%rsp)
leaq 0x28(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r8, 0x88(%rsp)
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq 0x20(%rsp), %rbp
movq %rbp, %rcx
subq %r15, %rcx
addq %r12, %rcx
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %rbp, %r8
callq 0x16749b
movq %rax, %r14
testl %r14d, %r14d
movl $0x0, %eax
cmovlel %eax, %r14d
cmpq %r13, %r14
cmovgl %r13d, %r14d
movq %r13, %rbx
movq %r12, %rcx
movslq %r14d, %r12
cmpq %r12, %rcx
cmovgeq %rbp, %r15
movq (%rsp), %rax
movl %r14d, 0x18(%rax)
cmovgeq %r12, %rcx
movq 0x28(%rsp), %r13
movq 0x88(%rsp), %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movq %r13, %r8
callq 0x16749b
movq (%rsp), %rsi
movl %eax, 0x14(%rsi)
testl %eax, %eax
js 0x187a63
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rbx, %rcx
jg 0x187a63
cmpl %eax, %r14d
jge 0x1878bb
movl %r14d, 0x14(%rsi)
movl %eax, 0x18(%rsi)
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r12, %rax
movq %rcx, %r12
jmp 0x1878c0
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rcx
movq 0x58(%rsp), %rdi
movq %rax, 0x10(%rsp)
movq %rax, 0xa0(%rsp)
movq %r12, 0xa8(%rsp)
movq %r13, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
incq %rdi
addq $0x28, %rsi
movq %rbp, %r15
movq %r13, %r8
movq 0x18(%rsp), %r13
jmp 0x1877dd
movq %r12, (%rsp)
movq 0x10(%rsp), %r12
testq %r12, %r12
movq 0x50(%rsp), %r12
js 0x18792c
cmpq $0x0, (%rsp)
js 0x18792c
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x70(%rsp), %r14
movq 0x78(%rsp), %r15
decq %rcx
movq 0x48(%rsp), %rax
movq 0xb0(%rsp), %rdx
leaq (%rdx,%rax), %rbx
addq $-0x28, %rbx
cmpq 0x80(%rsp), %rcx
jle 0x18791a
movq %rcx, 0x8(%rsp)
movq 0x68(%rsp), %rdi
movq %rbx, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x20(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x166527
movq 0x20(%rsp), %r8
movq %r14, %rbp
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %r13
movq %r12, %rdx
movq 0x60(%rsp), %rcx
movq %r8, (%rsp)
callq 0x16749b
movq %rax, %r15
movl %r15d, 0x18(%rbx)
testl %r15d, %r15d
movq 0x18(%rsp), %r12
js 0x187aa1
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %r12, %rax
jg 0x187aa1
movq 0x28(%rsp), %r14
movq %r13, %rdx
subq %rbp, %rdx
addq %r14, %rdx
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rcx
movq %r14, %r8
callq 0x16749b
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
cmpq %r12, %rax
cmoval %r12d, %eax
movl %eax, 0x14(%rbx)
cmpl %r15d, %eax
jle 0x187a05
movl %r15d, 0x14(%rbx)
movl %eax, 0x18(%rbx)
movl %eax, %ecx
jmp 0x187a0b
movl %r15d, %ecx
movl %eax, %r15d
movslq %r15d, %r12
movq %r12, 0x90(%rsp)
movl %ecx, %eax
movq %rax, 0x60(%rsp)
movq %rax, 0x98(%rsp)
movq %r14, 0x70(%rsp)
movq (%rsp), %r15
movq %r15, 0x78(%rsp)
movq 0x8(%rsp), %rcx
decq %rcx
addq $-0x28, %rbx
jmp 0x18794e
leaq 0x6d38d(%rip), %rdi     # 0x1f4dd8
leaq 0x6a0d1(%rip), %rsi     # 0x1f1b23
leaq 0x6d325(%rip), %rcx     # 0x1f4d7e
movl $0x3c9f, %edx           # imm = 0x3C9F
callq 0x2110
leaq 0x6d38e(%rip), %rdi     # 0x1f4df8
leaq 0x6a0b2(%rip), %rsi     # 0x1f1b23
leaq 0x6d306(%rip), %rcx     # 0x1f4d7e
movl $0x3cb2, %edx           # imm = 0x3CB2
callq 0x2110
leaq 0x6d36f(%rip), %rdi     # 0x1f4df8
leaq 0x6a093(%rip), %rsi     # 0x1f1b23
leaq 0x6d2e7(%rip), %rcx     # 0x1f4d7e
movl $0x3ca2, %edx           # imm = 0x3CA2
callq 0x2110
leaq 0x6d330(%rip), %rdi     # 0x1f4dd8
leaq 0x6a074(%rip), %rsi     # 0x1f1b23
leaq 0x6d2c8(%rip), %rcx     # 0x1f4d7e
movl $0x3cc2, %edx           # imm = 0x3CC2
callq 0x2110
leaq 0x6d204(%rip), %rdi     # 0x1f4ccb
leaq 0x6a055(%rip), %rsi     # 0x1f1b23
leaq 0x6d2a9(%rip), %rcx     # 0x1f4d7e
movl $0x3c8a, %edx           # imm = 0x3C8A
callq 0x2110
leaq 0x6d24d(%rip), %rdi     # 0x1f4d33
leaq 0x6a036(%rip), %rsi     # 0x1f1b23
leaq 0x6d28a(%rip), %rcx     # 0x1f4d7e
movl $0x3c99, %edx           # imm = 0x3C99
callq 0x2110