// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, %rax
movq %rsi, %r15
movq 0x108(%rsp), %rcx
movq 0x100(%rsp), %r10
movq 0xc0(%rsp), %r13
movq 0x98(%rsp), %r12
movq 0x90(%rsp), %r14
movq 0xa8(%rsp), %rsi
subq 0xa0(%rsp), %rsi
movq %rax, 0x10(%rsp)
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movq $0x0, 0x28(%rsp)
movq %r12, %rax
subq %r14, %rax
cmpq %rsi, %rax
cmovleq %rsi, %rax
cmpq $0x3, %r10
setne %r8b
testq %rcx, %rcx
sete %sil
orb %r8b, %sil
movq %rdx, 0x48(%rsp)
movq %rdi, 0x50(%rsp)
jne 0x65022
cmpq %r13, %rax
cmovgeq %r13, %rax
movq %r10, %rbx
leaq 0x28(%rsp), %r10
leaq 0xa8(%rsp), %r11
leaq 0xa0(%rsp), %rbp
movq %r15, %rsi
movq %r14, %r8
movq %r12, %r9
pushq %r10
pushq %r11
pushq %rbp
pushq %rax
callq 0x60281
movq %rbx, %r10
addq $0x20, %rsp
jmp 0x65090
leaq -0x1(%r10), %rcx
cmpq $0x1, %rcx
ja 0x65090
movq 0xd8(%rsp), %r9
movq 0xd0(%rsp), %r8
cmpq %r13, %rax
cmovgeq %r13, %rax
leaq 0x90(%rsp), %r14
leaq 0x98(%rsp), %r12
leaq 0xa0(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
movq %r14, %rdi
movq %r12, %rsi
pushq %r10
pushq %rax
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
movq %r10, %rbx
callq 0x64eab
movq %rbx, %r10
addq $0x20, %rsp
movq (%r12), %r12
movq (%r14), %r14
subq %r14, %r12
jle 0x65370
movq 0xa8(%rsp), %rbp
xorl %eax, %eax
cmpq $-0x1, %rbp
je 0x65372
movq 0xa0(%rsp), %rsi
movq %rbp, %r11
subq %rsi, %r11
jle 0x65372
cmpq $-0x1, %rsi
je 0x65372
movq 0xc8(%rsp), %rax
cmpq %r11, %r12
movq %r11, %r8
cmovgq %r12, %r8
cmpq %r13, %r8
cmovgeq %r13, %r8
cmpq (%rax), %r8
jle 0x65370
movq 0x110(%rsp), %rdi
movl 0xf8(%rsp), %ecx
movl 0xf0(%rsp), %ebx
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %r13
movq %r8, (%rax)
movslq %ecx, %rdx
testq %r15, %r15
movq %rsi, 0x8(%rsp)
je 0x6514b
movq (%r15), %rax
movq 0x10(%rax), %rcx
leaq (%rdx,%rdx,4), %rdx
movq 0x20(%rcx,%rdx,8), %rax
testb %bl, %bl
je 0x651c7
leaq (%rcx,%rdx,8), %rcx
movl (%rcx), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x651c9
movq (%r13), %r13
movq (%r9), %r15
movq 0x20(%rsp), %rcx
movq %r11, 0x8(%rcx)
movq (%rcx), %rax
cmpq %r11, 0x10(%rcx)
movq %r8, 0x30(%rsp)
jge 0x651ab
movq %rax, %rdi
movq %r11, %rsi
movq %r10, %rbx
movq %r11, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
callq 0x2370
movq 0x38(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %r11
movq 0x30(%rsp), %r8
movq %rbx, %r10
movl 0xf0(%rsp), %ebx
movq 0x8(%rsp), %rsi
movq %rax, (%rcx)
movq %r11, 0x10(%rcx)
cmpq %r13, %rsi
jne 0x651de
subq %r13, %r15
movq %r11, %r9
subq %r15, %r9
jle 0x65276
addq %r15, %rax
addq %rsi, %r15
jmp 0x651e4
xorl %ecx, %ecx
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rdx
addq %rsi, %rcx
addq %rcx, %rax
jmp 0x65298
movq %rsi, %r15
movq %r11, %r9
movq %r10, %r13
movq 0x50(%rsp), %rcx
testq %rcx, %rcx
je 0x6522c
movq (%rcx), %rcx
leaq (%rdx,%rdx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rcx), %rdx
subq $0x8, %rsp
movzbl %bl, %ecx
xorl %edi, %edi
movq %rax, %rsi
movq %r15, %r8
pushq $0x0
movq %r11, %rbx
callq 0x36049
movq %rbx, %r11
movq 0x18(%rsp), %rsi
addq $0x10, %rsp
movq 0x10(%rsp), %rbx
jmp 0x6525f
movl %ebx, %ecx
movq 0x48(%rsp), %r8
testq %r8, %r8
movq 0x10(%rsp), %rbx
je 0x6525f
movzbl %cl, %ecx
movq %rax, %rdi
movq %r15, %rsi
movq %rdx, %rax
movq %r9, %rdx
movq %rax, %r9
movq %r11, %r15
callq 0x32ef8
movq %r15, %r11
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rsp), %rdx
movq %r13, %r10
movq 0x30(%rsp), %r8
jmp 0x65280
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rdx
movq 0x110(%rsp), %rdi
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %r13
addq %r14, %rdx
movq %rsi, (%r13)
movq %rbp, (%r9)
testq %rdi, %rdi
je 0x652ce
cmpq $0x2, %r10
je 0x65309
cmpq $0x1, %r10
je 0x652f6
testq %r10, %r10
jne 0x6531c
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movq %rbx, %r9
callq 0x63d6a
jmp 0x65363
cmpq $0x2, %r10
je 0x65352
cmpq $0x1, %r10
je 0x6533f
testq %r10, %r10
jne 0x653b7
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movq %rbx, %r9
callq 0x63dad
jmp 0x65363
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movq %rbx, %r9
callq 0x63df0
jmp 0x65363
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movq %rbx, %r9
callq 0x63e76
jmp 0x65363
cmpq $0x3, %r10
jne 0x65368
movl 0x28(%rsp), %r9d
subq $0x8, %rsp
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
pushq %rbx
callq 0x63efc
jmp 0x653d5
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movq %rbx, %r9
callq 0x63e33
jmp 0x65363
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movq %rbx, %r9
callq 0x63eb9
movq 0x8(%rsp), %rsi
movl 0x2c(%rbx), %eax
cmpl 0x28(%rbx), %eax
jle 0x65381
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %xmm0
movd %esi, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x10(%rbx)
movq 0x1c(%rbx), %xmm0
movd %r14d, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x1c(%rbx)
movl $0x1, %eax
jmp 0x65372
cmpq $0x3, %r10
jne 0x65368
movl 0x28(%rsp), %r9d
subq $0x8, %rsp
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
pushq %rbx
callq 0x63f43
movq 0x18(%rsp), %rsi
addq $0x10, %rsp
jmp 0x65368