// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_exz_infi_adv(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, bit_extz_t*, char*, UC_Read*, long, long, long, long, long*, long*, long, long*, long, long)
int64_t cal_exz_infi_adv(overlap_region *z, 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 q_tot_l, int64_t mode)
{
    clear_align(*exz);
    int64_t aux_beg = 0, ql, tl, t_tot_l = -1, dd; 
    char *q_string, *t_string; int32_t rev = z->y_pos_strand, id = z->y_id; 
    ql = qe - qs; tl = te - ts; dd = MAX(ql, tl); 
    if(hpc_g) t_tot_l = hpc_len(*hpc_g, id);
    else if(uref) t_tot_l = uref->ug->u.a[id].len;
    else t_tot_l = Get_READ_LENGTH((*rref), id);
   
    if(mode == 3) {
        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(&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 = retrieve_str_seq_exz(tu, ts, tl, (*pts), (*pte)-(*pts), rev, uref, hpc_g, rref, id);
        (*pts) = ts; (*pte) = te;

        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 $0x48, %rsp
movq %r9, %r13
movq %r8, %r15
movq %rcx, 0x10(%rsp)
movq %rdx, %rbp
movq %rdi, %rcx
movq 0x90(%rsp), %r12
movq 0x88(%rsp), %r14
movq 0xa0(%rsp), %rdx
subq 0x98(%rsp), %rdx
movl $0x7fffffff, 0x2c(%r8)  # imm = 0x7FFFFFFF
andq $0x0, 0x28(%rsp)
movq 0xd0(%rsp), %rdi
movslq 0x10(%rcx), %r8
movq %r12, %rax
subq %r14, %rax
cmpq %rdx, %rax
cmovleq %rdx, %rax
testq %rbp, %rbp
movq %r8, 0x38(%rsp)
je 0x339f9
movq (%rbp), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %r8, %r8
movl (%rdx,%r8), %r9d
shrl %r9d
andl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
jmp 0x33a23
testq %rsi, %rsi
je 0x33a16
movq (%rsi), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %r8, %r8
movl (%rdx,%r8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
jmp 0x33a23
movq 0x10(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%r8,8), %r9
movq 0xb8(%rsp), %r10
movl 0x1c(%rcx), %edx
movl %edx, 0x1c(%rsp)
cmpq $0x3, %rdi
movq %rsi, 0x40(%rsp)
jne 0x33a83
cmpq %r10, %rax
cmovgeq %r10, %rax
leaq 0x28(%rsp), %r10
leaq 0xa0(%rsp), %r11
leaq 0x98(%rsp), %rbx
movq %rsi, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rdx
movq %r14, %r8
movq %r12, %r9
pushq %r10
pushq %r11
pushq %rbx
pushq %rax
callq 0x300a0
movq 0xd8(%rsp), %r10
addq $0x20, %rsp
jmp 0x33adf
leaq -0x1(%rdi), %rcx
cmpq $0x1, %rcx
ja 0x33adf
movq 0xc8(%rsp), %r8
cmpq %r10, %rax
cmovgeq %r10, %rax
leaq 0x88(%rsp), %rbx
leaq 0x90(%rsp), %r14
leaq 0x98(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
pushq 0xd0(%rsp)
pushq %rax
movq %r10, %r12
callq 0x3023a
movq %r12, %r10
addq $0x10, %rsp
movq (%r14), %r12
movq (%rbx), %r14
subq %r14, %r12
jle 0x33c5b
movq %r15, 0x20(%rsp)
movq 0xa0(%rsp), %r15
xorl %eax, %eax
cmpq $-0x1, %r15
je 0x33c5d
movq %r13, 0x8(%rsp)
movq 0x98(%rsp), %rcx
movq %r15, %r13
subq %rcx, %r13
jle 0x33c5d
cmpq $-0x1, %rcx
je 0x33c5d
movq 0xc0(%rsp), %rax
cmpq %r13, %r12
movq %r13, %rbx
cmovgq %r12, %rbx
cmpq %r10, %rbx
cmovgeq %r10, %rbx
cmpq (%rax), %rbx
jle 0x33c5b
movq 0xb0(%rsp), %rdx
movq 0xa8(%rsp), %r8
movq 0x80(%rsp), %rdi
movq %rbx, (%rax)
addq %r14, 0x8(%rsp)
movq %rcx, %rsi
movq %rcx, 0x30(%rsp)
movq (%r8), %rcx
movq (%rdx), %r8
subq %rcx, %r8
movzbl 0x1c(%rsp), %r9d
movq %r13, %rdx
pushq 0x38(%rsp)
pushq 0x18(%rsp)
pushq %rbp
pushq 0x58(%rsp)
callq 0x625fc
addq $0x20, %rsp
movq 0x30(%rsp), %rcx
movq %rcx, %rbp
movq 0xa8(%rsp), %rdx
movq %rcx, (%rdx)
movq 0xb0(%rsp), %rcx
movq %r15, (%rcx)
movq 0xd0(%rsp), %rcx
cmpq $0x2, %rcx
je 0x33c08
cmpq $0x1, %rcx
movq 0x20(%rsp), %r15
je 0x33bed
testq %rcx, %rcx
jne 0x33c28
movq %rax, %rdi
movl %r13d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r15, %r9
callq 0x335d2
jmp 0x33c51
movq %rax, %rdi
movl %r13d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq %r15, %r9
callq 0x33658
jmp 0x33c51
movq %rax, %rdi
movl %r13d, %esi
movq 0x8(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
movq 0x20(%rsp), %r15
movq %r15, %r9
callq 0x336de
jmp 0x33c51
cmpq $0x3, %rcx
jne 0x33c51
movl 0x28(%rsp), %r9d
subq $0x8, %rsp
movq %rax, %rdi
movl %r13d, %esi
movq 0x10(%rsp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
pushq %r15
callq 0x33764
popq %rax
popq %rcx
movl 0x2c(%r15), %eax
cmpl 0x28(%r15), %eax
jle 0x33c6c
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r15), %xmm0
movd %ebp, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x10(%r15)
movq 0x1c(%r15), %xmm0
movd %r14d, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x1c(%r15)
pushq $0x1
popq %rax
jmp 0x33c5d