// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %r8, %rbx
movq %rcx, %r8
movq %rdx, %r12
movq %rsi, %r11
movq %rdi, %rbp
movq 0xc0(%rsp), %rax
movq 0x90(%rsp), %r15
movabsq $0x7fffffff00000000, %rcx # imm = 0x7FFFFFFF00000000
movq %rcx, 0x28(%rbx)
movq $0x0, 0xa8(%rbx)
movslq 0x10(%rdi), %r10
testq %rdx, %rdx
je 0x64829
movq (%r12), %rcx
movq 0x10(%rcx), %rcx
leaq (%r10,%r10,4), %rdx
movl (%rcx,%rdx,8), %r14d
shrl %r14d
andl $0x3fffffff, %r14d      # imm = 0x3FFFFFFF
jmp 0x6484e
testq %r11, %r11
je 0x64846
movq (%r11), %rcx
movq 0x10(%rcx), %rcx
leaq (%r10,%r10,4), %rdx
movl (%rcx,%rdx,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
jmp 0x6484e
movq 0x18(%r8), %rcx
movq (%rcx,%r10,8), %r14
movq 0x98(%rsp), %r13
movl 0x1c(%rbp), %edi
subq 0x88(%rsp), %r15
cmpq $0x1, %rax
je 0x6490d
movq 0xa0(%rsp), %rcx
cmpq $0x2, %rax
je 0x64905
cmpq $0x3, %rax
jne 0x64972
movl %edi, 0xc(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %r9, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movl 0x4(%rbp), %eax
movq %rax, 0x38(%rsp)
movl 0x14(%rbp), %eax
movq %rax, 0x40(%rsp)
leaq 0x38(%rbp), %r15
movl 0x40(%rbp), %esi
decl %esi
movq %r15, %rdi
callq 0x3d110
cltq
movl 0x40(%rbp), %r13d
cmpq 0x88(%rsp), %rax
je 0x64928
testl %r13d, %r13d
je 0x64c27
xorl %r13d, %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x3d110
cltq
cmpq 0x88(%rsp), %rax
jg 0x64913
incq %r13
movl 0x40(%rbp), %eax
cmpq %rax, %r13
jb 0x648e1
jmp 0x6491f
movq %rcx, %r13
subq %r15, %r13
jmp 0x64972
leaq (%r15,%r13), %rcx
jmp 0x64972
testq %r13, %r13
je 0x64c27
movl 0x40(%rbp), %eax
cmpq %rax, %r13
je 0x64c27
movq 0x88(%rsp), %rax
subq 0x38(%rsp), %rax
movq 0x40(%rsp), %rbp
addq %rax, %rbp
decl %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x3d11b
movslq %eax, %r13
addq %rbp, %r13
movq 0x10(%rsp), %r15
leaq (%r15,%r13), %rcx
movq 0x18(%rsp), %r9
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %r11
movl 0xc(%rsp), %edi
xorl %eax, %eax
testq %r13, %r13
cmovleq %rax, %r13
cmpq %r14, %r13
cmovgeq %r14, %r13
cmpq %r14, %rcx
cmovlq %rcx, %r14
movq %r14, %rcx
subq %r13, %rcx
cmpq %rcx, %r15
jne 0x64c18
movq 0xb0(%rsp), %rdx
movq 0xa8(%rsp), %rbp
addq 0x88(%rsp), %r9
testq %r12, %r12
je 0x649e1
movq (%r12), %rax
movq 0x10(%rax), %rax
leaq (%r10,%r10,4), %rcx
movq 0x20(%rax,%rcx,8), %rsi
testb %dil, %dil
je 0x64a6c
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x64a6e
movq 0x80(%rsp), %rax
movq (%rbp), %rbp
movq (%rdx), %r12
movq %r15, 0x8(%rax)
movq (%rax), %rsi
cmpq %r15, 0x10(%rax)
movq %r15, 0x10(%rsp)
movq %r9, 0x18(%rsp)
jge 0x64a52
movl %edi, 0xc(%rsp)
movq %rsi, %rdi
movq %r15, %rsi
movq %r8, 0x30(%rsp)
movq %r10, 0x28(%rsp)
movq %r11, 0x20(%rsp)
callq 0x2370
movq 0xb0(%rsp), %rdx
movl 0xc(%rsp), %edi
movq 0x20(%rsp), %r11
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r8
movq %rax, %rsi
movq 0x80(%rsp), %rax
movq %rsi, (%rax)
movq %r15, 0x10(%rax)
cmpq %rbp, %r13
jne 0x64a76
subq %rbp, %r12
movq 0x10(%rsp), %r9
subq %r12, %r9
jle 0x64adc
addq %r12, %rsi
addq %r13, %r12
jmp 0x64a7e
xorl %eax, %eax
addq %r13, %rax
addq %rax, %rsi
jmp 0x64aee
movq %r13, %r12
movq 0x10(%rsp), %r9
movq %rax, %r15
movq %rdx, %rbp
testq %r11, %r11
je 0x64ab0
movq (%r11), %rax
leaq (%r10,%r10,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
movq $0x0, (%rsp)
movzbl %dil, %ecx
xorl %edi, %edi
movq %r12, %r8
callq 0x36049
jmp 0x64aca
testq %r8, %r8
je 0x64aca
movzbl %dil, %ecx
movq %rsi, %rdi
movq %r12, %rsi
movq %r9, %rdx
movq %r10, %r9
callq 0x32ef8
movq (%r15), %rsi
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r9
movq %rbp, %rdx
jmp 0x64ae6
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r9
movq 0xa8(%rsp), %rbp
movq %r13, (%rbp)
movq %r14, (%rdx)
movq %r9, %rdi
movq %r15, %rdx
callq 0x21a0
testl %eax, %eax
je 0x64b0b
xorl %eax, %eax
jmp 0x64c18
movl $0x0, 0x2c(%rbx)
movl %r15d, %ebp
cmpl $0x3fff, %r15d          # imm = 0x3FFF
jb 0x64b8b
movq 0xa8(%rbx), %rcx
movq 0xb8(%rbx), %rax
movl $0x2, %r14d
movl %r15d, %ebp
cmpq 0xb0(%rbx), %rcx
jne 0x64b69
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r14, %rsi
movq %rsi, 0xb0(%rbx)
addq %rsi, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0xa8(%rbx)
movw $0x3fff, (%rax,%rcx,2)  # imm = 0x3FFF
addl $0xffffc001, %ebp       # imm = 0xFFFFC001
movq %rdx, %rcx
cmpl $0x3ffe, %ebp           # imm = 0x3FFE
ja 0x64b35
testl %ebp, %ebp
je 0x64beb
movq 0xa8(%rbx), %rcx
cmpq 0xb0(%rbx), %rcx
jne 0x64bd5
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rcx
jmp 0x64bdc
movq 0xb8(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, 0xa8(%rbx)
movw %bp, (%rax,%rcx,2)
movl %r15d, 0x18(%rbx)
movl %r15d, 0x24(%rbx)
movl %r13d, 0x10(%rbx)
leal (%r15,%r13), %eax
decl %eax
movl %eax, 0x14(%rbx)
movq 0x88(%rsp), %rax
movl %eax, 0x1c(%rbx)
addl %r15d, %eax
decl %eax
movl %eax, 0x20(%rbx)
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x602c