// File: /workspace/github_c_makefile_star2_O2/pwelke[P]GraphMiningTools/wilsonsAlgorithm.c
// Function: loopErasedRandomWalk
char loopErasedRandomWalk(struct Graph* g, struct IntegerArrayStack* remaining, struct VertexList** previous, char* used, struct ShallowGraph* tree, struct ListPool* lp) {

	int index0 = -1;
	int index1 = -1;

	// Pick a location that’s not yet in the maze (if any).
	for (index0 = ias_pop(remaining); (index0 != -1) && (used[index0]); index0 = ias_pop(remaining));

	if (index0 == -1) {
		return 1;
	}

	// Perform a random walk starting at this location,
	previous[index0] = ROOT; // must be different from NULL, however, must not be a valid pointer.
	while (1) {
		struct VertexList* e;
		int neighborIndex = rand() % degree(g->vertices[index0]);
		int i = 0;

		for (e=g->vertices[index0]->neighborhood; e!=NULL; e=e->next) {
			if (i == neighborIndex) {
				index1 = e->endPoint->number;
				break;
			} else {
				++i;
			}
		}
		// e is set to the edge selected

		// If this new cell was visited previously during this walk,
		// erase the loop, rewinding the path to its earlier state.
		// Otherwise, just add it to the walk.
		if ((previous[index1] != NULL)) {
			eraseWalk(index0, index1, previous);
		} else {
			previous[index1] = e;
		}
		index0 = index1;

		// If this cell is part of the maze, we’re done walking.
		if (used[index1]) {
			// Add the random walk to the maze by backtracking to the starting cell.
			// Also erase this walk’s history to not interfere with subsequent walks.
			for (index0=previous[index1]->startPoint->number; index0!=index1; index0=previous[index1]->startPoint->number) {
				used[index0] = 1;
				appendEdge(tree, shallowCopyEdge(previous[index1], lp));
				previous[index1] = NULL;
				index1 = index0;
				if (previous[index1] == ROOT) {
					break;
				}
			}
			previous[index1] = NULL;
			return 0;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, 0x10(%rsp)
movl 0x8(%rsi), %edx
pushq $-0x1
popq %rax
movl %eax, %ecx
movl %eax, %r13d
cmpl $-0x1, %edx
je 0x1bf33
movq (%rsi), %rdi
leal -0x1(%rdx), %ecx
movl %ecx, 0x8(%rsi)
movslq %edx, %rdx
movl (%rdi,%rdx,4), %r13d
cmpl $-0x1, %r13d
je 0x1bf51
movslq %r13d, %rdx
cmpb $0x0, (%r15,%rdx)
je 0x1bf58
movl %ecx, %edx
movl %eax, %r13d
cmpl $-0x1, %ecx
movl %eax, %ecx
je 0x1bf33
jmp 0x1bf23
movb $0x1, %al
jmp 0x1c047
movq 0xe1f9(%rip), %rax      # 0x2a158
movq %rax, (%r12,%rdx,8)
pushq $-0x1
popq %rbp
movq %r15, 0x8(%rsp)
callq 0x1240
movl %eax, %ebx
movq 0x10(%rsp), %r15
movq 0x10(%r15), %rax
movslq %r13d, %r14
movq (%rax,%r14,8), %rdi
callq 0x5a8a
movl %eax, %ecx
movl %ebx, %eax
cltd
idivl %ecx
movq 0x10(%r15), %rax
movq (%rax,%r14,8), %rax
incl %edx
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x1bfac
decl %edx
jne 0x1bf98
movq 0x18(%rax), %rcx
movl 0x18(%rcx), %ebp
movslq %ebp, %rbx
cmpq $0x0, (%r12,%rbx,8)
movq 0x8(%rsp), %r15
je 0x1bfca
movl %r13d, %edi
movl %ebp, %esi
movq %r12, %rdx
callq 0x1bed8
jmp 0x1bfce
movq %rax, (%r12,%rbx,8)
movb (%r15,%rbx), %al
testb %al, %al
je 0x1c03a
movb %al, 0x7(%rsp)
movq (%r12,%rbx,8), %rax
movq 0x10(%rax), %rax
movl 0x18(%rax), %r13d
movslq %ebp, %rbx
cmpl %ebp, %r13d
je 0x1c028
movslq %r13d, %r14
movb $0x1, (%r15,%r14)
movq (%r12,%rbx,8), %rdi
movq 0x20(%rsp), %rsi
callq 0x5a28
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x6620
andq $0x0, (%r12,%rbx,8)
movq (%r12,%r14,8), %rax
movl %r13d, %ebp
cmpq 0xe134(%rip), %rax      # 0x2a158
jne 0x1bfde
jmp 0x1c02e
movq %rbx, %r14
movl %ebp, %r13d
andq $0x0, (%r12,%r14,8)
movl %r13d, %ebp
movb 0x7(%rsp), %al
movl %ebp, %r13d
testb %al, %al
je 0x1bf6b
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop