// File: /workspace/github_c_makefile_star2_O2/rikuu[P]kermit/miniasm/asm.c
// Function: ma_ug_gen
ma_ug_t *ma_ug_gen(asg_t *g)
{
	int32_t *mark;
	uint32_t i, v, n_vtx = g->n_seq * 2;
	kdq_t(uint64_t) *q;
	ma_ug_t *ug;

	ug = (ma_ug_t*)calloc(1, sizeof(ma_ug_t));
	ug->g = asg_init();
	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;
		if (g->seq[v>>1].del || arc_cnt(g, v) == 0 || mark[v]) continue;
		mark[v] = 1;
		q->count = 0, start = v, end = v^1, len = 0;
		// forward
		w = v;
		while (1) {
			if (arc_cnt(g, w) != 1) break;
			x = arc_first(g, w).v; // w->x
			if (arc_cnt(g, x^1) != 1) break;
			mark[x] = mark[w^1] = 1;
			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
			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));
			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);
		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
	for (v = 0; v < n_vtx; ++v) mark[v] = -1;
	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;
	}
	for (i = 0; i < g->n_arc; ++i) {
		asg_arc_t *p = &g->arc[i];
		if (p->del) continue;
		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];
		}
	}
	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, 0x8(%rsp)
movl 0x14(%rdi), %r12d
addl %r12d, %r12d
pushq $0x1
popq %r15
pushq $0x20
popq %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x10f0
movq %rax, %rbx
callq 0x2217
movq %rax, 0x30(%rsp)
movq %rbx, 0x28(%rsp)
movq %rax, 0x18(%rbx)
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0x10f0
movq %rax, %r14
movq %r15, %rdi
movq %r13, %rsi
callq 0x10f0
movq %rax, %rbx
movabsq $0x800000000000000, %rax # imm = 0x800000000000000
movq %rax, (%rbx)
movq $0x3, 0x10(%rbx)
movq %r13, %rdi
callq 0x1160
movq %rax, 0x18(%rbx)
xorl %r15d, %r15d
pushq $0x1
popq %rbp
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
movq %r14, (%rsp)
movq %r12, 0x40(%rsp)
cmpq %r12, %r13
je 0x5a24
movq 0x8(%rsp), %r8
movq 0x18(%r8), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,4)
js 0x5a1c
movq 0x20(%r8), %rax
cmpl $0x0, (%rax,%r13,8)
je 0x5a1c
cmpl $0x0, (%r14,%r13,4)
jne 0x5a1c
movq %r15, 0x20(%rsp)
movl $0x1, (%r14,%r13,4)
andq $0x0, 0x8(%rbx)
movl %r13d, %eax
xorl $0x1, %eax
xorl %r14d, %r14d
movl %r13d, %r12d
movq %r13, 0x38(%rsp)
movq 0x20(%r8), %rdi
movl %r12d, %esi
movq (%rdi,%rsi,8), %rcx
cmpl $0x1, %ecx
jne 0x5789
movq 0x8(%r8), %rdx
shrq $0x20, %rcx
shlq $0x4, %rcx
movl 0x8(%rdx,%rcx), %r12d
movq %r12, %r15
xorq $0x1, %r15
cmpl $0x1, (%rdi,%r15,8)
jne 0x5789
movl %esi, %eax
xorl %ebp, %eax
movq (%rsp), %rdi
movl %ebp, (%rdi,%rax,4)
movl %ebp, (%rdi,%r12,4)
movq (%rdx,%rcx), %r13
movl %r13d, %eax
shlq $0x20, %rsi
orq %rax, %rsi
movq %rbx, %rdi
callq 0x5b88
movq 0x8(%rsp), %r8
addl %r13d, %r14d
movq 0x38(%rsp), %r13
movl %r15d, %eax
cmpq %r12, %r13
jne 0x571e
jmp 0x578c
movl %eax, %r15d
movl %r15d, %esi
xorl $0x1, %esi
cmpq %rsi, %r13
jne 0x57af
movl $0x80000000, %edi       # imm = 0x80000000
cmpq $0x0, 0x8(%rbx)
je 0x57af
pushq $-0x1
popq %r13
movl %r13d, %r15d
jmp 0x58ef
movq 0x18(%r8), %rax
movl %r15d, %ecx
shrl %ecx
movl (%rax,%rcx,4), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
shlq $0x20, %rsi
orq %r12, %rsi
movq %rbx, %rdi
callq 0x5b88
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movq 0x8(%rsp), %r9
addl %r12d, %r14d
movq (%rsp), %r8
movq 0x20(%r9), %rdx
movl %r13d, %eax
xorl $0x1, %eax
movq (%rdx,%rax,8), %rsi
movl %r13d, %eax
cmpl $0x1, %esi
jne 0x58d1
movq 0x8(%r9), %rcx
shrq $0x1c, %rsi
andq $-0x10, %rsi
movl 0x8(%rcx,%rsi), %esi
movq %rsi, %r12
xorq $0x1, %r12
movq (%rdx,%r12,8), %rdx
cmpl $0x1, %edx
jne 0x58d1
movl %ebp, (%r8,%rsi,4)
movl %ebp, (%r8,%rax,4)
shrq $0x1c, %rdx
andq $-0x10, %rdx
movq (%rcx,%rdx), %r13
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rax, %rcx
shrq $0x3a, %rcx
pushq $0x1
popq %rsi
shlq %cl, %rsi
cmpq %rsi, %rdx
jne 0x5880
incl %ecx
movq %rbx, %rdi
movl %ecx, %esi
callq 0x6150
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movq 0x8(%rsp), %r9
movq (%rsp), %r8
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movq %rax, %rcx
shrq $0x3a, %rcx
movl %r13d, %esi
movq %r12, %rdi
shlq $0x20, %rdi
orq %rsi, %rdi
incq %rdx
movq %rdx, 0x8(%rbx)
leaq (%rax,%r10), %rdx
pushq $-0x1
popq %rsi
shlq %cl, %rsi
testq %r10, %rax
notq %rsi
cmovneq %rdx, %rsi
andq %r10, %rsi
movabsq $-0x400000000000000, %rcx # imm = 0xFC00000000000000
andq %rcx, %rax
orq %rsi, %rax
movq %rax, (%rbx)
movq 0x18(%rbx), %rax
movq %rdi, (%rax,%rsi,8)
addl %r13d, %r14d
movl %r12d, %r13d
jmp 0x57e8
cmpl $-0x1, %r13d
je 0x58e6
movl %r15d, %ecx
movl %ebp, (%r8,%rcx,4)
movl %ebp, (%r8,%rax,4)
xorl %edi, %edi
jmp 0x58ef
movl $0x80000000, %edi       # imm = 0x80000000
pushq $-0x1
popq %r13
movq 0x18(%rsp), %rcx
cmpq %rcx, 0x10(%rsp)
movq 0x28(%rsp), %rax
jne 0x5939
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x18(%rsp)
movq %rdx, 0x8(%rax)
imulq $0x28, %rdx, %rsi
movl %edi, %r12d
movq 0x20(%rsp), %rdi
callq 0x1180
movl %r12d, %edi
movq %rax, %r12
movq 0x28(%rsp), %rax
movq %r12, 0x10(%rax)
jmp 0x593e
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq %rdx, (%rax)
imulq $0x28, %rcx, %rdx
andq $0x0, 0x20(%r12,%rdx)
movl %r13d, 0x4(%r12,%rdx)
movl %r15d, 0x8(%r12,%rdx)
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl (%r12,%rdx), %eax
movl $0x80000000, %ecx       # imm = 0x80000000
andl %ecx, %eax
orl %r14d, %eax
movl %eax, (%r12,%rdx)
movq 0x8(%rbx), %r15
movl %r15d, 0x10(%r12,%rdx)
orl %edi, %r14d
movl %r14d, (%r12,%rdx)
movq %rdx, %r14
leal -0x1(%r15), %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(%r12,%rdx)
leal 0x8(,%rcx,8), %edi
callq 0x1160
movq %r12, %rdi
movq %rax, 0x18(%r12,%r14)
xorl %ecx, %ecx
movq (%rsp), %r14
movq 0x40(%rsp), %r12
movabsq $0x3ffffffffffffff, %r8 # imm = 0x3FFFFFFFFFFFFFF
movq 0x38(%rsp), %r13
movl %ecx, %ecx
cmpq %rcx, %r15
jbe 0x5a0f
movq (%rbx), %rdx
andq %r8, %rdx
addq %rcx, %rdx
andq 0x10(%rbx), %rdx
movq 0x18(%rbx), %rsi
movq (%rsi,%rdx,8), %rdx
movq %rdx, (%rax,%rcx,8)
incl %ecx
jmp 0x59eb
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdi, %r15
incq %r13
jmp 0x56c0
movq 0x18(%rbx), %rdi
callq 0x1040
movq %rbx, %rdi
callq 0x1040
movq (%rsp), %rdi
xorl %eax, %eax
cmpq %rax, %r12
je 0x5a49
orl $-0x1, (%rdi,%rax,4)
incq %rax
jmp 0x5a3b
xorl %eax, %eax
movq 0x8(%rsp), %r8
movl %eax, %eax
cmpq %rax, 0x10(%rsp)
jbe 0x5a80
imulq $0x28, %rax, %rcx
cmpl $0x0, (%r15,%rcx)
js 0x5a79
leaq (%r15,%rcx), %rdx
leal -0x1(%rbp), %esi
movl 0x4(%rdx), %edx
movl %esi, (%rdi,%rdx,4)
movl 0x8(%r15,%rcx), %ecx
movl %ebp, (%rdi,%rcx,4)
incl %eax
addl $0x2, %ebp
jmp 0x5a50
pushq $0xc
popq %rbx
xorl %r14d, %r14d
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl 0x4(%r8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
jae 0x5b33
movq 0x8(%r8), %r12
movl (%r12,%rbx), %eax
testl %eax, %eax
js 0x5b27
movl -0x8(%r12,%rbx), %ecx
shlq $0x2, %rcx
xorq $0x4, %rcx
movl (%rdi,%rcx), %r13d
testl %r13d, %r13d
js 0x5b27
movl -0x4(%r12,%rbx), %ecx
cmpl $0x0, (%rdi,%rcx,4)
js 0x5b27
movl %r13d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
movq %r15, %rbp
movl (%r15,%rcx), %ecx
andl %edx, %ecx
subl %eax, %ecx
pushq $0x1
popq %r15
js 0x5ae7
movl %ecx, %r15d
xorl $0x1, %r13d
movq 0x30(%rsp), %rdi
callq 0x4fc9
movl (%r12,%rbx), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
movl %ecx, 0xc(%rax)
shlq $0x20, %r13
movslq %r15d, %rcx
orq %r13, %rcx
movq %rcx, (%rax)
movl -0x4(%r12,%rbx), %ecx
movq (%rsp), %rdi
movl (%rdi,%rcx,4), %ecx
movl %ecx, 0x8(%rax)
movq 0x8(%rsp), %r8
movq %rbp, %r15
incq %r14
addq $0x10, %rbx
jmp 0x5a8b
xorl %eax, %eax
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x30(%rsp), %r14
movl %eax, %ebx
cmpq %rbx, 0x10(%rsp)
jbe 0x5b63
imulq $0x28, %rbx, %rax
movl (%r15,%rax), %edx
andl %ebp, %edx
movq %r14, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x230a
leal 0x1(%rbx), %eax
jmp 0x5b3f
movq %r14, %rdi
callq 0x2447
movq (%rsp), %rdi
callq 0x1040
movq 0x28(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq