// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %r14
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
callq 0xa02fa
movl 0x14(%r14), %r15d
addl %r15d, %r15d
movl $0x1, %edi
movl $0x38, %esi
callq 0x2120
movq %rax, %rbx
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rbx, 0x10(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x18(%rbx)
movl $0x4, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2120
movq %rax, %rbp
movq (%rax), %rax
andq %r12, %rax
btsq $0x3b, %rax
movq %rax, (%rbp)
movq $0x3, 0x10(%rbp)
movl $0x20, %edi
callq 0x2280
movq %r14, %r9
movq %rax, 0x18(%rbp)
movl %r15d, %r14d
movq %r15, 0x38(%rsp)
testl %r15d, %r15d
movq %r9, 0x20(%rsp)
je 0xb078e
xorl %r13d, %r13d
movq %r14, 0x40(%rsp)
movq 0x20(%r9), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xb0782
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax,%r13,4)
jne 0xb0782
movq 0x28(%r9), %rax
cmpl $0x0, (%rax,%r13,8)
je 0xb0771
movq 0x8(%rsp), %r8
movl $0x1, (%r8,%r13,4)
movq $0x0, 0x8(%rbp)
movl %r13d, %eax
xorl $0x1, %eax
movl %eax, 0x18(%rsp)
movq 0x28(%r9), %rdi
movq (%rdi,%r13,8), %rax
xorl %r14d, %r14d
cmpl $0x1, %eax
jne 0xb049e
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 0xb049e
xorl %r14d, %r14d
movl %r13d, %r11d
movq %r13, %r12
movl %r11d, %edi
movl $0x1, %r10d
xorl %r10d, %edi
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), %rbx
movl %esi, 0x18(%rsp)
cmpq %rdi, 0x8(%rbp)
jne 0xb0430
incl %ecx
movq %rbp, %rdi
movl %ecx, %esi
movl %r11d, 0x2c(%rsp)
callq 0x100f0f
movl 0x2c(%rsp), %r11d
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
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 %ebx, %eax
shlq $0x20, %r12
andq 0x10(%rbp), %rdx
orq %rax, %r12
movq %r12, (%rcx,%rdx,8)
addl %ebx, %r14d
cmpq %r15, %r13
je 0xb049e
movq 0x28(%r9), %rdi
movq (%rdi,%r15,8), %rax
cmpl $0x1, %eax
jne 0xb049e
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 0xb03d4
movl 0x18(%rsp), %eax
movl %eax, %r15d
xorl $0x1, %r15d
cmpq %r15, %r13
jne 0xb04c7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq $0x0, 0x8(%rbp)
je 0xb04c7
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
jmp 0xb063b
movq 0x20(%r9), %rax
movl 0x18(%rsp), %ecx
movl %ecx, %edx
movq (%rbp), %rcx
shrq $0x3a, %rcx
movl $0x1, %esi
shlq %cl, %rsi
shrl %edx
movl (%rax,%rdx,8), %ebx
cmpq %rsi, 0x8(%rbp)
jne 0xb0502
incl %ecx
movq %rbp, %rdi
movl %ecx, %esi
callq 0x100f0f
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
andl $0x7fffffff, %ebx       # 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 %rbx, %r15
movq %r15, (%rcx,%rdx,8)
addl %ebx, %r14d
movq 0x28(%r9), %rax
movl %r13d, %ecx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rcx
movl %r13d, %ebx
cmpl $0x1, %ecx
jne 0xb063b
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 0xb063b
movl $0x1, %esi
movl %esi, (%r8,%r12,4)
movl %ebx, %ecx
movl %esi, (%r8,%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), %rbx
cmpq %rsi, 0x8(%rbp)
jne 0xb05c5
incl %ecx
movq %rbp, %rdi
movl %ecx, %esi
callq 0x100f0f
movq 0x20(%rsp), %r9
movq 0x8(%rsp), %r8
movl %ebx, %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, %r10 # imm = 0x3FFFFFFFFFFFFFF
leaq (%rax,%r10), %rdi
testq %r10, %rax
notq %rsi
cmovneq %rdi, %rsi
andq %r10, %rsi
movabsq $-0x400000000000000, %rdi # imm = 0xFC00000000000000
andq %rdi, %rax
orq %rsi, %rax
movq %rax, (%rbp)
movq %rdx, (%rcx,%rsi,8)
addl %ebx, %r14d
movq 0x28(%r9), %rax
movq (%rax,%r12,8), %rcx
movl %r15d, %ebx
cmpl $0x1, %ecx
je 0xb0559
movl %r15d, %ebx
cmpl $-0x1, %ebx
movq 0x10(%rsp), %rdx
je 0xb0658
movl 0x18(%rsp), %eax
movl $0x1, %ecx
movl %ecx, (%r8,%rax,4)
movl %ebx, %eax
movl %ecx, (%r8,%rax,4)
movq (%rdx), %r12
movq 0x8(%rdx), %rax
cmpq %rax, %r12
jne 0xb0692
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rdx)
movq 0x10(%rdx), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
movq 0x10(%rsp), %rdx
movq %rax, 0x10(%rdx)
xorl %eax, %eax
cmpl $-0x1, %ebx
sete %al
movq 0x10(%rdx), %r15
leaq 0x1(%r12), %rcx
movq %rcx, (%rdx)
leaq (%r12,%r12,4), %r12
movq $0x0, 0x20(%r15,%r12,8)
movl %ebx, 0x4(%r15,%r12,8)
movl 0x18(%rsp), %ecx
movl %ecx, 0x8(%r15,%r12,8)
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl (%r15,%r12,8), %ecx
movl $0x80000000, %edx       # imm = 0x80000000
andl %edx, %ecx
orl %r14d, %ecx
movl %ecx, (%r15,%r12,8)
movq 0x8(%rbp), %rbx
movl %ebx, 0x10(%r15,%r12,8)
shll $0x1f, %eax
orl %r14d, %eax
movl %eax, (%r15,%r12,8)
leal -0x1(%rbx), %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)
testq %rbx, %rbx
movq 0x20(%rsp), %r9
movq 0x40(%rsp), %r14
je 0xb0782
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, %rbx
ja 0xb0755
jmp 0xb0782
movl %r13d, %ecx
xorq $0x1, %rcx
cmpl $0x0, (%rax,%rcx,8)
je 0xb0377
incq %r13
cmpq %r14, %r13
jne 0xb0345
movq 0x18(%rbp), %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
cmpl $0x0, 0x38(%rsp)
je 0xb07bc
shlq $0x2, %r14
movq 0x8(%rsp), %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x20e0
movq 0x10(%rsp), %rax
movq (%rax), %r10
testq %r10, %r10
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r9
je 0xb0810
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
addq $0x8, %rax
movl $0x1, %ecx
movl $0x1, %edx
cmpl $0x0, -0x8(%rax)
js 0xb0800
leal -0x1(%rcx), %esi
movl -0x4(%rax), %edi
movl %esi, (%r8,%rdi,4)
movl (%rax), %esi
movl %ecx, (%r8,%rsi,4)
movl %edx, %esi
addq $0x28, %rax
incl %edx
addl $0x2, %ecx
cmpq %rsi, %r10
ja 0xb07ea
movq %r10, 0x18(%rsp)
testl $0x7fffffff, 0x4(%r9)   # imm = 0x7FFFFFFF
movq 0x10(%rsp), %rdx
je 0xb0959
movl $0x12, %r14d
xorl %r13d, %r13d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x30(%rsp), %r12
movq 0x8(%r9), %rbp
movl -0x6(%rbp,%r14), %eax
testl %eax, %eax
js 0xb0940
movl -0xe(%rbp,%r14), %ecx
shlq $0x2, %rcx
xorq $0x4, %rcx
movl (%r8,%rcx), %r15d
testl %r15d, %r15d
js 0xb0940
movl -0xa(%rbp,%r14), %ecx
cmpl $0x0, (%r8,%rcx,4)
js 0xb0940
movq 0x10(%rdx), %rcx
movl %r15d, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %ecx
andl %edi, %ecx
subl %eax, %ecx
movl $0x1, %ebx
js 0xb0893
movl %ecx, %ebx
movl 0x4(%r12), %ecx
andl %edi, %ecx
movl (%r12), %eax
cmpl %eax, %ecx
jne 0xb08d9
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
callq 0x2370
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %r9
movq 0x8(%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(%rbp,%r14), %edx
andl %edi, %edx
movl %edx, 0xc(%rax,%rcx,8)
shlq $0x20, %r15
movslq %ebx, %rdx
orq %r15, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0xa(%rbp,%r14), %edx
movl (%r8,%rdx,4), %edx
movl %edx, 0x8(%rax,%rcx,8)
andw $0xc000, 0x10(%rax,%rcx,8) # imm = 0xC000
movb (%rbp,%r14), %dl
movb %dl, 0x12(%rax,%rcx,8)
movq 0x10(%rsp), %rdx
incq %r13
movl 0x4(%r9), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq $0x18, %r14
cmpq %rax, %r13
jb 0xb083b
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
movq 0x30(%rsp), %r15
je 0xb0998
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r14
xorl %r12d, %r12d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%r12,%r12,4), %rax
movl (%r14,%rax,8), %edx
andl %ebp, %edx
movq %r15, %rdi
movl %r12d, %esi
xorl %ecx, %ecx
callq 0xa0115
incl %r12d
cmpq %r12, %rbx
ja 0xb0979
movq %r15, %rdi
callq 0xa02fa
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq