// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_backtrace_adv_exz(window_list*, overlap_region*, All_reads*, hpc_t*, ul_idx_t const*, char*, char*, bit_extz_t*, unsigned int, unsigned int)
inline uint32_t gen_backtrace_adv_exz(window_list *p, overlap_region *z, All_reads *rref, hpc_t *hpc_g, const ul_idx_t *uref, 
char *qstr, char *tstr, 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, t_tot_l; 
    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 {
        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);

        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; tl = t_pri_l;
    if(rref) {
        recover_UC_Read_sub_region(tstr, ts, t_pri_l, rev, rref, id); t_string = tstr;
    } else {
        t_string = return_str_seq_exz(tstr, ts, t_pri_l, rev, hpc_g, uref, id);
    }

    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_exz(q_string, tstr, ql, tl, thres, ts, exz->ps, exz->pe,
            exz->err, id, rev, exz, rref, hpc_g, uref, &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:
xorl %eax, %eax
cmpw $0x0, 0x14(%rdi)
js 0x573b0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
cmpl $0x0, 0xc(%rdi)
js 0x573a2
movq %r8, %r10
movq %rcx, %rbp
movq %rdx, %r8
movq %r9, 0x18(%rsp)
movl 0xa8(%rsp), %edx
movl 0xa0(%rsp), %r9d
movslq (%rbx), %rax
movslq 0x4(%rbx), %r14
subq %rax, %r14
movswq 0x16(%rbx), %rcx
movq %rcx, 0x10(%rsp)
leaq (%r14,%rcx,2), %r12
incq %r12
movslq 0x8(%rbx), %r11
movq %r11, (%rsp)
movq %r11, 0x30(%rsp)
movswq 0x10(%rbx), %r13
movq %r13, 0x28(%rsp)
movswq 0x12(%rbx), %rcx
movq %rcx, 0x20(%rsp)
movl %edx, %r11d
testq %rcx, %rcx
js 0x57160
addq %r13, %rcx
subq %rcx, %r12
jmp 0x571b5
testq %rbp, %rbp
je 0x5717e
movq (%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %r11, %rdx
movl (%rcx,%rdx), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x571a1
testq %r10, %r10
je 0x57199
movq (%r10), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %r11, %rdx
movl (%rcx,%rdx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0x571a1
movq 0x18(%r8), %rcx
movq (%rcx,%r11,8), %rcx
movq (%rsp), %rdx
addq %rdx, %r12
subq %r13, %r12
cmpq %rcx, %r12
cmovgeq %rcx, %r12
subq %rdx, %r12
movq %rsi, 0x8(%rsp)
movq 0x98(%rsp), %r15
movq 0x90(%rsp), %rdi
incq %r14
addq %rax, 0x18(%rsp)
testq %r8, %r8
movq %r8, 0x50(%rsp)
movq %rbp, 0x48(%rsp)
movq %r10, 0x40(%rsp)
movq %r11, 0x38(%rsp)
je 0x57206
movzbl %r9b, %ecx
movq %rdi, %rbp
movq (%rsp), %rsi
movq %r12, %rdx
movq %r11, %r9
callq 0x6c9e
movq %rbp, %rdi
jmp 0x57228
subq $0x8, %rsp
movzbl %r9b, %ecx
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %r8
movq %r10, %r9
pushq %r11
callq 0x578d7
popq %rcx
popq %rdx
movq %rax, %rdi
andl $0x0, 0x1c(%r15)
movl 0x4(%rbx), %eax
subl (%rbx), %eax
movl %eax, 0x20(%r15)
movl %r14d, 0x24(%r15)
orl $-0x1, 0x10(%r15)
movl 0xc(%rbx), %eax
subl 0x8(%rbx), %eax
movl %eax, 0x14(%r15)
movl %r12d, 0x18(%r15)
movswl 0x14(%rbx), %eax
movl %eax, 0x2c(%r15)
movswl 0x16(%rbx), %eax
movl %eax, 0x28(%r15)
subq $0x8, %rsp
movl %r12d, %esi
movq 0x20(%rsp), %rbp
movq %rbp, %rdx
movl %r14d, %ecx
movq 0x18(%rsp), %r8
movl %r13d, %r9d
pushq %r15
callq 0x59275
popq %rax
popq %rcx
movl 0x2c(%r15), %eax
cmpl 0x28(%r15), %eax
jle 0x57296
orw $-0x1, 0x14(%rbx)
xorl %eax, %eax
jmp 0x573a2
movq 0x10(%r15), %xmm0
movd (%rsp), %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), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1e842
movslq 0x14(%r15), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %r12
je 0x572e2
cmpl $0x0, 0x10(%r15)
jne 0x5738d
movslq 0x2c(%r15), %r10
testq %r10, %r10
jle 0x5738d
movslq 0x10(%r15), %r11
movq %rbp, %rdi
movq 0x90(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
movq 0x10(%rsp), %r8
movq (%rsp), %r9
leaq 0x20(%rsp), %r14
pushq %r14
leaq 0x30(%rsp), %r14
pushq %r14
leaq 0x40(%rsp), %r14
pushq %r14
pushq 0x58(%rsp)
pushq 0x68(%rsp)
pushq 0x78(%rsp)
pushq %r15
movl 0xd8(%rsp), %r14d
pushq %r14
pushq 0x78(%rsp)
pushq %r10
pushq %rax
pushq %r11
callq 0x65940
addq $0x60, %rsp
testl %eax, %eax
je 0x5738d
movl 0x18(%rbx), %eax
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1e842
movq 0x10(%r15), %xmm0
movd 0x30(%rsp), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rbx)
movzwl 0x2c(%r15), %eax
movw %ax, 0x14(%rbx)
movzwl 0x28(%rsp), %eax
movw %ax, 0x10(%rbx)
movzwl 0x20(%rsp), %eax
movw %ax, 0x12(%rbx)
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq