// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: ma_ug_gen(asg_t*)
ma_ug_t *ma_ug_gen(asg_t *g)
{
    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);

	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 bugs********************************/
		///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;
        /****************************may have 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;
		}
		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);

            // fprintf(stderr, "uId: %u, >%.*s (%u)\n", 
            // ug->u.n, (int)Get_NAME_LENGTH((R_INF), w>>1), Get_NAME((R_INF), w>>1), w>>1);

			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
	for (i = 0; i < ug->u.n; ++i) {
		if (ug->u.a[i].circ) continue;
		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;
		///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
		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
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, 0x40(%rsp)
movq %rbx, 0x30(%rsp)
movq %rax, 0x18(%rbx)
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, %r12
movq $0x0, 0x8(%rsp)
callq 0x77275
movq %rax, %rbx
pushq $0x1
popq %rbp
movq $0x0, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r15d, %r15d
movq %r12, 0x18(%rsp)
movq %r14, 0x50(%rsp)
cmpq %r14, %r15
je 0x77104
movq 0x20(%rsp), %r8
movq 0x20(%r8), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x770fc
cmpl $0x0, (%r12,%r15,4)
jne 0x770fc
movq 0x28(%r8), %rcx
movl %r15d, %eax
xorl $0x1, %eax
cmpl $0x0, (%rcx,%r15,8)
je 0x76faa
andq $0x0, 0x8(%rbx)
movl $0x1, (%r12,%r15,4)
xorl %r13d, %r13d
movl %r15d, %r14d
movq %r15, 0x48(%rsp)
movq 0x28(%r8), %rdx
movl %r14d, %esi
movq (%rdx,%rsi,8), %rdi
cmpl $0x1, %edi
jne 0x76eda
movq 0x8(%r8), %rcx
shrq $0x20, %rdi
imulq $0x18, %rdi, %rdi
movl 0x8(%rcx,%rdi), %r14d
movq %r14, %r12
xorq $0x1, %r12
cmpl $0x1, (%rdx,%r12,8)
jne 0x76eda
movl %esi, %eax
xorl %ebp, %eax
movq 0x18(%rsp), %rdx
movl %ebp, (%rdx,%rax,4)
movl %ebp, (%rdx,%r14,4)
movq (%rcx,%rdi), %r15
movl %r15d, %eax
shlq $0x20, %rsi
orq %rax, %rsi
movq %rbx, %rdi
callq 0x772b6
movq 0x20(%rsp), %r8
addl %r15d, %r13d
movq 0x48(%rsp), %r15
movl %r12d, %eax
cmpq %r14, %r15
jne 0x76e6e
jmp 0x76edd
movl %eax, %r12d
movl %r12d, %esi
xorl $0x1, %esi
cmpq %rsi, %r15
jne 0x76f00
movl $0x80000000, %edi       # imm = 0x80000000
cmpq $0x0, 0x8(%rbx)
je 0x76f00
pushq $-0x1
popq %r14
movl %r14d, %r12d
jmp 0x76fc4
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
movl %r15d, %r14d
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 0x76f92
movq 0x8(%rcx), %rcx
shrq $0x20, %rsi
imulq $0x18, %rsi, %rsi
movl 0x8(%rcx,%rsi), %esi
movq %rsi, %r15
xorq $0x1, %r15
movq (%rdx,%r15,8), %rdx
cmpl $0x1, %edx
jne 0x76f92
movq 0x18(%rsp), %rdi
movl %ebp, (%rdi,%rsi,4)
movl %ebp, (%rdi,%rax,4)
shrq $0x20, %rdx
imulq $0x18, %rdx, %rax
movq (%rcx,%rax), %r14
movl %r14d, %eax
movq %r15, %rsi
shlq $0x20, %rsi
orq %rax, %rsi
movq %rbx, %rdi
callq 0x77318
jmp 0x76f23
cmpl $-0x1, %r14d
je 0x76fbb
movl %r12d, %ecx
movq 0x18(%rsp), %rdx
movl %ebp, (%rdx,%rcx,4)
movl %ebp, (%rdx,%rax,4)
xorl %edi, %edi
jmp 0x76fc4
movl %eax, %edx
cmpl $0x0, (%rcx,%rdx,8)
jne 0x770fc
jmp 0x76e56
movl $0x80000000, %edi       # imm = 0x80000000
pushq $-0x1
popq %r14
movq 0x28(%rsp), %rcx
cmpq %rcx, 0x10(%rsp)
movq 0x30(%rsp), %rax
jne 0x77018
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x38(%rsp)
movq %rdx, 0x8(%rax)
imulq $0x28, %rdx, %rsi
movl %edi, %r15d
movq 0x8(%rsp), %rdi
callq 0x2370
movl %r15d, %edi
movq %rax, %r15
movq 0x30(%rsp), %rax
movq %r15, 0x10(%rax)
movq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rsp)
jmp 0x7701d
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x38(%rsp)
movq %rdx, (%rax)
imulq $0x28, %rcx, %rdx
andq $0x0, 0x20(%r15,%rdx)
movl %r14d, 0x4(%r15,%rdx)
movl %r12d, 0x8(%r15,%rdx)
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movl (%r15,%rdx), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %eax
orl %r13d, %eax
movl %eax, (%r15,%rdx)
movl 0x8(%rbx), %eax
movl %eax, 0x10(%r15,%rdx)
orl %edi, %r13d
movl %r13d, (%r15,%rdx)
movq %rdx, %r14
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(%r15,%rdx)
leal 0x8(,%rcx,8), %edi
callq 0x2280
movq %r15, 0x8(%rsp)
movq %rax, 0x18(%r15,%r14)
movq 0x8(%rbx), %rcx
xorl %edx, %edx
movq 0x18(%rsp), %r12
movq 0x50(%rsp), %r14
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
movq 0x48(%rsp), %r15
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x770f2
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 0x770ce
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
incq %r15
jmp 0x76e15
movq %rbx, %rdi
callq 0x773a7
xorl %eax, %eax
movq 0x8(%rsp), %r8
cmpq %rax, %r14
je 0x77122
orl $-0x1, (%r12,%rax,4)
incq %rax
jmp 0x77113
xorl %eax, %eax
movq 0x20(%rsp), %rdi
movl %eax, %eax
cmpq %rax, 0x10(%rsp)
jbe 0x7715b
imulq $0x28, %rax, %rcx
cmpl $0x0, (%r8,%rcx)
js 0x77154
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 0x77129
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 0x7721d
movq 0x8(%rdi), %r13
movl -0x6(%r13,%rbx), %eax
testl %eax, %eax
js 0x77211
movl -0xe(%r13,%rbx), %ecx
shlq $0x2, %rcx
xorq $0x4, %rcx
movl (%r12,%rcx), %r15d
testl %r15d, %r15d
js 0x77211
movl -0xa(%r13,%rbx), %ecx
cmpl $0x0, (%r12,%rcx,4)
js 0x77211
movl %r15d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movq 0x8(%rsp), %rdx
movl (%rdx,%rcx), %ecx
andl %ebp, %ecx
subl %eax, %ecx
pushq $0x1
popq %r12
js 0x771c8
movl %ecx, %r12d
xorl $0x1, %r15d
movq 0x40(%rsp), %rdi
callq 0x6c45d
movl -0x6(%r13,%rbx), %ecx
andl %ebp, %ecx
movl %ecx, 0xc(%rax)
shlq $0x20, %r15
movslq %r12d, %rcx
orq %r15, %rcx
movq %rcx, (%rax)
movl -0xa(%r13,%rbx), %ecx
movq 0x18(%rsp), %r12
movl (%r12,%rcx,4), %ecx
movl %ecx, 0x8(%rax)
andw $0xc000, 0x10(%rax)     # imm = 0xC000
movb (%r13,%rbx), %cl
movb %cl, 0x12(%rax)
movq 0x20(%rsp), %rdi
incq %r14
addq $0x18, %rbx
jmp 0x77166
xorl %eax, %eax
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x40(%rsp), %r14
movl %eax, %ebx
cmpq %rbx, 0x10(%rsp)
jbe 0x77251
imulq $0x28, %rbx, %rax
movq 0x8(%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 0x77229
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