// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_exact_exz(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, bit_extz_t*, char*, UC_Read*, long, long, long, long, long*, long*, long, long)
int64_t cal_exact_exz(overlap_region *z, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, 
bit_extz_t *exz, char* qstr, UC_Read *tu, int64_t qs, int64_t qe, int64_t ts, int64_t te, 
int64_t *pts, int64_t *pte, int64_t q_tot_l, int64_t mode)
{
    clear_align(*exz); exz->thre = 0; exz->cigar.n = 0;
    int64_t ql, tl, t_tot_l = -1; 
    char *q_string, *t_string; int32_t rev = z->y_pos_strand, id = z->y_id; ql = qe - qs;
    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);

    if(mode == 3) {//semi
        ts = (qs - z->x_pos_s) + z->y_pos_s; ts += y_start_offset(qs, &(z->f_cigar));
        te = ts + ql; 
    } else if(mode == 1) {///forward extension
        te = ts + ql; 
    } else if(mode == 2) {///backward extension
        ts = te - ql; 
    }
    if(ts < 0) ts = 0;
    if(ts > t_tot_l) ts = t_tot_l;
    if(te > t_tot_l) te = t_tot_l;
    ql = qe - qs; tl = te - ts; 
    if(ql != tl) return 0;

    q_string = qstr + qs; 
    t_string = retrieve_str_seq_exz(tu, ts, tl, (*pts), (*pte)-(*pts), rev, uref, hpc_g, rref, id);
    (*pts) = ts; (*pte) = te;

    if(memcmp(q_string, t_string, ql)) return 0;
    exz->err = 0; push_trace(&(exz->cigar), 0, ql);
    exz->pl = tl; exz->ps = 0; exz->pe = tl-1;
    exz->tl = ql; exz->ts = 0; exz->te = ql-1;
    // cigar_check(t_string, q_string, exz);
    // if(!cigar_check(t_string, q_string, exz)) {
    //     fprintf(stderr, "[M::%s::] cigar_n::%d\n", __func__, (int32_t)exz->cigar.n);
    //     fprintf(stderr, "[M::%s::] pstr::%.*s\n", __func__, (int32_t)tl, t_string);
    //     fprintf(stderr, "[M::%s::] tstr::%.*s\n", __func__, (int32_t)ql, q_string);
    // }
    exz->ps += ts; exz->pe += ts;
    exz->ts += qs; exz->te += qs;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %rbx
movq %rcx, %rbp
movq %rdx, %r11
movq %rsi, %r10
movq %rdi, %rsi
movq 0xa0(%rsp), %rax
movabsq $0x7fffffff00000000, %rcx # imm = 0x7FFFFFFF00000000
movq %rcx, 0x28(%r8)
andq $0x0, 0xa8(%r8)
movq 0x68(%rsp), %r8
movq 0x70(%rsp), %r15
movslq 0x10(%rdi), %rdi
testq %rdx, %rdx
je 0x33ee8
movq (%r11), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rdi, %rdx
movl (%rcx,%rdx), %r12d
shrl %r12d
andl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
jmp 0x33f0d
testq %r10, %r10
je 0x33f05
movq (%r10), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rdi, %rdx
movl (%rcx,%rdx), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
jmp 0x33f0d
movq 0x18(%rbp), %rcx
movq (%rcx,%rdi,8), %r12
movq 0x78(%rsp), %r14
movl 0x1c(%rsi), %edx
subq %r8, %r15
cmpq $0x1, %rax
je 0x33f99
movq 0x80(%rsp), %rcx
cmpq $0x2, %rax
je 0x33f91
cmpq $0x3, %rax
jne 0x33f9d
movl 0x4(%rsi), %eax
movl 0x14(%rsi), %r13d
movq %r8, %rcx
subq %rax, %rcx
addq %rcx, %r13
addq $0x38, %rsi
movq %rdi, 0x10(%rsp)
movq %r8, %rdi
movq %r9, %r14
movq %r10, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq %r11, %rbp
movl %edx, 0xc(%rsp)
callq 0x1bebf
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %rdi
movq %rbp, %r11
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %r10
movq %r14, %r9
movq 0x68(%rsp), %r8
movq %rax, %r14
addq %r13, %r14
leaq (%r14,%r15), %rcx
jmp 0x33f9d
movq %rcx, %r14
subq %r15, %r14
jmp 0x33f9d
leaq (%r15,%r14), %rcx
xorl %eax, %eax
testq %r14, %r14
cmovleq %rax, %r14
cmpq %r12, %r14
cmovgeq %r12, %r14
cmpq %r12, %rcx
cmovlq %rcx, %r12
movq %r12, %rcx
subq %r14, %rcx
cmpq %rcx, %r15
jne 0x3406c
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rcx
movq %rdi, %rax
movq 0x60(%rsp), %rdi
addq %r8, %r9
movq (%rcx), %rcx
movq (%rsi), %r8
subq %rcx, %r8
movq %r9, %r13
movzbl %dl, %r9d
movq %r14, %rsi
movq %r15, %rdx
pushq %rax
pushq %rbp
pushq %r11
pushq %r10
callq 0x625fc
addq $0x20, %rsp
movq 0x88(%rsp), %rcx
movq %r14, (%rcx)
movq 0x90(%rsp), %rcx
movq %r12, (%rcx)
movq %r13, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x21a0
testl %eax, %eax
je 0x3402f
xorl %eax, %eax
jmp 0x3406c
leaq 0xa8(%rbx), %rdi
andl $0x0, 0x2c(%rbx)
xorl %esi, %esi
movl %r15d, %edx
callq 0x62885
movl %r15d, 0x18(%rbx)
movl %r15d, 0x24(%rbx)
movl %r14d, 0x10(%rbx)
leal (%r15,%r14), %eax
decl %eax
movl %eax, 0x14(%rbx)
movq 0x68(%rsp), %rax
movl %eax, 0x1c(%rbx)
leal -0x1(%r15,%rax), %eax
movl %eax, 0x20(%rbx)
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq