// File: /workspace/github_cpp_makefile_star2_O2/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 0xa3b6
movq (%r9), %r10
jmp 0xa3b9
xorl %r10d, %r10d
cmpl %ebx, %eax
je 0xa4f3
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 0xa407
movq (%rdi), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rsi, %rsi
movl (%rdx,%rsi), %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 0xa441
leaq (%r14,%rbx,8), %rsi
addq $0x8, %rsi
movabsq $-0x1, %rdx
testq %r10, %r10
js 0xa475
movl -0x4(%rsi,%r10,8), %edi
cmpq %rdi, %rcx
jb 0xa43c
movl 0x4(%rsi,%r10,8), %edi
cmpq %rdi, %rcx
jb 0xa478
decq %r10
jmp 0xa423
movl 0xc(%r11,%r10,8), %edx
cmpq %rdx, %rcx
jb 0xa478
leaq (%r14,%rbx,8), %rdx
addq $0x8, %rdx
leaq 0x1(%r10), %rsi
cmpq %rax, %rsi
jae 0xa478
movl -0x4(%rdx,%r10,8), %edi
cmpq %rdi, %rcx
jb 0xa470
movl 0x4(%rdx,%r10,8), %edi
cmpq %rdi, %rcx
jb 0xa478
movq %rsi, %r10
jmp 0xa453
movq %rdx, %r10
testq %r9, %r9
je 0xa480
movq %r10, (%r9)
leaq 0x1(%r10), %rdx
cmpq %rax, %rdx
jge 0xa4f3
xorl %edx, %edx
testq %r10, %r10
cmovleq %rdx, %r10
incq %r10
cmpq %rax, %r10
jae 0xa4e5
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 0xa4e5
incq %r10
cmpq %rsi, %r8
jae 0xa495
xorl %eax, %eax
subq %rcx, %r8
cmovaeq %r8, %rax
addq %rdx, %rax
jmp 0xa4fc
xorl %eax, %eax
subq %rcx, %r8
cmovaeq %r8, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
retq