// File: /workspace/github_cpp_makefile_star2_O1/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 0x8b8e1
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
cmpl $0x0, 0xc(%rdi)
js 0x8b8d3
movq %r9, %rbp
movq %rsi, 0x18(%rsp)
movl 0xa8(%rsp), %esi
movl 0xa0(%rsp), %r10d
movslq (%rbx), %rax
movslq 0x4(%rbx), %r14
subq %rax, %r14
movswq 0x16(%rbx), %rdi
movq %rdi, 0x28(%rsp)
leaq (%r14,%rdi,2), %r13
incq %r13
movslq 0x8(%rbx), %r11
movq %r11, 0x10(%rsp)
movswq 0x10(%rbx), %rdi
movq %rdi, 0x8(%rsp)
movswq 0x12(%rbx), %r15
movq %r15, 0x30(%rsp)
movl %esi, %r9d
testq %r15, %r15
movq %rdx, 0x20(%rsp)
js 0x8b63c
addq %r15, %rdi
subq %rdi, %r13
jmp 0x8b68c
testq %rcx, %rcx
je 0x8b659
movq (%rcx), %rdx
movq 0x10(%rdx), %rdx
leaq (%r9,%r9,4), %rsi
movl (%rdx,%rsi,8), %edx
shrl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x8b67c
testq %r8, %r8
je 0x8b674
movq (%r8), %rdx
movq 0x10(%rdx), %rdx
leaq (%r9,%r9,4), %rsi
movl (%rdx,%rsi,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0x8b67c
movq 0x18(%rdx), %rdx
movq (%rdx,%r9,8), %rdx
addq %r11, %r13
subq %rdi, %r13
cmpq %rdx, %r13
cmovgeq %rdx, %r13
subq %r11, %r13
movq 0x98(%rsp), %r12
movq 0x90(%rsp), %r15
incq %r14
addq %rax, %rbp
movq 0x20(%rsp), %rax
testq %rax, %rax
movq %rcx, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %r9, 0x40(%rsp)
je 0x8b6d5
movzbl %r10b, %ecx
movq %r15, %rdi
movq %r11, %rsi
movq %r13, %rdx
movq %rax, %r8
callq 0x337f7
movq %r15, %rdi
jmp 0x8b73d
testq %rcx, %rcx
je 0x8b6fe
movq (%rcx), %rax
movq 0x10(%rax), %rax
leaq (%r9,%r9,4), %rdx
movq 0x20(%rax,%rdx,8), %rdi
testb %r10b, %r10b
je 0x8b735
leaq (%rax,%rdx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8b737
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), %r15
movq %r15, %rsi
movq %r11, %r8
movq %r13, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
jmp 0x8b6d0
xorl %eax, %eax
addq %r11, %rax
addq %rax, %rdi
movl $0x0, 0x1c(%r12)
movl 0x4(%rbx), %eax
subl (%rbx), %eax
movl %eax, 0x20(%r12)
movl %r14d, 0x24(%r12)
movl $0xffffffff, 0x10(%r12) # imm = 0xFFFFFFFF
movl 0xc(%rbx), %eax
subl 0x8(%rbx), %eax
movl %eax, 0x14(%r12)
movl %r13d, 0x18(%r12)
movswl 0x14(%rbx), %eax
movl %eax, 0x2c(%r12)
movswl 0x16(%rbx), %eax
movl %eax, 0x28(%r12)
movl 0x8(%rsp), %r9d
subq $0x8, %rsp
movl %r13d, %esi
movq %rbp, %rdx
movl %r14d, %ecx
movq 0x30(%rsp), %r8
pushq %r12
callq 0x8dd8a
addq $0x10, %rsp
movl 0x2c(%r12), %eax
cmpl 0x28(%r12), %eax
jle 0x8b7bb
movw $0xffff, 0x14(%rbx)     # imm = 0xFFFF
xorl %eax, %eax
jmp 0x8b8d3
movl 0x10(%rsp), %ecx
movl 0x10(%r12), %edx
addl %ecx, %edx
movl %edx, 0x8(%rbx)
addl 0x14(%r12), %ecx
movl %ecx, 0xc(%rbx)
movw %ax, 0x14(%rbx)
movq 0x18(%rsp), %rax
leaq 0x48(%rax), %r15
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4bdb5
movslq 0x14(%r12), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %r13
je 0x8b806
cmpl $0x0, 0x10(%r12)
jne 0x8b8bc
movslq 0x2c(%r12), %r10
testq %r10, %r10
jle 0x8b8bc
leaq 0x10(%rsp), %r11
movq (%r11), %r9
movslq 0x10(%r12), %rcx
movq %rcx, 0x38(%rsp)
movq %rbp, %rdi
movq 0x90(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
movq 0x28(%rsp), %r8
leaq 0x30(%rsp), %r14
pushq %r14
leaq 0x10(%rsp), %r14
pushq %r14
pushq %r11
pushq 0x60(%rsp)
pushq 0x70(%rsp)
pushq 0x48(%rsp)
pushq %r12
movl 0xd8(%rsp), %r11d
pushq %r11
pushq 0x80(%rsp)
pushq %r10
pushq %rax
pushq 0x90(%rsp)
callq 0x9bdcb
addq $0x60, %rsp
testl %eax, %eax
je 0x8b8bc
movl 0x18(%rbx), %eax
movq 0x18(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4bdb5
movl 0x10(%rsp), %eax
movl 0x10(%r12), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%rbx)
addl 0x14(%r12), %eax
movl %eax, 0xc(%rbx)
movzwl 0x2c(%r12), %eax
movw %ax, 0x14(%rbx)
movzwl 0x8(%rsp), %eax
movw %ax, 0x10(%rbx)
movzwl 0x30(%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