// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Process_Read.cpp
// Function: retrieve_u_cov_region(ul_idx_t const*, unsigned long, unsigned char, unsigned long, unsigned long, long*)
uint64_t retrieve_u_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->cc->interval.a + ul->cc->idx[id], cc = 0, o = 0, tk, ts, te, tcc = 0;
    int64_t a_n = ul->cc->idx[id+1]-ul->cc->idx[id], k = 0, cc_i = pi? *pi:0;
    if(a_n == 0) return 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;

	

	for (; k + 1 < a_n; k++) {
		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(s == 54201 && e == 58376 && id == 492) fprintf(stderr, "tcc:%lu\n", tcc);
    return tcc;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq 0x10(%rdi), %rbx
movq (%rbx), %r10
movl (%r10,%rsi,4), %eax
movl 0x4(%r10,%rsi,4), %r10d
testq %r9, %r9
je 0x36eea
movq (%r9), %r11
jmp 0x36eed
xorl %r11d, %r11d
cmpl %eax, %r10d
jne 0x36ef9
xorl %eax, %eax
jmp 0x37020
movq 0x18(%rbx), %r14
subl %eax, %r10d
leaq (%r14,%rax,8), %rbx
leaq 0x1(%r11), %r15
xorl %r12d, %r12d
testq %r11, %r11
cmovleq %r12, %r11
cmpq %r10, %r15
cmovgeq %r12, %r11
testb %dl, %dl
je 0x36f40
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(%rbx,%r11,8), %edx
cmpq %rdx, %rcx
jae 0x36f7b
leaq (%r14,%rax,8), %rax
addq $0x8, %rax
movl -0x4(%rax,%r11,8), %edx
cmpq %rdx, %rcx
jb 0x36f66
movl 0x4(%rax,%r11,8), %edx
cmpq %rdx, %rcx
jb 0x36fb9
leaq -0x1(%r11), %rdx
testq %r11, %r11
movq %rdx, %r11
jg 0x36f52
movq $-0x1, %r11
jmp 0x36fb9
movl 0xc(%rbx,%r11,8), %edx
cmpq %rdx, %rcx
jb 0x36fb9
leaq 0x1(%r11), %rdx
cmpq %r10, %rdx
jae 0x36fb9
leaq -0x1(%r10), %rdx
leaq (%r14,%rax,8), %rax
addq $0x8, %rax
movl -0x4(%rax,%r11,8), %esi
cmpq %rsi, %rcx
jb 0x36fae
movl 0x4(%rax,%r11,8), %esi
cmpq %rsi, %rcx
jb 0x36fb9
incq %r11
cmpq %r11, %rdx
jne 0x36f9a
movq %rdx, %r11
testq %r9, %r9
je 0x36fc1
movq %r11, (%r9)
incq %r11
xorl %edx, %edx
cmpq %r10, %r11
jge 0x3701d
movq -0x8(%rbx,%r11,8), %rax
movq (%rbx,%r11,8), %rsi
movl %eax, %edi
movq %rax, %r9
shrq $0x20, %r9
shrq $0x20, %rsi
cmpq %rsi, %r8
movq %rsi, %r14
cmovbq %r8, %r14
cmpq %r9, %rcx
movq %r9, %rax
cmovaq %rcx, %rax
subq %rax, %r14
movl $0x0, %eax
cmovaeq %r14, %rax
imulq %rdi, %rax
addq %rdx, %rax
incq %r11
movq %rax, %rdx
cmpq %r9, %r8
jb 0x36fc6
movq %rax, %rdx
cmpq %rsi, %r8
jae 0x36fc6
jmp 0x37020
movq %rdx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
retq