// File: /workspace/github_cpp_makefile_star2_O1/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, %rbp
movq %rdi, 0x10(%rsp)
testq %r8, %r8
je 0x1f53e0
movq %r9, %r13
movq %rdx, %r14
movq %rsi, %r15
movq 0x30(%rsi), %rax
movq 0x10(%rdx), %rcx
movl 0x4(%rcx,%rbp,8), %ecx
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rax
movl 0x4(%rax), %ebx
js 0x1f5322
movq %r13, 0x20(%rsp)
movl 0x18(%r14), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl 0x8(%rax), %r12d
addq %r8, %r12
movl 0x18(%r15), %eax
cmpq %rax, %r12
cmovgeq %rax, %r12
leaq 0x1(%rbp), %r13
movq %rcx, 0x8(%rsp)
cmpq %rcx, %r13
jge 0x1f53d0
movq 0x30(%r15), %rax
movq 0x10(%r14), %rcx
movl 0xc(%rcx,%rbp,8), %ecx
leaq (%rcx,%rcx,8), %rcx
movl 0x4(%rax,%rcx,4), %esi
movl 0x8(%rax,%rcx,4), %edx
cmpl %esi, %ebx
cmoval %ebx, %esi
cmpq %rdx, %r12
cmovlq %r12, %rdx
cmpq %rsi, %rdx
jle 0x1f53b5
leaq (%rax,%rcx,4), %rcx
movq 0x8(%rsp), %rax
addq $-0x2, %rax
movq %rax, 0x18(%rsp)
movq %rbp, %r13
movq 0x10(%rsp), %rdi
callq 0x1f50fd
testq %rax, %rax
je 0x1f53d0
movq 0x20(%rsp), %rcx
movq %rax, 0x8(%rcx,%r13,8)
cmpq %r13, 0x18(%rsp)
je 0x1f53cc
movq 0x30(%r15), %rax
movq 0x10(%r14), %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, %ebx
cmoval %ebx, %esi
cmpq %rdx, %r12
cmovlq %r12, %rdx
incq %r13
cmpq %rsi, %rdx
jg 0x1f52c6
jmp 0x1f53d0
addq %r8, %rbx
xorl %r12d, %r12d
testq %rbx, %rbx
cmovgq %rbx, %r12
testq %rbp, %rbp
jle 0x1f53ba
movl 0x8(%rax), %ebx
movq 0x30(%r15), %rax
movq 0x10(%r14), %rcx
movl -0x4(%rcx,%rbp,8), %ecx
leaq (%rcx,%rcx,8), %rcx
movl 0x4(%rax,%rcx,4), %esi
movl 0x8(%rax,%rcx,4), %edx
cmpq %rsi, %r12
cmovaq %r12, %rsi
cmpl %edx, %ebx
cmovbl %ebx, %edx
cmpq %rdx, %rsi
jae 0x1f53bd
leaq (%rax,%rcx,4), %rcx
movq 0x10(%rsp), %rdi
callq 0x1f50fd
testq %rax, %rax
je 0x1f53bd
leaq -0x1(%rbp), %rdi
movq %rax, -0x8(%r13,%rbp,8)
testq %rdi, %rdi
jle 0x1f53f2
movq 0x30(%r15), %rax
movq 0x10(%r14), %rcx
movl -0xc(%rcx,%rbp,8), %ecx
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,4), %rcx
movl 0x4(%rcx), %esi
movl 0x8(%rcx), %edx
cmpq %rsi, %r12
cmovaq %r12, %rsi
cmpl %edx, %ebx
cmovbl %ebx, %edx
movq %rdi, %rbp
cmpq %rdx, %rsi
jb 0x1f5368
jmp 0x1f53c0
movq %rbp, %r13
jmp 0x1f53d0
decq %rbp
movq %rbp, %rdi
movq %rdi, %rbp
shrq $0x3f, %rbp
addq %rdi, %rbp
jmp 0x1f53e0
addq $0x2, %r13
xorl %eax, %eax
cmpq 0x8(%rsp), %r13
setge %al
subq %rax, %r13
movq %r13, %rbp
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $-0x1, %rdi
jmp 0x1f53c0