// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_backtrace_non_retrieve_adv_exz(window_list*, overlap_region*, char*, char*, long, bit_extz_t*, unsigned int, unsigned int)
inline uint32_t gen_backtrace_non_retrieve_adv_exz(window_list *p, overlap_region *z, char *qstr, char *tstr, int64_t t_tot_l, bit_extz_t *exz, uint32_t rev, uint32_t id)
{
    if(p->error < 0 || p->y_end < 0) return 0;
    int64_t qs, qe, ql, tl, aln_l, t_pri_l, thres, ts; 
    int64_t aux_beg, aux_end;
    char *q_string, *t_string; 
    ///there is no problem for x
    qs = p->x_start; qe = p->x_end; ql = qe + 1 - qs;
    thres = p->error_threshold; aln_l = ql + (thres<<1);

    ///y_start is the real y_start
    ///for the window with cigar, y_start has already reduced extra_begin
    ts = p->y_start; aux_beg = p->extra_begin; aux_end = p->extra_end;
    if(aux_end >= 0) {
        t_pri_l = aln_l - aux_beg - aux_end;
    } else {
        t_pri_l = ts + aln_l - aux_beg; if(t_pri_l > t_tot_l) t_pri_l = t_tot_l;
        t_pri_l = t_pri_l - ts;
    }
    
    q_string = qstr + qs; t_string = tstr + ts; tl = t_pri_l;

    exz->ts = 0; exz->te = p->x_end-p->x_start; exz->tl = ql;
    exz->ps = -1; exz->pe = p->y_end-p->y_start; exz->pl = tl;  
    exz->err = p->error; exz->thre = p->error_threshold; 
    // clear_align(*exz);
    ed_band_cal_semi_64_w_absent_diag_trace(t_string, tl, q_string, ql, thres, aux_beg, exz);
    // if(id == 178 && p->x_start == 86800 && p->x_end == 86807) {
    //     fprintf(stderr, "\n[M::%s::semi] exz->ps::%d, exz->pe::%d, exz->ts::%d, exz->te::%d, exz->err::%d, exz->cigar.n::%d\n", 
    //     __func__, exz->ps, exz->pe, exz->ts, exz->te, exz->err, (int32_t)exz->cigar.n);
    //     fprintf(stderr, "[M::%s::semi] p->y_start::%d, p->y_end::%d, p->x_start::%d, p->x_end::%d, p->error::%d\n", 
    //     __func__, p->y_start, p->y_end, p->x_start, p->x_end, p->error);
    //     if(is_align(*exz)) {
    //         prt_cigar(exz->cigar.a, exz->cigar.n);
    //         fprintf(stderr, "[tstr] %.*s\n", exz->pe+1-exz->ps, t_string+exz->ps);
    //         fprintf(stderr, "[qstr] %.*s\n", exz->te+1-exz->ts, q_string+exz->ts);
    //     }
    // }
    // assert(is_align(*exz));
    // assert(cigar_check(t_string, q_string, exz));
    
            
    if(is_align(*exz)) {
        p->y_start = ts + exz->ps;///difference
        p->y_end = ts + exz->pe;
        p->error = exz->err;
        push_wcigar(p, &(z->w_list), exz);
        ///this condition is always wrong
        ///in best case, r_ts = threshold, t_end = aln_l - thres - 1
        if ((((exz->pe+1) == tl) || (exz->ps == 0)) && (exz->err > 0)) {
            if(recal_boundary_non_retrieve_exz(q_string, tstr, t_tot_l, ql, tl, thres, ts, exz->ps, exz->pe,
            exz->err, id, rev, exz, &ts, &aux_beg, &aux_end)) {
                //update cigar
                z->w_list.c.n = p->cidx; push_wcigar(p, &(z->w_list), exz);

                p->y_start = ts + exz->ps;///difference
                p->y_end = ts + exz->pe;
                p->error = exz->err;
            }
        }
        p->extra_begin = aux_beg; 
        p->extra_end = aux_end;
        return 1;
    }
    p->error = -1;
    return 0;
}

// Assembly:
movswl 0x14(%rdi), %r10d
xorl %eax, %eax
testl %r10d, %r10d
js 0x578d6
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %rbp
movq %rdi, %rbx
movl 0xc(%rdi), %edx
testl %edx, %edx
js 0x578c8
movq %r9, %r14
movq %rsi, 0x8(%rsp)
movslq (%rbx), %rax
movslq 0x4(%rbx), %rsi
movq %rsi, %r15
subq %rax, %r15
movswq 0x16(%rbx), %r11
leaq (%r15,%r11,2), %r13
incq %r13
incq %r15
movslq 0x8(%rbx), %r12
movq %r12, 0x20(%rsp)
movswq 0x10(%rbx), %r9
movq %r9, 0x18(%rsp)
movswq 0x12(%rbx), %rdi
movq %rdi, 0x10(%rsp)
testq %rdi, %rdi
movq %r8, 0x40(%rsp)
js 0x57747
addq %r9, %rdi
subq %rdi, %r13
jmp 0x57757
addq %r12, %r13
subq %r9, %r13
cmpq %r8, %r13
cmovgeq %r8, %r13
subq %r12, %r13
addq %rax, %rbp
andl $0x0, 0x1c(%r14)
movq %rcx, 0x38(%rsp)
leaq (%rcx,%r12), %rdi
subl %eax, %esi
movl %esi, 0x20(%r14)
movl %r15d, 0x24(%r14)
orl $-0x1, 0x10(%r14)
subl %r12d, %edx
movl %edx, 0x14(%r14)
movl %r13d, 0x18(%r14)
movl %r10d, 0x2c(%r14)
movl %r11d, 0x28(%r14)
subq $0x8, %rsp
movl %r13d, %esi
movq %rbp, %rdx
movl %r15d, %ecx
movq %r11, 0x38(%rsp)
movl %r11d, %r8d
pushq %r14
callq 0x59275
popq %rax
popq %rcx
movl 0x2c(%r14), %eax
cmpl 0x28(%r14), %eax
jle 0x577be
orw $-0x1, 0x14(%rbx)
xorl %eax, %eax
jmp 0x578c8
movq 0x10(%r14), %xmm0
movd %r12d, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rbx)
movw %ax, 0x14(%rbx)
movq 0x8(%rsp), %rax
leaq 0x48(%rax), %rsi
movq %rbx, %rdi
movq %rsi, 0x28(%rsp)
movq %r14, %rdx
callq 0x1e842
movslq 0x14(%r14), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %r13
je 0x5780c
cmpl $0x0, 0x10(%r14)
jne 0x578b3
movslq 0x2c(%r14), %r10
testq %r10, %r10
jle 0x578b3
movslq 0x10(%r14), %r11
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rcx
movq %r13, %r8
movq 0x30(%rsp), %r9
leaq 0x10(%rsp), %r15
pushq %r15
leaq 0x20(%rsp), %r15
pushq %r15
leaq 0x30(%rsp), %r15
pushq %r15
pushq %r14
movl 0xa0(%rsp), %r15d
pushq %r15
movl 0xb0(%rsp), %r15d
pushq %r15
pushq %r10
pushq %rax
pushq %r11
pushq %r12
callq 0x65b3f
addq $0x50, %rsp
testl %eax, %eax
je 0x578b3
movl 0x18(%rbx), %eax
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rdx
callq 0x1e842
movq 0x10(%r14), %xmm0
movd 0x20(%rsp), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rbx)
movzwl 0x2c(%r14), %eax
movw %ax, 0x14(%rbx)
movzwl 0x18(%rsp), %eax
movw %ax, 0x10(%rbx)
movzwl 0x10(%rsp), %eax
movw %ax, 0x12(%rbx)
pushq $0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq