// File: chhylp123[P]hifiasm/Correct.cpp
// Function: hc_aln_exz(overlap_region*, Candidates_list*, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, long, long, long, long, long, long, long, bit_extz_t*, long, double, long, long)
void hc_aln_exz(overlap_region *z, Candidates_list *cl, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, 
char* qstr, UC_Read *tu, int64_t qs, int64_t qe, int64_t ts, int64_t te, int64_t estimate_err,
int64_t mode, int64_t wl, bit_extz_t *exz, int64_t q_tot, double e_rate, int64_t maxl, int64_t maxe)
{
    clear_align(*exz);
    int64_t thre, ql = qe - qs, thre0, t_tot, id = z->y_id; 
    if(((ts == -1) && (te == -1))) mode = 3;///set to semi-global
    
    if(mode == 1 || mode == 2) {
        if(hpc_g) t_tot = hpc_len(*hpc_g, id);
        else if(uref) t_tot = uref->ug->u.a[id].len;
        else t_tot = Get_READ_LENGTH((*rref), id);
        // fprintf(stderr, "\n+[M::%s::ql->%ld::tl->%ld::mode->%ld] q::[%ld, %ld), t::[%ld, %ld), zx[%d, %d), zy[%d, %d)\n", 
        // __func__, q_tot, t_tot, mode, qs, qe, ts, te, z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1);
        ///find a aligned window >= qe to convert extension to global; or do extension directly
        adjust_specific_ext_offset(z, uref, hpc_g, rref, exz, qstr, tu, &qs, &qe, &ts, &te, q_tot, t_tot, wl, &mode);
        // fprintf(stderr, "-[M::%s::ql->%ld::tl->%ld::mode->%ld] q::[%ld, %ld), t::[%ld, %ld), zx[%d, %d), zy[%d, %d)\n", 
        // __func__, q_tot, t_tot, mode, qs, qe, ts, te, z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1);
    }

    // fprintf(stderr, "[M::%s::ql::%ld] qs::%ld, qe::%ld, ts::%ld, te::%ld, mode::%ld, estimate_err::%ld, e_rate::%f", 
    //                                                     __func__, ql, qs, qe, ts, te, mode, estimate_err, e_rate);

    if(ql <= maxl && (estimate_err*1.2) <= maxe) {
        thre = scale_ed_thre(estimate_err, maxe); if(thre > ql) thre = ql;
        if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
            // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(+)\n", exz->err, exz->thre, thre);
            return;
        }

        thre0 = thre; thre = ql*e_rate;
        thre = scale_ed_thre(thre, maxe); if(thre > ql) thre = ql;
        if(thre > thre0) {
            if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(-)\n", exz->err, exz->thre, thre);
                return;
            }
        }

        thre0 = thre; thre <<= 1;
        thre = scale_ed_thre(thre, maxe); if(thre > ql) thre = ql;
        if(thre > thre0) {
            if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(-)\n", exz->err, exz->thre, thre);
                return;
            }
        }

        thre0 = thre; thre = ql*0.51;
        thre = scale_ed_thre(thre, maxe); if(thre > ql) thre = ql;
        if(thre > thre0) {
            if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(*)\n", exz->err, exz->thre, thre);
                return;
            }
        }
    }
    // fprintf(stderr, ", err::%d, thre::%d\n", INT32_MAX, exz->thre);
    // anchor_aln(z, cl, uref, hpc_g, rref, qstr, tu, qs, qe, ts, te, thre, mode, wl, exz, q_tot, A_L);
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x68(%rbp), %rax
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
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, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movq 0x50(%rbp), %rax
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movq 0x20(%rbp), %rax
subq 0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x88(%rbp)
cmpq $-0x1, 0x28(%rbp)
jne 0x5922c
cmpq $-0x1, 0x30(%rbp)
jne 0x5922c
movq $0x3, 0x40(%rbp)
cmpq $0x1, 0x40(%rbp)
je 0x5923e
cmpq $0x2, 0x40(%rbp)
jne 0x5933a
cmpq $0x0, -0x48(%rbp)
je 0x5926c
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x88(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
sarl %eax
cltq
movq %rax, -0x80(%rbp)
jmp 0x592b1
cmpq $0x0, -0x40(%rbp)
je 0x59298
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, -0x88(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x80(%rbp)
jmp 0x592af
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
jmp 0x592b1
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq 0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, -0x98(%rbp)
movq 0x58(%rbp), %rbx
movq -0x80(%rbp), %r11
movq 0x48(%rbp), %r10
leaq 0x18(%rbp), %r13
leaq 0x20(%rbp), %r12
leaq 0x28(%rbp), %r15
leaq 0x30(%rbp), %r14
leaq 0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x98(%rbp), %rax
movq %rax, (%rsp)
movq -0x90(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0x58350
movq -0x70(%rbp), %rax
cmpq 0x60(%rbp), %rax
jg 0x59637
cvtsi2sdq 0x38(%rbp), %xmm1
movsd 0x328f5a(%rip), %xmm0   # 0x3822b0
mulsd %xmm0, %xmm1
cvtsi2sdq 0x68(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x59637
movq 0x38(%rbp), %rax
movl %eax, %edi
movq 0x68(%rbp), %rax
movl %eax, %esi
callq 0xb7290
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x59391
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq 0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r13
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %r15
movq 0x28(%rbp), %r14
movq 0x30(%rbp), %rbx
movq -0x68(%rbp), %r11
movq 0x58(%rbp), %r10
movq 0x40(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x58c20
cmpq $0x0, %rax
je 0x59400
jmp 0x59637
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
cvtsi2sdq -0x70(%rbp), %xmm0
mulsd -0x60(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl %eax, %edi
movq 0x68(%rbp), %rax
movl %eax, %esi
callq 0xb7290
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x59443
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x594be
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq 0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r13
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %r15
movq 0x28(%rbp), %r14
movq 0x30(%rbp), %rbx
movq -0x68(%rbp), %r11
movq 0x58(%rbp), %r10
movq 0x40(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x58c20
cmpq $0x0, %rax
je 0x594bc
jmp 0x59637
jmp 0x594be
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
shlq %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl %eax, %edi
movq 0x68(%rbp), %rax
movl %eax, %esi
callq 0xb7290
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x594f8
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x59573
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq 0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r13
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %r15
movq 0x28(%rbp), %r14
movq 0x30(%rbp), %rbx
movq -0x68(%rbp), %r11
movq 0x58(%rbp), %r10
movq 0x40(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x58c20
cmpq $0x0, %rax
je 0x59571
jmp 0x59637
jmp 0x59573
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
cvtsi2sdq -0x70(%rbp), %xmm0
movsd 0x328d2f(%rip), %xmm1   # 0x3822b8
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl %eax, %edi
movq 0x68(%rbp), %rax
movl %eax, %esi
callq 0xb7290
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jle 0x595bd
movq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x59635
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq 0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r13
movq 0x18(%rbp), %r12
movq 0x20(%rbp), %r15
movq 0x28(%rbp), %r14
movq 0x30(%rbp), %rbx
movq -0x68(%rbp), %r11
movq 0x58(%rbp), %r10
movq 0x40(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
callq 0x58c20
cmpq $0x0, %rax
je 0x59633
jmp 0x59637
jmp 0x59635
jmp 0x59637
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)