// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdx, %rbp
movq %rsi, %rbx
movq 0x108(%rsp), %rcx
movq 0x100(%rsp), %r10
movq 0xc0(%rsp), %r12
movq 0x98(%rsp), %r9
movq 0x90(%rsp), %r8
movq 0xa8(%rsp), %rdx
subq 0xa0(%rsp), %rdx
movl $0x7fffffff, 0x2c(%r13) # imm = 0x7FFFFFFF
movq $0x0, 0x28(%rsp)
movq %r9, %rax
subq %r8, %rax
cmpq %rdx, %rax
cmovleq %rdx, %rax
cmpq $0x3, %r10
setne %dl
testq %rcx, %rcx
sete %sil
orb %dl, %sil
movq %rbp, 0x50(%rsp)
movq %rdi, 0x40(%rsp)
jne 0x65375
cmpq %r12, %rax
cmovgeq %r12, %rax
movq %r10, %r14
leaq 0x28(%rsp), %r10
leaq 0xa8(%rsp), %r11
leaq 0xa0(%rsp), %r15
movq %rbx, %rsi
movq %rbp, %rdx
pushq %r10
pushq %r11
pushq %r15
pushq %rax
callq 0x609cc
jmp 0x653cf
leaq -0x1(%r10), %rcx
cmpq $0x1, %rcx
ja 0x653d6
movq 0xd8(%rsp), %r9
movq 0xd0(%rsp), %r8
cmpq %r12, %rax
cmovgeq %r12, %rax
leaq 0x90(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
pushq %r10
pushq %rax
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
movq %r10, %r14
callq 0x65209
movq %r14, %r10
addq $0x20, %rsp
movq 0x98(%rsp), %r15
movq 0x90(%rsp), %rdi
subq %rdi, %r15
jle 0x656f0
movq %r13, 0x38(%rsp)
movq 0xa8(%rsp), %r13
xorl %eax, %eax
cmpq $-0x1, %r13
je 0x656f2
movq 0xa0(%rsp), %rbp
movq %r13, %r11
subq %rbp, %r11
jle 0x656f2
cmpq $-0x1, %rbp
je 0x656f2
movq 0xc8(%rsp), %rax
cmpq %r11, %r15
movq %r11, %r14
cmovgq %r15, %r14
cmpq %r12, %r14
cmovgeq %r12, %r14
cmpq (%rax), %r14
jle 0x656f0
movq 0x110(%rsp), %r8
movl 0xf8(%rsp), %ecx
movl 0xf0(%rsp), %edx
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %r12
movq %r14, (%rax)
movslq %ecx, %rcx
testq %rbx, %rbx
movq %rdi, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
je 0x654ad
movq (%rbx), %rax
movq 0x10(%rax), %rax
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rax,%rcx,8), %rsi
testb %dl, %dl
je 0x6552b
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x6552d
movq %rcx, 0x30(%rsp)
movq (%r12), %r12
movq (%r9), %rbx
movq 0x20(%rsp), %rcx
movq %r11, 0x8(%rcx)
cmpq %r11, 0x10(%rcx)
jge 0x65507
movq (%rcx), %rdi
movq %r11, %rsi
movq %r14, 0x48(%rsp)
movq %r10, %r14
movq %rbx, %rbp
movq %r11, %rbx
callq 0x2370
movl 0xf0(%rsp), %edx
movq 0x20(%rsp), %rcx
movq %rbx, %r11
movq %rbp, %rbx
movq %r14, %r10
movq 0x48(%rsp), %r14
movq 0x8(%rsp), %rbp
movq %rax, (%rcx)
movq %r11, 0x10(%rcx)
movq (%rcx), %rsi
cmpq %r12, %rbp
jne 0x6553d
movq %rbx, %rax
subq %r12, %rax
movq %r11, %r9
subq %rax, %r9
movq 0x40(%rsp), %rcx
jle 0x655ce
addq %rax, %rsi
jmp 0x65548
xorl %eax, %eax
movq 0x18(%rsp), %rdx
addq %rbp, %rax
addq %rax, %rsi
jmp 0x655f0
movq %rbp, %rbx
movq %r11, %r9
movq 0x40(%rsp), %rcx
movl %edx, %edi
movq %r10, %r12
testq %rcx, %rcx
je 0x6558b
movq (%rcx), %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl %dil, %ecx
xorl %edi, %edi
movq %rbx, %r8
pushq $0x0
movq %r11, %rbx
callq 0x36b0d
movq %rbx, %r11
movq 0x18(%rsp), %rbp
addq $0x10, %rsp
jmp 0x655b7
movq 0x50(%rsp), %r8
testq %r8, %r8
je 0x655b7
movzbl %dil, %ecx
movq %rsi, %rdi
movq %rbx, %rsi
movq %r9, %rdx
movq 0x30(%rsp), %r9
movq %r11, %rbx
callq 0x337f7
movq %rbx, %r11
movq 0x8(%rsp), %rbp
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rdi
movq %r12, %r10
jmp 0x655d8
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rdi
movq 0x110(%rsp), %r8
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %r12
addq %rdi, %rdx
movq %rbp, (%r12)
movq %r13, (%r9)
testq %r8, %r8
je 0x6562e
cmpq $0x2, %r10
movq 0x38(%rsp), %rbx
je 0x65678
cmpq $0x1, %r10
je 0x65662
testq %r10, %r10
jne 0x6568e
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x6424a
jmp 0x656de
cmpq $0x2, %r10
movq 0x38(%rsp), %rbx
je 0x656ca
cmpq $0x1, %r10
je 0x656b4
testq %r10, %r10
jne 0x65714
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x6428d
jmp 0x656de
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x642d0
jmp 0x656de
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x64356
jmp 0x656de
cmpq $0x3, %r10
jne 0x656e8
movl 0x28(%rsp), %r9d
subq $0x8, %rsp
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
pushq %rbx
callq 0x643dc
jmp 0x65735
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x64313
jmp 0x656de
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x64399
movq 0x8(%rsp), %rbp
movq 0x10(%rsp), %rdi
movl 0x2c(%rbx), %eax
cmpl 0x28(%rbx), %eax
jle 0x65701
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %ebp, 0x10(%rbx)
addl %ebp, 0x14(%rbx)
addl %edi, 0x1c(%rbx)
addl %edi, 0x20(%rbx)
movl $0x1, %eax
jmp 0x656f2
cmpq $0x3, %r10
jne 0x656e8
movl 0x28(%rsp), %r9d
subq $0x8, %rsp
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
pushq %rbx
callq 0x64423
movq 0x18(%rsp), %rbp
movq 0x20(%rsp), %rdi
addq $0x10, %rsp
jmp 0x656e8