// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: extract_sub_cigar_cns(overlap_region*, long, long, long, long, long, rr_seq_t*, ul_ov_t*, cns_gfa*, asg32_v*, unsigned long, unsigned int)
uint32_t extract_sub_cigar_cns(overlap_region *z, int64_t s, int64_t e, int64_t iws, int64_t iwe, int64_t s_end, rr_seq_t *ssq, ul_ov_t *p, cns_gfa *cns, asg32_v* b32, uint64_t max_trace, uint32_t rid)
{
    // if(s == 10539 && e == 10760) {
        // fprintf(stderr, "\n>>>>>>iw::[%ld, %ld)\tw::[%ld, %ld)\tox::[%u, %u)<<<<<<\n", iws, iwe, s, e, z->x_pos_s, z->x_pos_e + 1);
    // }
    
    int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), yk = ovlp_cur_yoff(*p), ck = ovlp_cur_coff(*p), os, oe, ots, ote, ol, insert_pos = ((iws == iwe)? (0): (-1));
    bit_extz_t ez; int64_t bd = ovlp_bd(*p), s0, e0, ii[2], it[2]; uint64_t pe = (uint64_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

    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; char *tstr;
    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)); 
    }

    if(s_end == 0 && s == iws) s_end = 0;
    else s_end = 1;

    // if(s_end == 0 || s != iws) {///do not conside the insertion before s
    //     while (ck < cn && xk < s) {
    //     }
    // }

    // if(s == 10539 && e == 10760) {
        // fprintf(stderr, "ck::%ld, cn::%ld, xk::%ld, yk::%ld\n", ck, cn, xk, yk);
    // }

    // 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; 
    ssq->s = ssq->e = 0; ssq->n0 = e - s; 
    ssq->id = z->y_id; ssq->rev = z->y_pos_strand;
    if(ssq->n0 == 0) {ssq->n0 = ssq->n1;} 

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

        // fprintf(stderr, "ck::%ld, cn::%ld, op::%u, ol::%ld\n", ck, cn, op, ol);

        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((s_end == 0) && (op == 2) && (ws == s)) continue;///skip the insertion just before s
        
        

        if(op < 2) {
            ots = os - ws + wts; ote = oe - ws + wts;
        } else {///op == 2: more y; p == 3: more x
            ots = wts; ote = wte;
        } 



        if(ii[0] == -1) {
            ii[0] = os; it[0] = ots;
            // if(op < 2) {
            //     it[0] = os - ws + wts;
            // } else {///op == 2: more y; p == 3: more x
            //     it[0] = wts;
            // } 
        }

        ii[1] = oe; it[1] = ote;
        // 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;

        tstr = NULL;
        if(op != 0) tstr = get_sub_seq(ssq, ots, ote);

        // if(s == 10539 && e == 10760) {
            // fprintf(stderr, "+0-%ld%c(%.*s)\tpe::%lu", ol, cm[op], tstr?((int)(ote - ots)):0, tstr, pe);
        // }
        // fprintf(stderr, ">q::[%ld, %ld)\n", ws, we);
        // fprintf(stderr, "%ld%c(%.*s)", ol, cm[op], tstr?((int)(ote - ots)):0, tstr);

        pe = append_cns_g(cns, tstr, ote - ots, os - iws, oe - iws, op, ol, pe, b32, max_trace, rid, insert_pos);

        // if(s == 10539 && e == 10760) {
            // fprintf(stderr, "+1-pe::%lu\n", pe);
        // }
    }

    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;

        for (ck++; (ck < cn) && (op == (ez.cigar.a[ck]>>14)); ck++) {
            ol += (ez.cigar.a[ck]&(0x3fff));
        }
        yk += ol;//yk += (ez.cigar.a[ck]&(0x3fff)); 
        we = xk; wte = yk;


        if(ws >= s && ws <= e) {
            ots = wts; ote = wte;
            if(ii[0] == -1) {
                ii[0] = ws; it[0] = ots;
            }
            ii[1] = we; it[1] = ote;
            

            tstr = NULL;
            if(op != 0) tstr = get_sub_seq(ssq, ots, ote);
            // if(s == 10539 && e == 10760) {
                // fprintf(stderr, "-0-%ld%c(%.*s)\tpe::%lu", ol, cm[op], tstr?((int)(ote - ots)):0, tstr, pe);
            // }
            // fprintf(stderr, "%ld%c(%.*s)", ol, cm[op], tstr?((int)(ote - ots)):0, tstr);

            pe = append_cns_g(cns, tstr, ote - ots, ws - iws, we - iws, op, ol, pe, b32, max_trace, rid, insert_pos);

            // if(s == 10539 && e == 10760) {
                // fprintf(stderr, "-1-pe::%lu\n", pe);
            // }
        }
    }

    // if(s == 10539 && e == 10760) {
        // fprintf(stderr, "\tx::[%ld, %ld)\ty::[%ld, %ld)\tiw::[%ld, %ld)\n", ii[0], ii[1], it[0], it[1], iws, iwe);
    // }

    // prt_cns_arc(cns, 0, __func__);
    // prt_cns_arc(cns, 1, __func__);
    if(ii[1] == -1) return -1;///it is possible when s == e and the cigar here is not a match

    uint64_t ae = 1;
    if((ii[1] == iwe)) {
        ae = 1;///end node
    } else {
        ae = ii[1] + cns->off - iws;
    }

    // if(s == 10539 && e == 10760) {
        // fprintf(stderr, "pe::%lu, ae::%lu, n_nodes::%u, ii[1]::%ld\n", pe, ae, (uint32_t)cns->n, ii[1]);
    // }

    if(pe == ((uint64_t)-1)) pe = 0;///start node

    ///if iws == iwe and the cigar is a match, pe will be equal to ae
    if(pe != ae) {
        if(!add_cns_arc(cns, pe, ae, 1, /**ae==1?1:0**/1)) {
            insert_cns_arc(cns, pe, ae, 1, /**ae==1?1:0**/1, rid); 
            insert_cns_arc(cns, ae, pe, 0, /**ae==1?1:0**/1, rid);
        } else {
            add_cns_arc(cns, ae, pe, 0, /**ae==1?1:0**/1);
        }
    }    


    // prt_cns_arc(cns, 0, __func__);
    // prt_cns_arc(cns, 1, __func__);


    // if(s == 10539 && e == 10760) {
    //     fprintf(stderr, "-end-pe::%lu, ae::%lu\n", pe, ae);
    // }

    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
subq $0x68, %rsp
movq 0xa8(%rsp), %r14
movl (%r14), %ebx
movq $0x0, 0x28(%rsp)
cmpq %r8, %rcx
setne %r12b
movl 0x18(%r14), %r10d
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r13
shlq $0x5, %rbx
movslq (%r13,%rbx), %rbp
leaq (%r10,%rbp), %r11
movslq 0x4(%r13,%rbx), %r15
subq %r10, %r15
incq %r15
cmpq %rsi, %r11
cmovgq %r11, %rsi
cmpq %rdx, %r15
cmovgeq %rdx, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r15, 0x18(%rsp)
cmpq %rsi, %r15
jl 0x18226c
addq %rbx, %r13
movl 0x1c(%r13), %ebx
testq %rbx, %rbx
je 0x18226c
movq %rsi, %r11
movq %r8, 0x50(%rsp)
movl 0x4(%r14), %r15d
movl 0x10(%r14), %edx
movl 0x8(%r14), %r10d
movq 0x28(%rsp), %rax
movb %r12b, %al
movq %rax, 0x28(%rsp)
movq 0x70(%rdi), %rsi
movl 0x18(%r13), %eax
xorl %r12d, %r12d
cmpl %ebx, %r10d
cmovbel %r10d, %r12d
movslq 0x8(%r13), %r13
cmovbeq %rdx, %r13
cmovaq %rbp, %r15
testl %r12d, %r12d
movq %r11, 0x40(%rsp)
je 0x181d45
cmpq %r11, %r15
jl 0x181d45
movq %rsi, %rbp
leaq (%rsi,%rax,2), %rdx
addq $-0x2, %rdx
xorl %esi, %esi
movq %r12, %r8
movzwl (%rdx,%r12,2), %r10d
decq %r12
movl %r10d, %r11d
shrl $0xe, %r11d
andl $0x3fff, %r10d          # imm = 0x3FFF
cmpw $0x2, %r11w
movq %r10, %r14
cmoveq %rsi, %r14
subq %r14, %r15
cmpw $0x3, %r11w
cmoveq %rsi, %r10
subq %r10, %r13
cmpq $0x2, %r8
jb 0x181d4a
movq 0x40(%rsp), %r11
cmpq %r11, %r15
jge 0x181cff
jmp 0x181d4f
movq %rsi, %rbp
jmp 0x181d4f
movq 0x40(%rsp), %r11
movq %r11, %rdx
movq %rcx, 0x38(%rsp)
xorq %rcx, %rdx
orq %rdx, %r9
sete 0xf(%rsp)
xorps %xmm0, %xmm0
movq 0xa0(%rsp), %rcx
movups %xmm0, 0x10(%rcx)
movq 0x18(%rsp), %r9
movq %r9, %rdx
subq %r11, %rdx
movq %rdx, 0x20(%rcx)
movl 0x10(%rdi), %edx
movq %rdx, 0x30(%rcx)
movl 0x1c(%rdi), %edx
movq %rdx, 0x38(%rcx)
jne 0x181d98
movq 0x28(%rcx), %rdx
movq %rdx, 0x20(%rcx)
negq 0x28(%rsp)
leaq (%rbp,%rax,2), %rbp
movq $-0x1, 0x20(%rsp)
movq $-0x1, 0x48(%rsp)
movq %r15, %rax
movq %r13, %rdx
movq %r13, %rsi
movq 0x48(%rsp), %r15
cmpq %rbx, %r12
jge 0x182014
cmpq %r9, %rax
jge 0x182014
movzwl (%rbp,%r12,2), %ecx
movl %ecx, %r14d
shrl $0xe, %r14d
movl %ecx, %edi
andl $0x3fff, %edi           # imm = 0x3FFF
incq %r12
cmpq %rbx, %r12
jge 0x181e1d
movzwl (%rbp,%r12,2), %r8d
movl %r8d, %r9d
shrl $0xe, %r9d
cmpl %r14d, %r9d
jne 0x181e18
andl $0x3fff, %r8d           # imm = 0x3FFF
addq %r8, %rdi
incq %r12
cmpq %r12, %rbx
jne 0x181df1
movq %rbx, %r12
movq 0x18(%rsp), %r9
cmpw $0x2, %r14w
sete %r8b
cmpw $0x3, %r14w
movq %rdi, %r13
movl $0x0, %r10d
cmoveq %r10, %r13
addq %rsi, %r13
cmpq %r11, %r9
je 0x181e75
cmpw $0x2, %r14w
jne 0x181f0b
movq %r13, %rsi
cmpq %r11, %rax
jl 0x181dc2
movw $0x2, %r14w
movb $0x1, %r8b
movq %rax, %r15
movq %rax, 0x10(%rsp)
movq %rax, %r10
movq $0x0, 0x30(%rsp)
jmp 0x181ee6
cmpw $0x2, %r14w
movq %rdi, %r15
movl $0x0, %esi
cmoveq %rsi, %r15
addq %rax, %r15
cmpq %rax, %r11
movq %rax, %rsi
cmovgq %r11, %rsi
movq %rsi, 0x10(%rsp)
cmpq %r15, %r11
movq %r15, %r10
cmovlq %r11, %r10
cmpq 0x38(%rsp), %r11
jne 0x181db4
cmpq 0x50(%rsp), %r9
jne 0x181db4
movzwl %cx, %esi
cmpl $0x3fff, %esi           # imm = 0x3FFF
ja 0x181db4
cmpq %r11, %rax
jge 0x181db4
cmpq %r11, %r15
jle 0x181db4
movq %r10, %rsi
subq 0x10(%rsp), %rsi
movq %rsi, 0x30(%rsp)
movb 0xf(%rsp), %sil
andb %r8b, %sil
cmpb $0x1, %sil
jne 0x181efd
cmpq %r11, %rax
je 0x181db4
testb %r8b, %r8b
movq 0x30(%rsp), %r11
cmovneq %rdi, %r11
jmp 0x181f3f
addq %rax, %rdi
cmpq %rax, %r11
movq %rax, %rdx
cmovgq %r11, %rdx
cmpq %rdi, %r9
movq %rdi, %r10
cmovlq %r9, %r10
movq %r10, %r8
movq %rdi, %r15
movq %rdx, 0x10(%rsp)
subq %rdx, %r8
jle 0x181db4
movq %rsi, %rdx
movq %rdi, %r15
movq %r8, %r11
movq 0x10(%rsp), %r8
subq %rax, %r8
movq %rdx, %rsi
subq %rax, %rdx
addq %r10, %rdx
testw %cx, %cx
cmovsq %r13, %rdx
movl $0x0, %eax
cmovsq %rax, %r8
addq %rsi, %r8
movzwl %cx, %eax
movl $0x0, %esi
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x181fad
movq 0xa0(%rsp), %rdi
movq %r8, %rsi
movq %r10, 0x20(%rsp)
movq %r11, 0x30(%rsp)
movq %rdx, 0x60(%rsp)
movq %r8, 0x58(%rsp)
callq 0x181b58
movq 0x58(%rsp), %r8
movq 0x60(%rsp), %rdx
movq 0x30(%rsp), %r11
movq 0x20(%rsp), %r10
movq %rax, %rsi
subq %r8, %rdx
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
movq %r10, %r8
subq %rax, %r8
movzwl %r14w, %r9d
movq 0xb0(%rsp), %rdi
pushq 0x28(%rsp)
movl 0xd0(%rsp), %eax
pushq %rax
pushq 0xd0(%rsp)
pushq 0xd0(%rsp)
pushq 0x68(%rsp)
pushq %r11
movq %r10, %r14
callq 0x181ac4
addq $0x30, %rsp
movq %rax, 0x48(%rsp)
movq %r14, 0x20(%rsp)
movq 0x40(%rsp), %r11
movq 0x18(%rsp), %r9
jmp 0x181db4
cmpq %rbx, %r12
movq %rax, 0x18(%rsp)
jge 0x182108
cmpq %r9, %rax
jg 0x182108
movq %rax, %rcx
subq 0x38(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x20(%rsp), %rdi
movzwl (%rbp,%r12,2), %r14d
movl %r14d, %ecx
andl $0xc000, %ecx           # imm = 0xC000
negw %cx
jno 0x182112
andl $0x3fff, %r14d          # imm = 0x3FFF
incq %r12
cmpq %rbx, %r12
jge 0x18208e
movzwl (%rbp,%r12,2), %ecx
movl %ecx, %edx
andl $0xc000, %edx           # imm = 0xC000
cmpl $0x8000, %edx           # imm = 0x8000
jne 0x18208e
andl $0x3fff, %ecx           # imm = 0x3FFF
addq %rcx, %r14
incq %r12
cmpq %r12, %rbx
jne 0x182064
movq %rbx, %r12
leaq (%r14,%rsi), %r13
cmpq %r11, %rax
jl 0x1820fa
movq 0xa0(%rsp), %rdi
movq %r13, %rdx
callq 0x181b58
movl $0x2, %r9d
movq 0xb0(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
movq %rcx, %r8
pushq 0x28(%rsp)
movl 0xd0(%rsp), %eax
pushq %rax
pushq 0xd0(%rsp)
pushq 0xd0(%rsp)
pushq %r15
pushq %r14
callq 0x181ac4
movq 0x70(%rsp), %r11
addq $0x30, %rsp
movq %rax, %r15
movq 0x18(%rsp), %rax
movq %rax, %rdi
movq %r13, %rsi
cmpq %rbx, %r12
jl 0x18203d
jmp 0x182115
movq %rsi, %r13
movq 0x20(%rsp), %rdi
jmp 0x182115
movq %rsi, %r13
cmpq $-0x1, %rdi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
je 0x18226c
movl $0x1, %r14d
cmpq 0x50(%rsp), %rdi
je 0x182147
movq 0xb0(%rsp), %rax
movl 0x20(%rax), %ecx
subq 0x38(%rsp), %rdi
addq %rcx, %rdi
movq %rdi, %r14
xorl %ebx, %ebx
cmpq $-0x1, %r15
cmovneq %r15, %rbx
cmpq %r14, %rbx
je 0x182248
movq 0xb0(%rsp), %rax
movq 0x10(%rax), %rax
movl %ebx, %ecx
leaq (%rcx,%rcx,4), %rdx
movl 0x18(%rax,%rdx,8), %ecx
testq %rcx, %rcx
je 0x1821ff
movq 0x20(%rax,%rdx,8), %rdx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl (%rdx), %edi
andl %esi, %edi
cmpl %r14d, %edi
jne 0x182191
incl 0x4(%rdx)
jmp 0x1821ba
leaq -0x1(%rcx), %r8
xorl %edi, %edi
cmpq %rdi, %r8
je 0x1821b2
movl 0x8(%rdx,%rdi,8), %r9d
andl %esi, %r9d
incq %rdi
cmpl %r14d, %r9d
jne 0x182197
incl 0x4(%rdx,%rdi,8)
jmp 0x1821b5
incq %rdi
cmpq %rcx, %rdi
jae 0x1821ff
movl %r14d, %ecx
leaq (%rcx,%rcx,4), %rsi
movq 0x18(%rax,%rsi,8), %rdx
movl 0x8(%rax,%rsi,8), %ecx
subl %edx, %ecx
jbe 0x182248
movl %edx, %edx
movq 0x20(%rax,%rsi,8), %rax
leaq (%rax,%rdx,8), %rdi
addq $0x4, %rdi
xorl %esi, %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl -0x4(%rdi,%rsi,8), %r9d
andl %r8d, %r9d
cmpl %ebx, %r9d
je 0x18227b
incq %rsi
cmpl %esi, %ecx
jne 0x1821e5
jmp 0x182248
movq %r13, %rbp
movq 0xb0(%rsp), %r13
movq %r13, %rdi
movl %ebx, %esi
movl %r14d, %edx
movl $0x1, %ecx
movl $0x1, %r8d
movl 0xc8(%rsp), %r15d
movl %r15d, %r9d
callq 0x195f8c
movq %r13, %rdi
movq %rbp, %r13
movl %r14d, %esi
movl %ebx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
movl %r15d, %r9d
callq 0x195f8c
movq 0xa8(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %ecx, 0x4(%rax)
movl %r13d, 0x10(%rax)
movl %r12d, 0x8(%rax)
movl $0x0, 0x14(%rax)
movl $0x1, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (%rax,%rdx,8), %rax
incl 0x4(%rax,%rsi,8)
jmp 0x182248