// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_shared_intervals(overlap_region_alloc*, ul_idx_t const*, ug_opt_t const*, long, kvec_t_u64_warp*, kv_ul_ov_t*)
uint64_t gen_shared_intervals(overlap_region_alloc* ol, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t wl, kvec_t_u64_warp* idx, kv_ul_ov_t *res)
{
	int64_t i, j, k, on = ol->length, srt_n = on<<1, dp, old_dp, beg, end, is_srt = 1; 
	///collect overlapped regions
	kv_resize(uint64_t, idx->a, (ol->length<<1)); res->n = 0;
	for (i = k = 0; i < on; i++) {
		idx->a.a[k] = (ol->list[i].x_pos_s<<1); idx->a.a[k] <<= 32; idx->a.a[k] += i; k++;
		idx->a.a[k] = (ol->list[i].x_pos_e<<1)+1; idx->a.a[k] <<= 32; idx->a.a[k] += i; k++;
		ol->list[i].align_length = 0;
		// fprintf(stderr, "+++[M::%s::utg%.6dl] q[%u, %u), t[%u, %u)\n", __func__, (int32_t)ol->list[i].y_id+1,
		// ol->list[i].x_pos_s, ol->list[i].x_pos_e+1, 
		// ol->list[i].y_pos_s, ol->list[i].y_pos_e+1);
	}
	radix_sort_gfa64(idx->a.a, idx->a.a+k); idx->a.n = k;
	for (i = 0, dp = 0, beg = 0, end = -1; i < srt_n; ++i) {///[beg, end]
		old_dp = dp;
        ///if idx->a.a[] is qe
        if ((idx->a.a[i]>>32)&1) {
			--dp; 
		}else {
			//meet a new overlap; the overlaps are pushed by the x_pos_s
			++dp; kv_push(uint64_t, idx->a, ((uint32_t)idx->a.a[i]));
		}

		///old_dp < dp, idx->a.a[] is qs
		if (old_dp < 2 && dp >= 2) {
			beg = idx->a.a[i]>>33;
		} else if (old_dp >= 2 && dp < 2) {///old_dp > min_dp, idx->a.a[] is qe
			end = idx->a.a[i]>>33;///[beg, end]
			idx->a.n = srt_n + gen_shared_trace(ol, idx->a.a+srt_n, idx->a.n-srt_n, beg, end, uref, uopt, G_CHAIN_BW, N_GCHAIN_RATE, &is_srt, res);
        } 
	}
	if(!res->n) return res->n;
	
	int64_t res_n = res->n;
	// if(!is_srt) {
		radix_sort_ul_ov_srt_qn(res->a, res->a + res->n);
		for (i = 1, j = 0; i <= res_n; i++) {
			if (i == res_n || res->a[i].qn != res->a[j].qn) {
				if(i - j > 1) radix_sort_ul_ov_srt_qs(res->a+j, res->a+i);
				j = i;
			}
    	}
	// }
	

	// fprintf(stderr, "[M::%s::] res->n::%d\n", __func__, (int32_t)res->n);
	// for (i = 0; i < res_n; i++) {
	// 	fprintf(stderr, "---[M::%s::utg%.6dl] oid::%u, q[%u, %u)\n", __func__, 
	// 	(int32_t)ol->list[res->a[i].qn].y_id+1, res->a[i].qn, res->a[i].qs, res->a[i].qe);
	// }
	return res->n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %r12
movq %r8, %r14
movq %rdx, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
movq 0x10(%rdi), %rbx
leaq (%rbx,%rbx), %rax
movq $0x1, 0x30(%rsp)
movq %rax, 0x8(%rsp)
cmpq %rax, 0x8(%r8)
jae 0x1716ce
movq 0x8(%rsp), %rax
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
andq $0x0, 0x8(%r12)
xorl %eax, %eax
testq %rbx, %rbx
cmovleq %rax, %rbx
movabsq $0x100000000, %r10      # imm = 0x100000000
pushq $0x2
popq %rcx
xorl %r15d, %r15d
xorl %edx, %edx
cmpq %rdx, %rbx
je 0x171735
movq (%r13), %rsi
movq 0x10(%r14), %rdi
movl (%rsi,%rcx,2), %r8d
shlq $0x21, %r8
addq %rdx, %r8
movq %r8, (%rdi,%r15,8)
movl 0x4(%rsi,%rcx,2), %r8d
shlq $0x21, %r8
addq %rdx, %r8
addq %r10, %r8
movq %r8, 0x8(%rdi,%r15,8)
andl $0x0, 0x24(%rsi,%rcx,2)
addq $0x2, %r15
incq %rdx
addq $-0x10, %rax
addq $0x54, %rcx
jmp 0x1716ef
movq %r13, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movq 0x10(%r14), %rdi
movq %rdi, %rsi
subq %rax, %rsi
callq 0x156b0d
movabsq $0x100000000, %rdi      # imm = 0x100000000
movq %r15, (%r14)
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
testq %rax, %rax
movl $0x0, %ebx
cmovgq %rax, %rbx
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq %r15, %rbx
je 0x171871
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rcx
testq %rdi, %rcx
jne 0x1717d9
movq (%r14), %rdx
cmpq 0x8(%r14), %rdx
jne 0x1717c6
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movabsq $0x100000000, %rdi      # imm = 0x100000000
movq %rax, 0x10(%r14)
movl (%rax,%r15,8), %ecx
movq (%r14), %rdx
leaq 0x1(%r13), %r12
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movq %rcx, (%rax,%rdx,8)
jmp 0x1717dd
leaq -0x1(%r13), %r12
cmpq $0x2, %r13
setl %cl
cmpq $0x2, %r12
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1717fc
movq (%rax,%r15,8), %rbp
shrq $0x21, %rbp
jmp 0x171866
cmpq $0x2, %r13
setge %cl
cmpq $0x2, %r12
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x171866
movq (%rax,%r15,8), %r8
shrq $0x21, %r8
movq 0x8(%rsp), %r13
leaq (%rax,%r13,8), %rsi
movq (%r14), %rdx
subq %r13, %rdx
movq 0x18(%rsp), %rdi
movq %rbp, %rcx
movq 0x20(%rsp), %r9
movsd 0x71cfb(%rip), %xmm0    # 0x1e3538
pushq 0x10(%rsp)
leaq 0x38(%rsp), %rax
pushq %rax
pushq $0x10
pushq 0x40(%rsp)
callq 0x1712a7
movabsq $0x100000000, %rdi      # imm = 0x100000000
addq $0x20, %rsp
addq %r13, %rax
movq %rax, (%r14)
incq %r15
movq %r12, %r13
jmp 0x171774
movq 0x10(%rsp), %r13
movq 0x8(%r13), %rbx
testq %rbx, %rbx
je 0x171909
movq (%r13), %rdi
imulq $0x1c, %rbx, %rsi
addq %rdi, %rsi
callq 0x15775d
xorl %eax, %eax
testq %rbx, %rbx
movl $0x0, %r14d
cmovgq %rbx, %r14
negq %r14
pushq $0x1
popq %r15
pushq $0x1c
popq %r12
leaq (%r14,%r15), %rcx
cmpq $0x1, %rcx
je 0x171903
cmpq %r15, %rbx
je 0x1718d2
movq (%r13), %rcx
movl (%rcx,%r12), %edx
imulq $0x1c, %rax, %rsi
cmpl (%rcx,%rsi), %edx
jne 0x1718d2
movq %rax, %rcx
jmp 0x1718f7
movq %r15, %rdx
subq %rax, %rdx
movq %r15, %rcx
cmpq $0x2, %rdx
jl 0x1718f7
movq (%r13), %rsi
imulq $0x1c, %rax, %rdi
addq %rsi, %rdi
addq %r12, %rsi
callq 0x1571fa
movq %r15, %rcx
incq %r15
addq $0x1c, %r12
movq %rcx, %rax
jmp 0x1718ad
movq 0x8(%r13), %rax
jmp 0x17190b
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq