// File: /workspace/github_cpp_makefile_star2_O3/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
testl %eax, %eax
setle %cl
testl %ebx, %ebx
sets %dl
xorl %r10d, %r10d
orb %cl, %dl
jne 0x26cd
leaq 0x44(%r14), %rdx
movl 0x17d8(%r14), %esi
xorl %edi, %edi
xorps %xmm0, %xmm0
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movabsq $-0x4010000000000000, %rcx # imm = 0xBFF0000000000000
xorl %r9d, %r9d
xorl %r10d, %r10d
movl %ebx, %r11d
movl %r11d, %r12d
shlq $0x7, %r12
leaq (%rdx,%r12), %r15
cmpl $-0x1, (%rdx,%r12)
je 0x25aa
movslq %r10d, %r12
movl (%rsp,%r12,4), %ebp
cmpl $0x1, %ebp
je 0x2614
testl %ebp, %ebp
je 0x2603
cmpl $-0x1, %ebp
jne 0x2674
movl $0x0, (%rsp,%r12,4)
incl %r12d
movl 0x54(%r15), %r11d
jmp 0x266b
incl %esi
movl %esi, 0x17d8(%r14)
cmpl %ebx, %r11d
je 0x2688
incl %r9d
movl 0x58(%r15), %r11d
movq %rdi, 0x68(%r15)
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
movq $-0x1, 0x50(%r15)
movl %r8d, (%r15)
movq %rdi, 0x8(%r15)
movq %rcx, 0x60(%r15)
movl %r8d, 0x58(%r15)
movl $0x0, 0x70(%r15)
movq %rdi, 0x78(%r15)
decl %r10d
jmp 0x266e
movl $0x1, (%rsp,%r12,4)
incl %r12d
movl 0x50(%r15), %r11d
jmp 0x266b
incl %esi
movl %esi, 0x17d8(%r14)
cmpl %ebx, %r11d
je 0x2688
movl %r8d, (%rsp,%r12,4)
decl %r12d
movl 0x58(%r15), %r11d
movq %rdi, 0x68(%r15)
incl %r9d
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
movq $-0x1, 0x50(%r15)
movl %r8d, (%r15)
movq %rdi, 0x8(%r15)
movq %rcx, 0x60(%r15)
movl %r8d, 0x58(%r15)
movl $0x0, 0x70(%r15)
movq %rdi, 0x78(%r15)
movl %r12d, %r10d
cmpl $0x63, %r10d
jg 0x2684
cmpl %eax, %r9d
jge 0x26cd
testl %r11d, %r11d
jns 0x2567
jmp 0x26cd
xorl %eax, %eax
jmp 0x26d4
xorl %eax, %eax
movq %rax, 0x68(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movups %xmm0, 0x30(%r15)
movups %xmm0, 0x40(%r15)
movq $-0x1, 0x50(%r15)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, (%r15)
movq %rax, 0x8(%r15)
movq %rcx, 0x60(%r15)
movl %edx, 0x58(%r15)
movl $0x0, 0x70(%r15)
movq %rax, 0x78(%r15)
cmpl $-0x1, %r10d
sete %al
addq $0x190, %rsp            # imm = 0x190
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq