// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, (%rsp)
movq 0x10(%rdi), %r13
leaq (,%r13,2), %r15
movq $0x1, 0x20(%rsp)
cmpq %r15, 0x8(%r8)
jae 0x1b23d2
leaq -0x1(%r15), %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)
movabsq $0x100000000, %r8       # imm = 0x100000000
movq $0x0, 0x8(%rbx)
testq %r13, %r13
movq %rbx, 0x8(%rsp)
jle 0x1b243b
movq (%rsp), %rax
movq (%rax), %rax
movq 0x10(%r14), %rcx
addq $0x4, %rax
xorl %edx, %edx
xorl %ebx, %ebx
movl (%rax), %esi
shlq $0x21, %rsi
addq %rdx, %rsi
movq %rsi, (%rcx,%rbx,8)
movl 0x4(%rax), %esi
shlq $0x21, %rsi
addq %rdx, %rsi
addq %r8, %rsi
movq %rsi, 0x8(%rcx,%rbx,8)
addq $0x2, %rbx
movl $0x0, 0x24(%rax)
incq %rdx
addq $0xa8, %rax
cmpq %rdx, %r13
jne 0x1b2401
jmp 0x1b243d
xorl %ebx, %ebx
movq 0x10(%r14), %rdi
leaq (%rdi,%rbx,8), %rsi
callq 0x194fef
movabsq $0x100000000, %r8       # imm = 0x100000000
movq %rbx, (%r14)
testq %r15, %r15
jle 0x1b2560
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %r12, %rbx
movq 0x10(%r14), %rdi
testq %r8, (%rdi,%r13,8)
jne 0x1b24c5
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x1b24a9
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
callq 0x2370
movabsq $0x100000000, %r8       # imm = 0x100000000
movq %rax, 0x10(%r14)
leaq 0x1(%rbx), %r12
movq (%r14), %rax
movq 0x10(%r14), %rcx
movl (%rcx,%r13,8), %edx
leaq 0x1(%rax), %rsi
movq %rsi, (%r14)
movq %rdx, (%rcx,%rax,8)
jmp 0x1b24c9
leaq -0x1(%rbx), %r12
cmpq $0x2, %rbx
setl %al
cmpq $0x2, %r12
setge %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1b24ec
movq 0x10(%r14), %rax
movq (%rax,%r13,8), %rbp
shrq $0x21, %rbp
jmp 0x1b2554
cmpq $0x2, %rbx
setge %al
cmpq $0x2, %r12
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1b2554
movq (%r14), %rdx
movq 0x10(%r14), %rax
movq (%rax,%r13,8), %r8
shrq $0x21, %r8
leaq (%rax,%r15,8), %rsi
subq %r15, %rdx
movq (%rsp), %rdi
movq %rbp, %rcx
movq 0x10(%rsp), %r9
movsd 0x72fbd(%rip), %xmm0    # 0x2254e8
pushq 0x8(%rsp)
leaq 0x28(%rsp), %rax
pushq %rax
pushq $0x10
pushq 0x30(%rsp)
callq 0x1b1f96
movabsq $0x100000000, %r8       # imm = 0x100000000
addq $0x20, %rsp
addq %r15, %rax
movq %rax, (%r14)
incq %r13
cmpq %r13, %r15
jne 0x1b2468
movq 0x8(%rsp), %r12
movq 0x8(%r12), %r14
testq %r14, %r14
je 0x1b260e
movq (%r12), %rdi
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rdi, %rsi
addq %r14, %rsi
callq 0x195d3d
testq %r14, %r14
jle 0x1b2607
negq %r14
movl $0x1, %ebx
movl $0x1c, %r15d
xorl %eax, %eax
movq %r14, %rcx
addq %rbx, %rcx
je 0x1b25c4
movq (%r12), %rcx
movl (%rcx,%r15), %edx
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
cmpl (%rcx,%rsi), %edx
jne 0x1b25c4
movq %rax, %rcx
jmp 0x1b25f0
movq %rbx, %rdx
subq %rax, %rdx
movq %rbx, %rcx
cmpq $0x2, %rdx
jl 0x1b25f0
movq (%r12), %rsi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rax, %rdi
addq %rsi, %rdi
addq %r15, %rsi
callq 0x1957bd
movq %rbx, %rcx
leaq (%r14,%rbx), %rdx
incq %rdx
incq %rbx
addq $0x1c, %r15
movq %rcx, %rax
cmpq $0x1, %rdx
jne 0x1b259f
movq 0x8(%r12), %rax
jmp 0x1b2610
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq