// File: /workspace/github_cpp_makefile_star2_O3/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 0x8acf3
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
cmpl $0x0, 0xc(%rdi)
js 0x8ace5
movq %r9, %rbp
movq %rdx, %r11
movl 0xa8(%rsp), %edx
movl 0xa0(%rsp), %r10d
movslq (%rbx), %rax
movslq 0x4(%rbx), %r14
subq %rax, %r14
movswq 0x16(%rbx), %rdi
movq %rdi, 0x10(%rsp)
leaq (%r14,%rdi,2), %r12
incq %r12
movslq 0x8(%rbx), %r9
movq %r9, (%rsp)
movq %r9, 0x28(%rsp)
movswq 0x10(%rbx), %r15
movq %r15, 0x20(%rsp)
movswq 0x12(%rbx), %rdi
movq %rdi, 0x18(%rsp)
movl %edx, %r9d
testq %rdi, %rdi
movq %r15, 0x50(%rsp)
js 0x8aa55
addq %r15, %rdi
jmp 0x8aaa6
testq %rcx, %rcx
je 0x8aa72
movq (%rcx), %rdx
movq 0x10(%rdx), %rdi
leaq (%r9,%r9,4), %rdx
movl (%rdi,%rdx,8), %edx
shrl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x8aa95
testq %r8, %r8
je 0x8aa8d
movq (%r8), %rdx
movq 0x10(%rdx), %rdi
leaq (%r9,%r9,4), %rdx
movl (%rdi,%rdx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0x8aa95
movq 0x18(%r11), %rdx
movq (%rdx,%r9,8), %rdx
movq (%rsp), %rdi
addq %rdi, %r12
subq %r15, %r12
cmpq %rdx, %r12
cmovgeq %rdx, %r12
subq %rdi, %r12
movq 0x98(%rsp), %r15
movq 0x90(%rsp), %r13
incq %r14
addq %rax, %rbp
testq %r11, %r11
movq %rsi, 0x8(%rsp)
movq %r11, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq %r9, 0x30(%rsp)
je 0x8aaf8
movzbl %r10b, %ecx
movq %r13, %rdi
movq (%rsp), %rsi
movq %r12, %rdx
movq %r11, %r8
callq 0x32ef8
movq %r13, %rdi
jmp 0x8ab63
testq %rcx, %rcx
je 0x8ab21
movq (%rcx), %rax
movq 0x10(%rax), %rax
leaq (%r9,%r9,4), %rdx
movq 0x20(%rax,%rdx,8), %rdi
testb %r10b, %r10b
je 0x8ab5a
leaq (%rax,%rdx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8ab5c
movq (%r8), %rax
leaq (%r9,%r9,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl %r10b, %ecx
xorl %edi, %edi
movq 0x98(%rsp), %r13
movq %r13, %rsi
movq 0x8(%rsp), %r8
movq %r12, %r9
pushq $0x0
callq 0x36049
addq $0x10, %rsp
jmp 0x8aaf3
xorl %eax, %eax
addq (%rsp), %rax
addq %rax, %rdi
movl $0x0, 0x1c(%r15)
movl 0x4(%rbx), %eax
subl (%rbx), %eax
movl %eax, 0x20(%r15)
movl %r14d, 0x24(%r15)
movl $0xffffffff, 0x10(%r15) # imm = 0xFFFFFFFF
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 %rbp, %rdx
movl %r14d, %ecx
movq 0x18(%rsp), %r8
movq 0x58(%rsp), %r9
pushq %r15
callq 0x8d32c
addq $0x10, %rsp
movl 0x2c(%r15), %eax
cmpl 0x28(%r15), %eax
jle 0x8abd7
movw $0xffff, 0x14(%rbx)     # imm = 0xFFFF
xorl %eax, %eax
jmp 0x8ace5
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 0x4b41c
movslq 0x14(%r15), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %r12
je 0x8ac23
cmpl $0x0, 0x10(%r15)
jne 0x8acce
movslq 0x2c(%r15), %r10
testq %r10, %r10
jle 0x8acce
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 0x18(%rsp), %r14
pushq %r14
leaq 0x28(%rsp), %r14
pushq %r14
leaq 0x38(%rsp), %r14
pushq %r14
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x70(%rsp)
pushq %r15
movl 0xd8(%rsp), %r14d
pushq %r14
pushq 0x70(%rsp)
pushq %r10
pushq %rax
pushq %r11
callq 0x9c4f8
addq $0x60, %rsp
testl %eax, %eax
je 0x8acce
movl 0x18(%rbx), %eax
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x4b41c
movq 0x10(%r15), %xmm0
movd 0x28(%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 0x20(%rsp), %eax
movw %ax, 0x10(%rbx)
movzwl 0x18(%rsp), %eax
movw %ax, 0x12(%rbx)
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq