// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x88, %rsp
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x38(%rbp), %rax
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movq -0x38(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0xa8(%rax)
movq $-0x1, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x70(%rbp)
movq 0x20(%rbp), %rax
subq 0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5f943
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq -0x70(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
sarl %eax
cltq
movq %rax, -0x58(%rbp)
jmp 0x5f985
cmpq $0x0, -0x20(%rbp)
je 0x5f96f
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq -0x70(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0x5f983
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
jmp 0x5f985
cmpq $0x3, 0x50(%rbp)
jne 0x5f9cf
movq 0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq -0x18(%rbp), %rcx
movl 0x14(%rcx), %ecx
addq %rcx, %rax
movq %rax, 0x28(%rbp)
movq 0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x38, %rsi
callq 0x331a0
addq 0x28(%rbp), %rax
movq %rax, 0x28(%rbp)
movq 0x28(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, 0x30(%rbp)
jmp 0x5f9fb
cmpq $0x1, 0x50(%rbp)
jne 0x5f9e4
movq 0x28(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, 0x30(%rbp)
jmp 0x5f9f9
cmpq $0x2, 0x50(%rbp)
jne 0x5f9f7
movq 0x30(%rbp), %rax
subq -0x48(%rbp), %rax
movq %rax, 0x28(%rbp)
jmp 0x5f9f9
jmp 0x5f9fb
cmpq $0x0, 0x28(%rbp)
jge 0x5fa0a
movq $0x0, 0x28(%rbp)
movq 0x28(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x5fa1c
movq -0x58(%rbp), %rax
movq %rax, 0x28(%rbp)
movq 0x30(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x5fa2e
movq -0x58(%rbp), %rax
movq %rax, 0x30(%rbp)
movq 0x20(%rbp), %rax
subq 0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x30(%rbp), %rax
subq 0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
je 0x5fa5d
movq $0x0, -0x10(%rbp)
jmp 0x5fbbe
movq -0x40(%rbp), %rax
addq 0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x10(%rbp), %rdi
movq 0x28(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq 0x38(%rbp), %rax
movq (%rax), %rcx
movq 0x40(%rbp), %rax
movq (%rax), %r8
movq 0x38(%rbp), %rax
subq (%rax), %r8
movl -0x6c(%rbp), %eax
movb %al, %r9b
movq -0x20(%rbp), %rbx
movq -0x28(%rbp), %r11
movq -0x30(%rbp), %r10
movslq -0x70(%rbp), %rax
movzbl %r9b, %r9d
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0xc0c80
movq %rax, -0x68(%rbp)
movq 0x28(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
movq 0x30(%rbp), %rcx
movq 0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x20b0
cmpl $0x0, %eax
je 0x5faf9
movq $0x0, -0x10(%rbp)
jmp 0x5fbbe
movq -0x38(%rbp), %rax
movl $0x0, 0x2c(%rax)
movq -0x38(%rbp), %rdi
addq $0xa8, %rdi
movq -0x48(%rbp), %rax
movl %eax, %edx
xorl %esi, %esi
callq 0xc1070
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x50(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x48(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x20(%rax)
movq 0x28(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x10(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x10(%rax)
movq 0x28(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x14(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x14(%rax)
movq 0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x1c(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x1c(%rax)
movq 0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
movslq 0x20(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x20(%rax)
movq $0x1, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0x88, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)