// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %rdx, 0x10(%rsp)
movq %rdx, %rax
movq %rsi, (%rsp)
subq %rsi, %rax
cmpq $0x2, %rax
jge 0x167534
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, (%rsp)
js 0x1678b4
cmpq %r8, 0x10(%rsp)
jge 0x1678b4
movq %rcx, %r15
movq %rdi, %rbx
imulq $0x28, (%rsp), %r14
leaq (%rcx,%r14), %rsi
leaq 0x68(%rsp), %rcx
leaq 0x48(%rsp), %r9
leaq 0x60(%rsp), %rdx
leaq 0x40(%rsp), %r8
callq 0x1665b6
imulq $0x28, 0x10(%rsp), %rsi
addq %r15, %rsi
leaq 0x70(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x50(%rsp), %r8
movq %rbx, %rdi
leaq 0x58(%rsp), %rbx
movq %rdi, 0xb0(%rsp)
movq %rbx, %r9
callq 0x1665b6
movq -0x18(%rbx), %rax
testq %rax, %rax
js 0x1678d3
movq %rax, %r12
movq 0x48(%rsp), %rax
testq %rax, %rax
js 0x1678d3
movq %rax, %rbp
movq 0x50(%rsp), %rax
movq %rax, 0x38(%rsp)
testq %rax, %rax
js 0x1678d3
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
js 0x1678d3
leaq 0x60(%rsp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0xa8(%rsp)
movdqa 0x10(%rcx), %xmm0
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
movq %rax, 0x28(%rsp)
shlq $0x20, %rax
leaq 0xa0(%rsp), %rsi
movq %rax, -0x20(%rsi)
movq %rdx, 0x20(%rsp)
movq %rdx, %rax
shlq $0x20, %rax
incq %rax
movq %rax, -0x18(%rsi)
psllq $0x20, %xmm0
por 0x8a375(%rip), %xmm0    # 0x1f19b0
movdqa %xmm0, -0x10(%rsi)
movq 0x18(%rcx), %rax
movq %rax, 0xa0(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x156942
addq %r14, %r15
addq $0x28, %r15
addq $0x2, (%rsp)
movq (%rsp), %rax
decq %rax
cmpq 0x10(%rsp), %rax
jge 0x167522
movq 0xb0(%rsp), %rdi
movq %r15, %rsi
leaq 0x18(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1665b6
movq 0x18(%rsp), %rcx
cmpq 0x8(%rsp), %rcx
jg 0x167895
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
leaq 0x60(%rsp), %r14
movq %r14, %rsi
leaq 0x40(%rsp), %r13
movq %r13, %rdx
callq 0x1673ae
movl %eax, 0x14(%r15)
movq 0x8(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1673ae
movq %rax, %r14
movl %r14d, 0x18(%r15)
movl 0x14(%r15), %eax
testl %eax, %eax
setns %cl
movl %eax, %edx
cmpq %rdx, %r12
setle %sil
cmpq %rdx, 0x38(%rsp)
setge %r11b
testl %r14d, %r14d
setns %dil
movl %r14d, %r8d
movq %rbp, %rdx
cmpq %r8, %rbp
setle %r9b
cmpq %r8, 0x30(%rsp)
setge %r8b
cmpl %r14d, %eax
setle %r10b
andb %sil, %r11b
andb %r8b, %r11b
andb %r9b, %r10b
andb %r11b, %r10b
andb %cl, %r10b
testb %dil, %r10b
jne 0x167792
movq 0x8(%rsp), %r13
movq 0x20(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x30(%rsp), %rcx
movq %r13, %r8
callq 0x16749b
movq %rax, %r14
movslq %r14d, %rcx
movq 0x38(%rsp), %rax
cmpq %rax, %rcx
cmovgq 0xa8(%rsp), %r13
movl %r14d, 0x18(%r15)
cmovgeq %rax, %rcx
movq 0x18(%rsp), %r8
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x16749b
movq %rbp, %rdx
movl %eax, 0x14(%r15)
movslq %eax, %rcx
cmpq %rcx, %r12
jg 0x167876
cmpq %rcx, 0x38(%rsp)
jl 0x167876
cmpl %r14d, %eax
jg 0x167876
movslq %r14d, %rbp
cmpq %rbp, %rdx
jg 0x167876
cmpq %rbp, 0x30(%rsp)
jl 0x167876
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdi
movq (%rsp), %rdx
cmpq 0x10(%rsp), %rdx
jge 0x16785c
movq %rcx, 0x40(%rsp)
movq %rbp, 0x48(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, 0x68(%rsp)
shlq $0x20, %rsi
movq %rsi, 0x80(%rsp)
shlq $0x20, %rdi
incq %rdi
movq %rdi, 0x88(%rsp)
movdqa 0x70(%rsp), %xmm0
psllq $0x20, %xmm0
por 0x8a190(%rip), %xmm0    # 0x1f19b0
movdqa %xmm0, 0x90(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x156942
movq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
movslq 0x14(%r15), %rcx
movslq 0x18(%r15), %rbp
jmp 0x167866
movq %rdi, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
addq $0x28, %r15
incq (%rsp)
movq %rcx, %r12
jmp 0x167665
leaq 0x8b4f9(%rip), %rdi     # 0x1f2d76
leaq 0x8a29f(%rip), %rsi     # 0x1f1b23
leaq 0x8b450(%rip), %rcx     # 0x1f2cdb
movl $0x1306, %edx           # imm = 0x1306
callq 0x2110
leaq 0x8b4d1(%rip), %rdi     # 0x1f2d6d
leaq 0x8a280(%rip), %rsi     # 0x1f1b23
leaq 0x8b431(%rip), %rcx     # 0x1f2cdb
movl $0x12e2, %edx           # imm = 0x12E2
callq 0x2110
leaq 0x8b40c(%rip), %rdi     # 0x1f2cc7
leaq 0x8a261(%rip), %rsi     # 0x1f1b23
leaq 0x8b412(%rip), %rcx     # 0x1f2cdb
movl $0x12bd, %edx           # imm = 0x12BD
callq 0x2110
leaq 0x8b452(%rip), %rdi     # 0x1f2d2c
leaq 0x8a242(%rip), %rsi     # 0x1f1b23
leaq 0x8b3f3(%rip), %rcx     # 0x1f2cdb
movl $0x12d4, %edx           # imm = 0x12D4
callq 0x2110