// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movq %r8, %r11
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r15
movq 0xd0(%rsp), %r14
movq 0x90(%rsp), %r9
movq 0x88(%rsp), %r8
movq 0xa0(%rsp), %rax
subq 0x98(%rsp), %rax
movl $0x7fffffff, 0x2c(%r11) # imm = 0x7FFFFFFF
movq $0x0, 0x30(%rsp)
movslq 0x10(%rdi), %r13
movq %r9, %r10
subq %r8, %r10
cmpq %rax, %r10
cmovleq %rax, %r10
testq %rdx, %rdx
movq %r11, 0x18(%rsp)
je 0x64717
movq (%r12), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %rdx
addq %r13, %rdx
movl (%rax,%rdx,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x64741
testq %rbp, %rbp
je 0x64739
movq (%rbp), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %rdx
addq %r13, %rdx
movl (%rax,%rdx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x64741
movq 0x18(%rcx), %rax
movq (%rax,%r13,8), %rax
movq 0xb8(%rsp), %rbx
movl 0x1c(%r15), %edx
movl %edx, 0xc(%rsp)
cmpq $0x3, %r14
movq %rcx, 0x40(%rsp)
jne 0x64796
cmpq %rbx, %r10
cmovgeq %rbx, %r10
leaq 0x30(%rsp), %rax
leaq 0xa0(%rsp), %r11
leaq 0x98(%rsp), %r14
movq %rbp, %rdi
movq %r12, %rsi
movq %rcx, %rdx
movq %r15, %rcx
pushq %rax
pushq %r11
pushq %r14
pushq %r10
callq 0x609cc
addq $0x20, %rsp
jmp 0x647df
leaq -0x1(%r14), %rcx
cmpq $0x1, %rcx
ja 0x647df
movq 0xc8(%rsp), %r8
cmpq %rbx, %r10
cmovgeq %rbx, %r10
leaq 0x88(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x98(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
movq %rax, %r9
pushq %r14
pushq %r10
callq 0x60b76
addq $0x10, %rsp
movq 0x90(%rsp), %r15
movq 0x88(%rsp), %rdi
subq %rdi, %r15
jle 0x64a37
movq 0xa0(%rsp), %r8
xorl %eax, %eax
cmpq $-0x1, %r8
je 0x64a39
movq 0x98(%rsp), %r10
movq %r8, %r11
subq %r10, %r11
jle 0x64a39
cmpq $-0x1, %r10
je 0x64a39
movq 0xc0(%rsp), %rax
cmpq %r11, %r15
movq %r11, %r14
cmovgq %r15, %r14
cmpq %rbx, %r14
cmovgeq %rbx, %r14
cmpq (%rax), %r14
jle 0x64a37
movq 0xb0(%rsp), %r9
movq 0xa8(%rsp), %rcx
movq %r14, (%rax)
testq %r12, %r12
movq %rdi, 0x10(%rsp)
movq %r10, (%rsp)
je 0x6489b
movq (%r12), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
movq 0x20(%rax,%rcx,8), %rsi
cmpb $0x0, 0xc(%rsp)
je 0x64905
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x64907
movq %r8, 0x28(%rsp)
movq 0x80(%rsp), %rdx
movq (%rcx), %rbx
movq (%r9), %r12
movq %r11, 0x8(%rdx)
cmpq %r11, 0x10(%rdx)
jge 0x648e6
movq (%rdx), %rdi
movq %r11, %rsi
movq %r12, 0x38(%rsp)
movq %r11, %r12
callq 0x2370
movq 0x80(%rsp), %rdx
movq %r12, %r11
movq 0x38(%rsp), %r12
movq (%rsp), %r10
movq %rax, (%rdx)
movq %r11, 0x10(%rdx)
movq (%rdx), %rsi
cmpq %rbx, %r10
jne 0x6491c
movq %r12, %rax
subq %rbx, %rax
movq %r11, %r9
subq %rax, %r9
jle 0x649a0
addq %rax, %rsi
jmp 0x64922
xorl %eax, %eax
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %rdx
addq %r10, %rax
addq %rax, %rsi
jmp 0x649bc
movq %r10, %r12
movq %r11, %r9
movq 0x18(%rsp), %rbx
testq %rbp, %rbp
je 0x64969
movq (%rbp), %rax
leaq (,%r13,4), %rdx
addq %r13, %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl 0x14(%rsp), %ecx
xorl %edi, %edi
movq %r12, %r8
pushq $0x0
movq %r11, %r12
callq 0x36b0d
movq %r12, %r11
movq 0x10(%rsp), %r10
addq $0x10, %rsp
jmp 0x64993
movq 0x40(%rsp), %r8
testq %r8, %r8
je 0x64993
movzbl 0xc(%rsp), %ecx
movq %rsi, %rdi
movq %r12, %rsi
movq %r9, %rdx
movq %r13, %r9
movq %r11, %r12
callq 0x337f7
movq %r12, %r11
movq (%rsp), %r10
movq 0x80(%rsp), %rax
movq (%rax), %rsi
jmp 0x649a5
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r8
movq 0xb0(%rsp), %r9
addq %rdi, %rdx
movq 0xa8(%rsp), %rax
movq %r10, (%rax)
movq %r8, (%r9)
movq 0xd0(%rsp), %rax
cmpq $0x2, %rax
je 0x64a12
cmpq $0x1, %rax
je 0x649fc
testq %rax, %rax
jne 0x64a5d
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x6424a
jmp 0x64a26
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x642d0
jmp 0x64a26
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
movq %rbx, %r9
callq 0x64356
movq (%rsp), %r10
movq 0x10(%rsp), %rdi
movl 0x2c(%rbx), %eax
cmpl 0x28(%rbx), %eax
jle 0x64a48
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addl %r10d, 0x10(%rbx)
addl %r10d, 0x14(%rbx)
addl %edi, 0x1c(%rbx)
addl %edi, 0x20(%rbx)
movl $0x1, %eax
jmp 0x64a39
cmpq $0x3, %rax
jne 0x64a2f
movl 0x30(%rsp), %r9d
subq $0x8, %rsp
movq %rsi, %rdi
movl %r11d, %esi
movl %r15d, %ecx
movl %r14d, %r8d
pushq %rbx
callq 0x643dc
movq 0x10(%rsp), %r10
movq 0x20(%rsp), %rdi
addq $0x10, %rsp
jmp 0x64a2f