// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: get_specific_contig_length(asg_t*, unsigned char*)
long long get_specific_contig_length(asg_t *g, uint8_t *del)
{
    asg_cleanup(g);
    uint32_t v, n_vtx = g->n_seq * 2, q_occ;
    uint8_t *mark = NULL;
    ///is a queue
	//kdq_t(uint64_t) *q;
    ///each node has two directions
    //q = kdq_init(uint64_t);


    mark = (uint8_t*)calloc(n_vtx, 1);
    
    long long totalLen = 0;
    for (v = 0; v < n_vtx; ++v) 
    {
        uint32_t w, x, l, start, end, len;
        asg_arc_t arc;
        if (g->seq[v>>1].del || mark[v]) continue;
        if (get_available_cnt(g, v, del, NULL) == 0 && get_available_cnt(g, (v^1), del, NULL) != 0) continue;
        if (del[v>>1]) continue;

        mark[v] = 1;
		//q->count = 0, start = v, end = v^1, len = 0;
        q_occ =0, start = v, end = v^1, len = 0;
		// forward
		w = v;


        while (1) 
        {
			/**
			 * w----->x
			 * w<-----x
			 * that means the only suffix of w is x, and the only prefix of x is w
			 **/
			if (get_available_cnt(g, w, del, NULL) != 1) break;
            get_available_cnt(g, w, del, &arc);
			x = arc.v; // w->x
			if (get_available_cnt(g, x^1, del, NULL) != 1) break;

			/**
			 * another direction of w would be marked as used (since w has been used)
			**/
			mark[x] = mark[w^1] = 1;
			///l is the edge length, instead of overlap length
            ///note: edge length is different with overlap length
			///l = asg_arc_len(arc_first(g, w));
            get_available_cnt(g, w, del, &arc);
            l = ((uint32_t)((arc).ul));
			//kdq_push(uint64_t, q, (uint64_t)w<<32 | l);
            q_occ++;
			end = x^1, len += l;
			w = x;
			if (x == v) break;
		}


        //if (start != (end^1) || kdq_size(q) == 0) { // linear unitig
        if (start != (end^1) || q_occ == 0) { // linear unitig
			///length of seq, instead of edge
			l = g->seq[end>>1].len;
			//kdq_push(uint64_t, q, (uint64_t)(end^1)<<32 | l);
            q_occ++;
			len += l;
		} else { // circular unitig
			start = end = UINT32_MAX;
			goto add_unitig; // then it is not necessary to do the backward
		}

        // backward
		x = v;
		while (1) { // similar to forward but not the same
			if (get_available_cnt(g, x^1, del, NULL) != 1) break;
            get_available_cnt(g, x^1, del, &arc);
            w = arc.v ^ 1;
			if (get_available_cnt(g, w, del, NULL) != 1) break;
			mark[x] = mark[w^1] = 1;
			///l = asg_arc_len(arc_first(g, w));
            get_available_cnt(g, w, del, &arc);
            l = ((uint32_t)((arc).ul));
			///w is the seq id + direction, l is the length of edge
			///push element to the front of a queue
			//kdq_unshift(uint64_t, q, (uint64_t)w<<32 | l);
            q_occ++;

			start = w, len += l;
			x = w;
		}


        add_unitig:
        if (start != UINT32_MAX) mark[start] = mark[end] = 1;
        totalLen += len;
    }
    //kdq_destroy(uint64_t, q);
    free(mark);
    return totalLen;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
callq 0xa02fa
movl 0x14(%r14), %r15d
addl %r15d, %r15d
movl $0x1, %esi
movq %r15, %rdi
callq 0x2120
testl %r15d, %r15d
je 0x110789
movq 0x20(%r14), %r11
movl %r15d, %r12d
xorl %esi, %esi
xorl %r15d, %r15d
movq %r11, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movl %esi, %r9d
shrl %r9d
cmpl $0x0, (%r11,%r9,8)
js 0x11077b
cmpb $0x0, (%rax,%rsi)
jne 0x11077b
testq %rbx, %rbx
je 0x110196
cmpb $0x0, (%rbx,%r9)
jne 0x1101f4
movq 0x28(%r14), %rcx
movq (%rcx,%rsi,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x1101f4
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r8
addq $0xc, %r8
xorl %r10d, %r10d
xorl %ecx, %ecx
cmpl $0x0, (%r8,%r10)
js 0x1101e7
testq %rbx, %rbx
je 0x1101e5
movl -0x4(%r8,%r10), %edi
shrl %edi
cmpb $0x0, (%rbx,%rdi)
jne 0x1101e7
incl %ecx
addq $0x18, %r10
cmpq %r10, %rdx
jne 0x1101cc
testl %ecx, %ecx
jne 0x11026b
testq %rbx, %rbx
je 0x110203
movl %esi, %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x11026b
movq 0x28(%r14), %rcx
movl %esi, %edx
xorq $0x1, %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x11026b
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r8
addq $0xc, %r8
xorl %r10d, %r10d
xorl %ecx, %ecx
cmpl $0x0, (%r8,%r10)
js 0x11025a
testq %rbx, %rbx
je 0x110258
movl -0x4(%r8,%r10), %edi
shrl %edi
cmpb $0x0, (%rbx,%rdi)
jne 0x11025a
incl %ecx
addq $0x18, %r10
cmpq %r10, %rdx
jne 0x11023f
testl %ecx, %ecx
jne 0x11077b
cmpb $0x0, (%rbx,%r9)
jne 0x11077b
movq %r15, 0x30(%rsp)
movb $0x1, (%rax,%rsi)
movl %esi, %r11d
xorl $0x1, %r11d
xorl %ebp, %ebp
movl %esi, %r12d
xorl %r10d, %r10d
movl %r11d, %r8d
movl %r12d, %r15d
testq %rbx, %rbx
je 0x1102a8
movl %r15d, %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x1104db
movq 0x28(%r14), %rcx
movl %r15d, %r9d
movq (%rcx,%r9,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x1104db
movl %r8d, 0x4(%rsp)
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r8
addq $0xc, %r8
xorl %r11d, %r11d
xorl %ecx, %ecx
cmpl $0x0, (%r8,%r11)
js 0x110305
testq %rbx, %rbx
je 0x110303
movl -0x4(%r8,%r11), %edi
shrl %edi
cmpb $0x0, (%rbx,%rdi)
jne 0x110305
incl %ecx
addq $0x18, %r11
cmpq %r11, %rdx
jne 0x1102ea
cmpl $0x1, %ecx
jne 0x1104d2
testq %rbx, %rbx
je 0x110327
movl %r15d, %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x1103a0
movq 0x28(%r14), %rcx
movq (%rcx,%r9,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x1103a0
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %r11
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r12
addq $0xc, %r12
xorl %r8d, %r8d
xorl %edx, %edx
cmpl $0x0, (%r12,%r8)
js 0x110397
testq %rbx, %rbx
je 0x110376
movl -0x4(%r12,%r8), %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x110397
leaq (%r12,%r8), %rcx
addq $-0xc, %rcx
movl %edx, %edi
leaq (%rdi,%rdi,2), %rdi
movq 0x10(%rcx), %r13
movq %r13, 0x18(%rsp,%rdi,8)
movups (%rcx), %xmm0
movups %xmm0, 0x8(%rsp,%rdi,8)
incl %edx
addq $0x18, %r8
cmpq %r8, %r11
jne 0x11035d
movl 0x10(%rsp), %r12d
testq %rbx, %rbx
je 0x1103b9
movl %r12d, %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x1104d2
movq %r12, %r11
xorq $0x1, %r11
movq 0x28(%r14), %rcx
movq (%rcx,%r11,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x1104d2
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r8
addq $0xc, %r8
xorl %ecx, %ecx
xorl %edi, %edi
cmpl $0x0, (%r8,%rcx)
js 0x110416
testq %rbx, %rbx
je 0x110414
movl -0x4(%r8,%rcx), %r13d
shrl %r13d
cmpb $0x0, (%rbx,%r13)
jne 0x110416
incl %edi
addq $0x18, %rcx
cmpq %rcx, %rdx
jne 0x1103f9
cmpl $0x1, %edi
jne 0x1104d2
movq %r9, %rcx
xorq $0x1, %rcx
movb $0x1, %dl
movb %dl, (%rax,%rcx)
movb %dl, (%rax,%r12)
testq %rbx, %rbx
je 0x110447
shrl %r15d
cmpb $0x0, (%rbx,%r15)
jne 0x1104c0
movq 0x28(%r14), %rcx
movq (%rcx,%r9,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x1104c0
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %r9
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r15
addq $0xc, %r15
xorl %r8d, %r8d
xorl %edx, %edx
cmpl $0x0, (%r15,%r8)
js 0x1104b7
testq %rbx, %rbx
je 0x110496
movl -0x4(%r15,%r8), %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x1104b7
leaq (%r15,%r8), %rcx
addq $-0xc, %rcx
movl %edx, %edi
leaq (%rdi,%rdi,2), %rdi
movq 0x10(%rcx), %r13
movq %r13, 0x18(%rsp,%rdi,8)
movups (%rcx), %xmm0
movups %xmm0, 0x8(%rsp,%rdi,8)
incl %edx
addq $0x18, %r8
cmpq %r8, %r9
jne 0x11047d
incl %ebp
addl 0x8(%rsp), %r10d
cmpq %r12, %rsi
jne 0x11028e
jmp 0x1104de
movl 0x4(%rsp), %ecx
movl %ecx, %r11d
jmp 0x1104de
movl %r8d, %r11d
movl %esi, %ecx
xorl %r11d, %ecx
cmpl $0x1, %ecx
setne %cl
testl %ebp, %ebp
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x110748
movl %r11d, %ecx
shrl %ecx
movq 0x20(%rsp), %rdx
movl (%rdx,%rcx,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
addl %ecx, %r10d
movl %esi, %ebp
testq %rbx, %rbx
je 0x110525
movl %ebp, %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x110754
movl %ebp, %ecx
xorl $0x1, %ecx
movq 0x28(%r14), %rdx
movl %ecx, %r9d
movq (%rdx,%r9,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x110754
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r8
addq $0xc, %r8
xorl %ecx, %ecx
xorl %edi, %edi
cmpl $0x0, (%r8,%rcx)
js 0x110583
testq %rbx, %rbx
je 0x110581
movl -0x4(%r8,%rcx), %r15d
shrl %r15d
cmpb $0x0, (%rbx,%r15)
jne 0x110583
incl %edi
addq $0x18, %rcx
cmpq %rcx, %rdx
jne 0x110566
cmpl $0x1, %edi
jne 0x110754
testq %rbx, %rbx
je 0x1105a4
movl %ebp, %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x11061d
movq 0x28(%r14), %rcx
movq (%rcx,%r9,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x11061d
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %r9
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r15
addq $0xc, %r15
xorl %r8d, %r8d
xorl %edx, %edx
cmpl $0x0, (%r15,%r8)
js 0x110614
testq %rbx, %rbx
je 0x1105f3
movl -0x4(%r15,%r8), %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x110614
leaq (%r15,%r8), %rcx
addq $-0xc, %rcx
movl %edx, %edi
leaq (%rdi,%rdi,2), %rdi
movq 0x10(%rcx), %r12
movq %r12, 0x18(%rsp,%rdi,8)
movups (%rcx), %xmm0
movups %xmm0, 0x8(%rsp,%rdi,8)
incl %edx
addq $0x18, %r8
cmpq %r8, %r9
jne 0x1105da
movl 0x10(%rsp), %r15d
testq %rbx, %rbx
je 0x110636
movl %r15d, %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x110754
movq %r15, %r9
xorq $0x1, %r9
movq 0x28(%r14), %rcx
movq (%rcx,%r9,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x110754
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r8
addq $0xc, %r8
xorl %ecx, %ecx
xorl %edi, %edi
cmpl $0x0, (%r8,%rcx)
js 0x110693
testq %rbx, %rbx
je 0x110691
movl -0x4(%r8,%rcx), %r12d
shrl %r12d
cmpb $0x0, (%rbx,%r12)
jne 0x110693
incl %edi
addq $0x18, %rcx
cmpq %rcx, %rdx
jne 0x110676
cmpl $0x1, %edi
jne 0x110754
movb $0x1, %dl
movb %dl, (%rax,%r15)
movl %ebp, %ecx
movb %dl, (%rax,%rcx)
testq %rbx, %rbx
je 0x1106bf
shrl %r15d
cmpb $0x0, (%rbx,%r15)
jne 0x11073b
movq 0x28(%r14), %rcx
movl %r9d, %edx
movq (%rcx,%rdx,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rdx
je 0x11073b
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %r15
leaq (%rcx,%rcx,2), %rcx
leaq (%rdi,%rcx,8), %r12
addq $0xc, %r12
xorl %r8d, %r8d
xorl %edx, %edx
cmpl $0x0, (%r12,%r8)
js 0x110732
testq %rbx, %rbx
je 0x110711
movl -0x4(%r12,%r8), %ecx
shrl %ecx
cmpb $0x0, (%rbx,%rcx)
jne 0x110732
leaq (%r12,%r8), %rcx
addq $-0xc, %rcx
movl %edx, %edi
leaq (%rdi,%rdi,2), %rdi
movq 0x10(%rcx), %r13
movq %r13, 0x18(%rsp,%rdi,8)
movups (%rcx), %xmm0
movups %xmm0, 0x8(%rsp,%rdi,8)
incl %edx
addq $0x18, %r8
cmpq %r8, %r15
jne 0x1106f8
addl 0x8(%rsp), %r10d
movl %r9d, %ebp
jmp 0x110512
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x110757
movl %r11d, %ecx
cmpl $-0x1, %ebp
movq 0x30(%rsp), %r15
movq 0x20(%rsp), %r11
je 0x110770
movb $0x1, %dl
movb %dl, (%rax,%rcx)
movl %ebp, %ecx
movb %dl, (%rax,%rcx)
movl %r10d, %ecx
addq %rcx, %r15
movq 0x28(%rsp), %r12
incq %rsi
cmpq %r12, %rsi
jne 0x11016f
jmp 0x11078c
xorl %r15d, %r15d
movq %rax, %rdi
callq 0x2220
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq