// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rcx, %r15
movq %rdx, %r12
movq %rdi, 0x18(%rsp)
movslq 0x8(%rsi), %rax
cmpq $-0x1, %rax
je 0x1ede9
movq (%rsi), %rcx
leal -0x1(%rax), %edx
movl %edx, 0x8(%rsi)
movl (%rcx,%rax,4), %r13d
jmp 0x1edef
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movb $0x1, %al
cmpl $-0x1, %r13d
je 0x1ef46
movslq %r13d, %rcx
cmpb $0x0, (%r15,%rcx)
je 0x1ee26
movslq 0x8(%rsi), %rcx
cmpq $-0x1, %rcx
je 0x1ee1e
movq (%rsi), %rdx
leal -0x1(%rcx), %edi
movl %edi, 0x8(%rsi)
movl (%rdx,%rcx,4), %r13d
jmp 0x1edf1
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x1edf1
movq 0xe333(%rip), %rax      # 0x2d160
movq %rax, (%r12,%rcx,8)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %r14, 0x10(%rsp)
callq 0x1250
movl %eax, %ebp
movq 0x18(%rsp), %r14
movq 0x10(%r14), %rax
movslq %r13d, %r13
movq (%rax,%r13,8), %rdi
callq 0x7c22
movl %eax, %ecx
movl %ebp, %eax
cltd
idivl %ecx
movq 0x10(%r14), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x1ee85
incl %edx
movq 0x10(%rsp), %r14
decl %edx
je 0x1ee8c
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x1ee76
jmp 0x1ee93
movq 0x10(%rsp), %r14
jmp 0x1ee93
movq 0x18(%rax), %rcx
movl 0x18(%rcx), %ebx
movslq %ebx, %rcx
cmpq $0x0, (%r12,%rcx,8)
je 0x1eec8
leaq (%r12,%r13,8), %rax
movq (%rax), %rdx
movq 0x10(%rdx), %rdx
movl 0x18(%rdx), %edx
cmpl %ebx, %edx
je 0x1eebf
movq $0x0, (%rax)
movslq %edx, %rax
leaq (%r12,%rax,8), %rax
jmp 0x1eea1
movq $0x0, (%rax)
jmp 0x1eecc
movq %rax, (%r12,%rcx,8)
movb (%r15,%rcx), %al
testb %al, %al
je 0x1ef39
movb %al, 0xf(%rsp)
movq (%r12,%rcx,8), %rax
movq 0x10(%rax), %rax
movl 0x18(%rax), %r13d
cmpl %ebx, %r13d
je 0x1ef28
movslq %r13d, %rbp
movb $0x1, (%r15,%rbp)
movslq %ebx, %rbx
movq (%r12,%rbx,8), %rdi
movq 0x20(%rsp), %rsi
callq 0x7bc0
movq %r14, %rdi
movq %rax, %rsi
callq 0x88b9
movq $0x0, (%r12,%rbx,8)
movq (%r12,%rbp,8), %rax
movl %r13d, %ebx
cmpq 0xe23d(%rip), %rax      # 0x2d160
jne 0x1eedc
movl %r13d, %ebx
movslq %ebx, %rax
movq $0x0, (%r12,%rax,8)
movb 0xf(%rsp), %al
jmp 0x1ef3c
movl %ebx, %r13d
testb %al, %al
je 0x1ee3b
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)