// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %rdi, 0x28(%rsp)
movq %rdx, %rax
subq %rsi, %rax
cmpq $0x2, %rax
jl 0x1b056c
movq %rsi, %r15
testq %rsi, %rsi
js 0x1b05bc
movq %rdx, %r14
cmpq %r8, %rdx
jge 0x1b05bc
movq %rcx, %rbx
leaq (%r15,%r15,4), %rax
leaq (%rcx,%rax,8), %rsi
leaq 0x38(%rsp), %rcx
leaq 0x58(%rsp), %r9
leaq 0x30(%rsp), %rdx
leaq 0x50(%rsp), %r8
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x1af158
leaq (%r14,%r14,4), %rax
movq %rbx, 0x90(%rsp)
leaq (%rbx,%rax,8), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x60(%rsp), %r8
leaq 0x68(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %r9
callq 0x1af158
movq -0x18(%rbx), %r13
testq %r13, %r13
js 0x1b05db
movq 0x58(%rsp), %r12
testq %r12, %r12
js 0x1b05db
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
testq %rax, %rax
js 0x1b05db
movq 0x68(%rsp), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
js 0x1b05db
movdqa 0x40(%rsp), %xmm0
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %rbx
movq %r11, %rcx
shlq $0x20, %rcx
leaq 0xb0(%rsp), %rax
movq %rcx, (%rax)
movq %rbx, %rcx
shlq $0x20, %rcx
incq %rcx
movdqa %xmm0, %xmm1
psllq $0x20, %xmm1
por 0x81013(%rip), %xmm1    # 0x2312c0
movq %rcx, 0x8(%rax)
movdqa %xmm1, 0x10(%rax)
movl $0x8, %ecx
movq %rax, %rdx
movq %rdx, %rdi
leaq (%rsp,%rcx), %rdx
addq $0xb0, %rdx
movq (%rdx), %rsi
cmpq (%rdi), %rsi
jae 0x1b02ef
movq %rdx, %rdi
movq -0x8(%rdi), %r8
cmpq %r8, %rsi
jae 0x1b02ec
movq %r8, (%rdi)
addq $-0x8, %rdi
cmpq %rax, %rdi
ja 0x1b02d7
movq %rsi, (%rdi)
leaq 0x8(%rcx), %rsi
cmpq $0x18, %rcx
movq %rsi, %rcx
jb 0x1b02be
incq %r15
cmpq %r14, %r15
jge 0x1b056c
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
movq %xmm1, 0x80(%rsp)
movq %xmm0, 0x78(%rsp)
leaq 0xb0(%rsp), %rbp
movq %r14, 0x88(%rsp)
movq %rbx, 0x98(%rsp)
movq %r11, 0xa0(%rsp)
movq %r15, 0xa8(%rsp)
leaq (%r15,%r15,4), %rax
movq 0x90(%rsp), %rcx
leaq (%rcx,%rax,8), %r15
movq 0x28(%rsp), %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1af158
movq 0x10(%rsp), %rcx
cmpq 0x8(%rsp), %rcx
jg 0x1b059d
movq %rbp, %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
leaq 0x50(%rsp), %rbx
movq %rbx, %rdx
callq 0x1b0036
movl %eax, 0x14(%r15)
movq 0x8(%rsp), %rcx
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1b0036
movq %rax, %r14
movl %r14d, 0x18(%r15)
movl 0x14(%r15), %eax
testl %eax, %eax
setns %cl
movq %r13, %rbx
cmpq %rax, %r13
setle %r11b
cmpq %rax, 0x20(%rsp)
setge %sil
testl %r14d, %r14d
setns %dil
movl %r14d, %r8d
movq %r12, %rdx
cmpq %r8, %r12
setle %r9b
cmpq %r8, 0x18(%rsp)
setge %r8b
cmpl %r14d, %eax
setle %r10b
andb %sil, %r8b
andb %r11b, %r9b
andb %r8b, %r9b
andb %cl, %r10b
andb %r9b, %r10b
testb %dil, %r10b
jne 0x1b046b
movq 0x8(%rsp), %rbx
movq 0x98(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x18(%rsp), %rcx
movq %rbx, %r8
callq 0x1b0132
movq %rax, %r14
movslq %r14d, %rcx
movq 0x20(%rsp), %rax
cmpq %rax, %rcx
cmovgq 0x78(%rsp), %rbx
movl %r14d, 0x18(%r15)
cmovgeq %rax, %rcx
movq 0x10(%rsp), %r8
movq 0xa0(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rbx
movq %r13, %rdx
callq 0x1b0132
movq %r12, %rdx
movl %eax, 0x14(%r15)
movslq %eax, %r13
cmpq %r13, %rbx
jg 0x1b057e
cmpq %r13, 0x20(%rsp)
jl 0x1b057e
cmpl %r14d, %eax
jg 0x1b057e
movslq %r14d, %r12
cmpq %r12, %rdx
jg 0x1b057e
cmpq %r12, 0x18(%rsp)
jl 0x1b057e
movq 0xa8(%rsp), %r15
incq %r15
movq 0x10(%rsp), %r11
movq 0x8(%rsp), %rbx
movq 0x88(%rsp), %r14
cmpq %r14, %r15
jge 0x1b0563
movq %r13, 0x50(%rsp)
movq %r12, 0x58(%rsp)
movq %r11, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
shlq $0x20, %r11
movq %r11, 0xb0(%rsp)
shlq $0x20, %rbx
incq %rbx
movq %rbx, 0xb8(%rsp)
movdqa 0x40(%rsp), %xmm0
psllq $0x20, %xmm0
por 0x80db6(%rip), %xmm0    # 0x2312c0
movdqa %xmm0, 0xc0(%rsp)
movl $0x8, %edx
movq %rbp, %rsi
movq %rsi, %r8
leaq (%rsp,%rdx), %rsi
addq $0xb0, %rsi
movq (%rsi), %rdi
cmpq (%r8), %rdi
jae 0x1b054c
movq %rsi, %r8
movq -0x8(%r8), %r9
cmpq %r9, %rdi
jae 0x1b0549
movq %r9, (%r8)
addq $-0x8, %r8
cmpq %rbp, %r8
ja 0x1b0534
movq %rdi, (%r8)
leaq 0x8(%rdx), %rdi
cmpq $0x18, %rdx
movq %rdi, %rdx
jb 0x1b051b
movq 0x10(%rsp), %r11
movq 0x8(%rsp), %rbx
cmpq %r14, %r15
jne 0x1b032c
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x90929(%rip), %rdi     # 0x240eae
leaq 0x8f6cf(%rip), %rsi     # 0x23fc5b
leaq 0x90880(%rip), %rcx     # 0x240e13
movl $0x1306, %edx           # imm = 0x1306
callq 0x2110
leaq 0x90901(%rip), %rdi     # 0x240ea5
leaq 0x8f6b0(%rip), %rsi     # 0x23fc5b
leaq 0x90861(%rip), %rcx     # 0x240e13
movl $0x12e2, %edx           # imm = 0x12E2
callq 0x2110
leaq 0x9083c(%rip), %rdi     # 0x240dff
leaq 0x8f691(%rip), %rsi     # 0x23fc5b
leaq 0x90842(%rip), %rcx     # 0x240e13
movl $0x12bd, %edx           # imm = 0x12BD
callq 0x2110
leaq 0x90882(%rip), %rdi     # 0x240e64
leaq 0x8f672(%rip), %rsi     # 0x23fc5b
leaq 0x90823(%rip), %rcx     # 0x240e13
movl $0x12d4, %edx           # imm = 0x12D4
callq 0x2110