// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_exz_infi_non_retrieve_adv(overlap_region*, bit_extz_t*, char*, char*, long, long, long, long, long, long*, long, long, long)
int64_t cal_exz_infi_non_retrieve_adv(overlap_region *z, bit_extz_t *exz, char* qstr, char* tstr, int64_t qs, int64_t qe, int64_t ts, int64_t te, int64_t thre, int64_t *pthre, int64_t q_tot_l, int64_t t_tot_l, int64_t mode)
{
    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) {
        update_semi_non_retrieve_coord(z, qs, qe, ((thre>dd)?dd:thre), t_tot_l, &ts, &te, &aux_beg);
    } else if(mode == 1 || mode == 2) {
        adjust_ext_offset(&qs, &qe, &ts, &te, q_tot_l, t_tot_l, ((thre>dd)?dd:thre), mode);
    }
    
    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(thre <= (*pthre)) return 0;
        (*pthre) = thre;
        
        q_string = qstr + qs; t_string = tstr + ts;

        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)) {
            // cigar_check(t_string, q_string, exz); 
            // if(mode == 1) {
            //     fprintf(stderr, "\n[M::%s::ql::%ld] qs::%ld, qe::%ld, ts::%ld, te::%ld, mode::%ld, err::%d, thre::%d, exz_q[%d, %d], exz_t[%d, %d]\n", 
            //                      __func__, ql, qs, qe, ts, te, mode, exz->err, exz->thre, exz->ts, exz->te, exz->ps, exz->pe);
            //     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);
            //     fprintf(stderr, "[M::%s::] exz->cigar.n::%d\n", __func__, (int32_t)exz->cigar.n);
            // }
            exz->ps += ts; exz->pe += ts;
            exz->ts += qs; exz->te += qs;
            return 1;
        }
        return 0;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, %r15
movq %rsi, %rbx
movq 0x90(%rsp), %r13
movq 0x88(%rsp), %r10
movq 0x70(%rsp), %r14
movq 0x68(%rsp), %rcx
movq %r8, 0x18(%rsp)
movq %r9, 0x10(%rsp)
subq 0x60(%rsp), %rcx
movl $0x7fffffff, 0x2c(%rsi) # imm = 0x7FFFFFFF
movq $0x0, 0x8(%rsp)
movq %r9, %rax
subq %r8, %rax
cmpq %rcx, %rax
cmovleq %rcx, %rax
cmpq $0x3, %r13
jne 0x64b22
cmpq %r14, %rax
cmovgeq %r14, %rax
leaq 0x8(%rsp), %r12
leaq 0x68(%rsp), %rbp
leaq 0x60(%rsp), %r11
movq %r8, %rsi
movq %r9, %rdx
movq %rax, %rcx
movq %r10, %r8
movq %r11, %r9
pushq %r12
pushq %rbp
callq 0x60ac5
jmp 0x64b5a
leaq -0x1(%r13), %rcx
cmpq $0x1, %rcx
ja 0x64b5e
movq 0x80(%rsp), %r8
cmpq %r14, %rax
cmovgeq %r14, %rax
leaq 0x18(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x60(%rsp), %rdx
leaq 0x68(%rsp), %rcx
movq %r10, %r9
pushq %r13
pushq %rax
callq 0x60b76
addq $0x10, %rsp
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r12
subq %r12, %rcx
jle 0x64c1f
movq 0x68(%rsp), %rsi
xorl %eax, %eax
cmpq $-0x1, %rsi
je 0x64c21
movq 0x60(%rsp), %rbp
subq %rbp, %rsi
jle 0x64c21
cmpq $-0x1, %rbp
je 0x64c21
movq 0x78(%rsp), %rax
cmpq %rsi, %rcx
movq %rsi, %r8
cmovgq %rcx, %r8
cmpq %r14, %r8
cmovgeq %r14, %r8
cmpq (%rax), %r8
jle 0x64c1f
movq %r8, (%rax)
addq %r12, %r15
movq 0x20(%rsp), %rdi
addq %rbp, %rdi
cmpq $0x2, %r13
je 0x64bee
cmpq $0x1, %r13
je 0x64be1
testq %r13, %r13
jne 0x64bfb
movq %r15, %rdx
movq %rbx, %r9
callq 0x6424a
jmp 0x64c17
movq %r15, %rdx
movq %rbx, %r9
callq 0x642d0
jmp 0x64c17
movq %r15, %rdx
movq %rbx, %r9
callq 0x64356
jmp 0x64c17
cmpq $0x3, %r13
jne 0x64c17
movl 0x8(%rsp), %r9d
subq $0x8, %rsp
movq %r15, %rdx
pushq %rbx
callq 0x643dc
addq $0x10, %rsp
movl 0x2c(%rbx), %eax
cmpl 0x28(%rbx), %eax
jle 0x64c30
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %ebp, 0x10(%rbx)
addl %ebp, 0x14(%rbx)
addl %r12d, 0x1c(%rbx)
addl %r12d, 0x20(%rbx)
movl $0x1, %eax
jmp 0x64c21