// File: big-data-lab-team[P]OrpailleCC/./src/mondrian_coarse.hpp
// Function: int CoarseMondrianForest<double, funct, KappaMetrics<2, false>, 4, 2, 6000>::tree_fade_counts<100>(int)
int tree_fade_counts(int const tree_id){
	TreeBase const& base = tree_bases()[tree_id];
	int const root_id = base.root;

	//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;
	double smallest_count = -1;
	int smallest_id = 0;
	//a for loop instead of a while to avoid infinite loops. Since we don't expect to do more turn than node_count
	int i = 0;
	double leaf_count = 0;
	double sum_count_leaves = 0;
	while(i < node_count && node_id >= 0){
		Node& node = nodes()[node_id];
		if(!node.is_leaf()){  //Internal node
			if (stack[depth] == -1){ //Go 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
				stack[depth] = -1; //Reset to -1
				depth -= 1;
				i += 1;
				node_id = node.parent;
			}
		}
		else{ //Leaf
			stack[depth] = -1; //Reset to -1
			depth -= 1;
			i += 1;
			node.fading_score = node.fading_score * nodes_fading_f;
			node_id = node.parent;
		}
		if(depth >= max_stack_size)
			return -1;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x1a50
movslq -0x14(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x1c4(%rbp)
cmpl $0x64, -0x1c4(%rbp)
jge 0x2d31
movslq -0x1c4(%rbp), %rax
movl $0xffffffff, -0x1c0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c4(%rbp)
jmp 0x2d05
movl -0x24(%rbp), %eax
movl %eax, -0x1c8(%rbp)
movl $0x0, -0x1cc(%rbp)
movsd 0x82dc(%rip), %xmm0     # 0xb028
movsd %xmm0, -0x1d8(%rbp)
movl $0x0, -0x1dc(%rbp)
movl $0x0, -0x1e0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x1e8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x1f0(%rbp)
movq -0x200(%rbp), %rdx
movl -0x1e0(%rbp), %ecx
xorl %eax, %eax
cmpl 0x17d4(%rdx), %ecx
movb %al, -0x201(%rbp)
jge 0x2dab
cmpl $0x0, -0x1c8(%rbp)
setge %al
movb %al, -0x201(%rbp)
movb -0x201(%rbp), %al
testb $0x1, %al
jne 0x2dba
jmp 0x2f4f
movq -0x200(%rbp), %rdi
callq 0x1b00
movslq -0x1c8(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x1f8(%rbp)
movq -0x1f8(%rbp), %rdi
callq 0x29e0
testb $0x1, %al
jne 0x2ed1
movslq -0x1cc(%rbp), %rax
cmpl $-0x1, -0x1c0(%rbp,%rax,4)
jne 0x2e36
movslq -0x1cc(%rbp), %rax
movl $0x0, -0x1c0(%rbp,%rax,4)
movl -0x1cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1cc(%rbp)
movq -0x1f8(%rbp), %rax
movl 0x54(%rax), %eax
movl %eax, -0x1c8(%rbp)
jmp 0x2ecf
movslq -0x1cc(%rbp), %rax
cmpl $0x0, -0x1c0(%rbp,%rax,4)
jne 0x2e7a
movslq -0x1cc(%rbp), %rax
movl $0x1, -0x1c0(%rbp,%rax,4)
movl -0x1cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1cc(%rbp)
movq -0x1f8(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x1c8(%rbp)
jmp 0x2ecd
movslq -0x1cc(%rbp), %rax
cmpl $0x1, -0x1c0(%rbp,%rax,4)
jne 0x2ecb
movslq -0x1cc(%rbp), %rax
movl $0xffffffff, -0x1c0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0x1cc(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x1cc(%rbp)
movl -0x1e0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e0(%rbp)
movq -0x1f8(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1c8(%rbp)
jmp 0x2ecd
jmp 0x2ecf
jmp 0x2f38
movq -0x200(%rbp), %rax
movslq -0x1cc(%rbp), %rcx
movl $0xffffffff, -0x1c0(%rbp,%rcx,4) # imm = 0xFFFFFFFF
movl -0x1cc(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, -0x1cc(%rbp)
movl -0x1e0(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x1e0(%rbp)
movq -0x1f8(%rbp), %rcx
movsd 0x78(%rcx), %xmm0
mulsd 0x1830(%rax), %xmm0
movq -0x1f8(%rbp), %rax
movsd %xmm0, 0x78(%rax)
movq -0x1f8(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x1c8(%rbp)
cmpl $0x64, -0x1cc(%rbp)
jl 0x2f4a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2f56
jmp 0x2d7e
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)