// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %r8, %rbx
movq %rcx, %r8
movq %rdx, %r13
movq %rsi, %r11
movq %rdi, %rsi
movq 0xb0(%rsp), %rax
movq 0x80(%rsp), %r15
movabsq $0x7fffffff00000000, %rcx # imm = 0x7FFFFFFF00000000
movq %rcx, 0x28(%rbx)
movq $0x0, 0xa8(%rbx)
movslq 0x10(%rdi), %r10
testq %rdx, %rdx
je 0x64cad
movq (%r13), %rcx
movq 0x10(%rcx), %rcx
leaq (%r10,%r10,4), %rdx
movl (%rcx,%rdx,8), %ebp
shrl %ebp
andl $0x3fffffff, %ebp       # imm = 0x3FFFFFFF
jmp 0x64cd0
testq %r11, %r11
je 0x64cc8
movq (%r11), %rcx
movq 0x10(%rcx), %rcx
leaq (%r10,%r10,4), %rdx
movl (%rcx,%rdx,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
jmp 0x64cd0
movq 0x18(%r8), %rcx
movq (%rcx,%r10,8), %rbp
movq 0x88(%rsp), %r12
movl 0x1c(%rsi), %edi
subq 0x78(%rsp), %r15
cmpq $0x1, %rax
je 0x64d5e
movq 0x90(%rsp), %rcx
cmpq $0x2, %rax
je 0x64d56
cmpq $0x3, %rax
jne 0x64d62
movl 0x4(%rsi), %eax
movl 0x14(%rsi), %r14d
movl %edi, 0xc(%rsp)
movq 0x78(%rsp), %rdi
movq %rdi, %rcx
subq %rax, %rcx
addq %rcx, %r14
addq $0x38, %rsi
movq %r9, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq %r8, %r15
movq %r10, %r12
movq %r11, 0x20(%rsp)
callq 0x48df6
movl 0xc(%rsp), %edi
movq 0x20(%rsp), %r11
movq %r12, %r10
movq %r15, %r8
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r9
movq %rax, %r12
addq %r14, %r12
leaq (%r12,%r15), %rcx
jmp 0x64d62
movq %rcx, %r12
subq %r15, %r12
jmp 0x64d62
leaq (%r15,%r12), %rcx
xorl %eax, %eax
testq %r12, %r12
cmovleq %rax, %r12
cmpq %rbp, %r12
cmovgeq %rbp, %r12
cmpq %rbp, %rcx
cmovlq %rcx, %rbp
movq %rbp, %r14
subq %r12, %r14
cmpq %r14, %r15
jne 0x64ff3
movq 0xa0(%rsp), %rdx
movq 0x98(%rsp), %rax
addq 0x78(%rsp), %r9
testq %r13, %r13
je 0x64dce
movq (%r13), %rax
movq 0x10(%rax), %rax
leaq (%r10,%r10,4), %rcx
movq 0x20(%rax,%rcx,8), %rsi
testb %dil, %dil
je 0x64e4f
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x64e51
movq %r9, 0x18(%rsp)
movq 0x70(%rsp), %rcx
movq (%rax), %r13
movq (%rdx), %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rcx)
cmpq %r14, 0x10(%rcx)
jge 0x64e32
movl %edi, 0xc(%rsp)
movq (%rcx), %rdi
movq %r14, %rsi
movq %r8, 0x30(%rsp)
movq %r10, 0x28(%rsp)
movq %r11, 0x20(%rsp)
callq 0x2370
movq 0x70(%rsp), %rcx
movq 0xa0(%rsp), %rdx
movl 0xc(%rsp), %edi
movq 0x20(%rsp), %r11
movq 0x28(%rsp), %r10
movq 0x30(%rsp), %r8
movq %rax, (%rcx)
movq %r14, 0x10(%rcx)
movq (%rcx), %rsi
cmpq %r13, %r12
jne 0x64e59
movq 0x10(%rsp), %rax
subq %r13, %rax
movq %r14, %r9
subq %rax, %r9
jle 0x64ec1
addq %rax, %rsi
jmp 0x64e61
xorl %eax, %eax
addq %rax, %rsi
addq %r12, %rsi
jmp 0x64ec6
movq %r12, 0x10(%rsp)
movq %r14, %r9
movq %rcx, %r13
testq %r11, %r11
je 0x64e92
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 0x10(%rsp), %r8
callq 0x36b0d
jmp 0x64eae
testq %r8, %r8
je 0x64eae
movzbl %dil, %ecx
movq %rsi, %rdi
movq 0x10(%rsp), %rsi
movq %r9, %rdx
movq %r10, %r9
callq 0x337f7
movq (%r13), %rsi
movq 0x18(%rsp), %r9
movq 0xa0(%rsp), %rdx
jmp 0x64ec6
movq 0x18(%rsp), %r9
movq 0x98(%rsp), %rax
movq %r12, (%rax)
movq %rbp, (%rdx)
movq %r9, %rdi
movq %r15, %rdx
callq 0x21a0
testl %eax, %eax
je 0x64eea
xorl %eax, %eax
jmp 0x64ff3
movl $0x0, 0x2c(%rbx)
movl %r15d, %ebp
cmpl $0x3fff, %r15d          # imm = 0x3FFF
jb 0x64f6b
movl $0x2, %r13d
movl %r15d, %ebp
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x64f3e
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw $0x3fff, (%rcx,%rax,2)  # imm = 0x3FFF
addl $0xffffc001, %ebp       # imm = 0xFFFFC001
cmpl $0x3ffe, %ebp           # imm = 0x3FFE
ja 0x64f06
testl %ebp, %ebp
je 0x64fc9
movq 0xb0(%rbx), %rax
cmpq %rax, 0xa8(%rbx)
jne 0x64fac
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0xb0(%rbx)
movq 0xb8(%rbx), %rdi
addq %rsi, %rsi
callq 0x2370
movq %rax, 0xb8(%rbx)
movq 0xa8(%rbx), %rax
movq 0xb8(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0xa8(%rbx)
movw %bp, (%rcx,%rax,2)
movl %r14d, 0x18(%rbx)
movl %r15d, 0x24(%rbx)
movl %r12d, 0x10(%rbx)
leal (%r14,%r12), %eax
decl %eax
movl %eax, 0x14(%rbx)
movq 0x78(%rsp), %rax
movl %eax, 0x1c(%rbx)
addl %r15d, %eax
decl %eax
movl %eax, 0x20(%rbx)
movl $0x1, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq