// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
movq %rdx, %r13
movq %rdi, %r14
movq 0xd0(%rsp), %r10
movq 0x90(%rsp), %r12
movq 0x88(%rsp), %r9
movq 0xa0(%rsp), %rdx
subq 0x98(%rsp), %rdx
movq %r8, 0x10(%rsp)
movl $0x7fffffff, 0x2c(%r8)  # imm = 0x7FFFFFFF
movq $0x0, 0x40(%rsp)
movslq 0x10(%rdi), %rdi
movq %r12, %rax
movq %r9, %r8
subq %r9, %rax
cmpq %rdx, %rax
cmovleq %rdx, %rax
testq %r13, %r13
movq %rcx, 0x38(%rsp)
movq %rdi, 0x20(%rsp)
je 0x64233
movq (%r13), %rdx
movq 0x10(%rdx), %rdx
leaq (%rdi,%rdi,4), %rcx
movl (%rdx,%rcx,8), %r9d
shrl %r9d
andl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
jmp 0x64258
testq %rsi, %rsi
je 0x64250
movq (%rsi), %rdx
movq 0x10(%rdx), %rdx
leaq (%rdi,%rdi,4), %rcx
movl (%rdx,%rcx,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
jmp 0x64258
movq 0x18(%rcx), %rdx
movq (%rdx,%rdi,8), %r9
movq 0xb8(%rsp), %rbp
movl 0x1c(%r14), %ecx
movl %ecx, 0xc(%rsp)
cmpq $0x3, %r10
movq %rsi, 0x30(%rsp)
jne 0x642ba
cmpq %rbp, %rax
cmovgeq %rbp, %rax
movq %r10, %r15
leaq 0x40(%rsp), %r10
movq %rsi, %rdi
leaq 0xa0(%rsp), %r11
leaq 0x98(%rsp), %rbx
movq %r13, %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
movq %r8, %r14
movq %r12, %r9
pushq %r10
pushq %r11
pushq %rbx
pushq %rax
callq 0x60281
movq %r15, %r10
addq $0x20, %rsp
jmp 0x64314
leaq -0x1(%r10), %rcx
cmpq $0x1, %rcx
movq %r8, %r14
ja 0x64314
movq 0xc8(%rsp), %r8
cmpq %rbp, %rax
cmovgeq %rbp, %rax
leaq 0x88(%rsp), %rbx
leaq 0x90(%rsp), %r14
leaq 0x98(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
pushq %r10
pushq %rax
movq %r10, %r15
callq 0x60522
movq %r15, %r10
addq $0x10, %rsp
movq (%r14), %r12
movq (%rbx), %r14
subq %r14, %r12
jle 0x6455d
movq 0xa0(%rsp), %rdi
xorl %eax, %eax
cmpq $-0x1, %rdi
je 0x6455f
movq 0x98(%rsp), %rsi
movq %rdi, %r11
subq %rsi, %r11
jle 0x6455f
cmpq $-0x1, %rsi
je 0x6455f
movq 0xc0(%rsp), %rax
cmpq %r11, %r12
movq %r11, %rbx
cmovgq %r12, %rbx
cmpq %rbp, %rbx
cmovgeq %rbp, %rbx
cmpq (%rax), %rbx
jle 0x6455d
movq 0xb0(%rsp), %r8
movq 0xa8(%rsp), %r9
movq %rbx, (%rax)
testq %r13, %r13
movq %rsi, (%rsp)
je 0x643ba
movq (%r13), %rax
movq 0x10(%rax), %rcx
movq 0x20(%rsp), %rax
leaq (%rax,%rax,4), %rdx
movq 0x20(%rcx,%rdx,8), %rax
cmpb $0x0, 0xc(%rsp)
je 0x64427
leaq (%rcx,%rdx,8), %rcx
movl (%rcx), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x64429
movq 0x80(%rsp), %rdx
movq (%r9), %rbp
movq (%r8), %r13
movq %r11, 0x8(%rdx)
movq (%rdx), %rax
cmpq %r11, 0x10(%rdx)
movq %rdi, 0x28(%rsp)
jge 0x64406
movq %rax, %rdi
movq %r11, %rsi
movq %r11, %r15
callq 0x2370
movq 0x80(%rsp), %rdx
movq %r15, %r11
movq (%rsp), %rsi
movq 0xd0(%rsp), %r10
movq %rax, (%rdx)
movq %r15, 0x10(%rdx)
movq 0x30(%rsp), %rcx
cmpq %rbp, %rsi
jne 0x6443e
subq %rbp, %r13
movq %r11, %r9
subq %r13, %r9
jle 0x644d2
addq %r13, %rax
addq %rsi, %r13
jmp 0x64444
xorl %ecx, %ecx
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rdx
addq %rsi, %rcx
addq %rcx, %rax
jmp 0x644f1
movq %rsi, %r13
movq %r11, %r9
movq %rdx, %rbp
testq %rcx, %rcx
je 0x6448e
movq (%rcx), %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rdx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rcx), %rdx
subq $0x8, %rsp
movzbl 0x14(%rsp), %ecx
xorl %edi, %edi
movq %rax, %rsi
movq %r13, %r8
pushq $0x0
movq %r11, %r15
callq 0x36049
movq %r15, %r11
movq 0x10(%rsp), %rsi
addq $0x10, %rsp
movq 0x10(%rsp), %r15
jmp 0x644bf
movq 0x38(%rsp), %r8
testq %r8, %r8
movq 0x10(%rsp), %r15
je 0x644bf
movzbl 0xc(%rsp), %ecx
movq %rax, %rdi
movq %r13, %rsi
movq %r9, %rdx
movq 0x20(%rsp), %r9
movq %r11, %r13
callq 0x32ef8
movq %r13, %r11
movq (%rsp), %rsi
movq (%rbp), %rax
movq 0x18(%rsp), %rdx
movq 0xd0(%rsp), %r10
jmp 0x644dc
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rdx
movq 0x28(%rsp), %rdi
movq 0xb0(%rsp), %r8
movq 0xa8(%rsp), %r9
addq %r14, %rdx
movq %rsi, (%r9)
movq %rdi, (%r8)
cmpq $0x2, %r10
je 0x6453b
cmpq $0x1, %r10
je 0x64525
testq %r10, %r10
jne 0x645a8
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movl %ebx, %r8d
movq %r15, %r9
callq 0x63d6a
jmp 0x6454f
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movl %ebx, %r8d
movq %r15, %r9
callq 0x63df0
jmp 0x6454f
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movl %ebx, %r8d
movq %r15, %r9
callq 0x63e76
movq (%rsp), %rsi
movl 0x2c(%r15), %eax
cmpl 0x28(%r15), %eax
jle 0x6456e
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r15), %xmm0
movd %esi, %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)
movl $0x1, %eax
jmp 0x6455f
cmpq $0x3, %r10
jne 0x64553
movl 0x40(%rsp), %r9d
subq $0x8, %rsp
movq %rax, %rdi
movl %r11d, %esi
movl %r12d, %ecx
movl %ebx, %r8d
pushq %r15
callq 0x63efc
movq 0x10(%rsp), %rsi
addq $0x10, %rsp
jmp 0x64553