// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_ug_gen_primary(asg_t*, unsigned char)
ma_ug_t *ma_ug_gen_primary(asg_t *g, uint8_t flag)
{
    asg_cleanup(g);
	int32_t *mark;
	uint32_t i, v, n_vtx = g->n_seq * 2;
	///is a queue
	kdq_t(uint64_t) *q;
	ma_ug_t *ug;

	ug = (ma_ug_t*)calloc(1, sizeof(ma_ug_t));
	ug->g = asg_init();
    ///each node has two directions
	mark = (int32_t*)calloc(n_vtx, 4);

    ///for each untig, all node have the same direction
    ///and all node except the last one just have one edge
    ///the last one may have multiple edges
	q = kdq_init(uint64_t);
	for (v = 0; v < n_vtx; ++v) {
		uint32_t w, x, l, start, end, len;
		ma_utg_t *p;
        ///what's the usage of mark array
        ///mark array is used to mark if this node has already been included in a contig
        /****************************may have hap bugs********************************/
		///if (g->seq[v>>1].del || arc_cnt(g, v) == 0 || mark[v] || g->seq[v>>1].c != flag) continue;
        ///if (g->seq[v>>1].del || arc_cnt(g, v) == 0 || mark[v] || (g->seq[v>>1].c & flag) == 0) continue;
        ///if (g->seq[v>>1].del || arc_cnt(g, v) == 0 || mark[v]) continue;
        if (g->seq[v>>1].del || mark[v]) continue;
        if (arc_cnt(g, v) == 0 && arc_cnt(g, (v^1)) != 0) continue;
        if(flag == PRIMARY_LABLE && g->seq[v>>1].c == ALTER_LABLE) continue;
        if(flag == ALTER_LABLE && g->seq[v>>1].c != ALTER_LABLE) continue;
        /****************************may have hap bugs********************************/
		mark[v] = 1;
		q->count = 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 (arc_cnt(g, w) != 1) break;
			x = arc_first(g, w).v; // w->x
			if (arc_cnt(g, x^1) != 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));
			kdq_push(uint64_t, q, (uint64_t)w<<32 | l);
			end = x^1, len += l;
			w = x;
			if (x == v) break;
		}
        ///kdq_size(q) == 0 means there is just one read
		if (start != (end^1) || kdq_size(q) == 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);
			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 (arc_cnt(g, x^1) != 1) break;
			w = arc_first(g, x^1).v ^ 1; // w->x
			if (arc_cnt(g, w) != 1) break;
			mark[x] = mark[w^1] = 1;
			l = asg_arc_len(arc_first(g, w));
			///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);
			start = w, len += l;
			x = w;
		}
add_unitig:
		if (start != UINT32_MAX) mark[start] = mark[end] = 1;
		kv_pushp(ma_utg_t, ug->u, &p);
		p->s = 0, p->start = start, p->end = end, p->len = len, p->n = kdq_size(q), p->circ = (start == UINT32_MAX);
		p->m = p->n;
		kv_roundup32(p->m);
		p->a = (uint64_t*)malloc(8 * p->m);
		//all elements are saved here
		for (i = 0; i < kdq_size(q); ++i)
			p->a[i] = kdq_at(q, i);
	}
	kdq_destroy(uint64_t, q);

	// add arcs between unitigs; reusing mark for a different purpose
	//ug saves all unitigs
	for (v = 0; v < n_vtx; ++v) mark[v] = -1;

    //mark all start nodes and end nodes of all unitigs
    ///note ug->u.a[i].start == ug->u.a[i].a[0]
    ///but ug->u.a[i].end = ug->u.a[i].a[ug->u.a[i].n-1]^1
    ///ug->u.a[i].start has the same direction as other non-end node
    ///while ug->u.a[i].end is the only one with reverse direction
	for (i = 0; i < ug->u.n; ++i) {
		if (ug->u.a[i].circ) continue;
        ///i is the untig id
		mark[ug->u.a[i].start] = i<<1 | 0;
		mark[ug->u.a[i].end] = i<<1 | 1;
	}

	//scan all edges
	for (i = 0; i < g->n_arc; ++i) {
		asg_arc_t *p = &g->arc[i];
		if (p->del) continue;
		/**
	p->ul: |____________31__________|__________1___________|______________32_____________|
	                    qns            direction of overlap       length of this node (not overlap length)
						                 (based on query)
	p->v : |___________31___________|__________1___________|
				        tns             reverse direction of overlap
						                  (based on target)
	p->ol: overlap length
	**/
		///to connect two unitigs, we need to connect the end of unitig x to the start of unitig y
		///so we need to ^1 to get the reverse direction of (x's end)?
        ///>=0 means this node is a start/end node of an unitig
        ///means this node is a intersaction node
        /**for one untig,
          start                end
         ----->              <------
         so if we want to find the edge between two untigs, their start and end look like: 

         start                end
         ----->              <------
         ---------------------------

                                    start                end
                                    ----->              <------
                                    ---------------------------
        p->ul>>32^1 is the end of the first untig,
        p->v is the start of the second untig
        **/
		if (mark[p->ul>>32^1] >= 0 && mark[p->v] >= 0) {
			asg_arc_t *q;
			uint32_t u = mark[p->ul>>32^1]^1;
			int l = ug->u.a[u>>1].len - p->ol;
			if (l < 0) l = 1;
			q = asg_arc_pushp(ug->g);
			q->ol = p->ol, q->del = 0;
			q->ul = (uint64_t)u<<32 | l;
			q->v = mark[p->v]; q->ou = 0;
            q->el = p->el;
		}
	}
	for (i = 0; i < ug->u.n; ++i)
		asg_seq_set(ug->g, i, ug->u.a[i].len, 0);
	asg_cleanup(ug->g);
	free(mark);
	return ug;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebp
