// File: 8igMac[P]random-forest/src/decision_tree.cpp
// Function: decision_tree::build_tree(node*, std::vector<data_inst, std::allocator<data_inst>>, int, int)
void decision_tree::build_tree(node* nodePtr, vector<data_inst> sampleSet, int depth, int parentMaj)
{
//	cout << "depth: " << depth << " sample size: " << sampleSet.size() << endl; //debug
	nodePtr->depth = depth;
	nodePtr->samplePool = sampleSet;
	nodePtr->numSample = sampleSet.size();
	nodePtr->parentMajCls = parentMaj;
	if (nodePtr->set_size_of_class() < 0)
		return;

	if (depth <= MAXDEPTH && 
			nodePtr->numSample >= MINSAMPLE &&
			!(nodePtr->poolPure()))
	{
		// select attribut
		nodePtr->attr = selectAttr(sampleSet);

		// split sample into two group
		vector<data_inst> leftSample, rightSample;
		for (auto sample: sampleSet)
		{
			if (sample.attr.at(nodePtr->attr.first) <= nodePtr->attr.second)
				leftSample.push_back(sample);
			else
				rightSample.push_back(sample);
		}

		// generate next node
		nodePtr->leftChild = new node;
		build_tree(nodePtr->leftChild, leftSample, depth+1, nodePtr->majClass());
		nodePtr->rightChild = new node;
		build_tree(nodePtr->rightChild, rightSample, depth+1, nodePtr->majClass());
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdx, -0x108(%rbp)
movq %rsi, %rax
movq -0x108(%rbp), %rsi
movq %rsi, -0xf8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
callq 0x8320
movq -0xf8(%rbp), %rdi
callq 0x3f80
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x2c(%rax)
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x58e0
cmpl $0x0, %eax
jge 0x5c79
jmp 0x6003
cmpl $0xc, -0x1c(%rbp)
jg 0x6003
movq -0x10(%rbp), %rax
cmpl $0x5, 0x2c(%rax)
jl 0x6003
movq -0x10(%rbp), %rdi
callq 0x5840
testb $0x1, %al
jne 0x6003
movq -0xf8(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x2a60
movq -0x100(%rbp), %rdi
movq -0x118(%rbp), %rsi
callq 0x6020
movq %rax, -0x110(%rbp)
jmp 0x5cd5
movq -0x110(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
addq $0x20, %rdi
leaq -0x28(%rbp), %rsi
callq 0x8570
leaq -0x40(%rbp), %rdi
callq 0x2b50
leaq -0x68(%rbp), %rdi
callq 0x28a0
leaq -0x80(%rbp), %rdi
callq 0x28a0
movq -0xf8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0x7e00
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0x7e30
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x7e60
testb $0x1, %al
jne 0x5d5c
jmp 0x5e3c
leaq -0x90(%rbp), %rdi
callq 0x7ea0
movq %rax, %rsi
leaq -0xc0(%rbp), %rdi
callq 0x44d0
jmp 0x5d79
leaq -0xb8(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq 0x20(%rax), %rsi
callq 0x85a0
movq %rax, -0x120(%rbp)
jmp 0x5d96
movq -0x120(%rbp), %rax
movss (%rax), %xmm1
movq -0x10(%rbp), %rax
movss 0x24(%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x5e0b
leaq -0x68(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x7d90
jmp 0x5dc1
jmp 0x5e1f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x2b50
jmp 0x600c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x5fef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x3540
jmp 0x5fef
leaq -0x80(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x7d90
jmp 0x5e1d
jmp 0x5e1f
leaq -0xc0(%rbp), %rdi
callq 0x3540
leaq -0x90(%rbp), %rdi
callq 0x7ee0
jmp 0x5d40
movl $0x58, %edi
callq 0x2170
movq %rax, -0x128(%rbp)
jmp 0x5e4f
movq -0x128(%rbp), %rdi
callq 0x56a0
jmp 0x5e5d
movq -0x128(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x130(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2a60
jmp 0x5e8d
movl -0x1c(%rbp), %eax
incl %eax
movl %eax, -0x134(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5790
movq -0x100(%rbp), %rdi
movq -0x130(%rbp), %rsi
movl -0x134(%rbp), %ecx
movl %eax, %r8d
leaq -0xd8(%rbp), %rdx
callq 0x5bf0
jmp 0x5ec6
leaq -0xd8(%rbp), %rdi
callq 0x2b50
movl $0x58, %edi
callq 0x2170
movq %rax, -0x140(%rbp)
jmp 0x5ee5
movq -0x140(%rbp), %rdi
callq 0x56a0
jmp 0x5ef3
movq -0x140(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x148(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2a60
jmp 0x5f23
movl -0x1c(%rbp), %eax
incl %eax
movl %eax, -0x14c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5790
movq -0x100(%rbp), %rdi
movq -0x148(%rbp), %rsi
movl -0x14c(%rbp), %ecx
movl %eax, %r8d
leaq -0xf0(%rbp), %rdx
callq 0x5bf0
jmp 0x5f5c
leaq -0xf0(%rbp), %rdi
callq 0x2b50
leaq -0x80(%rbp), %rdi
callq 0x2b50
leaq -0x68(%rbp), %rdi
callq 0x2b50
jmp 0x6003
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x58, %esi
callq 0x2180
jmp 0x5fef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x2b50
jmp 0x5fef
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x58, %esi
callq 0x2180
jmp 0x5fef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x2b50
leaq -0x80(%rbp), %rdi
callq 0x2b50
leaq -0x68(%rbp), %rdi
callq 0x2b50
jmp 0x600c
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2270
nopw %cs:(%rax,%rax)