// File: /workspace/github_cpp_makefile_star2_O3/alan0201tw[P]RayTracer/src/bvh.cpp
// Function: bvh_node::bvh_node(std::vector<std::shared_ptr<hitable>, std::allocator<std::shared_ptr<hitable>>> const&, float, float)
bvh_node::bvh_node(const std::vector<hitable_reference>& _list, float _t0, float _t1)
{
    list = _list;

    int list_size = (int)list.size();

    //std::cout << "list_size = " << list_size << "\n";

    int random_axis = int(3 * drand48());
    if(random_axis == 0)
    {
        // Reference : 
        // according to https://stackoverflow.com/questions/12308243/trying-to-use-qsort-with-vector
        // using std::sort with functor is the fastest, comparing to qsort and
        // std::sort with custom function as compare operator
        std::sort(list.begin(), list.end(), box_x_compare());
    }
    else if(random_axis == 1)
    {
        std::sort(list.begin(), list.end(), box_y_compare());
    }
    else
    {
        std::sort(list.begin(), list.end(), box_z_compare());
    }

    // edge cases when the bounding volume contains only one or two elements
    if(list_size == 1)
    {
        left = right = _list[0];
    }
    else if(list_size == 2)
    {
        left = _list[0];
        right = _list[1];
    }
    else
    {
        int half_size = list_size / 2;
        std::vector<hitable_reference> left_list(list.begin(), list.begin() + half_size);
        std::vector<hitable_reference> right_list(list.begin() + half_size, list.end());

        left = std::make_shared<bvh_node>(left_list, _t0, _t1);
        right = std::make_shared<bvh_node>(right_list, _t0, _t1);
    }

    aabb box_left, box_right;
    if(left->bounding_box(_t0, _t1, box_left) == false || right->bounding_box(_t0, _t1, box_right) == false)
    {
        std::cerr << "No bounding box in bvh_node constructor!\n";
    }
    box = surrounding_box(box_left, box_right);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movss %xmm0, 0x48(%rsp)
movss %xmm0, 0x44(%rsp)
movss %xmm1, 0x4c(%rsp)
movss %xmm1, 0x40(%rsp)
leaq 0x448e6(%rip), %rax     # 0x4e2e0
movq %rax, (%rdi)
leaq 0x40(%rdi), %r14
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rdi)
movq $0x0, 0x50(%rdi)
movupd %xmm0, 0x8(%rdi)
movupd %xmm0, 0x18(%rdi)
movq %r14, %rdi
callq 0x77e4
movq 0x48(%rbx), %rbp
subq 0x40(%rbx), %rbp
callq 0x63a0
mulsd 0x32997(%rip), %xmm0    # 0x3c3d0
cvttsd2si %xmm0, %eax
movq 0x40(%rbx), %r13
movq 0x48(%rbx), %r12
cmpl $0x1, %eax
je 0x9a86
testl %eax, %eax
jne 0x9aba
cmpq %r12, %r13
je 0x9aec
movq %r12, %rax
subq %r13, %rax
sarq $0x4, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x9f35
movq %r13, %rdi
movq %r12, %rsi
callq 0x9fe8
jmp 0x9aec
cmpq %r12, %r13
je 0x9aec
movq %r12, %rax
subq %r13, %rax
sarq $0x4, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0xa622
movq %r13, %rdi
movq %r12, %rsi
callq 0xa6d5
jmp 0x9aec
cmpq %r12, %r13
je 0x9aec
movq %r12, %rax
subq %r13, %rax
sarq $0x4, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0xacb7
movq %r13, %rdi
movq %r12, %rsi
callq 0xad6a
shrq $0x4, %rbp
cmpl $0x2, %ebp
je 0x9b25
cmpl $0x1, %ebp
jne 0x9b65
movq (%r15), %rsi
movq (%rsi), %rax
movq %rax, 0x18(%rbx)
leaq 0x20(%rbx), %r15
addq $0x8, %rsi
movq %r15, %rdi
callq 0x7a28
movq 0x18(%rbx), %rax
movq %rax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
movq %r15, %rsi
jmp 0x9b4f
movq (%r15), %rsi
movq (%rsi), %rax
movq %rax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
addq $0x8, %rsi
callq 0x7a28
movq (%r15), %rsi
movq 0x10(%rsi), %rax
movq %rax, 0x18(%rbx)
leaq 0x20(%rbx), %rdi
addq $0x18, %rsi
callq 0x7a28
movss 0x4c(%rsp), %xmm1
movss 0x48(%rsp), %xmm0
jmp 0x9c82
movl %ebp, %eax
shrl $0x1f, %eax
addl %ebp, %eax
sarl %eax
movq (%r14), %rsi
movslq %eax, %r15
shlq $0x4, %r15
leaq (%rsi,%r15), %rdx
leaq 0x80(%rsp), %rdi
leaq 0x68(%rsp), %rcx
callq 0x9ef4
addq 0x40(%rbx), %r15
movq 0x48(%rbx), %rdx
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rcx
movq %r15, %rsi
callq 0x9ef4
leaq 0x58(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x50(%rsp), %rsi
leaq 0x3f(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0x44(%rsp), %r8
leaq 0x40(%rsp), %r9
callq 0xb3ec
movapd 0x50(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x50(%rsp)
movq 0x10(%rbx), %rdi
movupd %xmm0, 0x8(%rbx)
testq %rdi, %rdi
je 0x9c06
callq 0x7774
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x9c06
callq 0x7774
leaq 0x58(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x50(%rsp), %rsi
leaq 0x3f(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x44(%rsp), %r8
leaq 0x40(%rsp), %r9
callq 0xb3ec
movaps 0x50(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x50(%rsp)
movq 0x20(%rbx), %rdi
movups %xmm0, 0x18(%rbx)
testq %rdi, %rdi
je 0x9c5f
callq 0x7774
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x9c5f
callq 0x7774
leaq 0x68(%rsp), %rdi
callq 0x7726
leaq 0x80(%rsp), %rdi
callq 0x7726
movss 0x44(%rsp), %xmm0
movss 0x40(%rsp), %xmm1
leaq 0x8(%rbx), %rax
movq (%rax), %rdi
movq (%rdi), %rax
leaq 0x80(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
je 0x9cba
movq 0x18(%rbx), %rdi
movss 0x44(%rsp), %xmm0
movss 0x40(%rsp), %xmm1
movq (%rdi), %rax
leaq 0x68(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
jne 0x9cd2
movq 0x4531f(%rip), %rdi     # 0x4efe0
leaq 0x32710(%rip), %rsi     # 0x3c3d8
movl $0x29, %edx
callq 0x6470
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movups 0x68(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x80(%rsp), %xmm0
movupd %xmm0, (%rsp)
leaq 0x50(%rsp), %rdi
callq 0xc370
movq 0x60(%rsp), %rax
movq %rax, 0x38(%rbx)
movups 0x50(%rsp), %xmm0
movups %xmm0, 0x28(%rbx)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9d31
movq %rax, %r15
leaq 0x68(%rsp), %rdi
callq 0x7726
jmp 0x9d43
movq %rax, %r15
leaq 0x80(%rsp), %rdi
callq 0x7726
jmp 0x9d5d
jmp 0x9d5a
jmp 0x9d5a
jmp 0x9d5a
jmp 0x9d5a
movq %rax, %r15
movq %r14, %rdi
callq 0x7726
movq 0x20(%rbx), %rdi
testq %rdi, %rdi
je 0x9d73
callq 0x7774
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x9d81
callq 0x7774
movq %r15, %rdi
callq 0x66e0
nop