// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: update_uovlp_chain_qse(ma_ug_t*, long, long, mg_lchain_t*, long)
void update_uovlp_chain_qse(ma_ug_t *ug, int64_t sidx, int64_t eidx, mg_lchain_t *a, int64_t a_n)
{
	// fprintf(stderr, "******[M::%s::] sidx:%ld, eidx:%ld\n", __func__, sidx, eidx);
	if(eidx - sidx <= 1) return;
	///for ug chains, sidx >= 0 && eidx < a_n
	assert(sidx>=0 && eidx<a_n);
	
	int64_t r_pos[4], q_pos[4];  uint64_t r_srt[4];
	int64_t i, rs, re, prs, pre, ars, are;//qs or qe might be -1, while rs and re should >= 0
	int64_t pqs, pqe, aqs, aqe, fail_s, fail_e;
	if(sidx >= 0) {
		get_u_offset(ug, &(a[sidx]), &r_pos[0], &r_pos[1], &q_pos[0], &q_pos[1]);
	} else {
		get_u_offset(ug, &(a[0]), &r_pos[0], &r_pos[1], &q_pos[0], &q_pos[1]);
	}

	if(eidx < a_n) {
		get_u_offset(ug, &(a[eidx]), &r_pos[2], &r_pos[3], &q_pos[2], &q_pos[3]);
	} else {
		get_u_offset(ug, &(a[a_n-1]), &r_pos[2], &r_pos[3], &q_pos[2], &q_pos[3]);
	}
	prs = r_pos[0]; pre = r_pos[1]; ars = r_pos[2]; are = r_pos[3]; 
	pqs = q_pos[0]; pqe = q_pos[1]; aqs = q_pos[2]; aqe = q_pos[3];
	// fprintf(stderr, "\n[M::%s::] sidx->%ld<r[0]::%ld, r[1]::%ld, q[0]::%ld, q[1]::%ld>, eidx->%ld<r[2]::%ld, r[3]::%ld, q[2]::%ld, q[3]::%ld>\n", __func__, 
	// sidx, r_pos[0], r_pos[1], q_pos[0], q_pos[1], 
	// eidx, r_pos[2], r_pos[3], q_pos[2], q_pos[3]);
	// assert((left_q[0] >= 0 && left_q[1] >= 0) || (right_q[0] >= 0 && right_q[1] >= 0)); ///assert(re >= rs);
	///for ug chains, sidx >= 0 && eidx < a_n
	assert(q_pos[0] >= 0 && q_pos[1] >= 0 && q_pos[2] >= 0 && q_pos[3] >= 0);
	r_srt[0] = r_pos[0]; r_srt[0] <<= 32;
	r_srt[1] = r_pos[1]; r_srt[1] <<= 32; r_srt[1] += 1;
	r_srt[2] = r_pos[2]; r_srt[2] <<= 32; r_srt[2] += 2;
	r_srt[3] = r_pos[3]; r_srt[3] <<= 32; r_srt[3] += 3;
	radix_sort_gfa64(r_srt, r_srt + 4);
	// assert(r_pos[(uint32_t)r_srt[0]] == (r_srt[0]>>32));
	// assert(r_pos[(uint32_t)r_srt[1]] == (r_srt[1]>>32));
	// assert(r_pos[(uint32_t)r_srt[2]] == (r_srt[2]>>32));
	// assert(r_pos[(uint32_t)r_srt[3]] == (r_srt[3]>>32));

	for (i = sidx+1; i < eidx; i++) {
		get_u_offset(ug, &(a[i]), &rs, &re, NULL, NULL);
		// assert(rs >= prs && rs <= ars && re >= pre && re <= are); 
		assert(rs <= re);
		a[i].qs = adjust_utg_chain_qoffset(r_srt, r_pos, q_pos, rs); fail_s = 1;
		a[i].qe = adjust_utg_chain_qoffset(r_srt, r_pos, q_pos, re); fail_e = 1;
		if(a[i].qs >= 0 && a[i].qs >= pqs && a[i].qs <= aqs) fail_s = 0;
		if(a[i].qe >= 0 && a[i].qe >= pqe && a[i].qe <= aqe) fail_e = 0;
		if(a[i].qs > a[i].qe) fail_s = fail_e = 1;

		if(fail_s || fail_e) {
			//  if(re >= pre && are >= pre && re <= are) {///aqe >= pqe is always true
			// 	a[i].qe = pqe + get_offset_adjust(re-pre, are-pre, aqe-pqe);///first priority
			//  } else {///abnormal coordinates
			// 	pd = ((re >= pre)?(re-pre):(pre-re));
			// 	ad = ((re >= are)?(re-are):(are-re));
			// 	a[i].qe = ((ad <= pd)?aqe:pqe);
			//  }
			a[i].qe = cal_qext_coor(pre, are, pqe, aqe, re);
			//  if(aqs <= a[i].qe) {
			// 	if(rs >= prs && ars >= prs && rs <= ars) {///aqs >= pqs is always true
			// 		a[i].qs = pqs + get_offset_adjust(rs-prs, ars-prs, aqs-pqs);
			// 	} else {
			// 		pd = ((rs >= prs)?(rs-prs):(prs-rs));
			// 		ad = ((rs >= ars)?(rs-ars):(ars-rs));
			// 		a[i].qs = ((ad <= pd)?aqs:pqs);
			// 	}
			//  } else {
			// 	if(rs >= prs && re >= prs && rs <= re) {///as a[i].qe >= pqe, a[i].qe >= pqs
			// 		a[i].qs = pqs + get_offset_adjust(rs-prs, re-prs, a[i].qe-pqs);
			// 	} else {
			// 		pd = ((rs >= prs)?(rs-prs):(prs-rs));
			// 		ad = ((rs >= re)?(rs-re):(re-rs));
			// 		a[i].qs = ((ad <= pd)?a[i].qe:pqs);
			// 	}
			//  }
			a[i].qs = cal_qext_coor(prs, (a[i].qe<=aqs)?re:ars, pqs, (a[i].qe<=aqs)?a[i].qe:aqs, rs);
		}
		// assert(a[i].qs >= pqs && a[i].qs <= aqs && a[i].qe >= pqe && a[i].qe <= aqe); assert(a[i].qs <= a[i].qe);
		assert(a[i].qs >= pqs && a[i].qs <= aqs && a[i].qe >= pqe && a[i].qe <= aqe && a[i].qs <= a[i].qe);
		// fprintf(stderr, "[M::%s::i->%ld] rs::%ld, re::%ld, a[i].qs::%d, a[i].qe::%d\n", 
		// 						__func__, i, rs, re, a[i].qs, a[i].qe);
		
		if(i + 1 < eidx) {
			q_pos[0] = a[i].qs; q_pos[1] = a[i].qe; r_pos[0] = rs; r_pos[1] = re;
			r_srt[0] = r_pos[0]; r_srt[0] <<= 32;
			r_srt[1] = r_pos[1]; r_srt[1] <<= 32; r_srt[1] += 1;
			r_srt[2] = r_pos[2]; r_srt[2] <<= 32; r_srt[2] += 2;
			r_srt[3] = r_pos[3]; r_srt[3] <<= 32; r_srt[3] += 3;
			radix_sort_gfa64(r_srt, r_srt + 4);
			// assert(r_pos[(uint32_t)r_srt[0]] == (r_srt[0]>>32));
			// assert(r_pos[(uint32_t)r_srt[1]] == (r_srt[1]>>32));
			// assert(r_pos[(uint32_t)r_srt[2]] == (r_srt[2]>>32));
			// assert(r_pos[(uint32_t)r_srt[3]] == (r_srt[3]>>32));
			// fprintf(stderr, "[Srt::i->%ld] <r[0]::%ld, r[1]::%ld, q[0]::%ld, q[1]::%ld>, <r[2]::%ld, r[3]::%ld, q[2]::%ld, q[3]::%ld>\n", i, 
			// 	r_pos[0], r_pos[1], q_pos[0], q_pos[1], 
			// 	r_pos[2], r_pos[3], q_pos[2], q_pos[3]);
		}
		prs = rs; pre = re; pqs = a[i].qs; pqe = a[i].qe;
	}
	


	// if(right_q[0] < 0 || right_q[1] < 0) {
	// 	for (i = sidx+1; i < eidx; i++) {
	// 		get_u_offset(ug, &(a[i]), &rs, &re, NULL, NULL);
	// 		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] + (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_u_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;
	// 	}
	// }

	// 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 $0xc8, %rsp
movq %rcx, 0x58(%rsp)
movq %rdi, 0x50(%rsp)
movq %rdx, %rax
movq %rdx, 0x8(%rsp)
subq %rsi, %rax
cmpq $0x2, %rax
jl 0x1a731d
movq %rsi, %r15
testq %rsi, %rsi
js 0x1a736d
cmpq %r8, 0x8(%rsp)
jge 0x1a736d
leaq (%r15,%r15,4), %rax
movq 0x58(%rsp), %rbx
leaq (%rbx,%rax,8), %rsi
leaq 0x38(%rsp), %rcx
leaq 0x68(%rsp), %r9
leaq 0x30(%rsp), %rdx
leaq 0x60(%rsp), %r8
movq 0x50(%rsp), %r12
movq %r12, %rdi
callq 0x1a5eda
movq 0x8(%rsp), %rax
leaq (%rax,%rax,4), %rax
leaq (%rbx,%rax,8), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x70(%rsp), %r8
leaq 0x78(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %r9
callq 0x1a5eda
movq -0x18(%rbx), %r13
testq %r13, %r13
js 0x1a738c
movq 0x68(%rsp), %r12
testq %r12, %r12
js 0x1a738c
movq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
testq %rax, %rax
js 0x1a738c
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
js 0x1a738c
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %r14
movq %r11, %rcx
shlq $0x20, %rcx
leaq 0x80(%rsp), %rax
movq %rcx, (%rax)
movq %r14, %rcx
shlq $0x20, %rcx
incq %rcx
movq %rcx, 0x8(%rax)
movq %rdx, 0xa8(%rsp)
movq %rdx, %rcx
shlq $0x20, %rcx
orq $0x2, %rcx
movq %rcx, 0x10(%rax)
movq %rsi, 0xa0(%rsp)
movq %rsi, %rcx
shlq $0x20, %rcx
orq $0x3, %rcx
movq %rcx, 0x18(%rax)
movl $0x8, %ecx
movq %rax, %rdx
movq %rdx, %rdi
leaq (%rsp,%rcx), %rdx
addq $0x80, %rdx
movq (%rdx), %rsi
cmpq (%rdi), %rsi
jae 0x1a70d7
movq %rdx, %rdi
movq -0x8(%rdi), %r8
cmpq %r8, %rsi
jae 0x1a70d4
movq %r8, (%rdi)
addq $-0x8, %rdi
cmpq %rax, %rdi
ja 0x1a70bf
movq %rsi, (%rdi)
leaq 0x8(%rcx), %rsi
cmpq $0x18, %rcx
movq %rsi, %rcx
jb 0x1a70a6
incq %r15
cmpq 0x8(%rsp), %r15
jge 0x1a731d
leaq 0x80(%rsp), %rbx
movq %r14, 0xb0(%rsp)
movq %r11, 0xb8(%rsp)
movq %r15, 0xc0(%rsp)
leaq (%r15,%r15,4), %rax
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
movq 0x50(%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1a5eda
movq 0x18(%rsp), %rcx
cmpq 0x10(%rsp), %rcx
jg 0x1a734e
movq %rbx, %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
leaq 0x60(%rsp), %rbp
movq %rbp, %rdx
callq 0x1a6e03
movl %eax, 0x14(%r15)
movq 0x10(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1a6e03
movl %eax, 0x18(%r15)
movl 0x14(%r15), %ecx
cmpl %eax, %ecx
movq %r12, %rbp
jg 0x1a71b0
testl %ecx, %ecx
js 0x1a71b0
cmpq %rcx, %r13
jg 0x1a71b0
cmpq %rcx, 0x28(%rsp)
jl 0x1a71b0
testl %eax, %eax
js 0x1a71b0
cmpl %eax, %ecx
jg 0x1a71b0
movl %eax, %eax
cmpq %rax, %rbp
jg 0x1a71b0
cmpq %rax, 0x20(%rsp)
jge 0x1a720d
movq 0x10(%rsp), %r14
movq 0xb0(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq %rbp, %rdx
movq 0x20(%rsp), %rcx
movq %r14, %r8
callq 0x1a6ef6
movslq %eax, %rcx
movq 0x28(%rsp), %rdx
cmpq %rdx, %rcx
cmovgq 0xa8(%rsp), %r14
movl %eax, 0x18(%r15)
cmovgeq %rdx, %rcx
movq 0x18(%rsp), %r8
movq 0xb8(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1a6ef6
movl %eax, 0x14(%r15)
movslq 0x14(%r15), %rax
cmpq %rax, %r13
jg 0x1a732f
cmpq %rax, 0x28(%rsp)
jl 0x1a732f
movslq 0x18(%r15), %r12
cmpl %r12d, %eax
jg 0x1a732f
cmpq %r12, %rbp
jg 0x1a732f
cmpq %r12, 0x20(%rsp)
jl 0x1a732f
movq 0xc0(%rsp), %r15
incq %r15
cmpq 0x8(%rsp), %r15
jge 0x1a7305
movq %rax, 0x60(%rsp)
movq %r12, 0x68(%rsp)
movq 0x18(%rsp), %rdx
movq %rdx, 0x30(%rsp)
movq 0x10(%rsp), %rsi
movq %rsi, 0x38(%rsp)
shlq $0x20, %rdx
movq %rdx, 0x80(%rsp)
shlq $0x20, %rsi
incq %rsi
movq %rsi, 0x88(%rsp)
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rsi
shlq $0x20, %rdx
orq $0x2, %rdx
movq %rdx, 0x90(%rsp)
shlq $0x20, %rsi
orq $0x3, %rsi
movq %rsi, 0x98(%rsp)
movl $0x8, %edx
movq %rbx, %rsi
movq %rsi, %r8
leaq (%rsp,%rdx), %rsi
addq $0x80, %rsi
movq (%rsi), %rdi
cmpq (%r8), %rdi
jae 0x1a72f8
movq %rsi, %r8
movq -0x8(%r8), %r9
cmpq %r9, %rdi
jae 0x1a72f5
movq %r9, (%r8)
addq $-0x8, %r8
cmpq %rbx, %r8
ja 0x1a72e0
movq %rdi, (%r8)
leaq 0x8(%rdx), %rdi
cmpq $0x18, %rdx
movq %rdi, %rdx
jb 0x1a72c7
movq 0x18(%rsp), %r11
movq 0x10(%rsp), %r14
movq %rax, %r13
cmpq 0x8(%rsp), %r15
jne 0x1a70fa
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8d8a8(%rip), %rdi     # 0x234bde
leaq 0x8c64e(%rip), %rsi     # 0x23398b
leaq 0x8d7ff(%rip), %rcx     # 0x234b43
movl $0x1306, %edx           # imm = 0x1306
callq 0x2110
leaq 0x8d880(%rip), %rdi     # 0x234bd5
leaq 0x8c62f(%rip), %rsi     # 0x23398b
leaq 0x8d7e0(%rip), %rcx     # 0x234b43
movl $0x12e2, %edx           # imm = 0x12E2
callq 0x2110
leaq 0x8d7bb(%rip), %rdi     # 0x234b2f
leaq 0x8c610(%rip), %rsi     # 0x23398b
leaq 0x8d7c1(%rip), %rcx     # 0x234b43
movl $0x12bd, %edx           # imm = 0x12BD
callq 0x2110
leaq 0x8d801(%rip), %rdi     # 0x234b94
leaq 0x8c5f1(%rip), %rsi     # 0x23398b
leaq 0x8d7a2(%rip), %rcx     # 0x234b43
movl $0x12d4, %edx           # imm = 0x12D4
callq 0x2110