// File: /workspace/github_cpp_makefile_star2_O3/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, 0x28(%rsp)
movq %r8, %r12
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %rcx
movq 0xc8(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %rbp
movl $0x7fffffff, 0x2c(%r8)  # imm = 0x7FFFFFFF
movq $0x0, 0x30(%rsp)
movq %rsi, %r14
shrq $0x5, %r14
incl %r14d
movl %r14d, 0x20(%rsp)
movslq 0x10(%rcx), %r13
testq %rdx, %rdx
je 0x60a68
movq (%rdx), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %r8
addq %r13, %r8
movl (%rax,%r8,8), %r9d
shrl %r9d
andl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
jmp 0x60a94
testq %r15, %r15
je 0x60a8c
movq (%r15), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %r8
addq %r13, %r8
movl (%rax,%r8,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
jmp 0x60a94
movq 0x18(%rbx), %rax
movq (%rax,%r13,8), %r9
movl 0x1c(%rcx), %eax
movl %eax, 0x24(%rsp)
cmpq $0x3, %rdi
movq %rdx, 0x50(%rsp)
jne 0x60b12
leaq 0x30(%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 %rdx, %rsi
movq %rbx, %rdx
movq %rbp, %r8
movq %r10, %r9
movq %r14, 0x38(%rsp)
movq %r12, %r14
movq %r13, %r12
movq %rbp, %r13
movq %r15, %rbp
movq %rbx, %r15
movq %r10, %rbx
callq 0x60281
movq %rbx, %r10
movq %r15, %rbx
movq %rbp, %r15
movq %r13, %rbp
movq %r12, %r13
movq %r14, %r12
movq 0x38(%rsp), %r14
jmp 0x60b74
leaq -0x1(%rdi), %rax
cmpq $0x1, %rax
ja 0x60b74
movq 0xc0(%rsp), %r8
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, (%rsp)
leaq 0x98(%rsp), %rdi
leaq 0xa0(%rsp), %rbp
leaq 0xa8(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
movq %rbp, %rsi
callq 0x60522
movq (%rbp), %r10
leaq 0x98(%rsp), %rax
movq (%rax), %rbp
subq %rbp, %r10
jle 0x60c36
movq 0xb0(%rsp), %r11
xorl %eax, %eax
cmpq $-0x1, %r11
je 0x60ee0
movq %r12, 0x38(%rsp)
movq 0xa8(%rsp), %r12
subq %r12, %r11
jle 0x60ee0
cmpq $-0x1, %r12
je 0x60ee0
movq 0x90(%rsp), %rdx
cmpq %r11, 0x10(%rdx)
jge 0x60bf3
movq (%rdx), %rdi
movq %r11, %rsi
movq %r10, 0x48(%rsp)
movq %rbx, 0x40(%rsp)
movq %r11, %rbx
callq 0x2370
movq 0x90(%rsp), %rdx
movq %rbx, %r11
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r10
movq %rax, (%rdx)
movq %r11, 0x10(%rdx)
testq %rbx, %rbx
je 0x60c3d
movq (%rdx), %rdi
movzbl 0x24(%rsp), %ecx
movq %r12, %rsi
movq %rdx, %r12
movq %r11, %rdx
movq %rbx, %r8
movq %r13, %r9
movq %r10, %rbx
movq %r11, %r15
callq 0x32ef8
movq %r15, %r11
movq %rbx, %r10
movq (%r12), %r13
movq 0x28(%rsp), %rdx
movq 0xc8(%rsp), %rcx
jmp 0x60ccc
xorl %eax, %eax
jmp 0x60ee0
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x60c74
movq (%rax), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
movq 0x20(%rax,%rcx,8), %r13
cmpb $0x0, 0x24(%rsp)
je 0x60cb7
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x60cb9
movq %r13, %rcx
movq (%rdx), %r13
movq (%r15), %rax
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
movq $0x0, (%rsp)
movzbl 0x24(%rsp), %ecx
xorl %edi, %edi
movq %r13, %rsi
movq %r12, %r8
movq %r11, %r9
movq %r10, %rbx
movq %r11, %r15
callq 0x36049
movq %r15, %r11
movq %rbx, %r10
jmp 0x60c24
xorl %eax, %eax
movq 0x28(%rsp), %rdx
movq 0xc8(%rsp), %rcx
addq %r12, %rax
addq %rax, %r13
addq %rbp, %rdx
cmpl $0x1, %r14d
movq 0x38(%rsp), %rbx
jg 0x60d15
cmpq $0x2, %rcx
je 0x60db5
cmpq $0x1, %rcx
je 0x60d97
testq %rcx, %rcx
jne 0x60dd3
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
movq %rbx, %r9
callq 0x8bd1f
jmp 0x60ed5
cmpl $0x2, %r14d
jne 0x60d56
cmpq $0x2, %rcx
je 0x60e43
cmpq $0x1, %rcx
je 0x60e01
testq %rcx, %rcx
jne 0x60e7f
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
movq %rbx, %r9
callq 0x8d92c
jmp 0x60ed5
cmpq $0x2, %rcx
je 0x60e5e
cmpq $0x1, %rcx
je 0x60e1f
testq %rcx, %rcx
jne 0x60ea6
movq %rbx, (%rsp)
leaq 0x20(%rsp), %r9
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
callq 0x903b7
jmp 0x60ed5
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
movq %rbx, %r9
callq 0x8c25a
jmp 0x60ed5
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
movq %rbx, %r9
callq 0x8ca6d
jmp 0x60ed5
cmpq $0x3, %rcx
jne 0x60ed5
movl 0x30(%rsp), %r9d
movq %rbx, (%rsp)
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
callq 0x8d32c
jmp 0x60ed5
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
movq %rbx, %r9
callq 0x8e2c4
jmp 0x60ed5
movq %rbx, (%rsp)
leaq 0x20(%rsp), %r9
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
callq 0x90fb4
jmp 0x60ed5
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
movq %rbx, %r9
callq 0x8ee5b
jmp 0x60ed5
movq %rbx, (%rsp)
leaq 0x20(%rsp), %r9
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
callq 0x91dd5
jmp 0x60ed5
cmpq $0x3, %rcx
jne 0x60ed5
movl 0x30(%rsp), %r9d
movq %rbx, (%rsp)
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
callq 0x8fa96
jmp 0x60ed5
cmpq $0x3, %rcx
jne 0x60ed5
movl 0x30(%rsp), %r9d
movq %rbx, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movl %r11d, %esi
movl %r10d, %ecx
movq 0xb8(%rsp), %r8
callq 0x92d6f
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