movq %rdi, %rbx
callq 0x694eb
movq %rbx, 0x20(%rsp)
movl 0x14(%rbx), %r14d
addl %r14d, %r14d
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x2120
movq %rax, %rbx
callq 0x68f5d
movq %rax, 0x48(%rsp)
movq %rbx, 0x30(%rsp)
movq %rax, 0x18(%rbx)
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, %r12
movq $0x0, (%rsp)
callq 0x77275
movq %rax, %rbx
movl %ebp, 0x44(%rsp)
movzbl %bpl, %eax
movl %eax, 0x40(%rsp)
pushq $0x1
popq %rbp
xorl %edi, %edi
movq $0x0, 0x10(%rsp)
xorl %r9d, %r9d
movq %r12, 0x18(%rsp)
movq %r14, 0x28(%rsp)
cmpq %r14, %r9
je 0x7855c
movq 0x20(%rsp), %r8
movq 0x20(%r8), %rax
movl %r9d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x78554
cmpl $0x0, (%r12,%r9,4)
jne 0x78554
movq 0x28(%r8), %rdx
cmpl $0x0, (%rdx,%r9,8)
je 0x7829d
cmpb $0x1, 0x44(%rsp)
je 0x782b0
cmpl $0x0, 0x40(%rsp)
jne 0x782b7
cmpb $0x1, 0x4(%rax,%rcx,8)
jne 0x782b7
movq 0x28(%rsp), %r14
jmp 0x78554
movl %r9d, %esi
xorq $0x1, %rsi
cmpl $0x0, (%rdx,%rsi,8)
jne 0x78554
jmp 0x7827e
cmpb $0x1, 0x4(%rax,%rcx,8)
jne 0x78293
movq %rdi, 0x8(%rsp)
movl $0x1, (%r12,%r9,4)
andq $0x0, 0x8(%rbx)
movl %r9d, %eax
xorl $0x1, %eax
xorl %r13d, %r13d
movq %r9, 0x38(%rsp)
movq %r9, %r15
movq 0x28(%r8), %rdx
movl %r15d, %esi
movq (%rdx,%rsi,8), %rdi
cmpl $0x1, %edi
jne 0x78343
movq 0x8(%r8), %rcx
shrq $0x20, %rdi
imulq $0x18, %rdi, %rdi
movl 0x8(%rcx,%rdi), %r15d
movq %r15, %r12
xorq $0x1, %r12
cmpl $0x1, (%rdx,%r12,8)
jne 0x78343
movl %esi, %eax
xorl %ebp, %eax
movq 0x18(%rsp), %rdx
movl %ebp, (%rdx,%rax,4)
movl %ebp, (%rdx,%r15,4)
movq (%rcx,%rdi), %r14
movl %r14d, %eax
shlq $0x20, %rsi
orq %rax, %rsi
movq %rbx, %rdi
callq 0x772b6
movq 0x20(%rsp), %r8
addl %r14d, %r13d
movl %r12d, %eax
cmpq %r15, 0x38(%rsp)
jne 0x782da
jmp 0x78346
movl %eax, %r12d
movl %r12d, %esi
xorl $0x1, %esi
movq 0x38(%rsp), %r15
cmpq %rsi, %r15
jne 0x7836e
movl $0x80000000, %edi       # imm = 0x80000000
cmpq $0x0, 0x8(%rbx)
je 0x7836e
pushq $-0x1
popq %r15
movl %r15d, %r12d
jmp 0x78424
movq 0x20(%r8), %rax
movl %r12d, %ecx
shrl %ecx
movl (%rax,%rcx,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
shlq $0x20, %rsi
orq %r14, %rsi
movq %rbx, %rdi
callq 0x772b6
addl %r14d, %r13d
movq 0x20(%rsp), %rcx
movq 0x28(%rcx), %rdx
movl %r15d, %eax
xorl $0x1, %eax
movq (%rdx,%rax,8), %rsi
movl %r15d, %eax
cmpl $0x1, %esi
jne 0x78403
movq 0x8(%rcx), %rcx
shrq $0x20, %rsi
imulq $0x18, %rsi, %rsi
movl 0x8(%rcx,%rsi), %esi
movq %rsi, %r14
xorq $0x1, %r14
movq (%rdx,%r14,8), %rdx
cmpl $0x1, %edx
jne 0x78403
movq 0x18(%rsp), %rdi
movl %ebp, (%rdi,%rsi,4)
movl %ebp, (%rdi,%rax,4)
shrq $0x20, %rdx
imulq $0x18, %rdx, %rax
movq (%rcx,%rax), %r15
movl %r15d, %eax
movq %r14, %rsi
shlq $0x20, %rsi
orq %rax, %rsi
movq %rbx, %rdi
callq 0x77318
addl %r15d, %r13d
movl %r14d, %r15d
jmp 0x78394
cmpl $-0x1, %r15d
je 0x7841b
movl %r12d, %ecx
movq 0x18(%rsp), %rdx
movl %ebp, (%rdx,%rcx,4)
movl %ebp, (%rdx,%rax,4)
xorl %edi, %edi
jmp 0x78424
movl $0x80000000, %edi       # imm = 0x80000000
pushq $-0x1
popq %r15
movq 0x8(%rsp), %rcx
cmpq %rcx, 0x10(%rsp)
movq 0x30(%rsp), %rax
jne 0x7846d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsp)
movq %rdx, 0x8(%rax)
imulq $0x28, %rdx, %rsi
movl %edi, %r14d
movq (%rsp), %rdi
callq 0x2370
movl %r14d, %edi
movq %rax, %r14
movq 0x30(%rsp), %rax
movq %r14, 0x10(%rax)
jmp 0x78471
movq (%rsp), %r14
movq 0x10(%rsp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x50(%rsp)
movq %rdx, (%rax)
imulq $0x28, %rcx, %rdx
andq $0x0, 0x20(%r14,%rdx)
movl %r15d, 0x4(%r14,%rdx)
movl %r12d, 0x8(%r14,%rdx)
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl (%r14,%rdx), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %eax
orl %r13d, %eax
movl %eax, (%r14,%rdx)
movl 0x8(%rbx), %eax
movl %eax, 0x10(%r14,%rdx)
orl %edi, %r13d
movl %r13d, (%r14,%rdx)
movq %rdx, %r15
decl %eax
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
leal 0x1(%rcx), %eax
movl %eax, 0xc(%r14,%rdx)
leal 0x8(,%rcx,8), %edi
callq 0x2280
movq %r14, (%rsp)
movq %rax, 0x18(%r14,%r15)
movq 0x8(%rbx), %rcx
xorl %edx, %edx
movq 0x18(%rsp), %r12
movq 0x28(%rsp), %r14
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
movq 0x38(%rsp), %r9
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x78545
movq (%rbx), %rsi
andq %r8, %rsi
addq %rdx, %rsi
andq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdi
movq (%rdi,%rsi,8), %rsi
movq %rsi, (%rax,%rdx,8)
incl %edx
jmp 0x78521
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdi
incq %r9
jmp 0x78247
movq %rbx, %rdi
callq 0x773a7
xorl %eax, %eax
movq (%rsp), %r8
cmpq %rax, %r14
je 0x78579
orl $-0x1, (%r12,%rax,4)
incq %rax
jmp 0x7856a
xorl %eax, %eax
movq 0x20(%rsp), %rdi
movl %eax, %eax
cmpq %rax, 0x10(%rsp)
jbe 0x785b2
imulq $0x28, %rax, %rcx
cmpl $0x0, (%r8,%rcx)
js 0x785ab
leaq (%r8,%rcx), %rdx
leal -0x1(%rbp), %esi
movl 0x4(%rdx), %edx
movl %esi, (%r12,%rdx,4)
movl 0x8(%r8,%rcx), %ecx
movl %ebp, (%r12,%rcx,4)
incl %eax
addl $0x2, %ebp
jmp 0x78580
pushq $0x12
popq %rbx
xorl %r14d, %r14d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x4(%rdi), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
jae 0x78672
movq 0x8(%rdi), %r15
movl -0x6(%r15,%rbx), %eax
testl %eax, %eax
js 0x78666
movl -0xe(%r15,%rbx), %ecx
shlq $0x2, %rcx
xorq $0x4, %rcx
movl (%r12,%rcx), %r13d
testl %r13d, %r13d
js 0x78666
movl -0xa(%r15,%rbx), %ecx
cmpl $0x0, (%r12,%rcx,4)
js 0x78666
movl %r13d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movq (%rsp), %rdx
movl (%rdx,%rcx), %ecx
andl %ebp, %ecx
subl %eax, %ecx
pushq $0x1
popq %r12
js 0x7861e
movl %ecx, %r12d
xorl $0x1, %r13d
movq 0x48(%rsp), %rdi
callq 0x6c45d
movl -0x6(%r15,%rbx), %ecx
andl %ebp, %ecx
movl %ecx, 0xc(%rax)
shlq $0x20, %r13
movslq %r12d, %rcx
orq %r13, %rcx
movq %rcx, (%rax)
movl -0xa(%r15,%rbx), %ecx
movq 0x18(%rsp), %r12
movl (%r12,%rcx,4), %ecx
movl %ecx, 0x8(%rax)
andw $0xc000, 0x10(%rax)     # imm = 0xC000
movb (%r15,%rbx), %cl
movb %cl, 0x12(%rax)
movq 0x20(%rsp), %rdi
incq %r14
addq $0x18, %rbx
jmp 0x785bd
xorl %eax, %eax
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x48(%rsp), %r14
movl %eax, %ebx
cmpq %rbx, 0x10(%rsp)
jbe 0x786a5
imulq $0x28, %rbx, %rax
movq (%rsp), %rcx
movl (%rcx,%rax), %edx
andl %ebp, %edx
movq %r14, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x69316
leal 0x1(%rbx), %eax
jmp 0x7867e
movq %r14, %rdi
callq 0x694eb
movq %r12, %rdi
callq 0x2220
movq 0x30(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq