// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: ed_cut(ul_idx_t const*, hpc_t*, All_reads*, char*, char*, unsigned int, unsigned int, long, long, long, long, double, long, unsigned int, long*, long*, long*, long*)
inline uint32_t ed_cut(const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, 
char *qstr, char *tstr, uint32_t rev, uint32_t id, 
int64_t qs, int64_t qe, int64_t t_s, int64_t block_s, double e_rate, int64_t max_err,
uint32_t aln_dir, int64_t* r_err, int64_t* qoff, int64_t* toff, int64_t* aln_qlen)
{
    (*aln_qlen) = 0; (*r_err) = INT32_MAX;
    if(qoff) (*qoff) = -1; if(toff) (*toff) = -1; 
    int64_t ql, aln_l, t_tot_l, aux_beg, aux_end, t_pri_l, thres;
    char *q_string, *t_string; unsigned int error; int t_end, q_end;

    ql = qe + 1 - qs;
    ///there are two potiential reasons for unmatched window:
    ///1. this window has a large number of differences
    ///2. DP does not start from the right offset
    if(rref) {
        thres = double_error_threshold(get_init_err_thres(ql, e_rate, block_s, max_err), ql);
    } else {
        thres = double_ul_error_threshold(get_init_err_thres(ql, e_rate, block_s, max_err), ql);
    }
    
    aln_l = ql + (thres << 1);
    if(hpc_g) t_tot_l = hpc_len(*hpc_g, id);
    else if(uref) t_tot_l = uref->ug->u.a[id].len;
    else t_tot_l = Get_READ_LENGTH((*rref), id);

    if(!init_waln(thres, t_s, t_tot_l, aln_l, &aux_beg, &aux_end, &t_s, &t_pri_l)) return 0;
    // if(t_pri_l + thres < ql) return 0;

    q_string = qstr + qs; 
    if(rref) {
        fill_subregion(tstr, t_s, t_pri_l, rev, rref, id, aux_beg, aux_end); t_string = tstr;
    } else {
        t_string = return_str_seq(tstr, t_s, t_pri_l, rev, hpc_g, uref, id, aux_beg, aux_end);
    }

    // if(id == 6) {
    //     fprintf(stderr, "-[M::%s::aln_dir->%u] qs->%ld, ts->%ld, thres->%ld, aux_beg->%ld, aux_end->%ld, t_pri_l->%ld\n", 
    //     __func__, aln_dir, qs, t_s, thres, aux_beg, aux_end, t_pri_l);
    // }
    if(aln_dir == 0) {
        Reserve_Banded_BPM_Extension(t_string, aln_l, q_string, ql, thres, &error, &t_end, &q_end);
    } else {
        Reserve_Banded_BPM_Extension_REV(t_string, aln_l, q_string, ql, thres, &error, &t_end, &q_end);
    }

    if(t_end != -1 && q_end != -1) (*aln_qlen) = (aln_dir?(ql-q_end):(q_end+1));
    if(qoff) (*qoff) = q_end; if(toff) (*toff) = t_end; (*r_err) = error;

    if((*aln_qlen) == 0) return 0;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, (%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, 0x10(%rsp)
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq 0x98(%rsp), %rdx
movq %rdx, 0x30(%rsp)
andq $0x0, (%rax)
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rax
movq $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
testq %rdx, %rdx
je 0x57c96
orq $-0x1, (%rdx)
movq 0x90(%rsp), %rbx
testq %rcx, %rcx
je 0x57ca7
orq $-0x1, (%rcx)
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rax
movl 0x80(%rsp), %r13d
subq 0x88(%rsp), %rbx
incq %rbx
testq %r12, %r12
je 0x57d08
cmpq %rax, %rbx
jge 0x57cff
cvtsi2sd %rbx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
pushq $0x1f
popq %rcx
cmovlq %rax, %rcx
testq %rax, %rax
pushq $0x1
popq %rdi
cmovneq %rcx, %rdi
cmpq $0x4, %rbx
cmovlq %rcx, %rdi
movl %ebx, %esi
callq 0x51fcd
jmp 0x57d3f
cmpq %rax, %rbx
jge 0x57d38
cvtsi2sd %rbx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
pushq $0x1f
popq %rcx
cmovlq %rax, %rcx
testq %rax, %rax
pushq $0x1
popq %rdi
cmovneq %rcx, %rdi
cmpq $0x4, %rbx
cmovlq %rcx, %rdi
movl %ebx, %esi
callq 0x526f4
movslq %eax, %r14
leaq (%rbx,%r14,2), %r15
movl %r13d, %r13d
testq %rbp, %rbp
je 0x57d67
movq (%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r13, %rcx
movl (%rax,%rcx), %edx
shrl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x57d90
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x57d87
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %r13, %rcx
movl (%rax,%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0x57d90
movq 0x18(%r12), %rax
movq (%rax,%r13,8), %rdx
leaq 0x38(%rsp), %rax
leaq 0x98(%rsp), %r10
leaq 0x40(%rsp), %r8
leaq 0x20(%rsp), %r9
movq %r14, %rdi
movq 0x30(%rsp), %rsi
movq %r15, %rcx
pushq %rax
pushq %r10
callq 0x1c7e7
popq %rcx
popq %rdx
testl %eax, %eax
je 0x57dfc
movq 0x98(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %rax
testq %r12, %r12
je 0x57e03
movl 0x20(%rsp), %r10d
movzbl (%rsp), %ecx
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %r8
movq %r13, %r9
pushq %r10
pushq %rax
callq 0x1b475
addq $0x10, %rsp
jmp 0x57e2c
xorl %eax, %eax
jmp 0x57ee9
subq $0x8, %rsp
movzbl 0x8(%rsp), %ecx
movq 0x20(%rsp), %rdi
movq %rbp, %r8
movq 0x18(%rsp), %r9
pushq 0x28(%rsp)
pushq %rax
pushq %r13
callq 0x56ba5
addq $0x20, %rsp
movq %rax, %rbp
movq 0x28(%rsp), %rdx
addq 0x88(%rsp), %rdx
movl 0xb0(%rsp), %r12d
leaq 0x4(%rsp), %rax
leaq 0x8(%rsp), %r10
movzwl %r14w, %r8d
leaq 0xc(%rsp), %r9
movq %rbp, %rdi
movl %r15d, %esi
movl %ebx, %ecx
testl %r12d, %r12d
je 0x57e6b
pushq %rax
pushq %r10
callq 0x65c71
jmp 0x57e73
pushq %rax
pushq %r10
callq 0x64b04
popq %rcx
popq %rdx
movslq 0x8(%rsp), %rax
cmpq $-0x1, %rax
setne %dl
movl 0x4(%rsp), %ecx
movslq %ecx, %rcx
cmpl $-0x1, %ecx
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
movq 0xd0(%rsp), %rsi
jne 0x57eb1
subq %rcx, %rbx
leaq 0x1(%rcx), %rdx
testl %r12d, %r12d
cmovneq %rbx, %rdx
movq %rdx, (%rsi)
movq 0xc0(%rsp), %rdx
testq %rdx, %rdx
je 0x57ec1
movq %rcx, (%rdx)
movq 0xc8(%rsp), %rcx
testq %rcx, %rcx
je 0x57ed1
movq %rax, (%rcx)
movl 0xc(%rsp), %eax
movq 0xb8(%rsp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
cmpq $0x0, (%rsi)
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq