// File: /workspace/github_c_makefile_star2_O3/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 %rcx, %r15
movq %rdi, 0x18(%rsp)
movslq 0x8(%rsi), %rcx
movb $0x1, %al
cmpq $-0x1, %rcx
je 0x1edc7
movq %rdx, %r12
movq (%rsi), %rdi
leal -0x1(%rcx), %edx
movl %edx, 0x8(%rsi)
movl (%rdi,%rcx,4), %ebx
cmpl $-0x1, %ebx
je 0x1edc7
movq %r8, %r14
addl $-0x2, %ecx
movslq %edx, %rdx
movslq %ebx, %rdi
cmpb $0x0, (%r15,%rdi)
je 0x1ec9e
cmpl $-0x2, %ecx
je 0x1edc7
movq (%rsi), %rdi
movl %ecx, 0x8(%rsi)
movl (%rdi,%rdx,4), %ebx
decl %ecx
decq %rdx
cmpl $-0x1, %ebx
jne 0x1ec73
jmp 0x1edc7
movq 0xe4bb(%rip), %rax      # 0x2d160
movq %rax, (%r12,%rdi,8)
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r14, 0x10(%rsp)
callq 0x1250
movl %eax, %ebp
movq 0x18(%rsp), %r14
movq 0x10(%r14), %rax
movslq %ebx, %rbx
movq (%rax,%rbx,8), %rdi
callq 0x7c4e
movl %eax, %ecx
movl %ebp, %eax
cltd
idivl %ecx
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rax
movq 0x8(%rax), %rcx
testq %rcx, %rcx
je 0x1ed00
incl %edx
movq 0x10(%rsp), %r14
decl %edx
je 0x1ed09
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
jne 0x1ecef
xorl %ecx, %ecx
jmp 0x1ed11
xorl %ecx, %ecx
movq 0x10(%rsp), %r14
jmp 0x1ed11
movq 0x18(%rcx), %rax
movl 0x18(%rax), %r13d
movslq %r13d, %rax
cmpq $0x0, (%r12,%rax,8)
je 0x1ed47
leaq (%r12,%rbx,8), %rcx
movq (%rcx), %rdx
movq 0x10(%rdx), %rdx
movl 0x18(%rdx), %edx
cmpl %r13d, %edx
je 0x1ed3e
movq $0x0, (%rcx)
movslq %edx, %rcx
leaq (%r12,%rcx,8), %rcx
jmp 0x1ed1f
movq $0x0, (%rcx)
jmp 0x1ed4b
movq %rcx, (%r12,%rax,8)
movb (%r15,%rax), %cl
testb %cl, %cl
je 0x1edba
movb %cl, 0xf(%rsp)
movq (%r12,%rax,8), %rax
movq 0x10(%rax), %rax
movl 0x18(%rax), %ebx
cmpl %r13d, %ebx
je 0x1eda5
movslq %ebx, %rbp
movb $0x1, (%r15,%rbp)
movslq %r13d, %r13
movq (%r12,%r13,8), %rdi
movq 0x20(%rsp), %rsi
callq 0x7bec
movq %r14, %rdi
movq %rax, %rsi
callq 0x8916
movq $0x0, (%r12,%r13,8)
movq (%r12,%rbp,8), %rax
movl %ebx, %r13d
cmpq 0xe3bf(%rip), %rax      # 0x2d160
jne 0x1ed5b
jmp 0x1edab
movslq %r13d, %rbp
movl %r13d, %ebx
movq $0x0, (%r12,%rbp,8)
movl %ebx, %r13d
movb 0xf(%rsp), %cl
movl %r13d, %ebx
testb %cl, %cl
je 0x1ecb4
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop