// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, %r13
movq %r8, %r14
movq %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %rbx
movq 0x90(%rsp), %rbp
movq 0x68(%rsp), %rcx
movq %r8, 0x20(%rsp)
movq %r9, 0x18(%rsp)
subq 0x60(%rsp), %rcx
movl $0x7fffffff, 0x2c(%rsi) # imm = 0x7FFFFFFF
andq $0x0, (%rsp)
movq 0x88(%rsp), %r10
movq 0x70(%rsp), %r12
movq %r9, %rax
subq %r8, %rax
cmpq %rcx, %rax
cmovleq %rcx, %rax
cmpq $0x3, %rbp
jne 0x33d3a
cmpq %r12, %rax
cmovgeq %r12, %rax
movq %rsp, %r11
leaq 0x68(%rsp), %r15
leaq 0x60(%rsp), %r9
movq %r14, %rsi
movq %r13, %rdx
movq %rax, %rcx
movq %r10, %r8
pushq %r11
pushq %r15
callq 0x30191
popq %rcx
popq %rdx
jmp 0x33d80
leaq -0x1(%rbp), %rcx
cmpq $0x1, %rcx
ja 0x33d80
movq 0x80(%rsp), %r8
cmpq %r12, %rax
cmovgeq %r12, %rax
leaq 0x20(%rsp), %r14
leaq 0x18(%rsp), %r13
leaq 0x60(%rsp), %rdx
leaq 0x68(%rsp), %rcx
movq %r14, %rdi
movq %r13, %rsi
movq %r10, %r9
pushq %rbp
pushq %rax
callq 0x3023a
popq %rax
popq %rcx
movq (%r13), %r13
movq (%r14), %r14
subq %r14, %r13
jle 0x33e39
movq 0x68(%rsp), %rsi
xorl %eax, %eax
cmpq $-0x1, %rsi
je 0x33e3b
movq 0x60(%rsp), %r15
subq %r15, %rsi
jle 0x33e3b
cmpq $-0x1, %r15
je 0x33e3b
movq 0x78(%rsp), %rax
cmpq %rsi, %r13
movq %rsi, %r8
cmovgq %r13, %r8
cmpq %r12, %r8
cmovgeq %r12, %r8
cmpq (%rax), %r8
jle 0x33e39
movq %r8, (%rax)
movq 0x10(%rsp), %rdx
addq %r14, %rdx
movq 0x8(%rsp), %rdi
addq %r15, %rdi
cmpq $0x2, %rbp
je 0x33e0b
cmpq $0x1, %rbp
je 0x33dfe
testq %rbp, %rbp
jne 0x33e18
movl %r13d, %ecx
movq %rbx, %r9
callq 0x335d2
jmp 0x33e31
movl %r13d, %ecx
movq %rbx, %r9
callq 0x33658
jmp 0x33e31
movl %r13d, %ecx
movq %rbx, %r9
callq 0x336de
jmp 0x33e31
cmpq $0x3, %rbp
jne 0x33e31
movl (%rsp), %r9d
subq $0x8, %rsp
movl %r13d, %ecx
pushq %rbx
callq 0x33764
popq %rax
popq %rcx
movl 0x2c(%rbx), %eax
cmpl 0x28(%rbx), %eax
jle 0x33e4a
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %xmm0
movd %r15d, %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)
pushq $0x1
popq %rax
jmp 0x33e3b