// File: 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:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movq -0x38(%rbp), %rax
movswl 0x14(%rax), %eax
cmpl $0x0, %eax
jl 0xa7a11
movq -0x38(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jge 0xa7a1d
movl $0x0, -0x2c(%rbp)
jmp 0xa7f04
movq -0x38(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rax
movslq 0x4(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
addq $0x1, %rax
subq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movswq 0x16(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x78(%rbp), %rax
movq -0x98(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
movslq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x38(%rbp), %rax
movswq 0x10(%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0x38(%rbp), %rax
movswq 0x12(%rax), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
jl 0xa7ac6
movq -0x88(%rbp), %rax
subq -0xb0(%rbp), %rax
subq -0xb8(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0xa7b8b
cmpq $0x0, -0x50(%rbp)
je 0xa7af6
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x28(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
sarl %eax
cltq
movq %rax, -0xa8(%rbp)
jmp 0xa7b3c
cmpq $0x0, -0x58(%rbp)
je 0xa7b24
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x28(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xa8(%rbp)
jmp 0xa7b3a
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
jmp 0xa7b3c
movq -0xa0(%rbp), %rax
addq -0x88(%rbp), %rax
subq -0xb0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jle 0xa7b76
movq -0xa8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
subq -0xa0(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x60(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xa7be0
movq 0x10(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x90(%rbp), %rdx
movl 0x20(%rbp), %eax
movq -0x48(%rbp), %r8
movl 0x28(%rbp), %ecx
movl %ecx, %r9d
movzbl %al, %ecx
callq 0x9a30
movq 0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0xa7c15
movq 0x10(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x90(%rbp), %rdx
movl 0x20(%rbp), %eax
movb %al, %cl
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movl 0x28(%rbp), %eax
movzbl %cl, %ecx
movq %rax, (%rsp)
callq 0xa8880
movq %rax, -0xc8(%rbp)
movq 0x18(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x38(%rbp), %rax
subl (%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x78(%rbp), %rax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x24(%rax)
movq 0x18(%rbp), %rax
movl $0xffffffff, 0x10(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x38(%rbp), %rax
subl 0x8(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x80(%rbp), %rax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rax
movswl 0x14(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x38(%rbp), %rax
movswl 0x16(%rax), %ecx
movq 0x18(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0xc8(%rbp), %rdi
movq -0x80(%rbp), %rax
movl %eax, %esi
movq -0xc0(%rbp), %rdx
movq -0x78(%rbp), %rax
movl %eax, %ecx
movq -0x98(%rbp), %rax
movl %eax, %r8d
movq -0xb0(%rbp), %rax
movl %eax, %r9d
movq 0x18(%rbp), %rax
movq %rax, (%rsp)
callq 0xab870
movq 0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movq 0x18(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jg 0xa7ef3
movq -0xa0(%rbp), %rax
movq 0x18(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xa0(%rbp), %rax
movq 0x18(%rbp), %rcx
movslq 0x14(%rcx), %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movq 0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x48, %rsi
movq 0x18(%rbp), %rdx
callq 0x38180
movq 0x18(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x1, %eax
cltq
cmpq -0x80(%rbp), %rax
je 0xa7d58
movq 0x18(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xa7ec6
movq 0x18(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jle 0xa7ec6
movq -0xc0(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x80(%rbp), %rcx
movq -0x98(%rbp), %r8
movq -0xa0(%rbp), %r9
movq 0x18(%rbp), %rax
movslq 0x10(%rax), %rax
movq %rax, -0xf0(%rbp)
movq 0x18(%rbp), %rax
movslq 0x14(%rax), %rax
movq %rax, -0xe8(%rbp)
movq 0x18(%rbp), %rax
movslq 0x2c(%rax), %rax
movq %rax, -0xe0(%rbp)
movl 0x28(%rbp), %eax
movq %rax, -0xd8(%rbp)
movl 0x20(%rbp), %r13d
movq 0x18(%rbp), %r12
movq -0x48(%rbp), %r15
movq -0x50(%rbp), %r14
movq -0x58(%rbp), %rbx
leaq -0xa0(%rbp), %r11
leaq -0xb0(%rbp), %r10
leaq -0xb8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, (%rsp)
movq -0xe8(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0xe0(%rbp), %rax
movq %rax, 0x10(%rsp)
movq -0xd8(%rbp), %rax
movq %rax, 0x18(%rsp)
movq -0xd0(%rbp), %rax
movl %r13d, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
movq %r11, 0x48(%rsp)
movq %r10, 0x50(%rsp)
movq %rax, 0x58(%rsp)
callq 0xc6e80
cmpl $0x0, %eax
je 0xa7ec4
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, %ecx
movq -0x40(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x48, %rsi
movq 0x18(%rbp), %rdx
callq 0x38180
movq -0xa0(%rbp), %rax
movq 0x18(%rbp), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xa0(%rbp), %rax
movq 0x18(%rbp), %rcx
movslq 0x14(%rcx), %rcx
addq %rcx, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movq 0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, 0x14(%rax)
jmp 0xa7ec6
movq -0xb0(%rbp), %rax
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, 0x10(%rax)
movq -0xb8(%rbp), %rax
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, 0x12(%rax)
movl $0x1, -0x2c(%rbp)
jmp 0xa7f04
movq -0x38(%rbp), %rax
movw $0xffff, 0x14(%rax)     # imm = 0xFFFF
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)