// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x18(%rbp)
movq $0x0, -0x28(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x18111c
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x14(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x180e97
movq -0x28(%rbp), %rax
movl -0x14(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x180e9c
jmp 0x18110e
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1861d0
cmpl $0x0, %eax
jne 0x180ed6
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
xorl $0x1, %esi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1861d0
cmpl $0x0, %eax
je 0x180ed6
jmp 0x18110e
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x180eec
jmp 0x18110e
movq -0x28(%rbp), %rax
movl -0x14(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl $0x0, -0x1c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x14(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1861d0
cmpl $0x1, %eax
je 0x180f38
jmp 0x180fd6
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x1861d0
movl -0x58(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
xorl $0x1, %esi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1861d0
cmpl $0x1, %eax
je 0x180f70
jmp 0x180fd6
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x1861d0
movq -0x60(%rbp), %rax
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x38(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x3c(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x180fd1
jmp 0x180fd6
jmp 0x180f1a
movl -0x40(%rbp), %eax
movl -0x44(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x180fe9
cmpl $0x0, -0x1c(%rbp)
jne 0x181017
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x44(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x3c(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x18102a
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
jmp 0x1810e7
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
xorl $0x1, %esi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1861d0
cmpl $0x1, %eax
je 0x181051
jmp 0x1810e5
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
xorl $0x1, %esi
movq -0x10(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x1861d0
movl -0x58(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x1861d0
cmpl $0x1, %eax
je 0x18108c
jmp 0x1810e5
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x1861d0
movq -0x60(%rbp), %rax
movl %eax, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x181030
jmp 0x1810e7
cmpl $-0x1, -0x40(%rbp)
je 0x181103
movq -0x28(%rbp), %rax
movl -0x44(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movl -0x48(%rbp), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x180e64
movq -0x28(%rbp), %rdi
callq 0x2210
movq -0x30(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nop