// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, %r12
movq 0xa0(%rsp), %r14
movl (%r14), %r15d
movl 0x18(%r14), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r13
shlq $0x5, %r15
movslq (%r13,%r15), %r9
leaq (%r9,%rax), %rbx
movslq 0x4(%r13,%r15), %r10
subq %rax, %r10
leaq 0x1(%r10), %r11
cmpq %rcx, %rbx
cmovgq %rbx, %rcx
cmpq %r8, %r11
cmovlq %r11, %r8
pushq $-0x1
popq %rax
cmpq %r8, %rcx
jg 0x13dbfe
cmpq %r12, %rbx
setge %bpl
testq %rbx, %rbx
setne %bl
testb %bl, %bpl
jne 0x13dbfe
cmpq 0x90(%rsp), %r10
setl %r10b
cmpq %rsi, %r11
setne %sil
testb %sil, %r10b
jne 0x13dbfe
addq %r15, %r13
movl 0x1c(%r13), %esi
testq %rsi, %rsi
je 0x13dbfe
movq %r12, 0x30(%rsp)
movq %rdx, 0x50(%rsp)
movl 0x4(%r14), %ebx
movl 0x10(%r14), %eax
movl 0x8(%r14), %r12d
movslq 0x8(%r13), %rdx
xorl %r10d, %r10d
movq %rsi, 0x40(%rsp)
cmpl %esi, %r12d
cmovbeq %rax, %rdx
movq %rdi, 0x48(%rsp)
movq 0x70(%rdi), %rax
movl 0x18(%r13), %esi
cmoval %r10d, %r12d
leaq (%rax,%rsi,2), %rax
cmovaq %r9, %rbx
testq %r12, %r12
jle 0x13d870
cmpq %rcx, %rbx
jl 0x13d870
movzwl -0x2(%rax,%r12,2), %esi
decq %r12
movl %esi, %r9d
shrl $0xe, %r9d
andl $0x3fff, %esi           # imm = 0x3FFF
cmpw $0x2, %r9w
movq %rsi, %r11
cmoveq %r10, %r11
subq %r11, %rbx
cmpw $0x3, %r9w
cmoveq %r10, %rsi
subq %rsi, %rdx
jmp 0x13d833
pushq $-0x1
popq %rbp
movq %rbp, 0x28(%rsp)
movq %rbp, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %edi, %edi
movq %r12, %r15
movq %rdi, 0x38(%rsp)
movq %rbp, 0x8(%rsp)
movq %rbx, %rbp
movq %rdx, %rbx
movq %rdx, %r13
movq 0x40(%rsp), %r9
movq %r15, %r12
cmpq %r9, %r15
jge 0x13da55
cmpq %r8, %rbp
jge 0x13da55
movzwl (%rax,%r12,2), %esi
movl %esi, %r10d
shrl $0xe, %r10d
movl %esi, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
cmpw $0x2, %r10w
sete %r14b
cmpw $0x3, %r10w
movl $0x0, %edx
cmovneq %r11, %rdx
addq %r13, %rdx
cmpq %r8, %rcx
je 0x13d908
cmpw $0x2, %r10w
jne 0x13d947
leaq 0x1(%r12), %r15
movq %rdx, %r13
cmpq %rcx, %rbp
jl 0x13d8a8
jmp 0x13d986
movq %rbx, %r13
cmpw $0x2, %r10w
movq %r11, %rbx
movl $0x0, %r15d
cmoveq %r15, %rbx
addq %rbp, %rbx
cmpq 0x30(%rsp), %rcx
jne 0x13d972
cmpq 0x90(%rsp), %r8
jne 0x13d972
cmpw $0x3fff, %si            # imm = 0x3FFF
ja 0x13d972
cmpq %rcx, %rbp
jge 0x13d972
cmpq %rcx, %rbx
jle 0x13d972
jmp 0x13da0a
movq %rcx, %rdi
cmpq %rbp, %rcx
jg 0x13d952
movq %rbp, %rdi
leaq (%r11,%rbp), %rbx
movq %r8, %r15
cmpq %rbx, %r8
jl 0x13d961
movq %rbx, %r15
movq %r15, %r9
subq %rdi, %r9
jg 0x13da16
movq 0x40(%rsp), %r9
incq %r12
movq %rbx, %rbp
movq %rdx, %rbx
movq %r12, %r15
movq %rdx, %r13
jmp 0x13d8a8
movq %rbx, %r13
movb $0x1, %r14b
movw $0x2, %r10w
movq %rbp, %rbx
movq %rbp, 0x8(%rsp)
movq %rbp, %rdi
xorl %r9d, %r9d
movq 0x28(%rsp), %r15
cmpq $-0x1, %r15
jne 0x13d9d0
testw %si, %si
js 0x13d9c6
movq %r13, %r15
subq %rbp, %r15
addq 0x8(%rsp), %r15
movq %r15, 0x18(%rsp)
movq 0x8(%rsp), %r15
jmp 0x13d9d0
movq 0x8(%rsp), %r15
movq %r13, 0x18(%rsp)
movq %r15, 0x28(%rsp)
leaq 0x1(%r12), %r15
subq %rbp, %r13
movq %rdi, %rbp
addq %rdi, %r13
testw %si, %si
cmovsq %rdx, %r13
movq %r13, 0x20(%rsp)
testb %r14b, %r14b
cmovneq %r11, %r9
movq 0x38(%rsp), %rdi
addq %r9, %rdi
cmpq $0x6, %rdi
jg 0x13d890
jmp 0x13da26
movq 0x30(%rsp), %rdi
movq %rdi, 0x8(%rsp)
jmp 0x13d99c
movq %rdi, 0x8(%rsp)
xorl %r14d, %r14d
movq %r15, %rdi
jmp 0x13d99f
movzwl %r10w, %esi
testq %r9, %r9
movl $0x0, %r10d
cmovleq %r10, %r9
incq %r12
movq 0x10(%rsp), %r10
subq $0x1, %r9
jb 0x13da4b
leal (%rsi,%r10,4), %r10d
jmp 0x13da3f
movq %r10, 0x10(%rsp)
jmp 0x13d88d
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r10
movq 0x38(%rsp), %r11
movq 0x28(%rsp), %r14
movq %r13, %rbx
cmpq %r9, %r12
jge 0x13dadc
cmpq %r8, %rbp
jg 0x13dadc
movzwl (%rax,%r12,2), %edx
movl %edx, %edi
andl $0xc000, %edi           # imm = 0xC000
negw %di
jno 0x13dadc
movl %edx, %edi
andl $0x3fff, %edi           # imm = 0x3FFF
leaq (%rbx,%rdi), %r13
incq %r12
cmpq %rcx, %rbp
jl 0x13da69
cmpq $-0x1, %r14
cmoveq %rbp, %r14
cmoveq %rbx, %r10
addq %rdi, %r11
movq %rbp, 0x8(%rsp)
movq %r13, 0x20(%rsp)
cmpq $0x6, %r11
jg 0x13da69
andl $0x3fff, %edx           # imm = 0x3FFF
subq $0x1, %rdx
jb 0x13dad0
leal 0x2(,%rsi,4), %esi
jmp 0x13dac1
movq %rbp, 0x8(%rsp)
movq %r13, 0x20(%rsp)
jmp 0x13da69
pushq $-0x1
popq %rax
cmpq $0x6, %r11
jg 0x13dbe8
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
jl 0x13dbe8
movq %rdi, %rcx
subq %r14, %rcx
cmpq $0x6, %rcx
jg 0x13dbe8
movq 0x20(%rsp), %rdx
subq %r10, %rdx
jl 0x13dbe8
cmpq $0x6, %rdx
jg 0x13dbe8
cmpq 0x30(%rsp), %r14
jne 0x13dbe8
cmpq 0x90(%rsp), %rdi
jne 0x13dbe8
shll $0x1c, %r11d
movq %rsi, %rdi
shll $0x10, %edi
orl %r11d, %edi
testq %rdx, %rdx
jle 0x13dbdc
movq 0x98(%rsp), %rax
movq (%rax), %r13
cmpq %rdx, 0x10(%rax)
movq %rdx, %r14
movq %rdi, %r15
jge 0x13db8b
movq %r13, %rdi
movq %rdx, %rsi
movq %r10, %r13
callq 0x2370
movq %r14, %rdx
movq %r13, %r10
movq %rax, %r13
movq 0x98(%rsp), %rax
movq %r13, (%rax)
movq %r14, 0x10(%rax)
movq 0x48(%rsp), %rax
movzbl 0x1c(%rax), %ecx
movl 0x10(%rax), %r9d
movq %r13, %rdi
movq %r10, %rsi
movq 0x50(%rsp), %r8
callq 0x6c9e
movq %r14, %rdx
xorl %eax, %eax
leaq 0x115a7c(%rip), %rcx    # 0x253630
xorl %r8d, %r8d
movq %r15, %rdi
cmpq %r8, %rdx
je 0x13dbd7
shll $0x2, %eax
movsbq (%r13,%r8), %rsi
movl %esi, %esi
movzbl (%rcx,%rsi), %esi
orl %eax, %esi
incq %r8
movl %esi, %eax
jmp 0x13dbba
movzwl %ax, %eax
jmp 0x13dbde
xorl %eax, %eax
orl %eax, %edi
shll $0xc, %edx
movzwl %dx, %eax
orl %edi, %eax
movq 0xa0(%rsp), %rcx
movl %ebp, 0x4(%rcx)
movl %ebx, 0x10(%rcx)
movl %r12d, 0x8(%rcx)
andl $0x0, 0x14(%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq