// File: /workspace/github_cpp_makefile_star2_O1/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), %r8d
movl 0x18(%rcx), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r14
shlq $0x5, %r8
movslq (%r14,%r8), %r15
leaq (%r15,%rax), %r10
movslq 0x4(%r14,%r8), %r9
subq %rax, %r9
incq %r9
cmpq %rsi, %r10
cmovgq %r10, %rsi
cmpq %rdx, %r9
cmovgeq %rdx, %r9
movq $-0x1, %rax
cmpq %r9, %rsi
jge 0x17985a
addq %r8, %r14
movl 0x1c(%r14), %edx
testq %rdx, %rdx
je 0x17985a
movl 0x4(%rcx), %r10d
movl 0x10(%rcx), %eax
movl 0x8(%rcx), %r8d
movq 0x70(%rdi), %r11
movl 0x18(%r14), %ebx
xorl %edi, %edi
cmpl %edx, %r8d
cmovbel %r8d, %edi
cmoval 0x8(%r14), %eax
cmovaq %r15, %r10
testl %edi, %edi
je 0x1796b0
cmpq %rsi, %r10
jl 0x1796b0
leaq (%r11,%rbx,2), %r14
addq $-0x2, %r14
xorl %r15d, %r15d
movq %rdi, %r8
movzwl (%r14,%rdi,2), %r12d
movl %r12d, %ebp
shrl $0xe, %ebp
andl $0x3fff, %r12d          # imm = 0x3FFF
cmpl $0x2, %ebp
movl %r12d, %r13d
cmovel %r15d, %r13d
decq %rdi
subq %r13, %r10
cmpl $0x3, %ebp
movl $0x0, %r13d
cmovneq %r12, %r13
subq %r13, %rax
cmpq $0x2, %r8
jl 0x1796b0
cmpq %rsi, %r10
jge 0x179670
cmpq %rdx, %rdi
jge 0x179841
cmpq %r9, %r10
jge 0x179841
leaq (%r11,%rbx,2), %r11
movabsq $0x100000001, %rbx      # imm = 0x100000001
movq %rsi, %r8
shlq $0x4, %r8
movq -0x10(%rsp), %r14
subq %r8, %r14
addq $0x18, %r14
movq %r14, -0x8(%rsp)
movzwl (%r11,%rdi,2), %r8d
movl %r8d, %r15d
shrl $0xe, %r15d
movl %r8d, %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
cmpl $0x2, %r15d
movl %r14d, %r12d
movl $0x0, %r13d
cmovel %r13d, %r12d
addq %r10, %r12
cmpl $0x3, %r15d
movl $0x0, %r13d
cmovneq %r14, %r13
cmpq %r10, %rsi
movq %r10, %rbp
cmovgq %rsi, %rbp
cmpq %r12, %r9
movq %r12, %r14
cmovlq %r9, %r14
cmpl $0x2, %r15d
jne 0x179744
cmpq %rsi, %r10
jge 0x17974d
jmp 0x179828
cmpq %rbp, %r14
jle 0x179828
movzwl %r8w, %r8d
cmpl $0x3fff, %r8d           # imm = 0x3FFF
ja 0x1797b1
leaq 0x1(%rbp), %r8
cmpq %r14, %r8
jge 0x179788
movq %rbp, %r8
subq %r14, %r8
incq %r8
movq %rbp, %r15
shlq $0x4, %r15
addq -0x8(%rsp), %r15
addq %rbx, -0x8(%r15)
addq %rbx, (%r15)
addq $0x10, %r15
incq %r8
jne 0x179778
cmpq %rbp, %r14
jle 0x179828
subq %rsi, %rbp
movq %rbp, %r8
shlq $0x4, %r8
movq -0x10(%rsp), %r14
addq %rbx, (%r14,%r8)
cmpq %rsi, %r10
jge 0x179828
addq %rbp, %rbp
movq %rbx, %r8
jmp 0x1797c6
cmpl $0x2, %r15d
jne 0x1797d2
subq %rsi, %r10
addq %r10, %r10
movl $0x1, %r8d
movq %r10, %rbp
movq -0x10(%rsp), %r10
addq %r8, 0x8(%r10,%rbp,8)
jmp 0x179828
leaq 0x1(%rbp), %r8
cmpq %r14, %r8
jge 0x179800
movq %rbp, %r8
subq %r14, %r8
incq %r8
movq %rbp, %r15
shlq $0x4, %r15
addq -0x8(%rsp), %r15
incq -0x8(%r15)
incq (%r15)
addq $0x10, %r15
incq %r8
jne 0x1797f0
cmpq %rbp, %r14
jle 0x179828
subq %rsi, %rbp
movq %rbp, %r8
shlq $0x4, %r8
movq -0x10(%rsp), %r14
incq (%r14,%r8)
cmpq %rsi, %r10
jge 0x179828
addq %rbp, %rbp
movl $0x1, %r8d
jmp 0x1797c6
addq %r13, %rax
incq %rdi
cmpq %rdx, %rdi
jge 0x179844
movq %r12, %r10
cmpq %r9, %r12
jl 0x1796e8
jmp 0x179844
movq %r10, %r12
movl %r12d, 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