// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: extract_sub_cigar_mm(overlap_region*, long, long, ul_ov_t*, unsigned long*)
int64_t extract_sub_cigar_mm(overlap_region *z, int64_t s, int64_t e, ul_ov_t *p, uint64_t *ct)
{
    int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, t;
    bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0; 
    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;
    os = MAX(s, s0); oe = MIN(e, e0);
    if(oe <= os) return -1;

    set_bit_extz_t(ez, (*z), wk);
    if(!ez.cigar.n) return -1;
    int64_t cn = ez.cigar.n, op; int64_t ws, we, ovlp; 
    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)); 
    }

     //some cigar will span s or e
    while (ck < cn && xk < e) {//[s, e)
        ws = xk; 
        op = ez.cigar.a[ck]>>14;
        ///op == 3: -> x; op == 2: -> y;
        if(op!=2) xk += (ez.cigar.a[ck]&(0x3fff));
        if(op!=3) yk += (ez.cigar.a[ck]&(0x3fff)); 
        ck++; we = xk;

        os = MAX(s, ws); oe = MIN(e, we);
        ovlp = ((oe>os)? (oe-os):0);
        if(op != 2) {
            if(!ovlp) continue;
        } else {///ws == we
            if(ws < s || ws >= e) continue;
        }
        

        if(op == 0) {
            for (t = os + 1; t < oe; t++) {
                ct[(t-s)<<1]++; ct[(t-s)<<1] += ((uint64_t)(0x100000000));
                ct[((t-s)<<1)+1]++; ct[((t-s)<<1)+1] += ((uint64_t)(0x100000000));
            }

            t = os;
            if(t < oe) {
                ct[(t-s)<<1]++; ct[(t-s)<<1] += ((uint64_t)(0x100000000));
                if(os > ws) {
                    ct[((t-s)<<1)+1]++; ct[((t-s)<<1)+1] += ((uint64_t)(0x100000000));
                }
            }
        } else if(op!=2) {
            for (t = os + 1; t < oe; t++) {
                ct[(t-s)<<1]++; 
                ct[((t-s)<<1)+1]++;
            }

            t = os;
            if(t < oe) {
                ct[(t-s)<<1]++; 
                if(os > ws) {
                    ct[((t-s)<<1)+1]++;
                }
            }
        } else {
            ct[((ws-s)<<1)+1]++; ///ct[((ws-s)<<1)+1] += ((uint64_t)(0x100000000));
        }
    }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl (%rcx), %r10d
movl 0x18(%rcx), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %rbx
shlq $0x5, %r10
movslq (%rbx,%r10), %r14
leaq (%r14,%rax), %r11
movslq 0x4(%rbx,%r10), %r9
subq %rax, %r9
incq %r9
cmpq %rsi, %r11
cmovgq %r11, %rsi
cmpq %rdx, %r9
cmovgeq %rdx, %r9
movabsq $-0x1, %rax
cmpq %r9, %rsi
jge 0x13d748
addq %r10, %rbx
movl 0x1c(%rbx), %edx
testq %rdx, %rdx
je 0x13d748
movl 0x4(%rcx), %r10d
movl 0x10(%rcx), %eax
movl 0x8(%rcx), %r13d
movq 0x70(%rdi), %rdi
xorl %r11d, %r11d
cmpl %edx, %r13d
cmoval 0x8(%rbx), %eax
movl 0x18(%rbx), %ebx
cmoval %r11d, %r13d
leaq (%rdi,%rbx,2), %rdi
cmovaq %r14, %r10
testq %r13, %r13
jle 0x13d596
cmpq %rsi, %r10
jl 0x13d596
movzwl -0x2(%rdi,%r13,2), %ebp
decq %r13
movl %ebp, %ebx
shrl $0xe, %ebx
andl $0x3fff, %ebp           # imm = 0x3FFF
cmpl $0x2, %ebx
movl %ebp, %r14d
cmovel %r11d, %r14d
subq %r14, %r10
cmpl $0x3, %ebx
cmovel %r11d, %ebp
subl %ebp, %eax
jmp 0x13d560
movq %rsi, %r11
shlq $0x4, %r11
movq %r8, -0x10(%rsp)
subq %r11, %r8
addq $0x10, %r8
movq %r8, -0x8(%rsp)
xorl %ebx, %ebx
movdqa 0xa5ec8(%rip), %xmm0    # 0x1e3480
pcmpeqd %xmm1, %xmm1
movq %r13, %r12
cmpq %rdx, %r13
jge 0x13d72f
cmpq %r9, %r10
jge 0x13d72f
movzwl (%rdi,%r12,2), %r15d
movl %r15d, %r14d
shrl $0xe, %r14d
movl %r15d, %r8d
andl $0x3fff, %r8d           # imm = 0x3FFF
cmpl $0x3, %r14d
cmovel %ebx, %r8d
addl %r8d, %eax
cmpl $0x2, %r14d
jne 0x13d633
leaq 0x1(%r12), %r13
movq %r10, %rbp
subq %rsi, %rbp
jl 0x13d5bc
movq %r10, %r13
movq %r10, %r8
movq %r10, %r11
cmpw $0x4000, %r15w          # imm = 0x4000
jb 0x13d665
addq %rbp, %rbp
movq %rbp, %r8
movabsq $0x1, %r14
movq %r10, %r11
movq -0x10(%rsp), %r15
jmp 0x13d71c
movl %r15d, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
addq %r10, %r11
cmpq %r10, %rsi
movq %r10, %r8
cmovgq %rsi, %r8
cmpq %r11, %r9
movq %r11, %r13
cmovlq %r9, %r13
cmpq %r8, %r13
jle 0x13d721
cmpw $0x4000, %r15w          # imm = 0x4000
jae 0x13d6c5
leaq 0x1(%r8), %r15
movq %r8, %rbp
shlq $0x4, %rbp
addq -0x8(%rsp), %rbp
cmpq %r13, %r15
jge 0x13d691
movdqu (%rbp), %xmm2
paddq %xmm0, %xmm2
movdqu %xmm2, (%rbp)
incq %r15
addq $0x10, %rbp
jmp 0x13d675
cmpl $0x2, %r14d
je 0x13d721
subq %rsi, %r8
movq %r8, %r14
shlq $0x4, %r14
movq -0x10(%rsp), %r15
movabsq $0x100000001, %r13      # imm = 0x100000001
addq %r13, (%r15,%r14)
cmpq %rsi, %r10
jge 0x13d721
movq %r13, %r14
addq %r8, %r8
jmp 0x13d71c
leaq 0x1(%r8), %r15
movq %r8, %rbp
shlq $0x4, %rbp
addq -0x8(%rsp), %rbp
cmpq %r13, %r15
jge 0x13d6f1
movdqu (%rbp), %xmm2
psubq %xmm1, %xmm2
movdqu %xmm2, (%rbp)
incq %r15
addq $0x10, %rbp
jmp 0x13d6d5
cmpl $0x2, %r14d
je 0x13d721
subq %rsi, %r8
movq %r8, %r14
shlq $0x4, %r14
movq -0x10(%rsp), %r15
incq (%r15,%r14)
cmpq %rsi, %r10
jge 0x13d721
addq %r8, %r8
movabsq $0x1, %r14
addq %r14, 0x8(%r15,%r8,8)
incq %r12
movq %r11, %r10
movq %r12, %r13
jmp 0x13d5bc
movl %r10d, 0x4(%rcx)
movl %eax, 0x10(%rcx)
movl %r12d, 0x8(%rcx)
andl $0x0, 0x14(%rcx)
movabsq $0x1, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq