// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: update_shared_intervals(overlap_region_alloc*, ul_idx_t const*, ug_opt_t const*, All_reads*, UC_Read*, kvec_t_u64_warp*, st_mt_t*, long, long, kv_ul_ov_t*, unsigned long)
void update_shared_intervals(overlap_region_alloc* ol, const ul_idx_t *uref, const ug_opt_t *uopt, 
All_reads *rref, UC_Read* tu, kvec_t_u64_warp* idx, st_mt_t *sps, int64_t ql, int64_t wl, kv_ul_ov_t *aln, uint64_t rid)
{
	if(!aln->n) return;
	uint64_t i, k, own, srt_n; int64_t dp, old_dp, beg, end; overlap_region *z;
    for (i = 0; i < ol->length; i++) {
		z = &(ol->list[i]); append_unmatched_wins(z, wl); 
		own = z->w_list.n; z->align_length = (uint32_t)-1;
		for (k = 0; k < own; k++) {
			if(z->w_list.a[k].extra_end < 0) z->w_list.a[k].extra_end = 0;
		}		
    }

    kv_resize(uint64_t, idx->a, (aln->n<<1)); kv_resize(uint64_t, *sps, aln->n);
    for (i = srt_n = 0; i < aln->n; i++) {
		if(i == 0 || aln->a[i].qn != aln->a[i-1].qn) ol->list[aln->a[i].qn].align_length = i;
        idx->a.a[srt_n] = aln->a[i].qs<<1; idx->a.a[srt_n] <<= 32; idx->a.a[srt_n] += i; srt_n++;
        idx->a.a[srt_n] = ((aln->a[i].qe-1)<<1)+1; idx->a.a[srt_n] <<= 32; idx->a.a[srt_n] += i; srt_n++;
		aln->a[i].el = 1;
    }

    radix_sort_gfa64(idx->a.a, idx->a.a+srt_n); idx->a.n = srt_n; resize_UC_Read(tu, (wl<<1));
    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; end = (idx->a.a[i]>>33)+1;
		}else {
			//meet a new overlap; the overlaps are pushed by the x_pos_s
			++dp; end = (idx->a.a[i]>>33);
			kv_push(uint64_t, idx->a, ((uint32_t)idx->a.a[i]));
		}
		///[beg, end)
		// if(rid == 92) {
		// 	fprintf(stderr, "[M::%s::input] beg::%ld, end::%ld, old_dp::%ld, dp::%ld\n", 
		// 	__func__, beg, end, old_dp, dp);
		// }
		if((end > beg) && (end - beg > wl) && (old_dp >= 2) ) {
			idx->a.n = srt_n + 
					gen_commen_win(rref, uref, ol, idx->a.a+srt_n, idx->a.n-srt_n, beg, end, ql, wl, sps->a, old_dp, tu->seq, tu->seq+wl, aln);
		}
        beg = end;
	}

	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq 0x88(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x172644
movq %rdi, %rbp
xorl %ebx, %ebx
pushq $0x12
popq %r14
movq %r9, (%rsp)
cmpq 0x10(%rbp), %rbx
jae 0x17236c
movq (%rbp), %r12
imulq $0xa8, %rbx, %r13
leaq (%r12,%r13), %r15
movq %r15, %rdi
movq 0x80(%rsp), %rsi
callq 0x1cf34
movq 0x48(%r12,%r13), %rax
orl $-0x1, 0x28(%r12,%r13)
movq %r14, %rcx
subq $0x1, %rax
jb 0x172363
movq 0x58(%r15), %rdx
cmpw $0x0, (%rdx,%rcx)
jns 0x17235d
andw $0x0, (%rdx,%rcx)
addq $0x20, %rcx
jmp 0x172347
incq %rbx
movq (%rsp), %r9
jmp 0x172314
movq 0x88(%rsp), %rbx
movq 0x8(%rbx), %rax
leaq (%rax,%rax), %rcx
cmpq %rcx, 0x8(%r9)
jae 0x1723db
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r9)
movq 0x10(%r9), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r9)
movq 0x8(%rbx), %rax
movq 0x70(%rsp), %rcx
cmpq %rax, 0x8(%rcx)
jae 0x172440
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 0x70(%rsp), %r14
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r14)
pushq $0xc
popq %rax
movabsq $0x100000000, %r10      # imm = 0x100000000
xorl %r11d, %r11d
xorl %r12d, %r12d
xorl %ecx, %ecx
cmpq 0x8(%rbx), %rcx
jae 0x1724c5
movq (%rbx), %rdx
testq %rcx, %rcx
je 0x17246f
movl -0x18(%rdx,%rax,2), %esi
cmpl -0x34(%rdx,%rax,2), %esi
jne 0x172471
jmp 0x172482
movl (%rdx), %esi
movq (%rbp), %rdi
movl %esi, %esi
imulq $0xa8, %rsi, %rsi
movl %ecx, 0x28(%rdi,%rsi)
movq 0x10(%r9), %rsi
movl -0x14(%rdx,%rax,2), %edi
shlq $0x21, %rdi
addq %rcx, %rdi
movq %rdi, (%rsi,%r12,8)
movl -0x10(%rdx,%rax,2), %edi
leal -0x2(,%rdi,2), %edi
shlq $0x20, %rdi
addq %rcx, %rdi
addq %r10, %rdi
movq %rdi, 0x8(%rsi,%r12,8)
orb $0x40, 0x3(%rdx,%rax,2)
addq $0x2, %r12
incq %rcx
addq $-0x10, %r11
addq $0xe, %rax
jmp 0x172455
movq 0x10(%r9), %rdi
movq %rdi, %rsi
subq %r11, %rsi
negq %r11
movq %r11, 0x18(%rsp)
callq 0x156b0d
movq (%rsp), %rax
movq %r12, (%rax)
movq 0x80(%rsp), %rax
leaq (%rax,%rax), %rsi
movq 0x8(%rsp), %rdi
callq 0x562fa
movabsq $0x100000000, %r8       # imm = 0x100000000
movq (%rsp), %rdi
movq %r12, %rax
negq %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %rbp, 0x20(%rsp)
cmpq %r14, %r12
je 0x172644
movq 0x10(%rdi), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, %r15
shrq $0x21, %r15
testq %r8, %rcx
jne 0x17258f
movq (%rdi), %rdx
cmpq 0x8(%rdi), %rdx
jne 0x17257c
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movabsq $0x100000000, %r8       # imm = 0x100000000
movq (%rsp), %rdi
movq %rax, 0x10(%rdi)
movl (%rax,%r14,8), %ecx
movq (%rdi), %rdx
leaq 0x1(%rbx), %rbp
movl %ecx, %ecx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rdi)
movq %rcx, (%rax,%rdx,8)
jmp 0x172596
leaq -0x1(%rbx), %rbp
incq %r15
movq %r15, %rcx
subq %r13, %rcx
jbe 0x172636
cmpq 0x80(%rsp), %rcx
setg %cl
cmpq $0x2, %rbx
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x172636
addq 0x18(%rsp), %rax
movq (%rdi), %r8
addq 0x10(%rsp), %r8
movq 0x8(%rsp), %rcx
movq (%rcx), %r10
movq 0x80(%rsp), %rcx
leaq (%r10,%rcx), %r11
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rax, %rcx
movq %r13, %r9
pushq 0x88(%rsp)
pushq %r11
pushq %r10
pushq %rbx
movq 0x90(%rsp), %rax
pushq 0x10(%rax)
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq %r15
callq 0x171e58
movabsq $0x100000000, %r8       # imm = 0x100000000
movq 0x40(%rsp), %rdi
addq $0x40, %rsp
addq %r12, %rax
movq %rax, (%rdi)
incq %r14
movq %rbp, %rbx
movq %r15, %r13
jmp 0x17251f
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq