// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
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 0x1bbbdd
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(%r12)
testq %r13, %r13
movq %r12, 0x8(%rsp)
jle 0x1bbc47
movq (%rsp), %rax
movq (%rax), %rax
movq 0x10(%r14), %rdi
addq $0x4, %rax
xorl %ecx, %ecx
xorl %ebx, %ebx
movl (%rax), %edx
shlq $0x21, %rdx
addq %rcx, %rdx
movq %rdx, (%rdi,%rbx,8)
movl 0x4(%rax), %edx
shlq $0x21, %rdx
addq %rcx, %rdx
addq %r8, %rdx
movq %rdx, 0x8(%rdi,%rbx,8)
addq $0x2, %rbx
movl $0x0, 0x24(%rax)
incq %rcx
addq $0xa8, %rax
cmpq %rcx, %r13
jne 0x1bbc0d
jmp 0x1bbc4d
movq 0x10(%r14), %rdi
xorl %ebx, %ebx
leaq (%rdi,%rbx,8), %rsi
callq 0x19dd99
movq %rbx, (%r14)
testq %r15, %r15
movabsq $0x100000000, %rdi      # imm = 0x100000000
jle 0x1bbd68
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %r12, %rbx
movq 0x10(%r14), %rax
movq (%rax,%r13,8), %rcx
testq %rdi, %rcx
jne 0x1bbcd5
movq (%r14), %rdx
cmpq 0x8(%r14), %rdx
jne 0x1bbcc2
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
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,%r13,8), %ecx
movq (%r14), %rdx
leaq 0x1(%rbx), %r12
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r14)
movq %rcx, (%rax,%rdx,8)
jmp 0x1bbcd9
leaq -0x1(%rbx), %r12
cmpq $0x2, %rbx
setl %cl
cmpq $0x2, %r12
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1bbcf8
movq (%rax,%r13,8), %rbp
shrq $0x21, %rbp
jmp 0x1bbd5c
cmpq $0x2, %rbx
setge %cl
cmpq $0x2, %r12
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1bbd5c
movq (%rax,%r13,8), %r8
shrq $0x21, %r8
leaq (%rax,%r15,8), %rsi
movq (%r14), %rdx
subq %r15, %rdx
movq (%rsp), %rdi
movq %rbp, %rcx
movq 0x10(%rsp), %r9
movsd 0x758b5(%rip), %xmm0    # 0x2315e8
pushq 0x8(%rsp)
leaq 0x28(%rsp), %rax
pushq %rax
pushq $0x10
pushq 0x30(%rsp)
callq 0x1bb79d
movabsq $0x100000000, %rdi      # imm = 0x100000000
addq $0x20, %rsp
addq %r15, %rax
movq %rax, (%r14)
incq %r13
cmpq %r13, %r15
jne 0x1bbc74
movq 0x8(%rsp), %r12
movq 0x8(%r12), %r14
testq %r14, %r14
je 0x1bbe16
movq (%r12), %rdi
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rdi, %rsi
addq %r14, %rsi
callq 0x19ead4
testq %r14, %r14
jle 0x1bbe0f
negq %r14
movl $0x1, %ebx
movl $0x1c, %r15d
xorl %eax, %eax
movq %r14, %rcx
addq %rbx, %rcx
je 0x1bbdcc
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 0x1bbdcc
movq %rax, %rcx
jmp 0x1bbdf8
movq %rbx, %rdx
subq %rax, %rdx
movq %rbx, %rcx
cmpq $0x2, %rdx
jl 0x1bbdf8
movq (%r12), %rsi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rax, %rdi
addq %rsi, %rdi
addq %r15, %rsi
callq 0x19e55c
movq %rbx, %rcx
leaq (%r14,%rbx), %rdx
incq %rdx
incq %rbx
addq $0x1c, %r15
movq %rcx, %rax
cmpq $0x1, %rdx
jne 0x1bbda7
movq 0x8(%r12), %rax
jmp 0x1bbe18
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq