// File: 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 %rbp
movq %rsp, %rbp
subq $0x48, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx), %rcx
movq -0x18(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx), %rcx
movq -0x18(%rbp), %rdx
subl (%rcx,%rdx,4), %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1116c
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
jmp 0x11177
xorl %eax, %eax
movq %rax, -0x90(%rbp)
jmp 0x11177
movq -0x90(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x11199
movq $0x0, -0x8(%rbp)
jmp 0x114ad
movq -0x88(%rbp), %rax
addq $0x1, %rax
cmpq -0x78(%rbp), %rax
jge 0x111b4
cmpq $0x0, -0x88(%rbp)
jge 0x111bf
movq $0x0, -0x88(%rbp)
cmpb $0x0, -0x19(%rbp)
je 0x11215
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x18(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x18(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x58(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jae 0x11291
jmp 0x11243
cmpq $0x0, -0x80(%rbp)
jl 0x1128f
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jb 0x1127f
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jae 0x1127f
jmp 0x1128f
jmp 0x11281
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x11243
jmp 0x11304
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jb 0x11302
jmp 0x112ad
movq -0x80(%rbp), %rax
addq $0x1, %rax
cmpq -0x78(%rbp), %rax
jge 0x11300
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jb 0x112f0
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jae 0x112f0
jmp 0x11300
jmp 0x112f2
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x112ad
jmp 0x11302
jmp 0x11304
cmpq $0x0, -0x38(%rbp)
je 0x11316
movq -0x80(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
jmp 0x11318
movq -0x80(%rbp), %rax
addq $0x1, %rax
cmpq -0x78(%rbp), %rax
jge 0x114a5
movq -0x40(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq -0x80(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
shrq $0x20, %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x68(%rbp), %rax
ja 0x1137c
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x11387
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x60(%rbp), %rax
jb 0x113ac
movq -0x28(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x113b7
movq -0x60(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x11436
movq -0x30(%rbp), %rax
cmpq -0x68(%rbp), %rax
ja 0x113e1
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x113ec
movq -0x68(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x60(%rbp), %rax
jb 0x11411
movq -0x28(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x1141c
movq -0x60(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xc8(%rbp)
jmp 0x11441
xorl %eax, %eax
movq %rax, -0xc8(%rbp)
jmp 0x11441
movq -0xc8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
imulq -0x48(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jb 0x11492
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0x80(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
cmpq %rcx, %rax
jae 0x11492
jmp 0x114a5
jmp 0x11494
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x11318
movq -0x70(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x48, %rsp
popq %rbp
retq
nopw (%rax,%rax)