// File: /workspace/github_cpp_makefile_star2_O2/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 0xa261
movq (%r9), %r11
jmp 0xa264
xorl %r11d, %r11d
cmpl %eax, %r10d
jne 0xa270
xorl %eax, %eax
jmp 0xa38f
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 0xa2b7
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(%rbx,%r11,8), %edx
cmpq %rdx, %rcx
jae 0xa2f1
leaq (%r14,%rax,8), %rdx
addq $0x8, %rdx
movabsq $-0x1, %rax
testq %r11, %r11
js 0xa325
movl -0x4(%rdx,%r11,8), %esi
cmpq %rsi, %rcx
jb 0xa2ec
movl 0x4(%rdx,%r11,8), %esi
cmpq %rsi, %rcx
jb 0xa328
decq %r11
jmp 0xa2d3
movl 0xc(%rbx,%r11,8), %edx
cmpq %rdx, %rcx
jb 0xa328
leaq (%r14,%rax,8), %rax
addq $0x8, %rax
leaq 0x1(%r11), %rdx
cmpq %r10, %rdx
jae 0xa328
movl -0x4(%rax,%r11,8), %esi
cmpq %rsi, %rcx
jb 0xa320
movl 0x4(%rax,%r11,8), %esi
cmpq %rsi, %rcx
jb 0xa328
movq %rdx, %r11
jmp 0xa303
movq %rax, %r11
testq %r9, %r9
je 0xa330
movq %r11, (%r9)
incq %r11
xorl %edx, %edx
cmpq %r10, %r11
jge 0xa38c
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 0xa335
movq %rax, %rdx
cmpq %rsi, %r8
jae 0xa335
jmp 0xa38f
movq %rdx, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
retq