// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %rbx
movabsq $0x800000000000000, %r14 # imm = 0x800000000000000
callq 0xa0a7d
movq %rbx, 0x20(%rsp)
movl 0x14(%rbx), %ebx
addl %ebx, %ebx
movl $0x1, %edi
movl $0x38, %esi
callq 0x2120
movq %rax, %r15
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, 0x60(%rsp)
movq %rax, 0x18(%r15)
movl $0x4, %esi
movq %rbx, %rdi
callq 0x2120
movq %rax, 0x10(%rsp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2120
movq %rax, %r12
movq %r14, (%rax)
movq $0x3, 0x10(%rax)
movl $0x20, %edi
callq 0x2280
movq %r12, %r11
movq %rax, %r8
movq %rax, 0x18(%r12)
testl %ebx, %ebx
movq %r15, 0x30(%rsp)
je 0xb16d1
movq $0x0, 0x8(%rsp)
movq $0x0, 0x58(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %r10
movq %rbx, 0x48(%rsp)
movq %r11, 0x40(%rsp)
movq 0x20(%r10), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xb16b7
cmpl $0x0, (%rdi,%r13,4)
jne 0xb16b7
movq 0x28(%r10), %rax
movq (%rax,%r13,8), %rcx
movl %r13d, %r9d
xorl $0x1, %r9d
movq %rcx, %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %rdx
je 0xb1311
movq %rbp, 0x50(%rsp)
movl $0x1, (%rdi,%r13,4)
movq $0x0, 0x8(%r11)
cmpl $0x1, %edx
movq %r8, 0x18(%rsp)
movl %r9d, 0x3c(%rsp)
jne 0xb130d
movq 0x8(%r10), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
movl 0x8(%rdx,%rsi,8), %esi
movq %rsi, %rdi
xorq $0x1, %rdi
cmpl $0x1, (%rax,%rdi,8)
jne 0xb130d
xorl %r12d, %r12d
movl %r13d, %r8d
movq %r13, %r15
xorl %eax, %eax
movl $0x1, %ebp
xorl %ebp, %r8d
movq %r11, %r9
movq %r10, %r11
movq 0x10(%rsp), %r10
movl %ebp, (%r10,%r8,4)
movl %edi, 0x8(%rsp)
movl %esi, 0x2c(%rsp)
movl %esi, %ebx
movl %ebp, (%r10,%rbx,4)
movq %r11, %r10
movq %r9, %r11
leaq (%rcx,%rcx,2), %rcx
movq (%rdx,%rcx,8), %rbp
movq %r14, %rcx
shrq $0x3a, %rcx
movl $0x1, %edx
shlq %cl, %rdx
cmpq %rdx, %rax
movq 0x18(%rsp), %rdi
jne 0xb129c
incl %ecx
movq %r11, %rdi
movl %ecx, %esi
callq 0x105211
movq 0x40(%rsp), %r11
movq 0x20(%rsp), %r10
movq (%r11), %r14
movq 0x8(%r11), %rax
movq 0x18(%r11), %rdi
movl %ebp, %ecx
shlq $0x20, %r15
movq %r14, %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
addq %rax, %rdx
incq %rax
movq %rax, 0x8(%r11)
andq 0x10(%r11), %rdx
orq %rcx, %r15
movq %rdi, 0x18(%rsp)
movq %r15, (%rdi,%rdx,8)
addl %ebp, %r12d
cmpq %rbx, %r13
je 0xb1347
movq 0x28(%r10), %r8
movq (%r8,%rbx,8), %rcx
cmpl $0x1, %ecx
jne 0xb1347
movq 0x8(%r10), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
movl 0x8(%rdx,%rsi,8), %esi
movq %rsi, %rdi
xorq $0x1, %rdi
cmpl $0x1, (%r8,%rdi,8)
movq %rbx, %r15
movl 0x2c(%rsp), %r8d
je 0xb122f
jmp 0xb1347
xorl %eax, %eax
jmp 0xb133f
movl %r9d, %ecx
cmpl $0x0, (%rax,%rcx,8)
jne 0xb16b7
movq %rbp, 0x50(%rsp)
movq %r8, 0x18(%rsp)
movl $0x1, (%rdi,%r13,4)
movq $0x0, 0x8(%r11)
xorl %eax, %eax
movl %r9d, 0x3c(%rsp)
movl %r9d, 0x8(%rsp)
xorl %r12d, %r12d
movl 0x8(%rsp), %edx
movl %edx, %ebx
xorl $0x1, %ebx
movl $0x80000000, %edi       # imm = 0x80000000
cmpq %rbx, %r13
jne 0xb1379
testq %rax, %rax
je 0xb1379
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r15
movq 0x18(%rsp), %r9
jmp 0xb155b
movq 0x20(%r10), %rcx
shrl %edx
movl (%rcx,%rdx,8), %r15d
movq %r14, %rcx
shrq $0x3a, %rcx
movl $0x1, %edx
shlq %cl, %rdx
cmpq %rdx, %rax
movq 0x18(%rsp), %r9
jne 0xb13bd
incl %ecx
movq %r11, %rdi
movl %ecx, %esi
callq 0x105211
movq 0x40(%rsp), %r11
movq 0x20(%rsp), %r10
movq (%r11), %r14
movq 0x8(%r11), %rax
movq 0x18(%r11), %r9
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
shlq $0x20, %rbx
orq %r15, %rbx
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r11)
movq %r14, %rcx
movabsq $0x3ffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFF
andq %rdx, %rcx
addq %rax, %rcx
andq 0x10(%r11), %rcx
movq %rbx, (%r9,%rcx,8)
addl %r15d, %r12d
movq 0x28(%r10), %rax
movl 0x3c(%rsp), %ecx
movq (%rax,%rcx,8), %rdx
cmpl $0x1, %edx
jne 0xb151c
movq 0x8(%r10), %rcx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%rcx,%rdx,8), %edx
movq %rdx, %rbp
xorq $0x1, %rbp
movq (%rax,%rbp,8), %rsi
cmpl $0x1, %esi
jne 0xb151c
movl %r13d, %r8d
movl %r8d, %edi
movl %ebp, 0x18(%rsp)
movl %edx, %ebx
movl $0x1, %edx
movq 0x10(%rsp), %r8
movl %edx, (%r8,%rbx,4)
movl %edx, (%r8,%rdi,4)
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rdx
movq (%rcx,%rdx,8), %r15
movq 0x8(%r11), %rdx
movq %r14, %rcx
shrq $0x3a, %rcx
movl $0x1, %esi
shlq %cl, %rsi
cmpq %rsi, %rdx
jne 0xb1498
incl %ecx
movq %r11, %rdi
movl %ecx, %esi
callq 0x105211
movq 0x40(%rsp), %r11
movq 0x20(%rsp), %r10
movq (%r11), %r14
movq 0x8(%r11), %rdx
movq %r14, %rcx
shrq $0x3a, %rcx
movq 0x18(%r11), %r9
movq 0x28(%r10), %rax
movl %r15d, %esi
shlq $0x20, %rbp
orq %rsi, %rbp
incq %rdx
movq %rdx, 0x8(%r11)
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
leaq (%r14,%rdi), %rdx
movq $-0x1, %rsi
shlq %cl, %rsi
testq %rdi, %r14
notq %rsi
cmovneq %rdx, %rsi
andq %rdi, %rsi
movabsq $-0x400000000000000, %rcx # imm = 0xFC00000000000000
andq %rcx, %r14
orq %rsi, %r14
movq %r14, (%r11)
movq %rbp, (%r9,%rsi,8)
addl %r15d, %r12d
movq (%rax,%rbx,8), %rdx
cmpl $0x1, %edx
movl 0x18(%rsp), %r8d
jne 0xb151f
movq 0x8(%r10), %rcx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%rcx,%rdx,8), %edx
movq %rdx, %rbp
xorq $0x1, %rbp
movq (%rax,%rbp,8), %rsi
cmpl $0x1, %esi
je 0xb142d
jmp 0xb151f
movl %r13d, %r8d
cmpl $-0x1, %r8d
je 0xb1547
movl 0x8(%rsp), %esi
movl %esi, %eax
movl $0x1, %ecx
movq 0x10(%rsp), %rdx
movl %ecx, (%rdx,%rax,4)
movl %r8d, %eax
movl %ecx, (%rdx,%rax,4)
xorl %edi, %edi
movq 0x30(%rsp), %r15
jmp 0xb155b
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r15
movl 0x8(%rsp), %esi
movl $0x80000000, %edi       # imm = 0x80000000
movq %r9, 0x18(%rsp)
movq (%r15), %rbx
cmpq 0x8(%r15), %rbx
jne 0xb15b3
leaq (%rbx,%rbx), %rax
testq %rbx, %rbx
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x8(%r15)
shlq $0x3, %rax
movl %esi, 0x8(%rsp)
leaq (%rax,%rax,4), %rsi
movl %edi, 0x2c(%rsp)
movq 0x50(%rsp), %rdi
movl %r8d, %ebp
callq 0x2370
movl %ebp, %r8d
movl 0x2c(%rsp), %edi
movl 0x8(%rsp), %esi
movq 0x40(%rsp), %r11
movq %rax, 0x58(%rsp)
movq %rax, 0x10(%r15)
leaq 0x1(%rbx), %rcx
movq %rcx, 0x8(%rsp)
movq %rcx, (%r15)
leaq (%rbx,%rbx,4), %r15
movq 0x58(%rsp), %rbp
movq $0x0, 0x20(%rbp,%r15,8)
movl %r8d, 0x4(%rbp,%r15,8)
movl %esi, 0x8(%rbp,%r15,8)
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl (%rbp,%r15,8), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %eax
orl %r12d, %eax
movl %eax, (%rbp,%r15,8)
movq 0x8(%r11), %rbx
movl %ebx, 0x10(%rbp,%r15,8)
orl %edi, %r12d
movl %r12d, (%rbp,%r15,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(%rbp,%r15,8)
leal 0x8(,%rcx,8), %edi
movq %r11, %r12
callq 0x2280
movq %rax, 0x18(%rbp,%r15,8)
testq %rbx, %rbx
je 0xb169b
movq 0x10(%r12), %rcx
movq 0x18(%r12), %r8
movq %r14, %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
xorl %esi, %esi
movq 0x20(%rsp), %r10
movq %r12, %r11
leaq (%rdx,%rsi), %rdi
andq %rcx, %rdi
movq (%r8,%rdi,8), %rdi
movq %rdi, (%rax,%rsi,8)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rbx
ja 0xb1671
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rbx
jmp 0xb16b7
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %r10
movq 0x48(%rsp), %rbx
movq %r12, %r11
movq 0x18(%rsp), %r8
incq %r13
cmpq %rbx, %r13
jne 0xb119d
movq 0x18(%r11), %r8
movq %r11, %r14
movq 0x8(%rsp), %r12
jmp 0xb16d7
xorl %r12d, %r12d
movq %r11, %r14
movq %r8, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
testq %rbx, %rbx
je 0xb1703
leaq (,%rbx,4), %rdx
movq 0x10(%rsp), %rdi
movl $0xff, %esi
callq 0x20e0
testq %r12, %r12
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %r9
je 0xb174a
movq 0x10(%r15), %rax
addq $0x8, %rax
movl $0x1, %ecx
movl $0x1, %edx
cmpl $0x0, -0x8(%rax)
js 0xb173a
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, %r12
ja 0xb1724
movq %r12, 0x8(%rsp)
movl 0x4(%r9), %eax
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xb188b
movl $0x12, %ebx
xorl %r14d, %r14d
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x60(%rsp), %rbp
movq 0x8(%r9), %r12
movl -0x6(%r12,%rbx), %ecx
testl %ecx, %ecx
js 0xb1873
movl -0xe(%r12,%rbx), %edx
shlq $0x2, %rdx
xorq $0x4, %rdx
movl (%r8,%rdx), %r13d
testl %r13d, %r13d
js 0xb1873
movl -0xa(%r12,%rbx), %edx
cmpl $0x0, (%r8,%rdx,4)
js 0xb1873
movq 0x10(%r15), %rax
movl %r13d, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rax,%rdx,8), %eax
andl %edi, %eax
subl %ecx, %eax
movl $0x1, %r15d
js 0xb17ca
movl %eax, %r15d
movl 0x4(%rbp), %edx
movl %edx, %ecx
andl %edi, %ecx
cmpl (%rbp), %ecx
jne 0xb1817
leal (%rcx,%rcx), %eax
testl %ecx, %ecx
movl $0x10, %ecx
cmovel %ecx, %eax
movl %eax, (%rbp)
movq 0x8(%rbp), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %r9
movq 0x10(%rsp), %r8
movq %rax, 0x8(%rbp)
movl 0x4(%rbp), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0xb181b
movq 0x8(%rbp), %rax
xorl $0x1, %r13d
leal 0x1(%rdx), %esi
andl %edi, %esi
andl $0x80000000, %edx       # imm = 0x80000000
orl %esi, %edx
movl %edx, 0x4(%rbp)
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
movl -0x6(%r12,%rbx), %edx
andl %edi, %edx
movl %edx, 0xc(%rax,%rcx,8)
shlq $0x20, %r13
movslq %r15d, %rdx
orq %r13, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0xa(%r12,%rbx), %edx
movl (%r8,%rdx,4), %edx
movl %edx, 0x8(%rax,%rcx,8)
andw $0xc000, 0x10(%rax,%rcx,8) # imm = 0xC000
movb (%r12,%rbx), %dl
movb %dl, 0x12(%rax,%rcx,8)
movl 0x4(%r9), %eax
movq 0x30(%rsp), %r15
incq %r14
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq $0x18, %rbx
cmpq %rcx, %r14
jb 0xb1770
movq 0x8(%rsp), %r13
testq %r13, %r13
movq 0x60(%rsp), %r12
je 0xb18c2
movq 0x10(%r15), %r14
xorl %ebx, %ebx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%rbx,%rbx,4), %rax
movl (%r14,%rax,8), %edx
andl %ebp, %edx
movq %r12, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0xa0887
incl %ebx
cmpq %rbx, %r13
ja 0xb18a5
movq %r12, %rdi
callq 0xa0a7d
movq 0x10(%rsp), %rdi
callq 0x2220
movq %r15, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq