// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %r11
movq %rsi, %r14
movq %rdi, %r12
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rdx
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rcx
movq $0x0, (%rax)
movq $0x7fffffff, (%rcx)     # imm = 0x7FFFFFFF
testq %rdi, %rdi
je 0x8b7bc
movq $-0x1, (%rdi)
movq 0x90(%rsp), %rbx
movq 0x88(%rsp), %r10
testq %rdx, %rdx
je 0x8b7d8
movq $-0x1, (%rdx)
movq 0xa8(%rsp), %rax
movq 0xa0(%rsp), %rcx
subq %r10, %rbx
incq %rbx
testq %r11, %r11
je 0x8b850
cmpq %rcx, %rbx
jge 0x8b827
cvtsi2sd %rbx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
testq %rax, %rax
movl $0x1, %eax
cmovneq %rcx, %rax
cmpq $0x4, %rbx
cmovlq %rcx, %rax
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x2, %eax
cmovnel %ecx, %eax
cmpl $0x4, %ebx
cmovll %ecx, %eax
cmpl $0x20, %eax
movl $0x1f, %edx
cmovgel %eax, %edx
cmpl $0x12c, %ebx            # imm = 0x12C
cmovll %eax, %edx
jmp 0x8b8ac
cmpq %rcx, %rbx
jge 0x8b884
cvtsi2sd %rbx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
testq %rax, %rax
movl $0x1, %eax
cmovneq %rcx, %rax
cmpq $0x4, %rbx
cmovlq %rcx, %rax
testl %eax, %eax
movl $0x1, %edx
cmovnel %eax, %edx
cmpl $0x4, %ebx
cmovll %eax, %edx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0x1a5cdd(%rip), %xmm0   # 0x231580
cvttsd2si %xmm0, %eax
cmpl %eax, %edx
cmovlel %eax, %edx
movq 0x98(%rsp), %r13
movl 0x80(%rsp), %ecx
cmpl $0x1f, %edx
movl $0x1f, %edi
cmovll %edx, %edi
testq %r14, %r14
je 0x8b8e5
movq (%r14), %rax
movq 0x10(%rax), %rax
movl %ecx, %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rax,%rdx,8), %edx
shrl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x8b90d
testq %r12, %r12
je 0x8b903
movq (%r12), %rax
movq 0x10(%rax), %rax
movl %ecx, %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rax,%rdx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0x8b90d
movq 0x18(%r11), %rax
movl %ecx, %edx
movq (%rax,%rdx,8), %rdx
xorl %eax, %eax
testq %r13, %r13
js 0x8bb7c
cmpq %r13, %rdx
jle 0x8bb7c
movslq %edi, %r15
leaq (%r15,%r15), %rdi
leaq (%rbx,%r15,2), %rbp
subq %r13, %rdi
addq %rdx, %rdi
addq $0x1f, %rdi
cmpq %rbp, %rdi
jl 0x8bb7c
subq %r15, %r13
subq %r13, %rdx
cmpq %rbp, %rdx
cmovgeq %rbp, %rdx
movq %rbp, 0x40(%rsp)
movq %rbp, %rdi
subq %rdx, %rdi
movq %r13, %rax
negq %rax
movq %r13, %rbp
sarq $0x3f, %rbp
andq %rax, %rbp
xorl %eax, %eax
testq %r13, %r13
movl $0x0, %esi
cmovgq %r13, %rsi
cmovnsq %rax, %r13
addq %rdx, %r13
movl %ecx, %r9d
testq %r11, %r11
movq %r8, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
je 0x8b9cf
movl %ebp, %ebp
movq %r8, %r14
leaq (%r8,%rbp), %r12
movzbl 0x8(%rsp), %ecx
movq %r12, %rdi
movq %r13, %rdx
movq %r11, %r8
callq 0x32ef8
movq %r14, %rdi
movl $0x4e, %esi
movq %rbp, %rdx
callq 0x20e0
addq %r13, %r12
movslq 0x20(%rsp), %rdx
movq %r12, %rdi
movl $0x4e, %esi
jmp 0x8baa1
movq %r9, 0x38(%rsp)
movq %rsi, 0x18(%rsp)
testq %r14, %r14
je 0x8ba0c
movq (%r14), %rax
movq 0x10(%rax), %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rax,%rcx,8), %r12
cmpb $0x0, 0x8(%rsp)
movq %r8, %r14
je 0x8ba5a
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8ba5c
movq %r8, %rdi
movl $0x4e, %esi
movq %rbp, %rdx
movq %r8, %r14
callq 0x20e0
addq %r14, %rbp
movq (%r12), %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl 0x10(%rsp), %ecx
xorl %edi, %edi
movq %rbp, %rsi
movq 0x20(%rsp), %r8
movq %r13, %r9
pushq $0x0
callq 0x36049
addq $0x10, %rsp
jmp 0x8ba91
xorl %eax, %eax
addq %rax, %r12
movq %rdi, %rax
orq %rbp, %rax
je 0x8bb8b
movq %r14, %rdi
movl $0x4e, %esi
movq %rbp, %rdx
callq 0x20e0
addq %r14, %rbp
addq 0x18(%rsp), %r12
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2160
addq %r13, %rbp
movq %rbp, %rdi
movl $0x4e, %esi
movq 0x20(%rsp), %rdx
callq 0x20e0
movq 0x88(%rsp), %r10
addq %r10, 0x30(%rsp)
movl 0xb0(%rsp), %ebp
leaq 0xc(%rsp), %rax
leaq 0x10(%rsp), %r10
movzwl %r15w, %r8d
leaq 0x14(%rsp), %r9
testl %ebp, %ebp
je 0x8baec
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl %ebx, %ecx
pushq %rax
pushq %r10
callq 0x9c751
jmp 0x8bb05
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl %ebx, %ecx
pushq %rax
pushq %r10
callq 0x9b415
addq $0x10, %rsp
movslq 0x10(%rsp), %rax
cmpq $-0x1, %rax
setne %dl
movl 0xc(%rsp), %ecx
movslq %ecx, %rcx
cmpl $-0x1, %ecx
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
movq 0xd0(%rsp), %rsi
movq 0xb8(%rsp), %rdi
movq 0xc8(%rsp), %r8
movq 0xc0(%rsp), %r9
jne 0x8bb5c
subq %rcx, %rbx
leaq 0x1(%rcx), %rdx
testl %ebp, %ebp
cmovneq %rbx, %rdx
movq %rdx, (%rsi)
testq %r9, %r9
je 0x8bb64
movq %rcx, (%r9)
testq %r8, %r8
je 0x8bb6c
movq %rax, (%r8)
movl 0x14(%rsp), %eax
movq %rax, (%rdi)
xorl %eax, %eax
cmpq $0x0, (%rsi)
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq 0x18(%rsp), %r12
movq %r12, 0x28(%rsp)
jmp 0x8baae