// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
shlq %rax
movq %rax, -0x60(%rbp)
movq $0x1, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
shlq %rcx
cmpq %rcx, %rax
jae 0x2ae99f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
shlq %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x30(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x30(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x30(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x30(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x30(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2ae9a1
movq -0x38(%rbp), %rax
movq $0x0, 0x8(%rax)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x2aeac6
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
shll %eax
movl %eax, %eax
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x40(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shll %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
shlq $0x20, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rcx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x40(%rbp), %rcx
addq %rcx, %rax
movl $0x0, 0x28(%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2ae9bd
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x50(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x27a9b0
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq $-0x1, -0x80(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x2aecf1
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x2aeb56
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x2aec06
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x2aebd3
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2aeb92
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x98(%rbp)
jmp 0x2aeba0
movl $0x2, %eax
movq %rax, -0x98(%rbp)
jmp 0x2aeba0
movq -0x98(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x2aec06
cmpq $0x2, -0x70(%rbp)
jge 0x2aec31
cmpq $0x2, -0x68(%rbp)
jl 0x2aec31
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x78(%rbp)
jmp 0x2aecde
cmpq $0x2, -0x70(%rbp)
jl 0x2aecdc
cmpq $0x2, -0x68(%rbp)
jge 0x2aecdc
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x60(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x30(%rbp), %rax
movq (%rax), %rdx
subq -0x60(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x80(%rbp), %r8
movq -0x18(%rbp), %r9
movq -0x20(%rbp), %r11
movq -0x38(%rbp), %rax
movsd 0xd0388(%rip), %xmm0    # 0x37f030
leaq -0x88(%rbp), %r10
movq %r11, (%rsp)
movq $0x10, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x2ae150
movq -0xa0(%rbp), %rcx
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2aecde
jmp 0x2aece0
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2aeb11
movq -0x38(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2aed0d
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x2aede1
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
imulq $0x1c, 0x8(%rax), %rax
addq %rax, %rsi
callq 0x27c310
movq $0x1, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x90(%rbp), %rax
jg 0x2aedd5
movq -0x40(%rbp), %rax
cmpq -0x90(%rbp), %rax
je 0x2aed89
movq -0x38(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x40(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x48(%rbp), %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
je 0x2aedc2
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
cmpq $0x1, %rax
jle 0x2aedba
movq -0x38(%rbp), %rax
movq (%rax), %rdi
imulq $0x1c, -0x48(%rbp), %rax
addq %rax, %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
imulq $0x1c, -0x40(%rbp), %rax
addq %rax, %rsi
callq 0x27b890
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x2aedc4
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2aed4b
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nop