// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_exz_infi(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, bit_extz_t*, char*, UC_Read*, long, long, long, long, long, long, long)
int64_t cal_exz_infi(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 thre, int64_t q_tot_l, int64_t mode)
{
    clear_align(*exz);
    int64_t aux_beg = 0, bd = (((thre)<<1)+1), ql, tl, t_tot_l = -1; int32_t nword = ((bd>>bitw)+(!!(bd&bitz)));
    char *q_string, *t_string; int32_t rev = z->y_pos_strand, id = z->y_id; ql = qe - qs;
    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, &ts, &te, &aux_beg);
    } else if(mode == 1 || mode == 2) {
        adjust_ext_offset(&qs, &qe, &ts, &te, q_tot_l, t_tot_l, thre, mode);
    }
    
    if((qe > qs) && (te > ts) && (ts != -1) && (te != -1)) {
        ql = qe - qs; q_string = qstr + qs;
        tl = te - ts; resize_UC_Read(tu, tl);
        // fprintf(stderr, "q::[%ld, %ld), t::[%ld, %ld), thre::%ld, t_tot_l::%ld\n", qs, qe, ts, te, thre, t_tot_l);
        if(rref) {
            recover_UC_Read_sub_region(tu->seq, ts, tl, rev, rref, id); t_string = tu->seq;
        } else {
            t_string = return_str_seq_exz(tu->seq, ts, tl, rev, hpc_g, uref, id);
        }
        // fprintf(stderr, ", nword::%d", nword);
        // if(ql < 0) fprintf(stderr, "qs::%ld, qe::%ld\n", qs, qe);
        // return 0;
        if(nword <= 1) {
            if(mode == 0) { //global
                ed_band_cal_global_64_w_trace(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 1) {///forward extension
                // fprintf(stderr, "q::[%ld, %ld), t::[%ld, %ld), thre::%ld\n", qs, qe, ts, te, thre);
                ed_band_cal_extension_64_0_w_trace(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 2) {///backward extension
                ed_band_cal_extension_64_1_w_trace(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 3) {//semi-global
                ed_band_cal_semi_64_w_absent_diag_trace(t_string, tl, q_string, ql, thre, aux_beg, exz);
            }
        } else if(nword == 2) {
            if(mode == 0) { //global
                ed_band_cal_global_128_w_trace(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 1) {///forward extension
                ed_band_cal_extension_128_0_w_trace(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 2) {///backward extension
                ed_band_cal_extension_128_1_w_trace(t_string, tl, q_string, ql, thre, exz);
            } else if(mode == 3) {//semi-global
                ed_band_cal_semi_128_w_absent_diag_trace(t_string, tl, q_string, ql, thre, aux_beg, exz);
            }
        } else {
            if(mode == 0) { //global
                ed_band_cal_global_infi_w_trace(t_string, tl, q_string, ql, thre, &nword, exz);
            } else if(mode == 1) {///forward extension
                ed_band_cal_extension_infi_0_w_trace(t_string, tl, q_string, ql, thre, &nword, exz);
            } else if(mode == 2) {///backward extension
                ed_band_cal_extension_infi_1_w_trace(t_string, tl, q_string, ql, thre, &nword, exz);
            } else if(mode == 3) {//semi-global
                ed_band_cal_semi_infi_w_absent_diag_trace(t_string, tl, q_string, ql, thre, aux_beg, &nword, exz);
            }
        }
        if(is_align(*exz)) {

            return 1;
        }
        return 0;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r13
movq %rcx, %rbp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %rcx
movq 0xc8(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0xa0(%rsp), %r9
movq 0x98(%rsp), %r8
movl $0x7fffffff, 0x2c(%r13) # imm = 0x7FFFFFFF
movq $0x0, 0x38(%rsp)
movq %rsi, %rax
shrq $0x5, %rax
incl %eax
movl %eax, 0x28(%rsp)
movslq 0x10(%rcx), %r14
testq %rdx, %rdx
je 0x61135
movq (%rbx), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rdx
movl (%rax,%rdx,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x61157
testq %r15, %r15
je 0x6114f
movq (%r15), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rdx
movl (%rax,%rdx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x61157
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %rax
movl 0x1c(%rcx), %edx
movl %edx, 0x2c(%rsp)
cmpq $0x3, %rdi
jne 0x6119c
leaq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xa8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rsi, (%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x609cc
jmp 0x611df
leaq -0x1(%rdi), %rcx
cmpq $0x1, %rcx
ja 0x611df
movq 0xc0(%rsp), %r8
movq %rdi, 0x8(%rsp)
movq %rsi, (%rsp)
leaq 0x98(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
movq %rax, %r9
callq 0x60b76
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %r12
subq %r12, %r10
jle 0x612a7
movq 0xb0(%rsp), %r11
xorl %eax, %eax
cmpq $-0x1, %r11
je 0x614f6
movq %r13, 0x50(%rsp)
movq 0xa8(%rsp), %r13
subq %r13, %r11
jle 0x614f6
cmpq $-0x1, %r13
je 0x614f6
movq 0x90(%rsp), %rax
cmpq %r11, 0x10(%rax)
jge 0x61270
movq (%rax), %rdi
movq %r11, %rsi
movq %r10, 0x48(%rsp)
movq %r11, 0x40(%rsp)
callq 0x2370
movq 0x40(%rsp), %r11
movq 0x48(%rsp), %r10
movq 0x90(%rsp), %rcx
movq %rax, (%rcx)
movq 0x90(%rsp), %rax
movq %r11, 0x10(%rax)
testq %rbp, %rbp
je 0x612ae
movq (%rax), %rdi
movzbl 0x2c(%rsp), %ecx
movq %r13, %rsi
movq %r11, %rdx
movq %rbp, %r8
movq %r14, %r9
movq %r10, %rbx
movq %r11, %r14
movq %rax, %r15
callq 0x337f7
movq %r14, %r11
movq %rbx, %r10
movq (%r15), %rbp
movq 0x30(%rsp), %rdx
jmp 0x61323
xorl %eax, %eax
jmp 0x614f6
testq %rbx, %rbx
je 0x612d9
movq (%rbx), %rax
movq 0x10(%rax), %rax
leaq (%r14,%r14,4), %rcx
movq 0x20(%rax,%rcx,8), %rbp
cmpb $0x0, 0x2c(%rsp)
je 0x61316
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x61318
movq (%rax), %rbp
movq (%r15), %rax
leaq (%r14,%r14,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
movq $0x0, (%rsp)
movzbl 0x2c(%rsp), %ecx
xorl %edi, %edi
movq %rbp, %rsi
movq %r13, %r8
movq %r11, %r9
movq %r10, %rbx
movq %r11, %r14
callq 0x36b0d
movq %r14, %r11
movq %rbx, %r10
jmp 0x612a0
xorl %eax, %eax
movq 0x30(%rsp), %rdx
addq %r13, %rax
addq %rax, %rbp
addq %r12, %rdx
movl 0x28(%rsp), %eax
cmpl $0x1, %eax
movq 0x50(%rsp), %rbx
jg 0x61377
movq 0xc8(%rsp), %rax
cmpq $0x2, %rax
movq 0xb8(%rsp), %r8
je 0x6140e
cmpq $0x1, %rax
je 0x613f8
testq %rax, %rax
jne 0x61424
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq %rbx, %r9
callq 0x8cac9
jmp 0x614eb
cmpl $0x2, %eax
movq 0xb8(%rsp), %r8
movq 0xc8(%rsp), %rax
jne 0x613bf
cmpq $0x2, %rax
je 0x61479
cmpq $0x1, %rax
je 0x6144a
testq %rax, %rax
jne 0x614a5
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq %rbx, %r9
callq 0x8e379
jmp 0x614eb
cmpq $0x2, %rax
je 0x6148c
cmpq $0x1, %rax
je 0x61460
testq %rax, %rax
jne 0x614c4
movq %rbx, (%rsp)
leaq 0x28(%rsp), %r9
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
callq 0x90988
jmp 0x614eb
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq %rbx, %r9
callq 0x8cffc
jmp 0x614eb
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq %rbx, %r9
callq 0x8d669
jmp 0x614eb
cmpq $0x3, %rax
jne 0x614eb
movl 0x38(%rsp), %r9d
movq %rbx, (%rsp)
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
callq 0x8dd8a
jmp 0x614eb
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq %rbx, %r9
callq 0x8ebf7
jmp 0x614eb
movq %rbx, (%rsp)
leaq 0x28(%rsp), %r9
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
callq 0x91465
jmp 0x614eb
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq %rbx, %r9
callq 0x8f5ef
jmp 0x614eb
movq %rbx, (%rsp)
leaq 0x28(%rsp), %r9
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
callq 0x920ec
jmp 0x614eb
cmpq $0x3, %rax
jne 0x614eb
movl 0x38(%rsp), %r9d
movq %rbx, (%rsp)
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
callq 0x90059
jmp 0x614eb
cmpq $0x3, %rax
jne 0x614eb
movl 0x38(%rsp), %r9d
movq %rbx, 0x8(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movl %r11d, %esi
movl %r10d, %ecx
callq 0x92e05
movl 0x2c(%rbx), %ecx
xorl %eax, %eax
cmpl 0x28(%rbx), %ecx
setle %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq