// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %r9, 0x60(%rsp)
movq %r8, %r12
movq %rdx, %r14
movq %rdi, %r8
movq 0xb0(%rsp), %r10
movq 0xa8(%rsp), %rbx
movl $0x7fffffff, 0x2c(%r12) # imm = 0x7FFFFFFF
andq $0x0, 0x30(%rsp)
movq 0xd8(%rsp), %rbp
movq 0xc8(%rsp), %rdi
movq %rdi, %r15
shrq $0x5, %r15
incl %r15d
movl %r15d, 0x28(%rsp)
movslq 0x10(%r8), %rdx
testq %r14, %r14
movq %rdx, 0x38(%rsp)
je 0x306e4
movq (%r14), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %r9d
shrl %r9d
andl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
jmp 0x30709
testq %rsi, %rsi
je 0x30701
movq (%rsi), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
jmp 0x30709
movq 0x18(%rcx), %rax
movq (%rax,%rdx,8), %r9
movl 0x1c(%r8), %eax
movl %eax, 0x2c(%rsp)
cmpq $0x3, %rbp
movq %rsi, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
jne 0x3076e
leaq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rdi, (%rsp)
movq %rsi, %rdi
movq %r14, %rsi
movq %rcx, %rdx
movq %r8, %rcx
movq %rbx, %r8
movq %r10, %r9
movq %rbp, %r13
movq %r10, %rbp
callq 0x300a0
movq %rbp, %r10
movq %r13, %rbp
jmp 0x307c3
leaq -0x1(%rbp), %rax
cmpq $0x1, %rax
ja 0x307c3
movq 0xd0(%rsp), %r8
movq %rbp, 0x8(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, (%rsp)
leaq 0xa8(%rsp), %r13
leaq 0xb0(%rsp), %rbx
leaq 0xb8(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3023a
movq (%rbx), %r10
movq (%r13), %rbx
subq %rbx, %r10
jle 0x3084f
movq 0xc0(%rsp), %r13
xorl %eax, %eax
cmpq $-0x1, %r13
je 0x30a50
movq %r12, 0x48(%rsp)
movq 0xb8(%rsp), %r12
subq %r12, %r13
jle 0x30a50
cmpq $-0x1, %r12
je 0x30a50
movq %r10, 0x40(%rsp)
movq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x562fa
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
movq 0x58(%rsp), %r8
testq %r8, %r8
je 0x30856
movzbl 0x2c(%rsp), %ecx
movq %r12, %rsi
movq %r13, %r12
movq %r13, %rdx
movq 0x38(%rsp), %r9
callq 0x6c9e
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
jmp 0x3087d
xorl %eax, %eax
jmp 0x30a50
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movzbl 0x2c(%rsp), %ecx
movq %r12, %rsi
movq %r13, %r12
movq %r13, %rdx
movq %r14, %r8
movq 0x50(%rsp), %r9
callq 0x578d7
movq %rax, %rdi
movq 0x60(%rsp), %rdx
addq %rbx, %rdx
cmpl $0x1, %r15d
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %rcx
jg 0x308ca
cmpq $0x2, %rbp
je 0x30958
cmpq $0x1, %rbp
je 0x30940
testq %rbp, %rbp
jne 0x30970
movl %r12d, %esi
movq 0xc8(%rsp), %r8
movq %rbx, %r9
callq 0x58253
jmp 0x30a45
cmpl $0x2, %r15d
jne 0x30905
cmpq $0x2, %rbp
je 0x309cb
cmpq $0x1, %rbp
je 0x30998
testq %rbp, %rbp
jne 0x309fb
movl %r12d, %esi
movq 0xc8(%rsp), %r8
movq %rbx, %r9
callq 0x5975c
jmp 0x30a45
cmpq $0x2, %rbp
je 0x309e0
cmpq $0x1, %rbp
je 0x309b0
testq %rbp, %rbp
jne 0x30a1c
movq %rbx, (%rsp)
leaq 0x28(%rsp), %r9
movl %r12d, %esi
movq 0xc8(%rsp), %r8
callq 0x5b95f
jmp 0x30a45
movl %r12d, %esi
movq 0xc8(%rsp), %r8
movq %rbx, %r9
callq 0x586b5
jmp 0x30a45
movl %r12d, %esi
movq 0xc8(%rsp), %r8
movq %rbx, %r9
callq 0x58c53
jmp 0x30a45
cmpq $0x3, %rbp
jne 0x30a45
movl 0x30(%rsp), %r9d
movq %rbx, (%rsp)
movl %r12d, %esi
movq 0xc8(%rsp), %r8
callq 0x59275
jmp 0x30a45
movl %r12d, %esi
movq 0xc8(%rsp), %r8
movq %rbx, %r9
callq 0x59eef
jmp 0x30a45
movq %rbx, (%rsp)
leaq 0x28(%rsp), %r9
movl %r12d, %esi
movq 0xc8(%rsp), %r8
callq 0x5c367
jmp 0x30a45
movl %r12d, %esi
movq 0xc8(%rsp), %r8
movq %rbx, %r9
callq 0x5a7c8
jmp 0x30a45
movq %rbx, (%rsp)
leaq 0x28(%rsp), %r9
movl %r12d, %esi
movq 0xc8(%rsp), %r8
callq 0x5cedd
jmp 0x30a45
cmpq $0x3, %rbp
jne 0x30a45
movl 0x30(%rsp), %r9d
movq %rbx, (%rsp)
movl %r12d, %esi
movq 0xc8(%rsp), %r8
callq 0x5b166
jmp 0x30a45
cmpq $0x3, %rbp
jne 0x30a45
movl 0x30(%rsp), %r9d
movq %rbx, 0x8(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
movl %r12d, %esi
movq 0xc8(%rsp), %r8
callq 0x5dad3
movl 0x2c(%rbx), %ecx
xorl %eax, %eax
cmpl 0x28(%rbx), %ecx
setle %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq