// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movswl 0x14(%rdi), %eax
xorl %r13d, %r13d
testl %eax, %eax
js 0x8b364
movq %rdi, %rbx
movl 0xc(%rdi), %r15d
testl %r15d, %r15d
js 0x8b364
movq %r9, %r14
movq %rcx, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
movslq (%rbx), %r11
movslq 0x4(%rbx), %rsi
movq %rsi, %rdi
subq %r11, %rdi
leaq 0x1(%rdi), %rcx
movq %rcx, 0x30(%rsp)
movswq 0x16(%rbx), %rcx
movq %rcx, 0x50(%rsp)
movq %rdi, 0x38(%rsp)
leaq 0x1(%rdi,%rcx,2), %r12
movslq 0x8(%rbx), %rbp
movzwl 0x10(%rbx), %edi
movw %di, 0xe(%rsp)
movswl %di, %r9d
movswq %r9w, %rdi
movswq 0x12(%rbx), %r10
testq %r10, %r10
movq %r10, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq %r8, 0x48(%rsp)
js 0x8b1fc
addq %r10, %rdi
subq %rdi, %r12
jmp 0x8b20c
addq %rbp, %r12
subq %rdi, %r12
cmpq %r8, %r12
cmovgeq %r8, %r12
subq %rbp, %r12
addq %r11, %rdx
movq 0x18(%rsp), %rcx
leaq (%rcx,%rbp), %rdi
movl $0x0, 0x1c(%r14)
subl %r11d, %esi
movl %esi, 0x20(%r14)
movq 0x30(%rsp), %rcx
movl %ecx, 0x24(%r14)
movl $0xffffffff, 0x10(%r14) # imm = 0xFFFFFFFF
subl %ebp, %r15d
movl %r15d, 0x14(%r14)
movl %r12d, 0x18(%r14)
movl %eax, 0x2c(%r14)
movq 0x50(%rsp), %r15
movl %r15d, 0x28(%r14)
movq %r14, (%rsp)
movl %r12d, %esi
movq %rdx, 0x40(%rsp)
movl %r15d, %r8d
callq 0x8d32c
movl 0x2c(%r14), %eax
cmpl 0x28(%r14), %eax
jle 0x8b279
movw $0xffff, 0x14(%rbx)     # imm = 0xFFFF
jmp 0x8b364
movq 0x10(%r14), %xmm0
movq %rbp, %r13
movd %ebp, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rbx)
movw %ax, 0x14(%rbx)
movq 0x28(%rsp), %rax
leaq 0x48(%rax), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x4b41c
movslq 0x14(%r14), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %r12
je 0x8b2c7
cmpl $0x0, 0x10(%r14)
jne 0x8b34c
movl 0x2c(%r14), %r10d
testl %r10d, %r10d
jle 0x8b34c
cmpl $0x0, 0x10(%r14)
movq %r13, %r8
movq %r13, %r9
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rdx
je 0x8b2f7
cmpq %r12, %rcx
jne 0x8b34c
movq %r8, %r9
subq 0x38(%rsp), %r9
addq %rax, %r9
testq %r9, %r9
sets %al
cmpq %rdi, %r9
setge %cl
orb %al, %cl
jne 0x8b34c
leaq (%r15,%r15), %rax
addq %rdi, %rax
subq %r9, %rax
addq $0x1f, %rax
movq 0x20(%rsp), %rcx
cmpq %rcx, %rax
jl 0x8b34c
subq %r15, %r9
subq %r9, %rdi
cmpq %rcx, %rdi
cmovgeq %rcx, %rdi
xorl %esi, %esi
testq %r9, %r9
movl $0x0, %r13d
cmovgq %r9, %r13
cmovsq %r9, %rsi
addq %rdi, %rsi
cmpq %r8, %r13
jne 0x8b376
cmpq %r12, %rsi
jne 0x8b376
movzwl 0xe(%rsp), %eax
movq 0x10(%rsp), %rcx
movw %ax, 0x10(%rbx)
movw %cx, 0x12(%rbx)
movl $0x1, %r13d
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r12
movq %r9, %rax
negq %rax
sarq $0x3f, %r9
andq %rax, %r9
movq 0x18(%rsp), %rdi
addq %r13, %rdi
movl $0x7fffffff, 0x2c(%r14) # imm = 0x7FFFFFFF
movq %r14, (%rsp)
movq 0x30(%rsp), %rcx
movl %r15d, %r8d
movq %r9, 0x18(%rsp)
movl %r10d, %r15d
callq 0x8d32c
movl 0x2c(%r14), %eax
cmpl %r15d, %eax
jge 0x8b34c
cmpl 0x28(%r14), %eax
movzwl 0xe(%rsp), %eax
movq 0x10(%rsp), %rcx
jg 0x8b356
movq 0x20(%rsp), %r15
subq %r12, %r15
movl 0x18(%rbx), %eax
movq 0x28(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x4b41c
movq 0x10(%r14), %xmm0
movd %r13d, %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)
movq 0x18(%rsp), %rax
movl %r15d, %ecx
jmp 0x8b356