// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movl 0x18(%rax), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, %eax
movq %rax, -0x148(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
addq -0x148(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
addq $0x1, %rax
subq -0x148(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x150(%rbp), %rax
jge 0x24b564
movq -0x150(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x158(%rbp), %rax
jle 0x24b57c
movq -0x158(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x24b593
movq $-0x1, -0x8(%rbp)
jmp 0x24bc31
movq -0x18(%rbp), %rax
cmpq -0x150(%rbp), %rax
jl 0x24b5ad
movq -0x18(%rbp), %rax
movq %rax, -0x188(%rbp)
jmp 0x24b5bb
movq -0x150(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x158(%rbp), %rax
jg 0x24b5e0
movq -0x20(%rbp), %rax
movq %rax, -0x190(%rbp)
jmp 0x24b5ee
movq -0x158(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x190(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jg 0x24b610
movq $-0x1, -0x8(%rbp)
jmp 0x24bc31
jmp 0x24b612
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x38(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0x90(%rbp)
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x124(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x120(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x130(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x12c(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x114(%rbp)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x14(%rax), %eax
movl %eax, -0x118(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0x24b721
movq $-0x1, -0x8(%rbp)
jmp 0x24bc31
movq -0x98(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpq $0x0, -0x50(%rbp)
jl 0x24b743
movq -0x50(%rbp), %rax
cmpq -0x160(%rbp), %rax
jle 0x24b761
movq $0x0, -0x50(%rbp)
movslq -0x124(%rbp), %rax
movq %rax, -0x40(%rbp)
movslq -0x130(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x24b763
xorl %eax, %eax
cmpq $0x0, -0x50(%rbp)
movb %al, -0x191(%rbp)
jle 0x24b783
movq -0x40(%rbp), %rax
cmpq -0x18(%rbp), %rax
setge %al
movb %al, -0x191(%rbp)
movb -0x191(%rbp), %al
testb $0x1, %al
jne 0x24b792
jmp 0x24b816
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x168(%rbp)
cmpq $0x2, -0x168(%rbp)
je 0x24b7e5
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movslq %eax, %rcx
movq -0x40(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpq $0x3, -0x168(%rbp)
je 0x24b811
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movslq %eax, %rcx
movq -0x48(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0x24b763
jmp 0x24b818
movq -0x50(%rbp), %rcx
xorl %eax, %eax
cmpq -0x160(%rbp), %rcx
movb %al, -0x192(%rbp)
jge 0x24b83e
movq -0x40(%rbp), %rax
cmpq -0x20(%rbp), %rax
setl %al
movb %al, -0x192(%rbp)
movb -0x192(%rbp), %al
testb $0x1, %al
jne 0x24b84d
jmp 0x24bbf7
movq -0x40(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0xe, %eax
cltq
movq %rax, -0x168(%rbp)
cmpq $0x2, -0x168(%rbp)
je 0x24b89b
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x3, -0x168(%rbp)
je 0x24b8c3
movq -0x88(%rbp), %rax
movq -0x50(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
cltq
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x170(%rbp), %rax
jl 0x24b8f4
movq -0x18(%rbp), %rax
movq %rax, -0x1a0(%rbp)
jmp 0x24b902
movq -0x170(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x178(%rbp), %rax
jg 0x24b927
movq -0x20(%rbp), %rax
movq %rax, -0x1a8(%rbp)
jmp 0x24b935
movq -0x178(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x1a8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jle 0x24b95b
movq -0x60(%rbp), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x1b0(%rbp)
jmp 0x24b966
xorl %eax, %eax
movq %rax, -0x1b0(%rbp)
jmp 0x24b966
movq -0x1b0(%rbp), %rax
movq %rax, -0x180(%rbp)
cmpq $0x2, -0x168(%rbp)
je 0x24b98f
cmpq $0x0, -0x180(%rbp)
jne 0x24b98d
jmp 0x24b818
jmp 0x24b9b0
movq -0x170(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x24b9a9
movq -0x170(%rbp), %rax
cmpq -0x20(%rbp), %rax
jl 0x24b9ae
jmp 0x24b818
jmp 0x24b9b0
cmpq $0x0, -0x168(%rbp)
jne 0x24bb0b
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x24ba65
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movabsq $0x100000000, %rdx      # imm = 0x100000000
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq 0x8(%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movabsq $0x100000000, %rdx      # imm = 0x100000000
addq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x24b9ca
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x24bb06
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movabsq $0x100000000, %rdx      # imm = 0x100000000
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
cmpq -0x170(%rbp), %rax
jle 0x24bb04
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq 0x8(%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movabsq $0x100000000, %rdx      # imm = 0x100000000
addq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
jmp 0x24bb06
jmp 0x24bbf2
cmpq $0x2, -0x168(%rbp)
je 0x24bbd0
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x24bb75
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq 0x8(%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x24bb25
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x24bbce
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
cmpq -0x170(%rbp), %rax
jle 0x24bbcc
movq -0x30(%rbp), %rax
movq -0x68(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq 0x8(%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rax,%rcx,8)
jmp 0x24bbce
jmp 0x24bbf0
movq -0x30(%rbp), %rax
movq -0x170(%rbp), %rcx
subq -0x18(%rbp), %rcx
shlq %rcx
movq 0x8(%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, 0x8(%rax,%rcx,8)
jmp 0x24bbf2
jmp 0x24b818
movq -0x40(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x50(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x14(%rax)
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nop