// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x190, %rsp            # imm = 0x190
movl %esi, %ebx
movq %rdi, %r14
movq %rsp, %rdi
movl $0x190, %edx            # imm = 0x190
movl $0xff, %esi
callq 0x1040
movl 0x17d4(%r14), %eax
movl 0x17d8(%r14), %ecx
leaq 0x44(%r14), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movabsq $-0x4010000000000000, %r8 # imm = 0xBFF0000000000000
movl %ebx, %r10d
xorl %r11d, %r11d
xorl %r9d, %r9d
cmpl %eax, %r9d
jge 0x25e5
testl %r10d, %r10d
js 0x25e5
movl %r10d, %r12d
shlq $0x7, %r12
leaq (%rdx,%r12), %r15
cmpl $-0x1, (%rdx,%r12)
je 0x24c6
movslq %r11d, %r12
movl (%rsp,%r12,4), %ebp
cmpl $0x1, %ebp
je 0x2532
testl %ebp, %ebp
je 0x251e
cmpl $-0x1, %ebp
jne 0x25ce
movl $0x0, (%rsp,%r12,4)
incl %r12d
movl 0x54(%r15), %r10d
jmp 0x25cb
incl %ecx
movl %ecx, 0x17d8(%r14)
incl %r9d
cmpl %ebx, %r10d
je 0x2540
movl 0x58(%r15), %r10d
movq %rsi, 0x68(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
movq $-0x1, 0x50(%r15)
movl %edi, (%r15)
movq %rsi, 0x8(%r15)
movq %r8, 0x60(%r15)
movl %edi, 0x58(%r15)
movl $0x0, 0x70(%r15)
movq %rsi, 0x78(%r15)
decl %r11d
jmp 0x25ce
movl $0x1, (%rsp,%r12,4)
incl %r12d
movl 0x50(%r15), %r10d
jmp 0x25cb
incl %ecx
movl %ecx, 0x17d8(%r14)
cmpl %ebx, %r10d
jne 0x2582
movq %rsi, 0x68(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
movq $-0x1, 0x50(%r15)
movl %edi, (%r15)
movq %rsi, 0x8(%r15)
movq %r8, 0x60(%r15)
movl %edi, 0x58(%r15)
movl $0x0, 0x70(%r15)
movq %rsi, 0x78(%r15)
movl $0x6, %ebp
jmp 0x25d8
movl %edi, (%rsp,%r12,4)
movl 0x58(%r15), %r10d
movq %rsi, 0x68(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
decl %r12d
incl %r9d
movq $-0x1, 0x50(%r15)
movl %edi, (%r15)
movq %rsi, 0x8(%r15)
movq %r8, 0x60(%r15)
movl %edi, 0x58(%r15)
movl $0x0, 0x70(%r15)
movq %rsi, 0x78(%r15)
movl %r12d, %r11d
xorl %ebp, %ebp
cmpl $0x64, %r11d
setge %bpl
testl %ebp, %ebp
je 0x2471
cmpl $0x6, %ebp
jne 0x25ee
cmpl $-0x1, %r11d
sete %al
jmp 0x25f0
xorl %eax, %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq