// File: /workspace/github_cpp_makefile_star2_O1/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), %rbx
movl (%rbx), %r14d
xorl %ebp, %ebp
movq %rcx, 0x28(%rsp)
movq %r8, 0x50(%rsp)
cmpq %r8, %rcx
setne %cl
movl 0x18(%rbx), %r10d
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r8
shlq $0x5, %r14
movslq (%r8,%r14), %r13
leaq (%r10,%r13), %r11
movslq 0x4(%r8,%r14), %r15
subq %r10, %r15
incq %r15
cmpq %rsi, %r11
cmovgq %r11, %rsi
cmpq %rdx, %r15
cmovgeq %rdx, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rsi, 0x18(%rsp)
movq %r15, 0x10(%rsp)
cmpq %rsi, %r15
jl 0x17b5a2
addq %r14, %r8
movl 0x1c(%r8), %esi
testq %rsi, %rsi
je 0x17b5a2
movl 0x4(%rbx), %r11d
movl 0x10(%rbx), %edx
movl 0x8(%rbx), %r10d
movb %cl, %bpl
movq 0x70(%rdi), %rax
movl 0x18(%r8), %ecx
xorl %r15d, %r15d
movq %rsi, 0x8(%rsp)
cmpl %esi, %r10d
cmovbel %r10d, %r15d
movslq 0x8(%r8), %r12
cmovbeq %rdx, %r12
cmovaq %r13, %r11
testl %r15d, %r15d
je 0x17b08f
cmpq 0x18(%rsp), %r11
jl 0x17b08f
leaq (%rax,%rcx,2), %rdx
addq $-0x2, %rdx
xorl %esi, %esi
movq 0x8(%rsp), %r13
movq %r15, %r8
movzwl (%rdx,%r15,2), %r10d
decq %r15
movq %r11, %r14
movl %r10d, %r11d
shrl $0xe, %r11d
andl $0x3fff, %r10d          # imm = 0x3FFF
cmpw $0x2, %r11w
movq %r10, %rbx
cmoveq %rsi, %rbx
subq %rbx, %r14
cmpw $0x3, %r11w
cmoveq %rsi, %r10
subq %r10, %r12
cmpq $0x2, %r8
jl 0x17b096
cmpq 0x18(%rsp), %r14
movq %r14, %r11
jge 0x17b046
jmp 0x17b099
movq 0x8(%rsp), %r13
jmp 0x17b099
movq %r14, %r11
movq 0x18(%rsp), %rsi
movq %rsi, %rdx
xorq 0x28(%rsp), %rdx
orq %rdx, %r9
movq %rsi, %r9
sete 0x40(%rsp)
xorps %xmm0, %xmm0
movq 0xa0(%rsp), %rsi
movups %xmm0, 0x10(%rsi)
movq 0x10(%rsp), %r8
movq %r8, %rdx
subq %r9, %rdx
movq %rdx, 0x20(%rsi)
movl 0x10(%rdi), %edx
movq %rdx, 0x30(%rsi)
movl 0x1c(%rdi), %edx
movq %rdx, 0x38(%rsi)
jne 0x17b0e7
movq 0x28(%rsi), %rdx
movq %rdx, 0x20(%rsi)
negq %rbp
leaq (%rax,%rcx,2), %r14
movq $-0x1, %rsi
cmpq %r13, %r15
movq %rbp, 0x48(%rsp)
jge 0x17b32f
cmpq %r8, %r11
jge 0x17b32f
movq $-0x1, %rbp
movq $-0x1, %rax
movq %rax, 0x20(%rsp)
movq %rbp, 0x38(%rsp)
movq %r11, %rsi
movq %r12, %rcx
movzwl (%r14,%r15,2), %r10d
movl %r10d, %ebp
shrl $0xe, %ebp
movl %r10d, %edi
andl $0x3fff, %edi           # imm = 0x3FFF
incq %r15
cmpq %r13, %r15
jge 0x17b176
movzwl (%r14,%r15,2), %r8d
movl %r8d, %r9d
shrl $0xe, %r9d
cmpl %ebp, %r9d
jne 0x17b16c
andl $0x3fff, %r8d           # imm = 0x3FFF
addq %r8, %rdi
incq %r15
cmpq %r15, %r13
jne 0x17b146
movq %r13, %r15
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
cmpw $0x2, %bp
movq %rdi, %rbx
movl $0x0, %eax
cmoveq %rax, %rbx
addq %rsi, %rbx
cmpw $0x3, %bp
movq %rdi, %r12
cmoveq %rax, %r12
addq %rcx, %r12
cmpq %rsi, %r9
movq %rsi, %r13
cmovgq %r9, %r13
cmpq %rbx, %r8
movq %rbx, %rax
cmovlq %r8, %rbx
cmpq %r9, %r8
jne 0x17b1df
cmpq 0x28(%rsp), %r9
movq %rax, %r11
jne 0x17b214
cmpq 0x50(%rsp), %r8
jne 0x17b214
movzwl %r10w, %r8d
cmpl $0x3fff, %r8d           # imm = 0x3FFF
movq 0x10(%rsp), %r8
ja 0x17b214
cmpq %r9, %rsi
jge 0x17b214
cmpq %r8, %r11
jg 0x17b1f7
jmp 0x17b214
cmpw $0x2, %bp
jne 0x17b1ef
cmpq %r9, %rsi
movq %rax, %r11
jge 0x17b1f7
jmp 0x17b214
cmpq %r13, %rbx
movq %rax, %r11
jle 0x17b214
cmpw $0x2, %bp
sete %r8b
andb 0x40(%rsp), %r8b
cmpb $0x1, %r8b
movq 0x10(%rsp), %r8
jne 0x17b236
cmpq %r9, %rsi
jne 0x17b236
movq 0x8(%rsp), %r13
cmpq %r13, %r15
jge 0x17b338
movq %r11, %rsi
movq %r12, %rcx
cmpq %r8, %r11
jl 0x17b12a
jmp 0x17b338
movq %r11, 0x20(%rsp)
movq %rbx, %r11
subq %r13, %r11
movq %r13, %r8
subq %rsi, %r8
movq %rcx, %rdx
subq %rsi, %rdx
addq %rbx, %rdx
testw %r10w, %r10w
cmovsq %r12, %rdx
movl $0x0, %eax
cmovsq %rax, %r8
addq %rcx, %r8
cmpw $0x2, %bp
cmoveq %rdi, %r11
movzwl %r10w, %eax
movl $0x0, %esi
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x17b2ad
movq 0xa0(%rsp), %rdi
movq %r8, %rsi
movq %r11, 0x30(%rsp)
movq %rdx, 0x60(%rsp)
movq %r8, 0x58(%rsp)
callq 0x17aeaf
movq 0x58(%rsp), %r8
movq 0x60(%rsp), %rdx
movq 0x30(%rsp), %r11
movq %rax, %rsi
subq %r8, %rdx
movq 0x28(%rsp), %rax
subq %rax, %r13
movq %rbx, 0x30(%rsp)
subq %rax, %rbx
movq 0xb0(%rsp), %rdi
movq %r13, %rcx
movq %rbx, %r8
movq %rbp, %r9
movq 0x48(%rsp), %rax
pushq %rax
movl 0xd0(%rsp), %eax
pushq %rax
pushq 0xd0(%rsp)
pushq 0xd0(%rsp)
pushq 0x58(%rsp)
pushq %r11
callq 0x17ae16
addq $0x30, %rsp
movq %rax, %rbp
movq 0x8(%rsp), %r13
cmpq %r13, %r15
jge 0x17b5b1
movq 0x30(%rsp), %rsi
movq %rsi, %rax
movq 0x20(%rsp), %r11
movq 0x10(%rsp), %r8
cmpq %r8, %r11
movq 0x18(%rsp), %r9
jl 0x17b11a
jmp 0x17b342
movq $-0x1, %rbp
jmp 0x17b342
movq 0x20(%rsp), %rsi
movq 0x38(%rsp), %rbp
cmpq %r13, %r15
jge 0x17b441
cmpq %r8, %r11
jg 0x17b441
movq %r11, %rax
subq 0x28(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %r11, 0x20(%rsp)
movzwl (%r14,%r15,2), %r13d
movl %r13d, %ebx
shrl $0xe, %ebx
cmpl $0x2, %ebx
jne 0x17b441
andl $0x3fff, %r13d          # imm = 0x3FFF
incq %r15
cmpq 0x8(%rsp), %r15
jge 0x17b3b7
movzwl (%r14,%r15,2), %eax
movl %eax, %ecx
andl $0xc000, %ecx           # imm = 0xC000
cmpl $0x8000, %ecx           # imm = 0x8000
jne 0x17b3b7
andl $0x3fff, %eax           # imm = 0x3FFF
addq %rax, %r13
incq %r15
cmpq %r15, 0x8(%rsp)
jne 0x17b38b
movq 0x8(%rsp), %r15
leaq (%r12,%r13), %rdx
cmpq %r9, %r11
jge 0x17b3c5
movq %rdx, %r12
jmp 0x17b42c
movq 0xa0(%rsp), %rdi
movq %r12, %rsi
movq %rdx, %r12
callq 0x17aeaf
movq 0xb0(%rsp), %rdi
movq %rax, %rsi
movq %r13, %rdx
movq 0x40(%rsp), %rcx
movq %rcx, %r8
movq %rbx, %r9
pushq 0x48(%rsp)
movl 0xd0(%rsp), %eax
pushq %rax
pushq 0xd0(%rsp)
pushq 0xd0(%rsp)
pushq %rbp
pushq %r13
callq 0x17ae16
movq 0x40(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x50(%rsp), %r11
addq $0x30, %rsp
movq %rax, %rbp
movq %r11, %rsi
cmpq 0x8(%rsp), %r15
jge 0x17b441
movq %r12, %rcx
cmpq %r8, %r11
jle 0x17b366
jmp 0x17b444
movq %r12, %rcx
cmpq $-0x1, %rsi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
je 0x17b5a2
movq %rcx, 0x8(%rsp)
movl $0x1, %r14d
cmpq 0x50(%rsp), %rsi
je 0x17b47b
movq 0xb0(%rsp), %rax
movl 0x20(%rax), %eax
subq 0x28(%rsp), %rsi
addq %rax, %rsi
movq %rsi, %r14
xorl %ebx, %ebx
cmpq $-0x1, %rbp
cmovneq %rbp, %rbx
cmpq %r14, %rbx
je 0x17b57e
movq 0xb0(%rsp), %rax
movq 0x10(%rax), %rax
movl %ebx, %ecx
leaq (%rcx,%rcx,4), %rcx
movl 0x18(%rax,%rcx,8), %edx
testq %rdx, %rdx
setne %dil
je 0x17b4f4
movq 0x20(%rax,%rcx,8), %rcx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl (%rcx), %esi
andl %edi, %esi
cmpl %r14d, %esi
jne 0x17b4c7
movb $0x1, %dil
xorl %esi, %esi
jmp 0x17b4ea
movl $0x1, %esi
cmpq %rsi, %rdx
je 0x17b4f0
movl (%rcx,%rsi,8), %r8d
andl %edi, %r8d
incq %rsi
cmpl %r14d, %r8d
jne 0x17b4cc
decq %rsi
cmpq %rdx, %rsi
setb %dil
incl 0x4(%rcx,%rsi,8)
jmp 0x17b4f4
seta %dil
testb %dil, %dil
je 0x17b52f
movl %r14d, %ecx
leaq (%rcx,%rcx,4), %rsi
movq 0x8(%rax,%rsi,8), %rdx
movq 0x18(%rax,%rsi,8), %rcx
cmpl %edx, %ecx
jae 0x17b57e
movl %ecx, %ecx
movl %edx, %edx
movq 0x20(%rax,%rsi,8), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %edi
andl %esi, %edi
cmpl %ebx, %edi
je 0x17b57a
incq %rcx
cmpq %rcx, %rdx
jne 0x17b51c
jmp 0x17b57e
movq 0xb0(%rsp), %rbp
movq %rbp, %rdi
movl %ebx, %esi
movl %r14d, %edx
movl $0x1, %ecx
movl $0x1, %r8d
movl 0xc8(%rsp), %r13d
movl %r13d, %r9d
movq %r11, %r12
callq 0x18d14c
movq %rbp, %rdi
movl %r14d, %esi
movl %ebx, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
movl %r13d, %r9d
callq 0x18d14c
movq %r12, %r11
jmp 0x17b57e
incl 0x4(%rax,%rcx,8)
movq 0xa8(%rsp), %rax
movl %r11d, 0x4(%rax)
movq 0x8(%rsp), %rcx
movl %ecx, 0x10(%rax)
movl %r15d, 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
movq 0x20(%rsp), %r11
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x30(%rsp), %rsi
jmp 0x17b342