// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movl 0x40(%rbp), %eax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movsd %xmm0, -0x40(%rbp)
movq 0x60(%rbp), %rax
movq $0x0, (%rax)
movq 0x48(%rbp), %rax
movq $0x7fffffff, (%rax)     # imm = 0x7FFFFFFF
cmpq $0x0, 0x50(%rbp)
je 0xa907a
movq 0x50(%rbp), %rax
movq $-0x1, (%rax)
cmpq $0x0, 0x58(%rbp)
je 0xa908c
movq 0x58(%rbp), %rax
movq $-0x1, (%rax)
movq 0x20(%rbp), %rax
addq $0x1, %rax
subq 0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xa90ce
movq -0x48(%rbp), %rdi
movsd -0x40(%rbp), %xmm0
movq 0x30(%rbp), %rsi
movq 0x38(%rbp), %rdx
callq 0x9e1c0
movl %eax, %edi
movq -0x48(%rbp), %rax
movl %eax, %esi
callq 0x9e370
cltq
movq %rax, -0x78(%rbp)
jmp 0xa90f7
movq -0x48(%rbp), %rdi
movsd -0x40(%rbp), %xmm0
movq 0x30(%rbp), %rsi
movq 0x38(%rbp), %rdx
callq 0x9e1c0
movl %eax, %edi
movq -0x48(%rbp), %rax
movl %eax, %esi
callq 0x9f400
cltq
movq %rax, -0x78(%rbp)
movq -0x48(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xa9136
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
sarl %eax
cltq
movq %rax, -0x58(%rbp)
jmp 0xa9176
cmpq $0x0, -0x10(%rbp)
je 0xa9161
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0xa9174
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x10(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
jmp 0xa9176
movq -0x78(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
leaq -0x60(%rbp), %r8
leaq -0x68(%rbp), %r9
leaq 0x28(%rbp), %r10
leaq -0x70(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x33f90
cmpl $0x0, %eax
jne 0xa91b5
movl $0x0, -0x4(%rbp)
jmp 0xa9385
movq -0x28(%rbp), %rax
addq 0x18(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xa920b
movq -0x30(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0x70(%rbp), %rdx
movl -0x34(%rbp), %eax
movb %al, %cl
movq -0x20(%rbp), %r8
movl 0x10(%rbp), %eax
movl %eax, %r9d
movq -0x60(%rbp), %rax
movl %eax, %r10d
movq -0x68(%rbp), %rax
movzbl %cl, %ecx
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x32160
movq -0x30(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0xa924f
movq -0x30(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0x70(%rbp), %rdx
movl -0x34(%rbp), %eax
movb %al, %cl
movq -0x18(%rbp), %r8
movq -0x10(%rbp), %r9
movl 0x10(%rbp), %eax
movl %eax, %r11d
movq -0x60(%rbp), %r10
movq -0x68(%rbp), %rax
movzbl %cl, %ecx
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xa71f0
movq %rax, -0x88(%rbp)
cmpl $0x0, 0x40(%rbp)
jne 0xa929d
movq -0x88(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x80(%rbp), %rdx
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x78(%rbp), %rax
movw %ax, %r8w
leaq -0x8c(%rbp), %r9
leaq -0x90(%rbp), %r10
leaq -0x94(%rbp), %rax
movzwl %r8w, %r8d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xc54c0
jmp 0xa92e3
movq -0x88(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x80(%rbp), %rdx
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x78(%rbp), %rax
movw %ax, %r8w
leaq -0x8c(%rbp), %r9
leaq -0x90(%rbp), %r10
leaq -0x94(%rbp), %rax
movzwl %r8w, %r8d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xc72b0
cmpl $-0x1, -0x90(%rbp)
je 0xa9332
cmpl $-0x1, -0x94(%rbp)
je 0xa9332
cmpl $0x0, 0x40(%rbp)
je 0xa9312
movq -0x48(%rbp), %rax
movslq -0x94(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xa0(%rbp)
jmp 0xa9324
movl -0x94(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rcx
movq 0x60(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x50(%rbp)
je 0xa9347
movslq -0x94(%rbp), %rcx
movq 0x50(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x58(%rbp)
je 0xa935c
movslq -0x90(%rbp), %rcx
movq 0x58(%rbp), %rax
movq %rcx, (%rax)
movl -0x8c(%rbp), %eax
movl %eax, %ecx
movq 0x48(%rbp), %rax
movq %rcx, (%rax)
movq 0x60(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xa937e
movl $0x0, -0x4(%rbp)
jmp 0xa9385
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)