// File: /workspace/github_cpp_makefile_star2_O1/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, 0xc(%rsp)
movq %r8, %r9
movq %rcx, %r11
movq %rdx, %r8
movq %rdi, %r12
movq 0xd0(%rsp), %r10
movq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rax
movq $0x0, (%r10)
movq $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
testq %rdx, %rdx
je 0x8c4fd
movq $-0x1, (%rdx)
movq 0x90(%rsp), %rbx
movq 0x88(%rsp), %r10
testq %rcx, %rcx
je 0x8c519
movq $-0x1, (%rcx)
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %rdx
movl 0x80(%rsp), %eax
subq %r10, %rbx
incq %rbx
testq %r8, %r8
je 0x8c598
cmpq %rdx, %rbx
jge 0x8c56f
cvtsi2sd %rbx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
cmpq $0x1f, %rcx
movl $0x1f, %edx
cmovlq %rcx, %rdx
testq %rcx, %rcx
movl $0x1, %ecx
cmovneq %rdx, %rcx
cmpq $0x4, %rbx
cmovlq %rdx, %rcx
leal (%rcx,%rcx), %edx
testl %ecx, %ecx
movl $0x2, %ecx
cmovnel %edx, %ecx
cmpl $0x4, %ebx
cmovll %edx, %ecx
cmpl $0x20, %ecx
movl $0x1f, %edx
cmovgel %ecx, %edx
cmpl $0x12c, %ebx            # imm = 0x12C
cmovll %ecx, %edx
jmp 0x8c5f4
cmpq %rdx, %rbx
jge 0x8c5cc
cvtsi2sd %rbx, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
cmpq $0x1f, %rcx
movl $0x1f, %edx
cmovlq %rcx, %rdx
testq %rcx, %rcx
movl $0x1, %ecx
cmovneq %rdx, %rcx
cmpq $0x4, %rbx
cmovlq %rdx, %rcx
testl %ecx, %ecx
movl $0x1, %edx
cmovnel %ecx, %edx
cmpl $0x4, %ebx
cmovll %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0x198e95(%rip), %xmm0   # 0x225480
cvttsd2si %xmm0, %ecx
cmpl %ecx, %edx
cmovlel %ecx, %edx
cmpl $0x1f, %edx
movl $0x1f, %ebp
cmovll %edx, %ebp
movq 0x98(%rsp), %rcx
movslq %ebp, %r13
movl %eax, %r14d
testq %rsi, %rsi
je 0x8c629
movq (%rsi), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rdx
movl (%rax,%rdx,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8c64c
testq %r12, %r12
je 0x8c644
movq (%r12), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rdx
movl (%rax,%rdx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x8c64c
movq 0x18(%r8), %rax
movq (%rax,%r14,8), %rax
leaq (%rbx,%r13,2), %rdx
movq %rdx, 0x28(%rsp)
movb $0x1, %dl
movq $-0x1, 0x10(%rsp)
testq %rcx, %rcx
js 0x8c6c3
cmpq %rcx, %rax
jle 0x8c6c3
leaq (,%r13,2), %rdi
subq %rcx, %rdi
addq %rax, %rdi
addq $0x1f, %rdi
movq $-0x1, %rbp
movq $-0x1, (%rsp)
movq $-0x1, %r15
cmpq 0x28(%rsp), %rdi
jl 0x8c6ef
subq %r13, %rcx
subq %rcx, %rax
movq 0x28(%rsp), %rdx
cmpq %rdx, %rax
movq %rax, %r15
cmovgeq %rdx, %r15
subq %r15, %rdx
movq %rdx, (%rsp)
testq %rcx, %rcx
js 0x8c6db
xorl %ebp, %ebp
movq %rcx, 0x10(%rsp)
jmp 0x8c6ed
movq $-0x1, %rbp
movq $-0x1, (%rsp)
movq $-0x1, %r15
jmp 0x8c6ef
addq %rcx, %r15
negq %rcx
movq $0x0, 0x10(%rsp)
movq %rcx, %rbp
xorl %edx, %edx
xorl %eax, %eax
testb %dl, %dl
jne 0x8c92e
testq %r8, %r8
movq %r9, 0x40(%rsp)
movq %r13, 0x38(%rsp)
je 0x8c766
movslq %ebp, %rbp
leaq (%r9,%rbp), %r12
movzbl 0xc(%rsp), %ecx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %r9, %r13
movq %r14, %r9
movq %r11, %r14
callq 0x337f7
movq %r13, %rdi
movl $0x4e, %esi
movq %rbp, %rdx
callq 0x20e0
addq %r15, %r12
movslq (%rsp), %rdx
movq %r12, %rdi
movl $0x4e, %esi
callq 0x20e0
movq 0x38(%rsp), %r13
movq 0x88(%rsp), %r10
movq %r14, %r11
jmp 0x8c85e
testq %rsi, %rsi
je 0x8c791
movq (%rsi), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rcx
movq 0x20(%rax,%rcx,8), %r14
cmpb $0x0, 0xc(%rsp)
je 0x8c7ee
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8c7f0
movq %r9, %rdi
movl $0x4e, %esi
movq %rbp, %rdx
movq %r11, 0x30(%rsp)
movq %r9, %r13
callq 0x20e0
addq %r13, %rbp
movq (%r12), %rax
leaq (%r14,%r14,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl 0x14(%rsp), %ecx
xorl %edi, %edi
movq %rbp, %rsi
movq 0x18(%rsp), %r8
movq %r15, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
addq %r15, %rbp
movq %rbp, %rdi
movl $0x4e, %esi
movq (%rsp), %rdx
jmp 0x8c83b
xorl %eax, %eax
movq (%rsp), %r12
addq %rax, %r14
movq %r12, %rax
orq %rbp, %rax
je 0x8c854
movq %r9, %rdi
movl $0x4e, %esi
movq %rbp, %rdx
movq %r11, 0x30(%rsp)
movq %r9, %r13
callq 0x20e0
addq %r13, %rbp
addq 0x10(%rsp), %r14
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2160
addq %r15, %rbp
movq %rbp, %rdi
movl $0x4e, %esi
movq %r12, %rdx
callq 0x20e0
movq 0x38(%rsp), %r13
movq 0x88(%rsp), %r10
movq 0x30(%rsp), %r11
jmp 0x8c85e
addq 0x10(%rsp), %r14
movq %r14, 0x40(%rsp)
addq %r10, %r11
movl 0xb0(%rsp), %ebp
leaq 0x1c(%rsp), %rax
leaq 0x20(%rsp), %r10
movzwl %r13w, %r8d
leaq 0x24(%rsp), %r9
testl %ebp, %ebp
je 0x8c898
movq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r11, %rdx
movl %ebx, %ecx
pushq %rax
pushq %r10
callq 0x9c033
jmp 0x8c8af
movq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r11, %rdx
movl %ebx, %ecx
pushq %rax
pushq %r10
callq 0x9aced
addq $0x10, %rsp
movslq 0x20(%rsp), %rax
cmpq $-0x1, %rax
setne %dl
movl 0x1c(%rsp), %ecx
movslq %ecx, %rcx
cmpl $-0x1, %ecx
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
movq 0xb8(%rsp), %rsi
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %r8
jne 0x8c906
subq %rcx, %rbx
leaq 0x1(%rcx), %rdx
testl %ebp, %ebp
cmovneq %rbx, %rdx
movq 0xd0(%rsp), %r9
movq %rdx, (%r9)
testq %r8, %r8
je 0x8c90e
movq %rcx, (%r8)
testq %rdi, %rdi
je 0x8c916
movq %rax, (%rdi)
movl 0x24(%rsp), %eax
movq %rax, (%rsi)
xorl %eax, %eax
movq 0xd0(%rsp), %rcx
cmpq $0x0, (%rcx)
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq