// File: big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: bool CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::node_reset<100>(int)
bool node_reset(int const start_id) {
	int const root_id = start_id;
	//Initialize an array to keep track of where we have to go at each tree level.
	//The max depth expected is MAX_DEPTH.
	int stack[max_stack_size];
	for(int i = 0; i < max_stack_size; ++i)
		stack[i] = -1;

	//Start at the root, then dive inside the tree
	int node_id = root_id;
	int depth = 0;
	//a for loop instead of a while to avoid infinite loops. Since we don't expect to do more turn than node_count
	//*i* count the number of node deleted.
	int i = 0;
	while(i < node_count && node_id >= 0){
		Node& node = nodes()[node_id];
		if (node.is_leaf()){
			node_available += 1;
			i += 1;

			if(node_id == root_id){
				node.reset();
				break;
			}

			//Get the parent before reset (root parent will be a negative number)
			//Since the loop stops if node_id is below 0, it's fine
			node_id = node.parent;
			node.reset();

			depth -= 1;
		}
		else{  //Internal node
			if (stack[depth] == -1){ //going right
				stack[depth] = 0;
				depth += 1;
				node_id = node.child_right;
			}
			else if (stack[depth] == 0){ //Go left
				stack[depth] = 1;
				depth += 1;
				node_id = node.child_left;
			}
			else if (stack[depth] == 1){ //Go up
				node_available += 1;

				if(node_id == root_id){
					node.reset();
					break;
				}

				stack[depth] = -1; //Reset to -1
				depth -= 1;
				i += 1;

				//Get the parent before reset (root parent will be a negative number)
				//Since the loop stops if node_id is below 0, it's fine
				node_id = node.parent;
				node.reset();

			}
		}
		if(depth >= max_stack_size)
			return false;
	}

	if(depth != -1)
		return false;
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1b4(%rbp)
cmpl $0x64, -0x1b4(%rbp)
jge 0x3609
movslq -0x1b4(%rbp), %rax
movl $0xffffffff, -0x1b0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x35dd
movl -0x18(%rbp), %eax
movl %eax, -0x1b8(%rbp)
movl $0x0, -0x1bc(%rbp)
movl $0x0, -0x1c0(%rbp)
movq -0x1d0(%rbp), %rdx
movl -0x1c0(%rbp), %ecx
xorl %eax, %eax
cmpl 0x17d4(%rdx), %ecx
movb %al, -0x1d1(%rbp)
jge 0x3653
cmpl $0x0, -0x1b8(%rbp)
setge %al
movb %al, -0x1d1(%rbp)
movb -0x1d1(%rbp), %al
testb $0x1, %al
jne 0x3662
jmp 0x383a
movq -0x1d0(%rbp), %rdi
callq 0x1b00
movslq -0x1b8(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x1c8(%rbp)
movq -0x1c8(%rbp), %rdi
callq 0x29e0
testb $0x1, %al
jne 0x3695
jmp 0x3706
movq -0x1d0(%rbp), %rax
movl 0x17d8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x17d8(%rax)
movl -0x1c0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c0(%rbp)
movl -0x1b8(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x36d6
movq -0x1c8(%rbp), %rdi
callq 0x1b20
jmp 0x383a
movq -0x1c8(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1b8(%rbp)
movq -0x1c8(%rbp), %rdi
callq 0x1b20
movl -0x1bc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1bc(%rbp)
jmp 0x3826
movslq -0x1bc(%rbp), %rax
cmpl $-0x1, -0x1b0(%rbp,%rax,4)
jne 0x374d
movslq -0x1bc(%rbp), %rax
movl $0x0, -0x1b0(%rbp,%rax,4)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movq -0x1c8(%rbp), %rax
movl 0x54(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x3824
movslq -0x1bc(%rbp), %rax
cmpl $0x0, -0x1b0(%rbp,%rax,4)
jne 0x3794
movslq -0x1bc(%rbp), %rax
movl $0x1, -0x1b0(%rbp,%rax,4)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movq -0x1c8(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x3822
movslq -0x1bc(%rbp), %rax
cmpl $0x1, -0x1b0(%rbp,%rax,4)
jne 0x3820
movq -0x1d0(%rbp), %rax
movl 0x17d8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x17d8(%rax)
movl -0x1b8(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x37d4
movq -0x1c8(%rbp), %rdi
callq 0x1b20
jmp 0x383a
movslq -0x1bc(%rbp), %rax
movl $0xffffffff, -0x1b0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1bc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movl -0x1c0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c0(%rbp)
movq -0x1c8(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1b8(%rbp)
movq -0x1c8(%rbp), %rdi
callq 0x1b20
jmp 0x3822
jmp 0x3824
jmp 0x3826
cmpl $0x64, -0x1bc(%rbp)
jl 0x3835
movb $0x0, -0x1(%rbp)
jmp 0x384d
jmp 0x3626
cmpl $-0x1, -0x1bc(%rbp)
je 0x3849
movb $0x0, -0x1(%rbp)
jmp 0x384d
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopl (%rax,%rax)