// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0xa8, %rsp
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x38(%rbp), %rax
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movq $0x0, -0x48(%rbp)
movq 0x38(%rbp), %rax
shlq %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq $-0x1, -0x68(%rbp)
movq -0x50(%rbp), %rax
sarq $0x6, %rax
movq -0x50(%rbp), %rcx
andq $0x3f, %rcx
cmpq $0x0, %rcx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
addq %rcx, %rax
movl %eax, -0x6c(%rbp)
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x88(%rbp)
movq 0x20(%rbp), %rax
subq 0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x58d0e
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq -0x88(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
sarl %eax
cltq
movq %rax, -0x68(%rbp)
jmp 0x58d56
cmpq $0x0, -0x20(%rbp)
je 0x58d3d
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movslq -0x88(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0x68(%rbp)
jmp 0x58d54
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
jmp 0x58d56
cmpq $0x3, 0x48(%rbp)
jne 0x58d9f
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movq 0x38(%rbp), %rbx
leaq 0x28(%rbp), %r11
leaq 0x30(%rbp), %r10
leaq -0x48(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x57f40
jmp 0x58ddd
cmpq $0x1, 0x48(%rbp)
je 0x58dad
cmpq $0x2, 0x48(%rbp)
jne 0x58ddb
movq 0x40(%rbp), %r8
movq -0x68(%rbp), %r9
movq 0x38(%rbp), %r10
movq 0x48(%rbp), %rax
leaq 0x18(%rbp), %rdi
leaq 0x20(%rbp), %rsi
leaq 0x28(%rbp), %rdx
leaq 0x30(%rbp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x581e0
jmp 0x58ddd
movq 0x20(%rbp), %rax
cmpq 0x18(%rbp), %rax
jle 0x5916b
movq 0x30(%rbp), %rax
cmpq 0x28(%rbp), %rax
jle 0x5916b
cmpq $-0x1, 0x28(%rbp)
je 0x5916b
cmpq $-0x1, 0x30(%rbp)
je 0x5916b
movq 0x20(%rbp), %rax
subq 0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
addq 0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x30(%rbp), %rax
subq 0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0xa62c0
cmpq $0x0, -0x30(%rbp)
je 0x58e7c
movq 0x10(%rbp), %rax
movq (%rax), %rdi
movq 0x28(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x84(%rbp), %eax
movq -0x30(%rbp), %r8
movslq -0x88(%rbp), %r9
movzbl %al, %ecx
callq 0x9a30
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
jmp 0x58eb2
movq 0x10(%rbp), %rax
movq (%rax), %rdi
movq 0x28(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl -0x84(%rbp), %eax
movb %al, %cl
movq -0x28(%rbp), %r8
movq -0x20(%rbp), %r9
movslq -0x88(%rbp), %rax
movzbl %cl, %ecx
movq %rax, (%rsp)
callq 0xa8880
movq %rax, -0x80(%rbp)
cmpl $0x1, -0x6c(%rbp)
jg 0x58f87
cmpq $0x0, 0x48(%rbp)
jne 0x58eec
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0xa9890
jmp 0x58f82
cmpq $0x1, 0x48(%rbp)
jne 0x58f19
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0xaa130
jmp 0x58f80
cmpq $0x2, 0x48(%rbp)
jne 0x58f46
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0xaac90
jmp 0x58f7e
cmpq $0x3, 0x48(%rbp)
jne 0x58f7c
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x48(%rbp), %rax
movl %eax, %r9d
movq -0x38(%rbp), %rax
movq %rax, (%rsp)
callq 0xab870
jmp 0x58f7e
jmp 0x58f80
jmp 0x58f82
jmp 0x59147
cmpl $0x2, -0x6c(%rbp)
jne 0x5905c
cmpq $0x0, 0x48(%rbp)
jne 0x58fc1
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0xac2d0
jmp 0x59057
cmpq $0x1, 0x48(%rbp)
jne 0x58fee
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0xad640
jmp 0x59055
cmpq $0x2, 0x48(%rbp)
jne 0x5901b
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %r9
callq 0xaec30
jmp 0x59053
cmpq $0x3, 0x48(%rbp)
jne 0x59051
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x48(%rbp), %rax
movl %eax, %r9d
movq -0x38(%rbp), %rax
movq %rax, (%rsp)
callq 0xb02c0
jmp 0x59053
jmp 0x59055
jmp 0x59057
jmp 0x59145
cmpq $0x0, 0x48(%rbp)
jne 0x59094
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
leaq -0x6c(%rbp), %r9
movq %rax, (%rsp)
callq 0xb16d0
jmp 0x59143
cmpq $0x1, 0x48(%rbp)
jne 0x590c9
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
leaq -0x6c(%rbp), %r9
movq %rax, (%rsp)
callq 0xb2c20
jmp 0x59141
cmpq $0x2, 0x48(%rbp)
jne 0x590fe
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
leaq -0x6c(%rbp), %r9
movq %rax, (%rsp)
callq 0xb4420
jmp 0x5913f
cmpq $0x3, 0x48(%rbp)
jne 0x5913d
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rax
movl %eax, %esi
movq -0x78(%rbp), %rdx
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq 0x38(%rbp), %rax
movl %eax, %r8d
movq -0x48(%rbp), %rax
movl %eax, %r9d
movq -0x38(%rbp), %rax
leaq -0x6c(%rbp), %r10
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xb5cb0
jmp 0x5913f
jmp 0x59141
jmp 0x59143
jmp 0x59145
jmp 0x59147
movq -0x38(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x38(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jg 0x59161
movq $0x1, -0x10(%rbp)
jmp 0x59173
movq $0x0, -0x10(%rbp)
jmp 0x59173
movq $0x0, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0xa8, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)