// File: big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: int CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::node_depth<100>(int, int*) const
int node_depth(int const start_id, int* tree_node_count) const{
	//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;

	int node_id = start_id;
	int depth = 0, max_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;
	int depth_id = start_id;
	while(i < node_count && depth >= 0){
		Node const& node = nodes()[node_id];
		if (node.is_leaf()){
			i += 1;
			//acknowledge the depth
			if(depth > max_depth){
				max_depth = depth;
				depth_id = node_id;
			}

			node_id = node.parent;
			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
				i += 1;
				stack[depth] = -1; //Reset to -1
				depth -= 1;
				node_id = node.parent;
			}
			#ifdef DEBUG
			else{
				cout << __FILE__ << ":" << __LINE__ << " CoarseMondrianForest::node_depth: stack[" << depth << "] == " << stack[depth] << " (should be -1, 0, or 1)" << endl;
			}
			#endif
		}
	}
	if(tree_node_count != nullptr)
		*tree_node_count = i;
	return max_depth+1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movl $0x0, -0x1b4(%rbp)
cmpl $0x64, -0x1b4(%rbp)
jge 0x7187
movslq -0x1b4(%rbp), %rax
movl $0xffffffff, -0x1b0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1b4(%rbp)
jmp 0x715b
movl -0xc(%rbp), %eax
movl %eax, -0x1b8(%rbp)
movl $0x0, -0x1bc(%rbp)
movl $0x0, -0x1c0(%rbp)
movl $0x0, -0x1c4(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x1c8(%rbp)
movq -0x1d8(%rbp), %rdx
movl -0x1c4(%rbp), %ecx
xorl %eax, %eax
cmpl 0x17d4(%rdx), %ecx
movb %al, -0x1d9(%rbp)
jge 0x71e4
cmpl $0x0, -0x1bc(%rbp)
setge %al
movb %al, -0x1d9(%rbp)
movb -0x1d9(%rbp), %al
testb $0x1, %al
jne 0x71f3
jmp 0x7366
movq -0x1d8(%rbp), %rdi
callq 0x2a60
movslq -0x1b8(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x1d0(%rbp)
movq -0x1d0(%rbp), %rdi
callq 0x29e0
testb $0x1, %al
jne 0x7226
jmp 0x727f
movl -0x1c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c4(%rbp)
movl -0x1bc(%rbp), %eax
cmpl -0x1c0(%rbp), %eax
jle 0x725b
movl -0x1bc(%rbp), %eax
movl %eax, -0x1c0(%rbp)
movl -0x1b8(%rbp), %eax
movl %eax, -0x1c8(%rbp)
movq -0x1d0(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1b8(%rbp)
movl -0x1bc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1bc(%rbp)
jmp 0x7361
movslq -0x1bc(%rbp), %rax
cmpl $-0x1, -0x1b0(%rbp,%rax,4)
jne 0x72c6
movslq -0x1bc(%rbp), %rax
movl $0x0, -0x1b0(%rbp,%rax,4)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movq -0x1d0(%rbp), %rax
movl 0x54(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x735f
movslq -0x1bc(%rbp), %rax
cmpl $0x0, -0x1b0(%rbp,%rax,4)
jne 0x730a
movslq -0x1bc(%rbp), %rax
movl $0x1, -0x1b0(%rbp,%rax,4)
movl -0x1bc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movq -0x1d0(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x735d
movslq -0x1bc(%rbp), %rax
cmpl $0x1, -0x1b0(%rbp,%rax,4)
jne 0x735b
movl -0x1c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c4(%rbp)
movslq -0x1bc(%rbp), %rax
movl $0xffffffff, -0x1b0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1bc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1bc(%rbp)
movq -0x1d0(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1b8(%rbp)
jmp 0x735d
jmp 0x735f
jmp 0x7361
jmp 0x71b7
cmpq $0x0, -0x18(%rbp)
je 0x7379
movl -0x1c4(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x1c0(%rbp), %eax
addl $0x1, %eax
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopl (%rax,%rax)