// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %r15
callq 0x694eb
movl 0x14(%r15), %r12d
addl %r12d, %r12d
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq %r15, 0x18(%rsp)
movq %r12, 0x30(%rsp)
cmpq %r12, %rbp
je 0xd1177
movq 0x20(%r15), %rax
movl %ebp, %ebx
shrl %ebx
cmpl $0x0, (%rax,%rbx,8)
js 0xd116f
movq 0x10(%rsp), %rax
cmpb $0x0, (%rax,%rbp)
jne 0xd116f
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xd41a6
testl %eax, %eax
je 0xd101a
cmpb $0x0, (%r14,%rbx)
jne 0xd116f
movq %r13, 0x38(%rsp)
movq 0x10(%rsp), %rax
movb $0x1, (%rax,%rbp)
movl %ebp, %ecx
xorl $0x1, %ecx
xorl %ebx, %ebx
movq %rbp, 0x20(%rsp)
xorl %r13d, %r13d
movl %ecx, 0xc(%rsp)
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xd41a6
cmpl $0x1, %eax
jne 0xd1039
movl %ebx, 0x28(%rsp)
movl %r13d, 0x8(%rsp)
movq %r15, %rdi
movl %ebp, %ebx
movl %ebp, %esi
movq %r14, %rdx
leaq 0x40(%rsp), %r13
movq %r13, %rcx
callq 0xd41a6
movq %r14, %r12
movl 0x48(%rsp), %r14d
movl %r14d, %ebp
xorl $0x1, %ebp
movq %r15, %rdi
movl %ebp, %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xd41a6
cmpl $0x1, %eax
jne 0xd103f
movl %ebx, %eax
xorq $0x1, %rax
movq 0x10(%rsp), %rcx
movb $0x1, %dl
movb %dl, (%rcx,%rax)
movb %dl, (%rcx,%r14)
movq %r15, %rdi
movl %ebx, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0xd41a6
movl 0x8(%rsp), %r13d
addl 0x40(%rsp), %r13d
movl 0x28(%rsp), %ebx
incl %ebx
movl %r14d, %eax
movl %ebp, %ecx
cmpq %r14, 0x20(%rsp)
movq %r12, %r14
movl %ebp, %edx
movl %eax, %ebp
jne 0xd0f6f
jmp 0xd104f
movl %ebp, %esi
xorl $0x1, %esi
movq %r15, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xd41a6
testl %eax, %eax
jne 0xd116f
jmp 0xd0f47
movl 0xc(%rsp), %edx
jmp 0xd104f
movl 0xc(%rsp), %edx
movq %r12, %r14
movl 0x8(%rsp), %r13d
movl 0x28(%rsp), %ebx
movl %edx, %eax
movq 0x20(%rsp), %rbp
xorl %ebp, %eax
cmpl $0x1, %eax
setne %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0xd112a
movq 0x18(%rsp), %rbx
movq 0x20(%rbx), %rax
movl %edx, 0xc(%rsp)
movl %edx, %ecx
shrl %ecx
movl (%rax,%rcx,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
addl %eax, %r13d
movl %r13d, 0x8(%rsp)
movl %ebp, %r13d
movl %r13d, %ebp
xorl $0x1, %ebp
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xd41a6
movl %r13d, %r15d
movl %r13d, %esi
cmpl $0x1, %eax
movq %r14, %r12
leaq 0x40(%rsp), %r13
jne 0xd1136
movq %rbx, %rdi
movq %rsi, 0x28(%rsp)
movl %ebp, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0xd41a6
movl 0x48(%rsp), %r14d
movl %r14d, %ebp
xorl $0x1, %ebp
movq %rbx, %rdi
movl %ebp, %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xd41a6
movq 0x28(%rsp), %rsi
cmpl $0x1, %eax
jne 0xd1136
movq 0x10(%rsp), %rax
movb $0x1, %cl
movb %cl, (%rax,%r14)
movb %cl, (%rax,%rsi)
movq %rbx, %rdi
movl %ebp, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0xd41a6
movl 0x8(%rsp), %eax
addl 0x40(%rsp), %eax
movl %eax, 0x8(%rsp)
movl %ebp, %r13d
movq %r12, %r14
jmp 0xd1094
movq 0x18(%rsp), %r15
movq 0x30(%rsp), %r12
jmp 0xd1164
cmpl $-0x1, %r15d
je 0xd114d
movl 0xc(%rsp), %eax
movq 0x10(%rsp), %rcx
movb $0x1, %dl
movb %dl, (%rcx,%rax)
movb %dl, (%rcx,%rsi)
movq %r12, %r14
movq 0x18(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x20(%rsp), %rbp
movl 0x8(%rsp), %r13d
movl %r13d, %eax
movq 0x38(%rsp), %r13
addq %rax, %r13
incq %rbp
jmp 0xd0f06
movq 0x10(%rsp), %rdi
callq 0x2220
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq