// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x32b517
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x32b8e7
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x30(%rbp)
jge 0x32b706
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x28(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jge 0x32b579
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x28(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jl 0x32b6ee
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x58(%rbp), %rax
jl 0x32b625
movq -0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x32b630
movq -0x58(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x60(%rbp), %rax
jg 0x32b652
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x32b65d
movq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jg 0x32b680
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b6ee
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %r8
movq (%rax,%r8,8), %rax
shrq $0x20, %rax
imulq $0x24, %rax, %rax
addq %rax, %rcx
callq 0x32b320
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x32b6cd
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b6ee
movq -0x78(%rbp), %rdx
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b5af
cmpq $0x0, -0x50(%rbp)
jge 0x32b701
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b8df
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x28(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x28(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x18(%rcx), %ecx
cmpq %rcx, %rax
jle 0x32b779
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x80(%rbp), %rax
jge 0x32b8c7
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x58(%rbp), %rax
jl 0x32b7fe
movq -0x40(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x32b809
movq -0x58(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x60(%rbp), %rax
jg 0x32b82b
movq -0x48(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x32b836
movq -0x60(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jg 0x32b859
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b8c7
movq -0x10(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %r8
movq (%rax,%r8,8), %rax
shrq $0x20, %rax
imulq $0x24, %rax, %rax
addq %rax, %rcx
callq 0x32b320
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x32b8a6
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b8c7
movq -0x78(%rbp), %rdx
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b785
movq -0x50(%rbp), %rax
cmpq -0x80(%rbp), %rax
jl 0x32b8dd
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x32b8df
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)