// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: estimate_ul_len(ma_ug_t*, ul_vec_t*, ul_str_t*, long, long, unsigned long*)
int64_t estimate_ul_len(ma_ug_t *ug, ul_vec_t *raw_ov, ul_str_t *str, int64_t idx, int64_t ext_len, uint64_t *cov_buf)
{
    if(ext_len == 0) return idx;
    int64_t aim_s, aim_e, k = idx, qs, qe, ovlp_s, ovlp_e, cov_occ, str_n = str->cn;
    if(ext_len < 0) {
        aim_s = ((int64_t)(raw_ov->bb.a[str->a[idx]>>32].qs)) + ext_len; 
        if(aim_s < 0) aim_s = 0; 
        aim_e = raw_ov->bb.a[str->a[idx]>>32].qe;
        for (k = idx - 1; k >= 0; k--) {
            qs = raw_ov->bb.a[str->a[k]>>32].qs; qe = raw_ov->bb.a[str->a[k]>>32].qe;
            // if(qe <= aim_s) break;
            ovlp_s = MAX(aim_s, qs); ovlp_e = MIN(aim_e, qe);
            if(ovlp_e <= ovlp_s) {
                k++;
                break;
            }
            cov_occ = utg_cover_read_occ_by_qs(ug, ovlp_s, ovlp_e, &(raw_ov->bb.a[str->a[k]>>32]));
            if(cov_occ == 0) {
                k++;
                break;
            }
            cov_buf[k] = cov_occ;
        }
        if(k < 0) k++;
    } else {
        aim_s = raw_ov->bb.a[str->a[idx]>>32].qs;
        aim_e = ((int64_t)(raw_ov->bb.a[str->a[idx]>>32].qe)) + ext_len; 
        if(aim_e > (int64_t)(raw_ov->rlen)) aim_e = raw_ov->rlen;
        for (k = idx + 1; k < str_n; k++) {
            qs = raw_ov->bb.a[str->a[k]>>32].qs; qe = raw_ov->bb.a[str->a[k]>>32].qe;
            // if(qs >= aim_e)
            ovlp_s = MAX(aim_s, qs); ovlp_e = MIN(aim_e, qe);
            if(ovlp_e <= ovlp_s) {
                k--;
                break;
            }
            cov_occ = utg_cover_read_occ_by_qs(ug, ovlp_s, ovlp_e, &(raw_ov->bb.a[str->a[k]>>32]));
            if(cov_occ == 0) {
                k--;
                break;
            }
            cov_buf[k] = cov_occ;
        }
        if(k >= str_n) k--;
    }
    return k;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r14
movq %rdi, 0x10(%rsp)
testq %r8, %r8
je 0x20098d
movq %r9, %r12
movq %rdx, %r13
movq %rsi, %r15
movq 0x30(%rsi), %rax
movq 0x10(%rdx), %rcx
movl 0x4(%rcx,%r14,8), %ecx
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rax
movl 0x4(%rax), %ebp
js 0x2008d1
movq %r12, 0x20(%rsp)
movq %r13, 0x8(%rsp)
movl 0x18(%r13), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movl 0x8(%rax), %r12d
addq %r8, %r12
movl 0x18(%r15), %eax
cmpq %rax, %r12
cmovaeq %rax, %r12
leaq 0x1(%r14), %r13
cmpq %rbx, %r13
jge 0x20097f
movq 0x30(%r15), %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl 0xc(%rcx,%r14,8), %ecx
leaq (%rcx,%rcx,8), %rcx
movl 0x4(%rax,%rcx,4), %esi
movl 0x8(%rax,%rcx,4), %edx
cmpl %esi, %ebp
cmoval %ebp, %esi
cmpq %rdx, %r12
cmovlq %r12, %rdx
cmpq %rsi, %rdx
jbe 0x200965
leaq (%rax,%rcx,4), %rcx
leaq -0x2(%rbx), %rax
movq %rax, 0x18(%rsp)
movq %r14, %r13
movq 0x10(%rsp), %rdi
callq 0x2006a5
testq %rax, %rax
je 0x20097f
movq 0x20(%rsp), %rcx
movq %rax, 0x8(%rcx,%r13,8)
cmpq %r13, 0x18(%rsp)
je 0x20097c
movq 0x30(%r15), %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl 0x14(%rcx,%r13,8), %ecx
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rcx
movl 0x4(%rcx), %esi
movl 0x8(%rcx), %edx
cmpl %esi, %ebp
cmoval %ebp, %esi
cmpq %rdx, %r12
cmovlq %r12, %rdx
incq %r13
cmpq %rsi, %rdx
ja 0x200870
jmp 0x20097f
addq %r8, %rbp
xorl %ebx, %ebx
testq %rbp, %rbp
cmovgq %rbp, %rbx
testq %r14, %r14
jle 0x20096a
movl 0x8(%rax), %ebp
movq 0x30(%r15), %rax
movq 0x10(%r13), %rcx
movl -0x4(%rcx,%r14,8), %ecx
leaq (%rcx,%rcx,8), %rcx
movl 0x4(%rax,%rcx,4), %esi
movl 0x8(%rax,%rcx,4), %edx
cmpq %rsi, %rbx
cmovaq %rbx, %rsi
cmpl %edx, %ebp
cmovbl %ebp, %edx
cmpq %rdx, %rsi
jae 0x20096d
leaq (%rax,%rcx,4), %rcx
movq 0x10(%rsp), %rdi
callq 0x2006a5
testq %rax, %rax
je 0x20096d
leaq -0x1(%r14), %rdi
movq %rax, -0x8(%r12,%r14,8)
testq %rdi, %rdi
jle 0x20099f
movq 0x30(%r15), %rax
movq 0x10(%r13), %rcx
movl -0xc(%rcx,%r14,8), %ecx
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rcx
movl 0x4(%rcx), %esi
movl 0x8(%rcx), %edx
cmpq %rsi, %rbx
cmovaq %rbx, %rsi
cmpl %edx, %ebp
cmovbl %ebp, %edx
movq %rdi, %r14
cmpq %rdx, %rsi
jb 0x200917
jmp 0x200970
movq %r14, %r13
jmp 0x20097f
decq %r14
movq %r14, %rdi
movq %rdi, %r14
shrq $0x3f, %r14
addq %rdi, %r14
jmp 0x20098d
movq %rbx, %r13
xorl %eax, %eax
cmpq %rbx, %r13
setge %al
subq %rax, %r13
movq %r13, %r14
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $-0x1, %rdi
jmp 0x200970