// File: /workspace/github_cpp_makefile_star2_O2/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 $0x18, %rsp
movq %r9, (%rsp)
movq %rcx, %r13
movq %rdi, 0x8(%rsp)
testq %r8, %r8
je 0x1b4b1a
movq %rdx, %r15
movq %rsi, %r12
movq 0x30(%rsi), %rcx
movq 0x10(%rdx), %rax
movl 0x4(%rax,%r13,8), %eax
imulq $0x24, %rax, %rdx
leaq (%rcx,%rdx), %rax
movl 0x4(%rcx,%rdx), %r14d
testq %r8, %r8
js 0x1b4a87
movq %r15, 0x10(%rsp)
movl 0x18(%r15), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x8(%rax), %r15d
addq %r8, %r15
movl 0x18(%r12), %eax
cmpq %rax, %r15
cmovaeq %rax, %r15
leaq 0x1(%r13), %rbx
cmpq %rbp, %rbx
jge 0x1b4af8
movq 0x30(%r12), %rcx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x4(%rax,%rbx,8), %eax
imulq $0x24, %rax, %rax
movl 0x4(%rcx,%rax), %esi
movl 0x8(%rcx,%rax), %edx
cmpl %esi, %r14d
cmoval %r14d, %esi
cmpq %rdx, %r15
cmovlq %r15, %rdx
cmpq %rsi, %rdx
jbe 0x1b4af0
addq %rax, %rcx
movq 0x8(%rsp), %rdi
callq 0x1b48ad
testq %rax, %rax
je 0x1b4af5
movq (%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
incq %r13
jmp 0x1b4a27
addq %r8, %r14
xorl %ebx, %ebx
testq %r14, %r14
cmovgq %r14, %rbx
movl 0x8(%rax), %ebp
leaq -0x1(%r13), %r14
leaq 0x1(%r14), %rax
testq %rax, %rax
jle 0x1b4b08
movq 0x30(%r12), %rcx
movq 0x10(%r15), %rdx
movl 0x4(%rdx,%r14,8), %edx
imulq $0x24, %rdx, %rdi
movl 0x4(%rcx,%rdi), %esi
movl 0x8(%rcx,%rdi), %edx
cmpq %rsi, %rbx
cmovaq %rbx, %rsi
cmpl %edx, %ebp
cmovbl %ebp, %edx
cmpq %rdx, %rsi
jae 0x1b4b10
addq %rdi, %rcx
movq 0x8(%rsp), %rdi
callq 0x1b48ad
testq %rax, %rax
je 0x1b4b0d
movq (%rsp), %rcx
movq %rax, (%rcx,%r14,8)
decq %r14
decq %r13
jmp 0x1b4a9a
decq %rbx
jmp 0x1b4af8
movq %r13, %rbx
xorl %eax, %eax
cmpq %rbp, %rbx
setge %al
subq %rax, %rbx
movq %rbx, %r13
jmp 0x1b4b1a
movq %r14, %rax
jmp 0x1b4b10
movq %r13, %rax
movq %rax, %r13
shrq $0x3f, %r13
addq %rax, %r13
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq