// File: /workspace/github_cpp_makefile_star2_O1/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 %rdx, %r13
movq 0xa0(%rsp), %rdx
movl (%rdx), %r15d
movl 0x18(%rdx), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r12
shlq $0x5, %r15
movslq (%r12,%r15), %r14
leaq (%r14,%rax), %rbx
movslq 0x4(%r12,%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 0x179d10
cmpq %r9, %rbx
setge %bpl
testq %rbx, %rbx
setne %bl
testb %bl, %bpl
jne 0x179d10
cmpq 0x90(%rsp), %r10
setl %r10b
cmpq %rsi, %r11
setne %sil
testb %sil, %r10b
jne 0x179d10
addq %r15, %r12
movl 0x1c(%r12), %r10d
testq %r10, %r10
je 0x179d10
movq %r9, 0x38(%rsp)
movq %r13, 0x50(%rsp)
movl 0x4(%rdx), %r13d
movl 0x10(%rdx), %esi
movl 0x8(%rdx), %r9d
movq %rdi, 0x48(%rsp)
movq 0x70(%rdi), %rax
movl 0x18(%r12), %edx
xorl %r15d, %r15d
cmpl %r10d, %r9d
cmoval %r15d, %r9d
movslq 0x8(%r12), %rbp
movq %r9, %r12
cmovbeq %rsi, %rbp
cmovaq %r14, %r13
testl %r12d, %r12d
movq %r10, 0x20(%rsp)
je 0x179999
cmpq %rcx, %r13
jl 0x179999
leaq (%rax,%rdx,2), %rsi
addq $-0x2, %rsi
xorl %edi, %edi
movq %r12, %r9
movzwl (%rsi,%r12,2), %r10d
decq %r12
movl %r10d, %r11d
shrl $0xe, %r11d
andl $0x3fff, %r10d          # imm = 0x3FFF
cmpw $0x2, %r11w
movq %r10, %rbx
cmoveq %rdi, %rbx
subq %rbx, %r13
cmpw $0x3, %r11w
cmoveq %rdi, %r10
subq %r10, %rbp
cmpq $0x2, %r9
jl 0x179999
cmpq %rcx, %r13
jge 0x17995a
leaq (%rax,%rdx,2), %rax
movq %rax, 0x40(%rsp)
movq $-0x1, %r14
movq 0x20(%rsp), %r10
cmpq %r10, %r12
jge 0x179b4e
cmpq %r8, %r13
jge 0x179b4e
movq $-0x1, %rsi
xorl %eax, %eax
xorl %r11d, %r11d
xorl %r15d, %r15d
movq $-0x1, %r9
movq $-0x1, %rdi
movq %rdi, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r9, 0x30(%rsp)
movq %rsi, 0x8(%rsp)
movq %r14, 0x28(%rsp)
incq %r12
movq %r12, %rdx
movq %r13, %rbx
movq %rbp, %rdi
movq %rdx, %r12
movq 0x40(%rsp), %rdx
movzwl -0x2(%rdx,%r12,2), %esi
movl %esi, %edx
shrl $0xe, %edx
movl %esi, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
cmpl $0x2, %edx
movq %r11, %r13
cmoveq %rax, %r13
addq %rbx, %r13
cmpl $0x3, %edx
movq %r11, %rbp
cmoveq %rax, %rbp
cmpq %rbx, %rcx
movq %rbx, %r9
cmovgq %rcx, %r9
movq %r8, %r14
cmpq %r13, %r8
jl 0x179a4b
movq %r13, %r14
addq %rdi, %rbp
cmpq %r8, %rcx
jne 0x179a82
cmpq 0x38(%rsp), %rcx
jne 0x179a94
cmpq 0x90(%rsp), %r8
jne 0x179a94
movzwl %si, %r10d
cmpl $0x3fff, %r10d          # imm = 0x3FFF
movq 0x20(%rsp), %r10
ja 0x179a94
cmpq %rcx, %rbx
jge 0x179a94
cmpq %r8, %r13
jle 0x179a94
jmp 0x179ab6
cmpw $0x2, %dx
jne 0x179a8f
cmpq %rcx, %rbx
jl 0x179a94
jmp 0x179ab6
cmpq %r9, %r14
jg 0x179ab6
cmpq %r10, %r12
jge 0x179b68
leaq 0x1(%r12), %rdx
movq %r13, %rbx
movq %rbp, %rdi
cmpq %r8, %r13
jl 0x179a02
jmp 0x179b68
cmpq $-0x1, 0x10(%rsp)
jne 0x179ae0
movq %r9, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
testw %si, %si
js 0x179ae0
movq %rdi, %r10
subq %rbx, %r10
addq %r9, %r10
movq %r10, 0x8(%rsp)
movq %r9, 0x10(%rsp)
movq %r14, %r10
subq %r9, %r10
subq %rbx, %rdi
addq %r14, %rdi
testw %si, %si
movq %rdi, %r9
cmovsq %rbp, %r9
cmpw $0x2, %dx
cmoveq %r11, %r10
movq 0x18(%rsp), %r11
addq %r10, %r11
cmpq $0x7, %r11
setge %sil
testq %r10, %r10
setle %dil
orb %sil, %dil
jne 0x179b2f
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
leal (%rdx,%r15,4), %r15d
decq %r10
jne 0x179b24
jmp 0x179b39
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %r10
cmpq %r10, %r12
jge 0x179b81
cmpq %r8, %r13
jl 0x1799dd
jmp 0x179b81
movq $-0x1, %rdi
movq $-0x1, %r9
movq $-0x1, %rsi
xorl %r11d, %r11d
jmp 0x179b81
movq 0x28(%rsp), %r14
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %r9
movq 0x18(%rsp), %r11
movq 0x10(%rsp), %rdi
cmpq %r10, %r12
jge 0x179bef
cmpq %r8, %r13
jg 0x179bef
movq 0x40(%rsp), %rax
movzwl (%rax,%r12,2), %edx
movl %edx, %eax
andl $0xc000, %eax           # imm = 0xC000
negw %ax
jno 0x179bef
andl $0x3fff, %edx           # imm = 0x3FFF
leaq (%rdx,%rbp), %rbx
cmpq %rcx, %r13
jl 0x179bdd
cmpq $-0x1, %rdi
cmoveq %r13, %rdi
cmoveq %rbp, %rsi
addq %rdx, %r11
cmpq $0x6, %r11
jg 0x179bd7
testw %dx, %dx
je 0x179bd7
leal 0x2(,%r15,4), %r15d
decq %rdx
jne 0x179bca
movq %r13, %r14
movq %rbx, %r9
incq %r12
cmpq %r10, %r12
jge 0x179bf2
movq %rbx, %rbp
cmpq %r8, %r13
jle 0x179b8b
jmp 0x179bf2
movq %rbp, %rbx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x6, %r11
jg 0x179cf6
cmpq %rdi, %r14
jl 0x179cf6
movq %r14, %rcx
subq %rdi, %rcx
cmpq $0x6, %rcx
jg 0x179cf6
subq %rsi, %r9
jl 0x179cf6
cmpq $0x6, %r9
jg 0x179cf6
cmpq 0x38(%rsp), %rdi
jne 0x179cf6
cmpq 0x90(%rsp), %r14
jne 0x179cf6
shll $0x1c, %r11d
shll $0x10, %r15d
orl %r11d, %r15d
testq %r9, %r9
jle 0x179ce3
movq 0x98(%rsp), %rbp
cmpq %r9, 0x10(%rbp)
movq %r9, %r14
jge 0x179c89
movq (%rbp), %rdi
movq %rsi, 0x8(%rsp)
movq %r9, %rsi
callq 0x2370
movq 0x8(%rsp), %rsi
movq %rax, (%rbp)
movq %r14, 0x10(%rbp)
movq (%rbp), %rbp
movq 0x48(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movl 0x10(%rax), %r9d
movq %rbp, %rdi
movq %r14, %rdx
movq 0x50(%rsp), %r8
callq 0x337f7
movq %r14, %r9
cmpq $0x2, %r14
movl $0x1, %ecx
cmovgeq %r14, %rcx
xorl %edx, %edx
leaq 0x11096d(%rip), %rsi    # 0x28a630
xorl %eax, %eax
shll $0x2, %eax
movsbq (%rbp,%rdx), %rdi
movl %edi, %edi
movzbl (%rsi,%rdi), %edi
movzwl %ax, %eax
orl %edi, %eax
incq %rdx
cmpq %rdx, %rcx
jne 0x179cc5
jmp 0x179ce5
xorl %eax, %eax
shll $0xc, %r9d
movzwl %r9w, %ecx
orl %ecx, %r15d
orl %eax, %r15d
movl %r15d, %eax
movq 0xa0(%rsp), %rcx
movl %r13d, 0x4(%rcx)
movl %ebx, 0x10(%rcx)
movl %r12d, 0x8(%rcx)
movl $0x0, 0x14(%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq