// File: /workspace/github_cpp_makefile_star2_O2/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), %r15
movl (%r15), %ebx
xorl %r13d, %r13d
movq %rcx, 0x38(%rsp)
cmpq %r8, %rcx
setne %bpl
movl 0x18(%r15), %r10d
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movq 0x58(%rdi), %rcx
shlq $0x5, %rbx
movslq (%rcx,%rbx), %rax
leaq (%rax,%r10), %r11
movslq 0x4(%rcx,%rbx), %r14
subq %r10, %r14
incq %r14
cmpq %rsi, %r11
cmovgq %r11, %rsi
cmpq %rdx, %r14
cmovgeq %rdx, %r14
pushq $-0x1
popq %rdx
movq %r14, 0x50(%rsp)
cmpq %rsi, %r14
jl 0x13ea79
addq %rbx, %rcx
movl 0x1c(%rcx), %r12d
testq %r12, %r12
je 0x13ea79
movq %rsi, %r11
movq %r8, 0x60(%rsp)
movl 0x10(%r15), %edx
movl 0x8(%r15), %r14d
movb %bpl, %r13b
negq %r13
movq %r13, 0x58(%rsp)
movq 0x70(%rdi), %rsi
movl 0x18(%rcx), %r8d
leaq (%rsi,%r8,2), %r13
movslq 0x8(%rcx), %rbx
xorl %ecx, %ecx
cmpl %r12d, %r14d
cmovbeq %rdx, %rbx
movl 0x4(%r15), %ebp
cmovaq %rax, %rbp
movq 0xa0(%rsp), %r15
cmoval %ecx, %r14d
movq 0x38(%rsp), %r10
movq 0x50(%rsp), %r8
testq %r14, %r14
jle 0x13e5d2
cmpq %r11, %rbp
jl 0x13e5d2
movzwl -0x2(%r13,%r14,2), %eax
decq %r14
movl %eax, %edx
shrl $0xe, %edx
andl $0x3fff, %eax           # imm = 0x3FFF
cmpw $0x2, %dx
movq %rax, %rsi
cmoveq %rcx, %rsi
subq %rsi, %rbp
cmpw $0x3, %dx
cmoveq %rcx, %rax
subq %rax, %rbx
jmp 0x13e59a
movq %rbp, 0x18(%rsp)
movq %r11, %rax
xorq %r10, %rax
orq %rax, %r9
sete 0x17(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15)
movq %r8, %rax
subq %r11, %rax
movq %rax, 0x20(%r15)
movl 0x10(%rdi), %eax
movq %rax, 0x30(%r15)
movl 0x1c(%rdi), %eax
movq %rax, 0x38(%r15)
jne 0x13e60f
movq 0x28(%r15), %rax
movq %rax, 0x20(%r15)
pushq $-0x1
popq %rax
movq %rax, 0x28(%rsp)
movq %rax, 0x48(%rsp)
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq %rbx, %rax
movq %rbx, %rbp
cmpq %r12, %r14
jge 0x13e89b
cmpq %r8, 0x8(%rsp)
jge 0x13e89b
movzwl (%r13,%r14,2), %r9d
movl %r9d, %r15d
shrl $0xe, %r15d
movl %r9d, %edx
andl $0x3fff, %edx           # imm = 0x3FFF
incq %r14
cmpq %r12, %r14
jge 0x13e679
movzwl (%r13,%r14,2), %esi
movl %esi, %edi
shrl $0xe, %edi
cmpl %r15d, %edi
jne 0x13e67c
andl $0x3fff, %esi           # imm = 0x3FFF
addq %rsi, %rdx
jmp 0x13e656
movq %r12, %r14
cmpw $0x2, %r15w
sete %sil
cmpw $0x3, %r15w
movq %rdx, %rbx
movl $0x0, %edi
cmoveq %rdi, %rbx
addq %rbp, %rbx
cmpq %r11, %r8
je 0x13e6dc
cmpw $0x2, %r15w
jne 0x13e788
movq %rbx, %rbp
cmpq %r11, 0x8(%rsp)
jl 0x13e62c
movq %r10, %rcx
movw $0x2, %r15w
movb $0x1, %sil
movq 0x8(%rsp), %r10
movq %r10, 0x18(%rsp)
movq %r10, 0x30(%rsp)
movq $0x0, 0x20(%rsp)
jmp 0x13e753
cmpw $0x2, %r15w
movq %rdx, %rbp
movl $0x0, %edi
cmoveq %rdi, %rbp
movq 0x8(%rsp), %rdi
addq %rdi, %rbp
cmpq %rdi, %r11
cmovgq %r11, %rdi
movq %rdi, 0x30(%rsp)
cmpq %rbp, %r11
movq %rbp, 0x18(%rsp)
cmovlq %r11, %rbp
cmpq %r10, %r11
jne 0x13e61c
cmpq 0x60(%rsp), %r8
jne 0x13e61c
cmpw $0x3fff, %r9w           # imm = 0x3FFF
ja 0x13e61c
cmpq %r11, 0x8(%rsp)
jge 0x13e61c
cmpq %r11, 0x18(%rsp)
jle 0x13e61c
movq %r10, %rcx
movq %rbp, %r10
subq 0x30(%rsp), %rbp
movq %rbp, 0x20(%rsp)
movb 0x17(%rsp), %dil
andb %sil, %dil
cmpb $0x1, %dil
movq %r10, %rdi
movq %rcx, %r10
jne 0x13e772
cmpq %r11, 0x8(%rsp)
je 0x13e61c
testb %sil, %sil
movq 0x20(%rsp), %rcx
cmovneq %rdx, %rcx
movq %rcx, 0x20(%rsp)
movq %rax, %rcx
jmp 0x13e7c4
movq 0x8(%rsp), %rcx
addq %rcx, %rdx
cmpq %rcx, %r11
cmovgq %r11, %rcx
cmpq %rdx, %r8
movq %rdx, %rdi
cmovlq %r8, %rdi
movq %rdi, %rax
movq %rdx, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
subq %rcx, %rax
movq %rax, 0x20(%rsp)
jle 0x13e61c
movq %rbp, %rcx
movq %rdx, 0x18(%rsp)
movq %r11, 0x40(%rsp)
movq 0x30(%rsp), %rdx
movq 0x8(%rsp), %rax
subq %rax, %rdx
movq %rcx, %rbp
subq %rax, %rbp
addq %rdi, %rbp
testw %r9w, %r9w
cmovsq %rbx, %rbp
movl $0x0, %eax
cmovsq %rax, %rdx
addq %rcx, %rdx
movl $0x0, %esi
cmpw $0x4000, %r9w           # imm = 0x4000
jb 0x13e82f
movq %rdi, 0x8(%rsp)
movq 0xa0(%rsp), %rdi
movq %rdx, %rsi
movq %rdx, 0x28(%rsp)
movq %rbp, %rdx
callq 0x13e400
movq 0x28(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %r10
movq %rax, %rsi
subq %rdx, %rbp
movq 0x30(%rsp), %rcx
subq %r10, %rcx
movq %rdi, %r8
subq %r10, %r8
movzwl %r15w, %r9d
movq %rdi, %r15
movq 0xb0(%rsp), %rdi
movq %rbp, %rdx
pushq 0x58(%rsp)
movl 0xd0(%rsp), %eax
pushq %rax
pushq 0xd0(%rsp)
pushq 0xd0(%rsp)
pushq 0x68(%rsp)
pushq 0x48(%rsp)
callq 0x13e374
movq 0x68(%rsp), %r10
addq $0x30, %rsp
movq %rax, 0x48(%rsp)
movq %r15, 0x28(%rsp)
movq 0x40(%rsp), %r11
movq 0x50(%rsp), %r8
jmp 0x13e61c
movq 0x8(%rsp), %rcx
subq %r10, %rcx
movq %rcx, 0x18(%rsp)
pushq $-0x1
popq %rdx
movq 0x48(%rsp), %rax
movq %rbp, %rbx
cmpq %r12, %r14
jge 0x13e99c
cmpq %r8, 0x8(%rsp)
jg 0x13e99c
movzwl (%r13,%r14,2), %r15d
movl %r15d, %ecx
andl $0xc000, %ecx           # imm = 0xC000
negw %cx
jno 0x13e99c
andl $0x3fff, %r15d          # imm = 0x3FFF
incq %r14
cmpq %r12, %r14
jge 0x13e90f
movzwl (%r13,%r14,2), %esi
movl %esi, %ecx
andl $0xc000, %ecx           # imm = 0xC000
cmpl $0x8000, %ecx           # imm = 0x8000
jne 0x13e912
andl $0x3fff, %esi           # imm = 0x3FFF
addq %rsi, %r15
jmp 0x13e8e6
movq %r12, %r14
leaq (%r15,%rbx), %rbp
cmpq %r11, 0x8(%rsp)
jl 0x13e8b0
movq 0xa0(%rsp), %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq %r11, 0x40(%rsp)
movq %rax, %rbx
callq 0x13e400
movq 0xb0(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
movq %rcx, %r8
pushq $0x2
popq %r9
pushq 0x58(%rsp)
movl 0xd0(%rsp), %eax
pushq %rax
pushq 0xd0(%rsp)
pushq 0xd0(%rsp)
pushq %rbx
pushq %r15
callq 0x13e374
movq 0x80(%rsp), %r8
pushq $-0x1
popq %rdx
movq 0x70(%rsp), %r11
movq 0x68(%rsp), %r10
addq $0x30, %rsp
movq 0x8(%rsp), %rcx
movq %rcx, 0x28(%rsp)
jmp 0x13e8b0
movq 0x28(%rsp), %rbp
cmpq $-0x1, %rbp
je 0x13ea79
cmpq 0x60(%rsp), %rbp
jne 0x13e9c7
pushq $0x1
popq %rbp
movq 0xa8(%rsp), %r15
movq 0xb0(%rsp), %r13
jmp 0x13e9e1
movq 0xb0(%rsp), %r13
movl 0x20(%r13), %ecx
subq %r10, %rbp
addq %rcx, %rbp
movq 0xa8(%rsp), %r15
xorl %r12d, %r12d
cmpq $-0x1, %rax
cmovneq %rax, %r12
cmpq %rbp, %r12
je 0x13ea60
pushq $0x1
popq %rcx
movq %r13, %rdi
movl %r12d, %esi
movl %ebp, %edx
movl %ecx, %r8d
callq 0x14f092
testl %eax, %eax
je 0x13ea1d
pushq $0x1
popq %r8
movq %r13, %rdi
movl %ebp, %esi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0x14f092
jmp 0x13ea60
pushq $0x1
popq %rcx
movq %r13, %rdi
movl %r12d, %esi
movl %ebp, %edx
movl %ecx, %r8d
movq %rbp, %r15
movq %r13, %rbp
movl 0xc8(%rsp), %r13d
movl %r13d, %r9d
callq 0x14ef91
movq %rbp, %rdi
movl %r15d, %esi
movl %r12d, %edx
xorl %ecx, %ecx
pushq $0x1
popq %r8
movq 0xa8(%rsp), %r15
movl %r13d, %r9d
callq 0x14ef91
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rax
movl %eax, 0x4(%r15)
movl %ebx, 0x10(%r15)
movl %r14d, 0x8(%r15)
andl $0x0, 0x14(%r15)
movl %edx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq