// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_exz_infi_simi_adv(ul_idx_t const*, hpc_t*, All_reads*, bit_extz_t*, char*, UC_Read*, long, long, long, long, long*, long*, long, long*, long, long, long, long, int, int, long, overlap_region*, long)
int64_t cal_exz_infi_simi_adv(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 thre, int64_t *pthre, int64_t qmin, int64_t qmax, 
int64_t tmin, int64_t tmax, int32_t rev, int32_t id, int64_t mode, overlap_region *z,
int64_t gen_trace)
{   ///mode cannot be 3
    clear_align(*exz);
    int64_t aux_beg = 0, ql, tl, dd; char *q_string, *t_string; 
    ql = qe - qs; tl = te - ts; dd = MAX(ql, tl); 
    if(mode == 3 && z) {
        update_semi_coord(uref, hpc_g, rref, z, qs, qe, ((thre>dd)?dd:thre), &ts, &te, &aux_beg);
    } else if(mode == 1 || mode == 2) {
        adjust_ext_offset_fixed_t(&qs, &qe, &ts, &te, qmin, qmax, tmin, tmax, ((thre>dd)?dd:thre), mode);
    }

    // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
    //         fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld, pthre::%ld\n", 
    //         __func__, mode, qs, qe, ts, te, thre, *pthre);
    // }
    
    if((qe > qs) && (te > ts) && (ts != -1) && (te != -1)) {
        ql = qe - qs; tl = te - ts; 
        dd = MAX(ql, tl); 
        if(thre > dd) thre = dd; 
        // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld, pthre::%ld\n", 
        //     __func__, mode, qs, qe, ts, te, thre, *pthre);
        // }
        if(thre <= (*pthre)) return 0;
        (*pthre) = thre;
        
        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(!gen_trace) {
            if(mode == 0) { //global
                cal_exz_global_simi(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 1) {///forward extension
                cal_exz_extension_0_simi(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 2) {///backward extension
                cal_exz_extension_1_simi(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 3) {//semi-global; mode cannot be 3
                cal_exz_semi_simi(t_string, tl, q_string, ql, thre, aux_beg, exz);
            }
        } else {
            if(mode == 0) { //global
                cal_exz_global(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 1) {///forward extension
                cal_exz_extension_0(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 2) {///backward extension
                cal_exz_extension_1(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 3) {//semi-global
                cal_exz_semi(t_string, tl, q_string, ql, thre, aux_beg, exz);
            }
        }
        

        if(is_align(*exz)) {
            exz->ps += ts; exz->pe += ts;
            exz->ts += qs; exz->te += qs;
            // if(exz->ps == 18327 && exz->pe + 1 == 18601 && exz->ts == 145990 && exz->te + 1 == 146191) {
            //     fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld)\n", 
            //     __func__, mode, qs, qe, ts, te);
            //     fprintf(stderr, "[M::%s::] q_string::%.*s\n", __func__, (int32_t)ql, q_string);
            //     fprintf(stderr, "[M::%s::] t_string::%.*s\n", __func__, (int32_t)tl, t_string);
            // }
            return 1;
        }
        // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld)\n", 
        //     __func__, mode, qs, qe, ts, te);
        //     fprintf(stderr, "[M::%s::] q_string::%.*s\n", __func__, (int32_t)ql, q_string);
        //     fprintf(stderr, "[M::%s::] t_string::%.*s\n", __func__, (int32_t)tl, t_string);
        // }
        return 0;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x20(%rsp)
movq %r8, %r15
movq %rcx, %rbp
movq 0xe8(%rsp), %rcx
movq 0x78(%rsp), %r12
movq 0x70(%rsp), %r14
movq 0x88(%rsp), %r9
subq 0x80(%rsp), %r9
movl $0x7fffffff, 0x2c(%rbp) # imm = 0x7FFFFFFF
andq $0x0, 0x10(%rsp)
movq 0xe0(%rsp), %r8
movq 0xa0(%rsp), %r13
movq %r12, %rax
subq %r14, %rax
cmpq %r9, %rax
cmovleq %r9, %rax
cmpq $0x3, %r8
setne %r10b
testq %rcx, %rcx
sete %r9b
orb %r10b, %r9b
movq %rdi, (%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
jne 0x34301
cmpq %r13, %rax
cmovgeq %r13, %rax
leaq 0x10(%rsp), %r10
leaq 0x88(%rsp), %r11
leaq 0x80(%rsp), %rbx
movq %r14, %r8
movq %r12, %r9
pushq %r10
pushq %r11
pushq %rbx
pushq %rax
callq 0x300a0
addq $0x20, %rsp
jmp 0x3436d
leaq -0x1(%r8), %rcx
cmpq $0x1, %rcx
ja 0x3436d
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %r8
cmpq %r13, %rax
cmovgeq %r13, %rax
leaq 0x70(%rsp), %r14
movq %r15, %rbx
leaq 0x78(%rsp), %r15
leaq 0x80(%rsp), %rdx
leaq 0x88(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
pushq 0xe0(%rsp)
pushq %rax
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0x341a2
addq $0x20, %rsp
movq (%r15), %r12
movq %rbx, %r15
movq (%r14), %r14
subq %r14, %r12
jle 0x34575
movq %rbp, 0x18(%rsp)
movq 0x88(%rsp), %rbp
xorl %eax, %eax
cmpq $-0x1, %rbp
je 0x34577
movq %r15, 0x8(%rsp)
movq 0x80(%rsp), %rsi
movq %rbp, %r15
subq %rsi, %r15
jle 0x34577
cmpq $-0x1, %rsi
je 0x34577
movq 0xa8(%rsp), %rax
cmpq %r15, %r12
movq %r15, %rbx
cmovgq %r12, %rbx
cmpq %r13, %rbx
cmovgeq %r13, %rbx
cmpq (%rax), %rbx
jle 0x34575
movl 0xd8(%rsp), %edx
movl 0xd0(%rsp), %edi
movq 0x98(%rsp), %r8
movq 0x90(%rsp), %r13
movq %rbx, (%rax)
addq %r14, 0x8(%rsp)
movq (%r13), %rcx
movq (%r8), %r8
subq %rcx, %r8
movslq %edx, %rax
movzbl %dil, %r9d
movq 0x20(%rsp), %rdi
movq %r15, %rdx
pushq %rax
pushq 0x30(%rsp)
pushq 0x40(%rsp)
pushq 0x18(%rsp)
movq %rsi, 0x20(%rsp)
callq 0x625fc
movq 0x20(%rsp), %rcx
addq $0x20, %rsp
movq %rcx, (%r13)
movq 0x98(%rsp), %rdx
movq %rbp, (%rdx)
movq 0xe0(%rsp), %rdx
cmpq $0x0, 0xf0(%rsp)
je 0x34491
cmpq $0x2, %rdx
movq 0x18(%rsp), %r13
je 0x344ec
cmpq $0x1, %rdx
je 0x344d1
testq %rdx, %rdx
jne 0x34507
movq %rax, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r13, %r9
callq 0x335d2
jmp 0x34567
cmpq $0x2, %rdx
movq 0x18(%rsp), %r13
je 0x3454e
cmpq $0x1, %rdx
je 0x34533
testq %rdx, %rdx
jne 0x345be
movq %rax, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r13, %r9
callq 0x33615
jmp 0x34567
movq %rax, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r13, %r9
callq 0x33658
jmp 0x34567
movq %rax, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r13, %r9
callq 0x336de
jmp 0x34567
cmpq $0x3, %rdx
jne 0x3456b
movl 0x10(%rsp), %r9d
subq $0x8, %rsp
movq %rax, %rdi
movl %r15d, %esi
movq 0x10(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
pushq %r13
callq 0x33764
jmp 0x345e5
movq %rax, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r13, %r9
callq 0x3369b
jmp 0x34567
movq %rax, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r13, %r9
callq 0x33721
movq (%rsp), %rcx
movl 0x2c(%r13), %eax
cmpl 0x28(%r13), %eax
jle 0x34586
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r13), %xmm0
movd %ecx, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x10(%r13)
movq 0x1c(%r13), %xmm0
movd %r14d, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x1c(%r13)
pushq $0x1
popq %rax
jmp 0x34577
cmpq $0x3, %rdx
jne 0x3456b
movl 0x10(%rsp), %r9d
subq $0x8, %rsp
movq %rax, %rdi
movl %r15d, %esi
movq 0x10(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
pushq %r13
callq 0x337a9
movq 0x10(%rsp), %rcx
addq $0x10, %rsp
jmp 0x3456b