// File: Lut99[P]RayTracer/src/lib/ObjectTree.cpp
// Function: RayTracer::ObjectTreeBranch::ObjectTreeBranch(std::vector<RayTracer::RenderObject*, std::allocator<RayTracer::RenderObject*>>)
ObjectTreeBranch::ObjectTreeBranch(const std::vector<RenderObject*> elements)
    : ObjectTreeNode(false, BoundingBox())
{
    // First, construct the nodes by assigning them half each cluster
    std::vector<RenderObject*> left;
    std::vector<RenderObject*> right;
    cluster(elements, left, right);
    
    // Construct leaves instead if only one element is left
    if (left.size() == 1) {
        this->left = (ObjectTreeNode*) new ObjectTreeLeaf(left.at(0));
    } else {
        this->left = (ObjectTreeNode*) new ObjectTreeBranch(left);
    }
    if (right.size() == 1) {
        this->right = (ObjectTreeNode*) new ObjectTreeLeaf(right.at(0));
    } else {
        this->right = (ObjectTreeNode*) new ObjectTreeBranch(right);
    }

    // Update the bounding box to be a merge of those
    this->box.merge(this->left->box);
    this->box.merge(this->right->box);
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rsi, 0x40(%rsp)
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0xd8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x15a210
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x16d400
movq 0x38(%rsp), %rax
leaq 0x3ffc8(%rip), %rcx     # 0x1abc40
movq %rcx, (%rax)
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x15d930
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x15d930
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
callq 0x16b170
jmp 0x16bcb5
leaq 0xc0(%rsp), %rdi
callq 0x15d790
cmpq $0x1, %rax
jne 0x16bd5d
movl $0x58, %edi
callq 0xd05d0
movq %rax, 0x28(%rsp)
jmp 0x16bcdd
xorl %eax, %eax
movl %eax, %esi
leaq 0xc0(%rsp), %rdi
callq 0x15d980
movq %rax, 0x20(%rsp)
jmp 0x16bcf5
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rsi
callq 0x16c570
jmp 0x16bd09
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x50(%rax)
jmp 0x16be14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x16bf9b
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
movl $0x58, %esi
callq 0xd05e0
jmp 0x16bf9b
movl $0x60, %edi
callq 0xd05d0
movq %rax, 0x18(%rsp)
jmp 0x16bd6e
movb $0x1, 0x7f(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x15d7d0
jmp 0x16bd8a
movq 0x18(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x16bc20
jmp 0x16bd9e
movq 0x38(%rsp), %rax
movq 0x18(%rsp), %rcx
movb $0x0, 0x7f(%rsp)
movq %rcx, 0x50(%rax)
leaq 0x80(%rsp), %rdi
callq 0x15d8d0
jmp 0x16be14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x16bdf7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x15d8d0
testb $0x1, 0x7f(%rsp)
jne 0x16be00
jmp 0x16be0f
movq 0x18(%rsp), %rdi
movl $0x60, %esi
callq 0xd05e0
jmp 0x16bf9b
leaq 0xa8(%rsp), %rdi
callq 0x15d790
cmpq $0x1, %rax
jne 0x16be9f
movl $0x58, %edi
callq 0xd05d0
movq %rax, 0x10(%rsp)
jmp 0x16be38
xorl %eax, %eax
movl %eax, %esi
leaq 0xa8(%rsp), %rdi
callq 0x15d980
movq %rax, 0x8(%rsp)
jmp 0x16be50
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rax
movq (%rax), %rsi
callq 0x16c570
jmp 0x16be64
movq 0x38(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x58(%rax)
jmp 0x16bf43
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
movl $0x58, %esi
callq 0xd05e0
jmp 0x16bf9b
movl $0x60, %edi
callq 0xd05d0
movq %rax, (%rsp)
jmp 0x16beaf
movb $0x1, 0x5f(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x15d7d0
jmp 0x16bec8
movq (%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x16bc20
jmp 0x16bed8
movq 0x38(%rsp), %rax
movq (%rsp), %rcx
movb $0x0, 0x5f(%rsp)
movq %rcx, 0x58(%rax)
leaq 0x60(%rsp), %rdi
callq 0x15d8d0
jmp 0x16bf43
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0x16bf2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x15d8d0
testb $0x1, 0x5f(%rsp)
jne 0x16bf33
jmp 0x16bf41
movq (%rsp), %rdi
movl $0x60, %esi
callq 0xd05e0
jmp 0x16bf9b
movq 0x38(%rsp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movq 0x50(%rax), %rsi
addq $0x10, %rsi
callq 0x16ad60
jmp 0x16bf5e
movq 0x38(%rsp), %rax
movq %rax, %rdi
addq $0x10, %rdi
movq 0x58(%rax), %rsi
addq $0x10, %rsi
callq 0x16ad60
jmp 0x16bf79
leaq 0xa8(%rsp), %rdi
callq 0x15d8d0
leaq 0xc0(%rsp), %rdi
callq 0x15d8d0
addq $0x128, %rsp            # imm = 0x128
retq
leaq 0xa8(%rsp), %rdi
callq 0x15d8d0
leaq 0xc0(%rsp), %rdi
callq 0x15d8d0
movq 0x38(%rsp), %rdi
callq 0x16d6d0
movq 0xa0(%rsp), %rdi
callq 0xd0a10
nopl (%rax)