// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Process_Read.cpp
// Function: retrieve_r_cov_region(ul_idx_t const*, unsigned long, unsigned char, unsigned long, unsigned long, long*)
uint64_t retrieve_r_cov_region(const ul_idx_t *ul, uint64_t id, uint8_t strand, uint64_t s, uint64_t e, int64_t *pi)
{
    uint64_t *a = ul->cr->interval.a + ul->cr->idx[id], cc = 0, o = 0, tk, ts, te, tcc = 0;
    int64_t a_n = ul->cr->idx[id+1]-ul->cr->idx[id], k = 0, cc_i = pi? *pi:0;
    if(a_n == 0) return e>=s?e-s:0;
    if(cc_i + 1 >= a_n || cc_i < 0) cc_i = 0;
    if(strand) {
		tk = s;
		s = ul->ug->u.a[id].len - e;
		e = ul->ug->u.a[id].len - tk;
	}
	// fprintf(stderr,"\nul->ug->u.a[id].len:%u, fs:%lu, fe:%lu\n", ul->ug->u.a[id].len, a[k]>>32, a[k+1]>>32);
	k = cc_i; tk = s;
	if(tk < (a[k]>>32)) {
		for (; k >= 0; k--) {
            if(tk>=(a[k]>>32) && tk<(a[k+1]>>32)) break;
        }
	} else if(tk >= (a[k+1]>>32)) {
		for (; k + 1 < a_n; k++) {
            if(tk>=(a[k]>>32) && tk<(a[k+1]>>32)) break;
        }
	}
	if(pi) *pi = k;
	if(k + 1 >= a_n) return e>=s?e-s:0;
	if(k < 0) k = 0;
	
	///note: for unitig coverage, all 
	for (; k + 1 < a_n; k++) {
		// fprintf(stderr, "[M::%s] k:%ld, a_n:%ld\n", __func__, k, a_n);
		ts = a[k]>>32; te = a[k+1]>>32; cc = (uint32_t)a[k];
		o = (MIN(e, te) > MAX(s, ts))?(MIN(e, te)-MAX(s, ts)):0;
		tcc += o*cc;
		// fprintf(stderr, ">>k:%ld, s:%lu, e:%lu, ts:%lu, te:%lu, o:%lu, cc:%lu\n", k, s, e, ts, te, o, cc);
		if(e>=(a[k]>>32) && e<(a[k+1]>>32)) break;
		if(e<=(a[k]>>32)) break;///if may happend when [s, e) does not overlap with the first region
	}
    
    
    return tcc + (e>=s?e-s:0);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq 0x18(%rdi), %r11
movq (%r11), %rax
movl (%rax,%rsi,4), %ebx
movl 0x4(%rax,%rsi,4), %eax
testq %r9, %r9
je 0x3654f
movq (%r9), %r10
jmp 0x36552
xorl %r10d, %r10d
cmpl %ebx, %eax
je 0x36694
movq 0x18(%r11), %r14
subl %ebx, %eax
leaq (%r14,%rbx,8), %r11
leaq 0x1(%r10), %r15
xorl %r12d, %r12d
testq %r10, %r10
cmovleq %r12, %r10
cmpq %rax, %r15
cmovgeq %r12, %r10
testb %dl, %dl
je 0x365a0
movq (%rdi), %rdx
movq 0x10(%rdx), %rdx
leaq (%rsi,%rsi,4), %rsi
movl (%rdx,%rsi,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rdx, %rsi
subq %r8, %rsi
subq %rcx, %rdx
movq %rdx, %r8
movq %rsi, %rcx
movl 0x4(%r11,%r10,8), %edx
cmpq %rdx, %rcx
jae 0x365db
leaq (%r14,%rbx,8), %rdx
addq $0x8, %rdx
movl -0x4(%rdx,%r10,8), %esi
cmpq %rsi, %rcx
jb 0x365c6
movl 0x4(%rdx,%r10,8), %esi
cmpq %rsi, %rcx
jb 0x36619
leaq -0x1(%r10), %rsi
testq %r10, %r10
movq %rsi, %r10
jg 0x365b2
movq $-0x1, %r10
jmp 0x36619
movl 0xc(%r11,%r10,8), %edx
cmpq %rdx, %rcx
jb 0x36619
leaq 0x1(%r10), %rdx
cmpq %rax, %rdx
jae 0x36619
leaq -0x1(%rax), %rdx
leaq (%r14,%rbx,8), %rsi
addq $0x8, %rsi
movl -0x4(%rsi,%r10,8), %edi
cmpq %rdi, %rcx
jb 0x3660e
movl 0x4(%rsi,%r10,8), %edi
cmpq %rdi, %rcx
jb 0x36619
incq %r10
cmpq %r10, %rdx
jne 0x365fa
movq %rdx, %r10
testq %r9, %r9
je 0x36621
movq %r10, (%r9)
leaq 0x1(%r10), %rdx
cmpq %rax, %rdx
jge 0x36694
xorl %edx, %edx
testq %r10, %r10
cmovleq %rdx, %r10
incq %r10
cmpq %rax, %r10
jae 0x36686
movq -0x8(%r11,%r10,8), %rdi
movq (%r11,%r10,8), %rsi
movq %rdi, %r9
shrq $0x20, %r9
shrq $0x20, %rsi
cmpq %rsi, %r8
movq %rsi, %rbx
cmovbq %r8, %rbx
cmpq %r9, %rcx
movq %r9, %r14
cmovaq %rcx, %r14
subq %r14, %rbx
movl $0x0, %r14d
cmovaeq %rbx, %r14
movl %edi, %edi
imulq %rdi, %r14
addq %r14, %rdx
cmpq %r9, %r8
jbe 0x36686
incq %r10
cmpq %rsi, %r8
jae 0x36636
xorl %eax, %eax
subq %rcx, %r8
cmovaeq %r8, %rax
addq %rdx, %rax
jmp 0x3669d
xorl %eax, %eax
subq %rcx, %r8
cmovaeq %r8, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
retq