// File: /workspace/github_cpp_makefile_star2_O3/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
movq %r8, -0x10(%rsp)
movl (%rcx), %r10d
movl 0x18(%rcx), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r8
shlq $0x5, %r10
movslq (%r8,%r10), %rbx
leaq (%rbx,%rax), %r11
movslq 0x4(%r8,%r10), %r9
subq %rax, %r9
incq %r9
cmpq %rsi, %r11
cmovgq %r11, %rsi
cmpq %rdx, %r9
cmovgeq %rdx, %r9
movq $-0x1, %rax
cmpq %r9, %rsi
jge 0x18050b
addq %r10, %r8
movl 0x1c(%r8), %edx
testq %rdx, %rdx
je 0x18050b
movl 0x4(%rcx), %r13d
movl 0x10(%rcx), %eax
movl 0x8(%rcx), %ebp
movq 0x70(%rdi), %r10
movl 0x18(%r8), %r11d
xorl %edi, %edi
cmpl %edx, %ebp
cmovbel %ebp, %edi
cmoval 0x8(%r8), %eax
cmovaq %rbx, %r13
testl %edi, %edi
je 0x18031e
cmpq %rsi, %r13
jl 0x18031e
leaq (%r10,%r11,2), %r8
addq $-0x2, %r8
xorl %ebx, %ebx
movq %rdi, %r14
movzwl (%r8,%rdi,2), %ebp
decq %rdi
movl %ebp, %r15d
shrl $0xe, %r15d
andl $0x3fff, %ebp           # imm = 0x3FFF
cmpl $0x2, %r15d
movl %ebp, %r12d
cmovel %ebx, %r12d
subq %r12, %r13
cmpl $0x3, %r15d
cmovel %ebx, %ebp
subl %ebp, %eax
cmpq $0x2, %r14
jb 0x18031e
cmpq %rsi, %r13
jge 0x1802e4
leaq (%r10,%r11,2), %r10
movq %rsi, %r8
shlq $0x4, %r8
movq -0x10(%rsp), %r11
subq %r8, %r11
addq $0x10, %r11
movq %r11, -0x8(%rsp)
pcmpeqd %xmm0, %xmm0
movdqa 0xb11aa(%rip), %xmm1    # 0x2314f0
movq %r13, %r14
movq %r14, %r15
cmpq %rdx, %rdi
jge 0x1804f5
cmpq %r9, %r15
jge 0x1804f5
movzwl (%r10,%rdi,2), %ebx
movl %ebx, %r8d
shrl $0xe, %r8d
movl %ebx, %r11d
andl $0x3fff, %r11d          # imm = 0x3FFF
leaq (%r15,%r11), %r13
cmpl $0x2, %r8d
movq %r13, %r14
cmoveq %r15, %r14
cmpl $0x3, %r8d
jne 0x1803a7
incq %rdi
cmpq %r15, %rsi
movq %r15, %r12
cmovgq %rsi, %r12
cmpq %r13, %r9
cmovlq %r9, %r13
cmpq %r12, %r13
jle 0x180349
movq %r13, %rbp
jmp 0x180402
addl %r11d, %eax
incq %rdi
cmpq %r15, %rsi
movq %r15, %r12
cmovgq %rsi, %r12
cmpq %r14, %r9
movq %r14, %rbp
cmovlq %r9, %rbp
cmpq %r12, %rbp
setg %r11b
cmpl $0x2, %r8d
jne 0x1803f9
movq %r15, %r8
movq %r15, %r13
subq %rsi, %r8
jl 0x180346
cmpl $0x3fff, %ebx           # imm = 0x3FFF
jbe 0x18040d
addq %r8, %r8
movl $0x1, %r11d
movq %r8, %r12
movq %r15, %r14
jmp 0x1804e3
cmpq %r12, %rbp
jle 0x180346
movb $0x1, %r11b
cmpl $0x4000, %ebx           # imm = 0x4000
jae 0x180485
leaq 0x1(%r12), %r8
cmpq %rbp, %r8
jge 0x180441
movq %r12, %r8
notq %r8
addq %rbp, %r8
movq %r12, %rbx
shlq $0x4, %rbx
addq -0x8(%rsp), %rbx
movdqu (%rbx), %xmm2
paddq %xmm1, %xmm2
movdqu %xmm2, (%rbx)
addq $0x10, %rbx
decq %r8
jne 0x18042c
movq %r14, %r13
testb %r11b, %r11b
je 0x180346
subq %rsi, %r12
movq %r12, %r8
shlq $0x4, %r8
movq -0x10(%rsp), %r11
movabsq $0x100000001, %rbx      # imm = 0x100000001
addq %rbx, (%r11,%r8)
movq %r14, %r13
cmpq %rsi, %r15
jge 0x180346
addq %r12, %r12
movabsq $0x100000001, %r11      # imm = 0x100000001
jmp 0x1804e3
leaq 0x1(%r12), %r8
cmpq %rbp, %r8
jge 0x1804bb
movq %r12, %r8
notq %r8
addq %rbp, %r8
movq %r12, %r11
shlq $0x4, %r11
addq -0x8(%rsp), %r11
movdqu (%r11), %xmm2
psubq %xmm0, %xmm2
movdqu %xmm2, (%r11)
addq $0x10, %r11
decq %r8
jne 0x1804a4
subq %rsi, %r12
movq %r12, %r8
shlq $0x4, %r8
movq -0x10(%rsp), %r11
incq (%r11,%r8)
movq %r14, %r13
cmpq %rsi, %r15
jge 0x180346
addq %r12, %r12
movl $0x1, %r11d
movq -0x10(%rsp), %r8
addq %r11, 0x8(%r8,%r12,8)
movq %r14, %r13
jmp 0x180346
movl %r15d, 0x4(%rcx)
movl %eax, 0x10(%rcx)
movl %edi, 0x8(%rcx)
movl $0x0, 0x14(%rcx)
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq