// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %rbp
callq 0xa0a7d
movl 0x14(%rbp), %r14d
addl %r14d, %r14d
movl $0x1, %esi
movq %r14, %rdi
callq 0x2120
testl %r14d, %r14d
je 0x115eda
movq 0x20(%rbp), %r11
xorl %edx, %edx
movq $0x0, 0x48(%rsp)
movq %r11, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
movq %r14, 0x50(%rsp)
movq %rax, 0x28(%rsp)
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%r11,%rcx,8)
js 0x115ecc
cmpb $0x0, (%rax,%rdx)
jne 0x115ecc
testq %rbx, %rbx
je 0x1158c6
cmpb $0x0, (%rbx,%rcx)
je 0x1158c6
movl %edx, %r8d
xorl $0x1, %r8d
jmp 0x11593f
movq 0x8(%rbp), %rsi
movq 0x28(%rbp), %rdi
movq (%rdi,%rdx,8), %r8
movq %r8, %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
andq %r10, %r9
je 0x115933
shrq $0x20, %r8
shlq $0x3, %r9
leaq (%r9,%r9,2), %r15
leaq (%r8,%r8,2), %r8
leaq (%rsi,%r8,8), %r8
addq $0xc, %r8
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpl $0x0, (%r8,%r9)
js 0x11591c
testq %rbx, %rbx
je 0x115919
movl -0x4(%r8,%r9), %r11d
shrl %r11d
cmpb $0x0, (%rbx,%r11)
jne 0x11591c
incl %r10d
addq $0x18, %r9
cmpq %r9, %r15
jne 0x1158fe
testl %r10d, %r10d
movq 0x18(%rsp), %r11
jne 0x1159b3
movl %edx, %r8d
xorl $0x1, %r8d
testq %rbx, %rbx
je 0x115952
movl %r8d, %esi
shrl %esi
cmpb $0x0, (%rbx,%rsi)
jne 0x1159b3
movq 0x8(%rbp), %rsi
movq 0x28(%rbp), %rdi
movl %r8d, %r8d
movq (%rdi,%r8,8), %rdi
movq %rdi, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
andq %r9, %r8
je 0x1159b3
shrq $0x20, %rdi
shlq $0x3, %r8
leaq (%r8,%r8,2), %r10
leaq (%rdi,%rdi,2), %rdi
leaq (%rsi,%rdi,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
cmpl $0x0, (%rsi,%rdi)
js 0x1159a1
testq %rbx, %rbx
je 0x11599e
movl -0x4(%rsi,%rdi), %r9d
shrl %r9d
cmpb $0x0, (%rbx,%r9)
jne 0x1159a1
incl %r8d
addq $0x18, %rdi
cmpq %rdi, %r10
jne 0x115984
testl %r8d, %r8d
jne 0x115ecc
cmpb $0x0, (%rbx,%rcx)
jne 0x115ecc
movb $0x1, (%rax,%rdx)
movl %edx, %esi
xorl $0x1, %esi
xorl %r9d, %r9d
movl %edx, %r8d
xorl %r13d, %r13d
testq %rbx, %rbx
je 0x1159e3
movl %r8d, %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
jne 0x115c46
movl %esi, 0x10(%rsp)
movl %r13d, 0xc(%rsp)
movq 0x28(%rbp), %rcx
movl %r8d, %edi
movq (%rcx,%rdi,8), %rax
movq %rax, %r13
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %r13
je 0x115c2e
movl %r9d, 0x14(%rsp)
movq 0x8(%rbp), %rsi
shrq $0x20, %rax
leaq (,%r13,8), %r9
leaq (%r9,%r9,2), %r14
leaq (%rax,%rax,2), %r9
leaq (%rsi,%r9,8), %rbp
addq $0xc, %rbp
xorl %r15d, %r15d
xorl %eax, %eax
cmpl $0x0, (%rbp,%r15)
js 0x115a50
testq %rbx, %rbx
je 0x115a4e
movl -0x4(%rbp,%r15), %r12d
shrl %r12d
cmpb $0x0, (%rbx,%r12)
jne 0x115a50
incl %eax
addq $0x18, %r15
cmpq %r15, %r14
jne 0x115a32
cmpl $0x1, %eax
jne 0x115c18
testq %rbx, %rbx
je 0x115b89
movl %r8d, %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
je 0x115b89
movl 0x38(%rsp), %r12d
movl %r12d, %r15d
xorl $0x1, %r15d
movl %r15d, %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
jne 0x115c18
movl %r15d, %eax
movq (%rcx,%rax,8), %rax
movq %rax, %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
andq %r9, %rcx
je 0x115c18
shrq $0x20, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rsi
addq $0xc, %rsi
xorl %r9d, %r9d
xorl %eax, %eax
cmpl $0x0, (%rsi,%r9)
js 0x115ae8
testq %rbx, %rbx
je 0x115ae6
movl -0x4(%rsi,%r9), %r10d
shrl %r10d
cmpb $0x0, (%rbx,%r10)
jne 0x115ae8
incl %eax
addq $0x18, %r9
cmpq %r9, %rcx
jne 0x115acb
cmpl $0x1, %eax
movl 0x14(%rsp), %r9d
jne 0x115c35
xorq $0x1, %rdi
movq 0x28(%rsp), %rax
movb $0x1, %sil
movb %sil, (%rax,%rdi)
movl %r12d, %ecx
movb %sil, (%rax,%rcx)
testq %rbx, %rbx
je 0x115b25
shrl %r8d
cmpb $0x0, (%rbx,%r8)
jne 0x115b63
xorl %esi, %esi
cmpl $0x0, (%rbp)
js 0x115b5a
testq %rbx, %rbx
je 0x115b3d
movl -0x4(%rbp), %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
jne 0x115b5a
leaq -0xc(%rbp), %rax
movl %esi, %edi
leaq (%rdi,%rdi,2), %rdi
movq 0x10(%rax), %r8
movq %r8, 0x40(%rsp,%rdi,8)
movups (%rax), %xmm0
movups %xmm0, 0x30(%rsp,%rdi,8)
incl %esi
addq $0x18, %rbp
decq %r13
jne 0x115b27
movl 0xc(%rsp), %r13d
addl 0x30(%rsp), %r13d
incl %r9d
movl %r12d, %r8d
movl %r15d, %esi
cmpq %rcx, %rdx
movq 0x20(%rsp), %rbp
jne 0x1159cf
jmp 0x115c49
movq %r13, %r15
negq %r15
leaq (%rsi,%r9,8), %r12
addq $0xc, %r12
xorl %r14d, %r14d
xorl %r9d, %r9d
leaq (%r14,%r14,2), %rax
incq %r14
leaq (%r12,%rax,8), %rax
cmpl $0x0, (%rax)
js 0x115bf6
testq %rbx, %rbx
je 0x115bd9
movl -0x4(%rax), %r10d
shrl %r10d
cmpb $0x0, (%rbx,%r10)
je 0x115bd9
leaq (%r15,%r14), %r10
incq %r10
incq %r14
addq $0x18, %rax
cmpq $0x1, %r10
jne 0x115ba8
jmp 0x115a7a
movl %r9d, %r10d
leaq (%r10,%r10,2), %r10
movq 0x4(%rax), %r11
movq %r11, 0x40(%rsp,%r10,8)
movups -0xc(%rax), %xmm0
movups %xmm0, 0x30(%rsp,%r10,8)
incl %r9d
movq %r15, %rax
addq %r14, %rax
jne 0x115b9d
movl 0x38(%rsp), %r12d
movl %r12d, %r15d
xorl $0x1, %r15d
testq %rbx, %rbx
jne 0x115a86
jmp 0x115a95
movl 0x10(%rsp), %r15d
movq 0x20(%rsp), %rbp
movl 0xc(%rsp), %r13d
movl 0x14(%rsp), %r9d
jmp 0x115c49
movl 0x10(%rsp), %r15d
jmp 0x115c3f
movl 0x10(%rsp), %r15d
movq 0x20(%rsp), %rbp
movl 0xc(%rsp), %r13d
jmp 0x115c49
movl %esi, %r15d
movl %r15d, %eax
xorl %edx, %eax
cmpl $0x1, %eax
setne %al
testl %r9d, %r9d
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x115eb5
movl %r15d, %eax
shrl %eax
movq 0x18(%rsp), %rcx
movl (%rcx,%rax,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
addl %eax, %r13d
movl %edx, %r8d
testq %rbx, %rbx
je 0x115c93
movl %r8d, %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
jne 0x115e97
movl %r8d, %eax
xorl $0x1, %eax
movq 0x28(%rbp), %rsi
movl %eax, %eax
movq (%rsi,%rax,8), %rax
movq %rax, %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rcx
je 0x115e97
movq 0x8(%rbp), %rdi
shrq $0x20, %rax
leaq (,%rcx,8), %r9
leaq (%r9,%r9,2), %r9
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
xorl %eax, %eax
cmpl $0x0, (%r10,%r11)
js 0x115cf6
testq %rbx, %rbx
je 0x115cf4
movl -0x4(%r10,%r11), %r14d
shrl %r14d
cmpb $0x0, (%rbx,%r14)
jne 0x115cf6
incl %eax
addq $0x18, %r11
cmpq %r11, %r9
jne 0x115cd9
cmpl $0x1, %eax
jne 0x115e97
testq %rbx, %rbx
je 0x115e14
movl %r8d, %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
je 0x115e14
movl 0x38(%rsp), %r10d
movl %r10d, %ecx
xorl $0x1, %ecx
movl %ecx, %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
jne 0x115e97
movl %ecx, %eax
movq (%rsi,%rax,8), %rax
movq %rax, %rsi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
andq %r9, %rsi
je 0x115e97
shrq $0x20, %rax
leaq (,%rsi,8), %r9
leaq (%r9,%r9,2), %r9
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rdi
addq $0xc, %rdi
xorl %r11d, %r11d
xorl %eax, %eax
cmpl $0x0, (%rdi,%r11)
js 0x115d8f
testq %rbx, %rbx
je 0x115d8d
movl -0x4(%rdi,%r11), %r14d
shrl %r14d
cmpb $0x0, (%rbx,%r14)
jne 0x115d8f
incl %eax
addq $0x18, %r11
cmpq %r11, %r9
jne 0x115d72
cmpl $0x1, %eax
jne 0x115e97
movl %r10d, %eax
movq 0x28(%rsp), %r9
movb $0x1, %r10b
movb %r10b, (%r9,%rax)
movl %r8d, %eax
movb %r10b, (%r9,%rax)
testq %rbx, %rbx
je 0x115dc6
movl %ecx, %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
jne 0x115e07
xorl %r8d, %r8d
cmpl $0x0, (%rdi)
js 0x115dfe
testq %rbx, %rbx
je 0x115dde
movl -0x4(%rdi), %eax
shrl %eax
cmpb $0x0, (%rbx,%rax)
jne 0x115dfe
leaq -0xc(%rdi), %rax
movl %r8d, %r9d
leaq (%r9,%r9,2), %r9
movq 0x10(%rax), %r10
movq %r10, 0x40(%rsp,%r9,8)
movups (%rax), %xmm0
movups %xmm0, 0x30(%rsp,%r9,8)
incl %r8d
addq $0x18, %rdi
decq %rsi
jne 0x115dc9
addl 0x30(%rsp), %r13d
movl %ecx, %r8d
jmp 0x115c7f
negq %rcx
xorl %r11d, %r11d
xorl %r9d, %r9d
leaq (%r11,%r11,2), %rax
incq %r11
leaq (%r10,%rax,8), %rax
cmpl $0x0, (%rax)
js 0x115e76
testq %rbx, %rbx
je 0x115e59
movl -0x4(%rax), %r14d
shrl %r14d
cmpb $0x0, (%rbx,%r14)
je 0x115e59
leaq (%rcx,%r11), %r14
incq %r14
incq %r11
addq $0x18, %rax
cmpq $0x1, %r14
jne 0x115e28
jmp 0x115d20
movl %r9d, %r14d
leaq (%r14,%r14,2), %r14
movq 0x4(%rax), %r12
movq %r12, 0x40(%rsp,%r14,8)
movups -0xc(%rax), %xmm0
movups %xmm0, 0x30(%rsp,%r14,8)
incl %r9d
movq %rcx, %rax
addq %r11, %rax
jne 0x115e1d
movl 0x38(%rsp), %r10d
movl %r10d, %ecx
xorl $0x1, %ecx
testq %rbx, %rbx
jne 0x115d2b
jmp 0x115d39
cmpl $-0x1, %r8d
je 0x115eb5
movl %r15d, %ecx
movq 0x28(%rsp), %rax
movb $0x1, %sil
movb %sil, (%rax,%rcx)
movl %r8d, %ecx
movb %sil, (%rax,%rcx)
jmp 0x115eba
movq 0x28(%rsp), %rax
movq 0x18(%rsp), %r11
movl %r13d, %ecx
addq %rcx, 0x48(%rsp)
movq 0x50(%rsp), %r14
incq %rdx
cmpq %r14, %rdx
jne 0x115899
jmp 0x115ee3
movq $0x0, 0x48(%rsp)
movq %rax, %rdi
callq 0x2220
movq 0x48(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq