// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movl %esi, %r13d
movq %rdi, %rbx
callq 0xa02fa
movl 0x14(%rbx), %r14d
addl %r14d, %r14d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2120
movq %rax, %r15
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, 0x38(%rsp)
movq %rax, 0x18(%r15)
movl $0x4, %esi
movq %r14, %rdi
callq 0x2120
movq %rax, %r12
movl $0x1, %edi
movl $0x20, %esi
callq 0x2120
movq %rax, %rbp
movq (%rax), %rax
movabsq $0x3ffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFF
andq %rcx, %rax
btsq $0x3b, %rax
movq %rax, (%rbp)
movq $0x3, 0x10(%rbp)
movl $0x20, %edi
callq 0x2280
movq %rax, 0x18(%rbp)
movl %r14d, %eax
movq %rax, 0x20(%rsp)
movq %r14, 0x40(%rsp)
testl %r14d, %r14d
movq %r15, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %rbx, 0x18(%rsp)
je 0xb1fd4
movq %rbx, %r9
xorl %ebx, %ebx
movzbl %r13b, %eax
movl %eax, 0x30(%rsp)
movl %r13d, 0x2c(%rsp)
movq 0x20(%r9), %rax
movl %ebx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xb1b6a
cmpl $0x0, (%r12,%rbx,4)
je 0xb1b79
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0xb1b55
jmp 0xb1fd4
movq 0x28(%r9), %rdx
cmpl $0x0, (%rdx,%rbx,8)
je 0xb1cf4
cmpb $0x1, %r13b
je 0xb1ba1
cmpl $0x0, 0x30(%rsp)
jne 0xb1ba8
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xb1b6a
cmpb $0x1, %r13b
jne 0xb1ba8
cmpb $0x1, 0x4(%rax,%rcx,8)
jne 0xb1b6a
movl $0x1, (%r12,%rbx,4)
movq $0x0, 0x8(%rbp)
movl %ebx, %eax
xorl $0x1, %eax
movl %eax, 0x4(%rsp)
movq 0x28(%r9), %rdi
movq (%rdi,%rbx,8), %rax
xorl %r13d, %r13d
cmpl $0x1, %eax
jne 0xb1cca
movq 0x8(%r9), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rcx
movl 0x8(%rdx,%rcx,8), %ecx
movq %rcx, %rsi
xorq $0x1, %rsi
cmpl $0x1, (%rdi,%rsi,8)
jne 0xb1cca
xorl %r13d, %r13d
movl %ebx, %r11d
movq %rbx, %r12
movl %r11d, %edi
movl $0x1, %r10d
xorl %r10d, %edi
movq 0x8(%rsp), %r8
movl %r10d, (%r8,%rdi,4)
movl %ecx, %r11d
movl %ecx, %r15d
movl %r10d, (%r8,%r15,4)
leaq (%rax,%rax,2), %rax
movq (%rbp), %rcx
shrq $0x3a, %rcx
movl $0x1, %edi
shlq %cl, %rdi
movq (%rdx,%rax,8), %r14
movl %esi, 0x4(%rsp)
cmpq %rdi, 0x8(%rbp)
jne 0xb1c5b
incl %ecx
movq %rbp, %rdi
movl %ecx, %esi
movl %r11d, 0x34(%rsp)
callq 0x100f0f
movl 0x34(%rsp), %r11d
movq 0x18(%rsp), %r9
movq 0x8(%rbp), %rax
movq 0x18(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rbp)
movq (%rbp), %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
addq %rax, %rdx
movl %r14d, %eax
shlq $0x20, %r12
andq 0x10(%rbp), %rdx
orq %rax, %r12
movq %r12, (%rcx,%rdx,8)
addl %r14d, %r13d
cmpq %r15, %rbx
je 0xb1cca
movq 0x28(%r9), %rdi
movq (%rdi,%r15,8), %rax
cmpl $0x1, %eax
jne 0xb1cca
movq 0x8(%r9), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rcx
movl 0x8(%rdx,%rcx,8), %ecx
movq %rcx, %rsi
xorq $0x1, %rsi
cmpl $0x1, (%rdi,%rsi,8)
movq %r15, %r12
je 0xb1bff
movl 0x4(%rsp), %eax
movl %eax, %r15d
xorl $0x1, %r15d
cmpq %r15, %rbx
jne 0xb1d09
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpq $0x0, 0x8(%rbp)
je 0xb1d09
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
jmp 0xb1e7f
movl %ebx, %esi
xorq $0x1, %rsi
cmpl $0x0, (%rdx,%rsi,8)
jne 0xb1b6a
jmp 0xb1b87
movq 0x20(%r9), %rax
movl 0x4(%rsp), %ecx
movl %ecx, %edx
movq (%rbp), %rcx
shrq $0x3a, %rcx
movl $0x1, %esi
shlq %cl, %rsi
shrl %edx
movl (%rax,%rdx,8), %r14d
cmpq %rsi, 0x8(%rbp)
movq 0x8(%rsp), %r12
jne 0xb1d45
incl %ecx
movq %rbp, %rdi
movl %ecx, %esi
callq 0x100f0f
movq 0x18(%rsp), %r9
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
shlq $0x20, %r15
movq 0x8(%rbp), %rax
movq 0x18(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rbp)
movq (%rbp), %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
addq %rax, %rdx
andq 0x10(%rbp), %rdx
orq %r14, %r15
movq %r15, (%rcx,%rdx,8)
addl %r14d, %r13d
movq 0x28(%r9), %rax
movl %ebx, %ecx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rcx
movl %ebx, %r14d
cmpl $0x1, %ecx
jne 0xb1e84
movq 0x8(%r9), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%rdx,%rcx,8), %r12d
movq %r12, %r15
xorq $0x1, %r15
movq (%rax,%r15,8), %rax
cmpl $0x1, %eax
jne 0xb1e7f
movq 0x8(%rsp), %rsi
movl $0x1, %edi
movl %edi, (%rsi,%r12,4)
movl %r14d, %ecx
movl %edi, (%rsi,%rcx,4)
shrq $0x20, %rax
movq (%rbp), %rcx
shrq $0x3a, %rcx
movl $0x1, %esi
shlq %cl, %rsi
leaq (%rax,%rax,2), %rax
movq (%rdx,%rax,8), %r14
cmpq %rsi, 0x8(%rbp)
jne 0xb1e08
incl %ecx
movq %rbp, %rdi
movl %ecx, %esi
callq 0x100f0f
movq 0x18(%rsp), %r9
movl %r14d, %eax
movq %r15, %rdx
shlq $0x20, %rdx
orq %rax, %rdx
incq 0x8(%rbp)
movq (%rbp), %rax
movq %rax, %rcx
shrq $0x3a, %rcx
movq $-0x1, %rsi
shlq %cl, %rsi
movq 0x18(%rbp), %rcx
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
leaq (%rax,%r8), %rdi
testq %r8, %rax
notq %rsi
cmovneq %rdi, %rsi
andq %r8, %rsi
movabsq $-0x400000000000000, %rdi # imm = 0xFC00000000000000
andq %rdi, %rax
orq %rsi, %rax
movq %rax, (%rbp)
movq %rdx, (%rcx,%rsi,8)
addl %r14d, %r13d
movq 0x28(%r9), %rax
movq (%rax,%r12,8), %rcx
movl %r15d, %r14d
cmpl $0x1, %ecx
je 0xb1d9c
movl %r15d, %r14d
movq 0x8(%rsp), %r12
cmpl $-0x1, %r14d
movq 0x10(%rsp), %r15
je 0xb1ea3
movl 0x4(%rsp), %eax
movl $0x1, %ecx
movl %ecx, (%r12,%rax,4)
movl %r14d, %eax
movl %ecx, (%r12,%rax,4)
movq (%r15), %r12
movq 0x8(%r15), %rax
cmpq %rax, %r12
jne 0xb1ed8
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x10(%r15)
xorl %eax, %eax
cmpl $-0x1, %r14d
sete %al
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %r15
leaq 0x1(%r12), %rcx
movq 0x10(%rsp), %rdx
movq %rcx, (%rdx)
leaq (%r12,%r12,4), %r12
movq $0x0, 0x20(%r15,%r12,8)
movl %r14d, 0x4(%r15,%r12,8)
movl 0x4(%rsp), %ecx
movl %ecx, 0x8(%r15,%r12,8)
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl (%r15,%r12,8), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %r13d, %ecx
movl %ecx, (%r15,%r12,8)
movq 0x8(%rbp), %r14
movl %r14d, 0x10(%r15,%r12,8)
shll $0x1f, %eax
orl %r13d, %eax
movl %eax, (%r15,%r12,8)
leal -0x1(%r14), %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(%r15,%r12,8)
leal 0x8(,%rcx,8), %edi
callq 0x2280
movq %rax, 0x18(%r15,%r12,8)
movq 0x10(%rsp), %r15
testq %r14, %r14
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %r9
movl 0x2c(%rsp), %r13d
je 0xb1b6a
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %rdx
movq (%rbp), %rsi
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
andq %rdi, %rsi
xorl %edi, %edi
leaq (%rsi,%rdi), %r8
andq %rcx, %r8
movq (%rdx,%r8,8), %r8
movq %r8, (%rax,%rdi,8)
incq %rdi
movl %edi, %r8d
cmpq %r8, %r14
ja 0xb1fb5
jmp 0xb1b6a
movq 0x18(%rbp), %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
cmpl $0x0, 0x40(%rsp)
je 0xb2002
movq 0x20(%rsp), %rdx
shlq $0x2, %rdx
movq %r12, %rdi
movl $0xff, %esi
callq 0x20e0
movq (%r15), %rbx
testq %rbx, %rbx
movq 0x18(%rsp), %r8
je 0xb2047
movq 0x10(%r15), %rax
addq $0x8, %rax
movl $0x1, %ecx
movl $0x1, %edx
cmpl $0x0, -0x8(%rax)
js 0xb2037
leal -0x1(%rcx), %esi
movl -0x4(%rax), %edi
movl %esi, (%r12,%rdi,4)
movl (%rax), %esi
movl %ecx, (%r12,%rsi,4)
movl %edx, %esi
addq $0x28, %rax
incl %edx
addl $0x2, %ecx
cmpq %rsi, %rbx
ja 0xb2021
testl $0x7fffffff, 0x4(%r8)   # imm = 0x7FFFFFFF
je 0xb2196
movl $0x12, %r13d
xorl %ebp, %ebp
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq %rbx, 0x20(%rsp)
movq 0x8(%r8), %r14
movl -0x6(%r14,%r13), %eax
testl %eax, %eax
js 0xb217d
movl -0xe(%r14,%r13), %ecx
shlq $0x2, %rcx
xorq $0x4, %rcx
movl (%r12,%rcx), %r15d
testl %r15d, %r15d
js 0xb217d
movl -0xa(%r14,%r13), %ecx
cmpl $0x0, (%r12,%rcx,4)
js 0xb217d
movq 0x10(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl %r15d, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %ecx
andl %edi, %ecx
subl %eax, %ecx
movl $0x1, %r9d
js 0xb20c6
movl %ecx, %r9d
movq 0x38(%rsp), %r12
movl 0x4(%r12), %ecx
andl %edi, %ecx
movl (%r12), %eax
cmpl %eax, %ecx
jne 0xb2117
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%r12)
movq 0x8(%r12), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movl %r9d, %ebx
callq 0x2370
movl %ebx, %r9d
movq 0x20(%rsp), %rbx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %r8
movq %rax, 0x8(%r12)
xorl $0x1, %r15d
movq 0x8(%r12), %rax
movl 0x4(%r12), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl %edi, %esi
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r12)
leaq (%rdx,%rdx,2), %rcx
movl -0x6(%r14,%r13), %edx
andl %edi, %edx
movl %edx, 0xc(%rax,%rcx,8)
shlq $0x20, %r15
movslq %r9d, %rdx
orq %r15, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0xa(%r14,%r13), %edx
movq 0x8(%rsp), %r12
movl (%r12,%rdx,4), %edx
movl %edx, 0x8(%rax,%rcx,8)
andw $0xc000, 0x10(%rax,%rcx,8) # imm = 0xC000
movb (%r14,%r13), %dl
movb %dl, 0x12(%rax,%rcx,8)
incq %rbp
movl 0x4(%r8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x18, %r13
cmpq %rax, %rbp
jb 0xb2067
testq %rbx, %rbx
movq 0x10(%rsp), %r15
movq 0x38(%rsp), %r13
je 0xb21d0
movq 0x10(%r15), %r14
xorl %r12d, %r12d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%r12,%r12,4), %rax
movl (%r14,%rax,8), %edx
andl %ebp, %edx
movq %r13, %rdi
movl %r12d, %esi
xorl %ecx, %ecx
callq 0xa0115
incl %r12d
cmpq %r12, %rbx
ja 0xb21b1
movq %r13, %rdi
callq 0xa02fa
movq 0x8(%rsp), %rdi
callq 0x2220
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq