// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %rcx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x88(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1b347a
movq %r9, 0x8(%rsp)
movq 0x80(%rsp), %rbp
movq 0x18(%rsp), %r13
cmpq $0x0, 0x10(%r13)
je 0x1b318d
xorl %ebx, %ebx
movq (%r13), %r14
imulq $0xa8, %rbx, %r12
leaq (%r14,%r12), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4a082
movq 0x48(%r14,%r12), %rax
movl $0xffffffff, 0x28(%r14,%r12) # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x1b3184
movq 0x58(%r15), %rcx
addq $0x12, %rcx
cmpw $0x0, (%rcx)
jns 0x1b317b
movw $0x0, (%rcx)
addq $0x20, %rcx
decq %rax
jne 0x1b3170
incq %rbx
cmpq 0x10(%r13), %rbx
jb 0x1b313b
movq 0x88(%rsp), %rbx
movq 0x8(%rbx), %rax
addq %rax, %rax
movq 0x8(%rsp), %r9
cmpq %rax, 0x8(%r9)
jae 0x1b31fd
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(%r9)
movq 0x10(%r9), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq %rax, 0x10(%r9)
movq 0x8(%rbx), %rax
movq 0x70(%rsp), %rcx
cmpq %rax, 0x8(%rcx)
jae 0x1b3267
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 0x8(%rsp), %r9
movq %rax, 0x10(%r14)
movabsq $0x100000000, %r10      # imm = 0x100000000
cmpq $0x0, 0x8(%rbx)
je 0x1b32ec
xorl %eax, %eax
xorl %ecx, %ecx
xorl %ebp, %ebp
testq %rcx, %rcx
je 0x1b328f
movq (%rbx), %rdx
movl (%rdx,%rax), %esi
cmpl -0x1c(%rdx,%rax), %esi
je 0x1b32a4
movq (%r13), %rdx
movq (%rbx), %rsi
movl (%rsi,%rax), %esi
imulq $0xa8, %rsi, %rsi
movl %ecx, 0x28(%rdx,%rsi)
movq (%rbx), %rdx
movq 0x10(%r9), %rsi
movl 0x4(%rdx,%rax), %edi
shlq $0x21, %rdi
addq %rcx, %rdi
movq %rdi, (%rsi,%rbp,8)
movl 0x8(%rdx,%rax), %edi
leal -0x2(,%rdi,2), %edi
shlq $0x20, %rdi
addq %rcx, %rdi
addq %r10, %rdi
movq %rdi, 0x8(%rsi,%rbp,8)
addq $0x2, %rbp
orb $0x40, 0x1b(%rdx,%rax)
incq %rcx
addq $0x1c, %rax
cmpq 0x8(%rbx), %rcx
jb 0x1b327e
jmp 0x1b32ee
xorl %ebp, %ebp
movq 0x10(%r9), %rdi
leaq (%rdi,%rbp,8), %rsi
callq 0x194fef
movq 0x8(%rsp), %r8
movq %rbp, (%r8)
movq 0x80(%rsp), %rax
leaq (%rax,%rax), %r15
movq 0x10(%rsp), %rax
cmpq %r15, 0x10(%rax)
jge 0x1b3336
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x2370
movq 0x8(%rsp), %r8
movq %rax, (%rbx)
movq %r15, 0x10(%rbx)
testq %rbp, %rbp
movabsq $0x100000000, %r9       # imm = 0x100000000
je 0x1b347a
xorl %r14d, %r14d
leaq (,%rbp,8), %rax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %rbx, %r13
movq %r15, %r12
movq 0x10(%r8), %rdi
movq (%rdi,%r14,8), %rax
movq %rax, %r15
shrq $0x21, %r15
testq %r9, %rax
jne 0x1b33cd
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x1b33b1
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
callq 0x2370
movabsq $0x100000000, %r9       # imm = 0x100000000
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r8)
leaq 0x1(%r13), %rbx
movq (%r8), %rax
movq 0x10(%r8), %rcx
movl (%rcx,%r14,8), %edx
leaq 0x1(%rax), %rsi
movq %rsi, (%r8)
movq %rdx, (%rcx,%rax,8)
jmp 0x1b33d4
leaq -0x1(%r13), %rbx
incq %r15
movq %r15, %rax
subq %r12, %rax
jbe 0x1b346e
cmpq 0x80(%rsp), %rax
setg %al
cmpq $0x2, %r13
setge %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1b346e
movq 0x10(%r8), %rcx
addq 0x20(%rsp), %rcx
movq (%r8), %r8
subq %rbp, %r8
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x80(%rsp), %r11
leaq (%rax,%r11), %r10
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r12, %r9
pushq 0x88(%rsp)
pushq %r10
pushq %rax
pushq %r13
movq 0x90(%rsp), %rax
pushq 0x10(%rax)
pushq %r11
pushq 0xa8(%rsp)
pushq %r15
callq 0x1b2c0a
movabsq $0x100000000, %r9       # imm = 0x100000000
movq 0x48(%rsp), %r8
addq $0x40, %rsp
addq %rbp, %rax
movq %rax, (%r8)
incq %r14
cmpq %r14, %rbp
jne 0x1b335e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq