// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movq $0x0, -0x40(%rbp)
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x18(%rbp), %rax
subq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x5f55e
movq -0x48(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x5f566
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x3, 0x40(%rbp)
jne 0x5f5e8
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x20(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x5f5a7
movq -0x58(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x5f5b2
movq 0x20(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rdx
movq -0x80(%rbp), %rsi
movq -0x88(%rbp), %rdi
movq -0x90(%rbp), %rcx
movq 0x38(%rbp), %r8
leaq 0x10(%rbp), %r9
leaq 0x18(%rbp), %r10
leaq -0x40(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x580d0
jmp 0x5f667
cmpq $0x1, 0x40(%rbp)
je 0x5f5f6
cmpq $0x2, 0x40(%rbp)
jne 0x5f665
movq 0x30(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0x98(%rbp)
movq 0x20(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x5f623
movq -0x58(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x5f62e
movq 0x20(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x98(%rbp), %r9
movq -0xa0(%rbp), %r8
movq -0xa8(%rbp), %r10
movq 0x40(%rbp), %rax
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq 0x10(%rbp), %rdx
leaq 0x18(%rbp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x581e0
jmp 0x5f667
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jle 0x5f85f
movq 0x18(%rbp), %rax
cmpq 0x10(%rbp), %rax
jle 0x5f85f
cmpq $-0x1, 0x10(%rbp)
je 0x5f85f
cmpq $-0x1, 0x18(%rbp)
je 0x5f85f
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x18(%rbp), %rax
subq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x5f6c8
movq -0x48(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x5f6d3
movq -0x50(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq 0x20(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x5f6f0
movq -0x58(%rbp), %rax
movq %rax, 0x20(%rbp)
movq 0x20(%rbp), %rax
movq 0x28(%rbp), %rcx
cmpq (%rcx), %rax
jg 0x5f70a
movq $0x0, -0x8(%rbp)
jmp 0x5f867
movq 0x20(%rbp), %rcx
movq 0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
addq 0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, 0x40(%rbp)
jne 0x5f75d
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x60(%rbp), %rdx
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %eax, %r8d
movq -0x18(%rbp), %r9
callq 0x5e760
jmp 0x5f7f3
cmpq $0x1, 0x40(%rbp)
jne 0x5f78a
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x60(%rbp), %rdx
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %eax, %r8d
movq -0x18(%rbp), %r9
callq 0x5e8e0
jmp 0x5f7f1
cmpq $0x2, 0x40(%rbp)
jne 0x5f7b7
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x60(%rbp), %rdx
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %eax, %r8d
movq -0x18(%rbp), %r9
callq 0x5ea60
jmp 0x5f7ef
cmpq $0x3, 0x40(%rbp)
jne 0x5f7ed
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %esi
movq -0x60(%rbp), %rdx
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %eax, %r8d
movq -0x40(%rbp), %rax
movl %eax, %r9d
movq -0x18(%rbp), %rax
movq %rax, (%rsp)
callq 0x5ebe0
jmp 0x5f7ef
jmp 0x5f7f1
jmp 0x5f7f3
movq -0x18(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x18(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jg 0x5f855
movq 0x10(%rbp), %rdx
movq -0x18(%rbp), %rax
movslq 0x10(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x10(%rax)
movq 0x10(%rbp), %rdx
movq -0x18(%rbp), %rax
movslq 0x14(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x14(%rax)
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movslq 0x1c(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x1c(%rax)
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movslq 0x20(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x20(%rax)
movq $0x1, -0x8(%rbp)
jmp 0x5f867
movq $0x0, -0x8(%rbp)
jmp 0x5f867
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)