// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: extract_sub_cigar_ii(overlap_region*, long, All_reads*, long, long, long, long, UC_Read*, ul_ov_t*)
uint32_t extract_sub_cigar_ii(overlap_region *z, int64_t ql, All_reads *rref, int64_t s, int64_t e, int64_t iws, int64_t iwe, UC_Read* tu, ul_ov_t *p)
{
    int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, ol;
    bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0, ii[2], it[2]; uint32_t res = (uint32_t)-1;
    s0 = ((int64_t)(z->w_list.a[wk].x_start)) + bd; 
    e0 = ((int64_t)(z->w_list.a[wk].x_end)) + 1 - bd;
    if(s < s0) s = s0; if(e > e0) e = e0;///exclude boundary
    if(s > e) return -1;///it is possible s == e
    os = MAX(s, s0); oe = MIN(e, e0);
    if(oe < os) return -1;///it is possible os == oe
    // fprintf(stderr, "[M::%s] s0::%ld, e0::%ld, iws::%ld, iwe::%ld\n", __func__, s0, e0, iws, iwe);
    ///make sure that this alignment block could cover the whole [iws, iwe) -> s0 < iws && e0 > iwe
    // if((s0 >= iws) || (e0 <= iwe)) return -1;///!(s0 < iws && e0 > iwe) -> only consider the alignment that could cover the whole [s, e)
    if(!(((s0 < iws) || (s0 == 0)) && ((e0 > iwe) || (e0 == ql)))) return -1;///!(s0 < iws && e0 > iwe) -> only consider the alignment that could cover the whole [s, e)

    set_bit_extz_t(ez, (*z), wk);
    if(!ez.cigar.n) return -1;
    int64_t cn = ez.cigar.n; uint16_t op; int64_t ws, we, wts, wte, ovlp, cc = 0, cci; 
    if((ck < 0) || (ck > cn)) {//(*ck) == cn is allowed
        ck = 0; xk = ez.ts; yk = ez.ps;
    }
    
    while (ck > 0 && xk >= s) {///x -> t; y -> p; first insertion and then match/mismatch
        --ck;
        op = ez.cigar.a[ck]>>14;
        if(op!=2) xk -= (ez.cigar.a[ck]&(0x3fff));
        if(op!=3) yk -= (ez.cigar.a[ck]&(0x3fff)); 
    }

    // char cm[4]; cm[0] = 'M'; cm[1] = 'S'; cm[2] = 'I'; cm[3] = 'D';  
    //some cigar will span s or e
    ii[0] = ii[1] = it[0] = it[1] = -1; res = cc = 0;
    while (ck < cn && xk < e) {//[s, e)
        ws = xk; wts = yk;
        op = ez.cigar.a[ck]>>14; ol = (ez.cigar.a[ck]&(0x3fff));
        ///op == 3: -> x; op == 2: -> y;
        if(op!=2) xk += ol;
        if(op!=3) yk += ol; 
        ck++; we = xk; wte = yk;

        // if(s == 10480) {
        //     fprintf(stderr, "[%ld, %ld)\t%c\n", ws, we, cm[op]);
        // }

        os = MAX(s, ws); oe = MIN(e, we);
        ovlp = ((oe>os)? (oe-os):0);

        if(s == e) {///insertion in comparsion with the reference
            if(op != 0 || ws >= s || we <= e || e != iwe || s != iws) continue;///must be a match
        } else {
            if(op != 2) {
                if(!ovlp) continue;
            } else {///ws == we
                if(ws < s || ws >= e) continue;
            }
        }
        
        
        if(ii[0] == -1) {
            ii[0] = os;
            if(op < 2) {
                it[0] = os - ws + wts;
            } else {///op == 2: more y; p == 3: more x
                it[0] = wts;
            } 
        }

        ii[1] = oe;
        if(op < 2) {
            it[1] = oe - ws + wts;
        } else {///op == 2: more y; p == 3: more x
            it[1] = wte;
        } 




        if(op != 2) ol = oe-os;
        cc += ol;
        // if(s == 11851 && e == 11853) {
        // if(!ol) fprintf(stderr, "%ld%c", ol, cm[op]);
        // }
        if(cc <= simp_vote_len) {
            for (cci = 0; cci < ol; cci++) {
                res <<= 2; res |= op;
            }
        }
    }

    while (ck < cn && xk <= e) {//[s, e)
        ws = xk; wts = yk;
        op = ez.cigar.a[ck]>>14; ol = (ez.cigar.a[ck]&(0x3fff));
        if(op != 2) break;
        yk += (ez.cigar.a[ck]&(0x3fff)); 
        ck++; we = xk; wte = yk;
        if(ws >= s && ws <= e) {
            
            if(ii[0] == -1) {
                ii[0] = ws; it[0] = wts;
            }
            ii[1] = we; it[1] = wte;
            
            cc += ol;
            // if(s == 11851 && e == 11853) {
            //     fprintf(stderr, "%ld%c", ol, cm[op]);
            // }
            if(cc <= simp_vote_len) {
                for (cci = 0; cci < ol; cci++) {
                    res <<= 2; res |= op;
                }
            }
        }
    }
    // if(s == 11851 && e == 11853) {
    //     fprintf(stderr, "\tx::[%ld, %ld)\ty::[%ld, %ld)\tcc::%ld\n", ii[0], ii[1], it[0], it[1], cc);
    // }
    if((cc <= simp_vote_len) 
            && (ii[1] >= ii[0]) && (ii[1] - ii[0] <= simp_vote_len) 
            && (it[1] >= it[0]) && (it[1] - it[0] <= simp_vote_len)) {
        // ii[0] = ii[0] - s; ii[1] = e - ii[1];
        if((ii[0] == iws) && (ii[1] == iwe)) {
            op = cc; op <<= 12; res |= op;

            char *ystr = NULL; res <<= 16; cc = it[1] - it[0]; op = 0;
            if(cc > 0) {
                UC_Read_resize(*tu, (it[1] - it[0])); ystr = tu->seq;
                recover_UC_Read_sub_region(ystr, it[0], (it[1] - it[0]), z->y_pos_strand, rref, z->y_id);
                
                for (cci = 0; cci < cc; cci++) {
                    op <<= 2; op |= seq_nt6_table[(uint32_t)(ystr[cci])];
                }
            } 
            res |= op;

            op = it[1] - it[0]; op <<= 12; res |= op;
        } else {
            res = (uint32_t)-1;
        }
    } else {
        res = (uint32_t)-1;
    }
    

    ovlp_cur_xoff(*p) = xk; ovlp_cur_yoff(*p) = yk; ovlp_cur_coff(*p) = ck; ovlp_cur_ylen(*p) = 0;
    
    return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0xa0(%rsp), %r12
movl (%r12), %r15d
movl 0x18(%r12), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r14
shlq $0x5, %r15
movslq (%r14,%r15), %r13
leaq (%rax,%r13), %rbx
movslq 0x4(%r14,%r15), %r10
subq %rax, %r10
leaq 0x1(%r10), %r11
cmpq %rcx, %rbx
cmovgq %rbx, %rcx
cmpq %r8, %r11
cmovlq %r11, %r8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %r8, %rcx
jg 0x180a0c
movq %r9, 0x38(%rsp)
cmpq %r9, %rbx
setge %bpl
testq %rbx, %rbx
setne %bl
testb %bl, %bpl
jne 0x180a0c
cmpq 0x90(%rsp), %r10
setl %r10b
cmpq %rsi, %r11
setne %sil
testb %sil, %r10b
jne 0x180a0c
addq %r15, %r14
movl 0x1c(%r14), %esi
testq %rsi, %rsi
je 0x180a0c
movq %rdx, %r9
movq %r12, %rax
movl 0x4(%r12), %r12d
movl 0x10(%rax), %r10d
movl 0x8(%rax), %ebx
movq %rdi, 0x48(%rsp)
movq 0x70(%rdi), %rdi
movl 0x18(%r14), %eax
xorl %edx, %edx
cmpl %esi, %ebx
cmoval %edx, %ebx
movslq 0x8(%r14), %rbp
cmovbeq %r10, %rbp
cmovaq %r13, %r12
testl %ebx, %ebx
movq %r9, 0x50(%rsp)
je 0x180648
cmpq %rcx, %r12
jl 0x180648
movq %rdi, %rdx
leaq (%rdi,%rax,2), %rdi
addq $-0x2, %rdi
xorl %r9d, %r9d
movq %rbx, %r10
movzwl (%rdi,%rbx,2), %r11d
decq %rbx
movl %r11d, %r14d
shrl $0xe, %r14d
andl $0x3fff, %r11d          # imm = 0x3FFF
cmpw $0x2, %r14w
movq %r11, %r15
cmoveq %r9, %r15
subq %r15, %r12
cmpw $0x3, %r14w
cmoveq %r9, %r11
subq %r11, %rbp
cmpq $0x2, %r10
jb 0x18064b
cmpq %rcx, %r12
jge 0x180607
jmp 0x18064b
movq %rdi, %rdx
leaq (%rdx,%rax,2), %rax
movq $-0x1, 0x10(%rsp)
movq $-0x1, 0x28(%rsp)
movq $-0x1, 0x8(%rsp)
movq $-0x1, 0x18(%rsp)
xorl %r15d, %r15d
movq $0x0, 0x30(%rsp)
movq %r12, %r14
movq %rbp, %r9
movq %rbx, %rdx
movq %rbp, %r13
movq %rdx, %rbx
cmpq %rsi, %rdx
jge 0x180848
cmpq %r8, %r14
jge 0x180848
movzwl (%rax,%rbx,2), %r10d
movl %r10d, %edx
shrl $0xe, %edx
movl %r10d, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
cmpw $0x2, %dx
sete %dil
cmpw $0x3, %dx
movl $0x0, %ebp
cmovneq %r11, %rbp
addq %r13, %rbp
cmpq %r8, %rcx
je 0x18073b
cmpw $0x2, %dx
jne 0x180790
leaq 0x1(%rbx), %rdx
movq %rbp, %r13
cmpq %rcx, %r14
jl 0x18068b
movq %r9, 0x20(%rsp)
movb $0x1, %dil
movw $0x2, %dx
movq %r14, %r12
movq %r14, 0x8(%rsp)
movq %r14, 0x10(%rsp)
xorl %r9d, %r9d
cmpq $-0x1, 0x28(%rsp)
jne 0x1807d0
testw %r10w, %r10w
js 0x1807bc
movq 0x20(%rsp), %r13
subq %r14, %r13
addq 0x8(%rsp), %r13
movq %r13, 0x18(%rsp)
movq 0x8(%rsp), %r13
movq %r13, 0x28(%rsp)
jmp 0x1807d0
movq %r9, 0x20(%rsp)
cmpw $0x2, %dx
movq %r11, %r12
movl $0x0, %r9d
cmoveq %r9, %r12
addq %r14, %r12
cmpq 0x38(%rsp), %rcx
jne 0x1807b4
cmpq 0x90(%rsp), %r8
jne 0x1807b4
movzwl %r10w, %r9d
cmpl $0x3fff, %r9d           # imm = 0x3FFF
ja 0x1807b4
cmpq %rcx, %r14
jge 0x1807b4
cmpq %rcx, %r12
jle 0x1807b4
movq 0x38(%rsp), %r9
movq %r9, 0x8(%rsp)
movq %r9, 0x10(%rsp)
jmp 0x180701
movq %rcx, %rdi
cmpq %r14, %rcx
jg 0x18079b
movq %r14, %rdi
leaq (%r14,%r11), %r12
movq %r8, %r9
cmpq %r12, %r8
jl 0x1807aa
movq %r12, %r9
movq %r9, 0x40(%rsp)
subq %rdi, %r9
jg 0x18082d
incq %rbx
jmp 0x18067f
movq 0x8(%rsp), %r13
movq %r13, 0x28(%rsp)
movq 0x20(%rsp), %r13
movq %r13, 0x18(%rsp)
incq %rbx
movq 0x20(%rsp), %r13
subq %r14, %r13
addq 0x10(%rsp), %r13
testw %r10w, %r10w
movq %r13, %r10
cmovsq %rbp, %r10
movq %r10, 0x8(%rsp)
testb %dil, %dil
cmovneq %r11, %r9
movq 0x30(%rsp), %r10
addq %r9, %r10
movq %r10, 0x30(%rsp)
cmpq $0x7, %r10
setge %dil
testq %r9, %r9
setle %r10b
orb %dil, %r10b
jne 0x18067f
movzwl %dx, %edx
leal (%rdx,%r15,4), %r15d
decq %r9
jne 0x18081f
jmp 0x18067f
movq %rdi, 0x8(%rsp)
xorl %edi, %edi
movq %r13, 0x20(%rsp)
movq 0x40(%rsp), %r13
movq %r13, 0x10(%rsp)
jmp 0x180704
cmpq %rsi, %rbx
jge 0x1808da
cmpq %r8, %r14
movq 0x38(%rsp), %r9
movq 0x30(%rsp), %r11
movq 0x28(%rsp), %r12
movq 0x8(%rsp), %rbp
jg 0x1808d3
movq 0x18(%rsp), %r10
movq %r13, %rdi
movzwl (%rax,%rbx,2), %edx
movl %edx, %r8d
andl $0xc000, %r8d           # imm = 0xC000
negw %r8w
jno 0x1808ce
andl $0x3fff, %edx           # imm = 0x3FFF
leaq (%rdi,%rdx), %r13
cmpq %rcx, %r14
jl 0x1808c4
cmpq $-0x1, %r12
cmoveq %r14, %r12
cmoveq %rdi, %r10
addq %rdx, %r11
cmpq $0x6, %r11
jg 0x1808bc
testw %dx, %dx
je 0x1808bc
leal 0x2(,%r15,4), %r15d
decq %rdx
jne 0x1808af
movq %r14, 0x10(%rsp)
movq %r13, %rbp
incq %rbx
cmpq %rsi, %rbx
jl 0x18086f
jmp 0x1808f3
movq %rdi, %r13
jmp 0x1808f3
movq 0x18(%rsp), %r10
jmp 0x1808f3
movq 0x38(%rsp), %r9
movq 0x18(%rsp), %r10
movq 0x30(%rsp), %r11
movq 0x28(%rsp), %r12
movq 0x8(%rsp), %rbp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x6, %r11
jg 0x1809f2
movq 0x10(%rsp), %rdx
cmpq %r12, %rdx
jl 0x1809f2
movq %rdx, %rcx
subq %r12, %rcx
cmpq $0x6, %rcx
jg 0x1809f2
subq %r10, %rbp
jl 0x1809f2
cmpq $0x6, %rbp
jg 0x1809f2
cmpq %r9, %r12
jne 0x1809f2
cmpq 0x90(%rsp), %rdx
jne 0x1809f2
shll $0x1c, %r11d
shll $0x10, %r15d
orl %r11d, %r15d
testq %rbp, %rbp
jle 0x1809e1
movq 0x98(%rsp), %rax
movq %rbp, %r12
movq (%rax), %rbp
cmpq %r12, 0x10(%rax)
jge 0x180995
movq %rbp, %rdi
movq %r12, %rsi
movq %r10, %rbp
callq 0x2370
movq %rbp, %r10
movq %rax, %rbp
movq 0x98(%rsp), %rax
movq %rbp, (%rax)
movq %r12, 0x10(%rax)
movq 0x48(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movl 0x10(%rax), %r9d
movq %rbp, %rdi
movq %r10, %rsi
movq %r12, %rdx
movq 0x50(%rsp), %r8
callq 0x32ef8
xorl %ecx, %ecx
leaq 0x114c72(%rip), %rdx    # 0x295630
xorl %eax, %eax
shll $0x2, %eax
movsbq (%rbp,%rcx), %rsi
movl %esi, %esi
movzbl (%rdx,%rsi), %esi
movzwl %ax, %eax
orl %esi, %eax
incq %rcx
cmpq %rcx, %r12
jne 0x1809c0
movq %r12, %rbp
jmp 0x1809e3
xorl %eax, %eax
shll $0xc, %ebp
movzwl %bp, %ecx
orl %ecx, %r15d
orl %eax, %r15d
movl %r15d, %eax
movq 0xa0(%rsp), %rcx
movl %r14d, 0x4(%rcx)
movl %r13d, 0x10(%rcx)
movl %ebx, 0x8(%rcx)
movl $0x0, 0x14(%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq