// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movswl 0x14(%rdi), %eax
xorl %r15d, %r15d
testl %eax, %eax
js 0x8c0fe
movq %rdi, %rbx
movl 0xc(%rdi), %r10d
testl %r10d, %r10d
js 0x8c0fe
movq %r9, %r14
movq %r8, 0x50(%rsp)
movq %rdx, 0x20(%rsp)
movslq (%rbx), %rdx
movslq 0x4(%rbx), %r8
movq %r8, %rbp
subq %rdx, %rbp
leaq 0x1(%rbp), %r11
movswq 0x16(%rbx), %rdi
movq %rdi, 0x40(%rsp)
leaq 0x1(,%rdi,2), %r12
addq %rbp, %r12
movl 0x8(%rbx), %edi
movl %edi, 0x1c(%rsp)
movslq %edi, %r13
movswq 0x10(%rbx), %r9
movswq 0x12(%rbx), %rdi
testq %rdi, %rdi
movq %rdi, 0x10(%rsp)
movq %rsi, 0x58(%rsp)
movq %r12, 0x60(%rsp)
js 0x8bea2
addq %r9, %rdi
subq %rdi, %r12
jmp 0x8beb7
addq %r13, %r12
subq %r9, %r12
movq 0x50(%rsp), %rsi
cmpq %rsi, %r12
cmovgeq %rsi, %r12
subq %r13, %r12
movq 0x20(%rsp), %rsi
addq %rdx, %rsi
movq %rcx, 0x70(%rsp)
leaq (%rcx,%r13), %rdi
movl $0x0, 0x1c(%r14)
subl %edx, %r8d
movl %r8d, 0x20(%r14)
movl %r11d, 0x24(%r14)
movl $0xffffffff, 0x10(%r14) # imm = 0xFFFFFFFF
movq %r13, 0x48(%rsp)
subl %r13d, %r10d
movq %rsi, %r13
movl %r10d, 0x14(%r14)
movl %r12d, 0x18(%r14)
movl %eax, 0x2c(%r14)
movq 0x40(%rsp), %r8
movl %r8d, 0x28(%r14)
movq %r14, (%rsp)
movl %r12d, %esi
movq %r13, %rdx
movq %r11, 0x68(%rsp)
movl %r11d, %ecx
movq %r9, 0x20(%rsp)
callq 0x8dd8a
movl 0x2c(%r14), %eax
cmpl 0x28(%r14), %eax
jle 0x8bf34
movw $0xffff, 0x14(%rbx)     # imm = 0xFFFF
jmp 0x8c0fe
movl 0x10(%r14), %ecx
movl 0x1c(%rsp), %edx
addl %edx, %ecx
movl %ecx, 0x8(%rbx)
movl 0x14(%r14), %ecx
addl %edx, %ecx
movl %ecx, 0xc(%rbx)
movw %ax, 0x14(%rbx)
movq 0x58(%rsp), %rax
leaq 0x48(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x4bdb5
movslq 0x14(%r14), %rsi
leaq 0x1(%rsi), %rdx
cmpq %rdx, %r12
je 0x8bf83
cmpl $0x0, 0x10(%r14)
je 0x8bf83
movq 0x20(%rsp), %r8
jmp 0x8c040
movq %r15, 0x30(%rsp)
movl 0x2c(%r14), %r15d
testl %r15d, %r15d
movq 0x20(%rsp), %r8
jle 0x8c040
cmpl $0x0, 0x10(%r14)
movq 0x48(%rsp), %rax
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %rcx
movq 0x50(%rsp), %r11
je 0x8bfcc
movb $0x1, %r9b
cmpq %r12, %rdx
jne 0x8c066
movq 0x48(%rsp), %rax
subq %rbp, %rax
addq %rsi, %rax
testq %rax, %rax
sets %sil
cmpq %r11, %rax
setge %dl
movq $-0x1, 0x28(%rsp)
orb %sil, %dl
jne 0x8c04a
movq 0x40(%rsp), %rdx
addq %rdx, %rdx
addq %r11, %rdx
subq %rax, %rdx
addq $0x1f, %rdx
movq $-0x1, %rbp
movq $-0x1, 0x38(%rsp)
movq $-0x1, %rsi
movb $0x1, %r9b
movq 0x60(%rsp), %r10
cmpq %r10, %rdx
jl 0x8c08c
subq 0x40(%rsp), %rax
subq %rax, %r11
cmpq %r10, %r11
movq %r11, %rsi
cmovgeq %r10, %rsi
subq %rsi, %r10
testq %rax, %rax
js 0x8c072
xorl %ebp, %ebp
movq %rax, 0x28(%rsp)
jmp 0x8c084
movq 0x10(%rsp), %r12
jmp 0x8c0ee
movq $-0x1, %rbp
movq $-0x1, 0x38(%rsp)
movq $-0x1, %rsi
movb $0x1, %r9b
jmp 0x8c08c
movq 0x10(%rsp), %r12
movq 0x30(%rsp), %rsi
jmp 0x8c0af
addq %rax, %rsi
negq %rax
movq $0x0, 0x28(%rsp)
movq %rax, %rbp
movq %r10, 0x38(%rsp)
xorl %r9d, %r9d
testb %r9b, %r9b
jne 0x8c0a2
movq 0x28(%rsp), %rax
cmpq 0x48(%rsp), %rax
jne 0x8c110
cmpq %r12, %rsi
jne 0x8c110
movq 0x10(%rsp), %r12
movq 0x30(%rsp), %rsi
movb $0x1, %r9b
testb %r9b, %r9b
jne 0x8c0ee
movl 0x18(%rbx), %eax
movq 0x58(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq %rbx, %rdi
movq %r14, %rdx
movq %r8, %r15
callq 0x4bdb5
movq %r15, %r8
movl 0x10(%r14), %eax
movl 0x1c(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x8(%rbx)
addl 0x14(%r14), %ecx
movl %ecx, 0xc(%rbx)
movzwl 0x2c(%r14), %eax
movw %ax, 0x14(%rbx)
movw %r8w, 0x10(%rbx)
movw %r12w, 0x12(%rbx)
movl $0x1, %r15d
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq 0x28(%rsp), %rdi
movl $0x7fffffff, 0x2c(%r14) # imm = 0x7FFFFFFF
movq %r14, (%rsp)
movq %r13, %rdx
movq 0x40(%rsp), %r8
movl %ebp, %r9d
callq 0x8dd8a
movl 0x2c(%r14), %eax
cmpl %r15d, %eax
jge 0x8c16f
cmpl 0x28(%r14), %eax
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %r12
movq 0x30(%rsp), %rsi
movb $0x1, %r9b
jg 0x8c0af
xorl %r9d, %r9d
movq 0x28(%rsp), %rax
movl %eax, 0x1c(%rsp)
movq %rbp, %r8
movq 0x38(%rsp), %r12
jmp 0x8c0af
movq 0x20(%rsp), %r8
jmp 0x8c0a2