// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movss %xmm0, 0x3c(%rsp)
movss %xmm1, 0x38(%rsp)
leaq 0x447aa(%rip), %rax     # 0x4e2f8
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 0x77d8
movq 0x48(%rbx), %rbp
subq 0x40(%rbx), %rbp
callq 0x63a0
mulsd 0x32823(%rip), %xmm0    # 0x3c3b0
cvttsd2si %xmm0, %eax
movq 0x40(%rbx), %r13
movq 0x48(%rbx), %r12
cmpl $0x1, %eax
je 0x9bda
testl %eax, %eax
jne 0x9c0e
cmpq %r12, %r13
je 0x9c40
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 0xa065
movq %r13, %rdi
movq %r12, %rsi
callq 0xa118
jmp 0x9c40
cmpq %r12, %r13
je 0x9c40
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 0xa77c
movq %r13, %rdi
movq %r12, %rsi
callq 0xa82f
jmp 0x9c40
cmpq %r12, %r13
je 0x9c40
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 0xae3a
movq %r13, %rdi
movq %r12, %rsi
callq 0xaeed
shrq $0x4, %rbp
cmpl $0x2, %ebp
je 0x9c79
cmpl $0x1, %ebp
jne 0x9cad
movq (%r15), %rsi
movq (%rsi), %rax
movq %rax, 0x18(%rbx)
leaq 0x20(%rbx), %r15
addq $0x8, %rsi
movq %r15, %rdi
callq 0x7a04
movq 0x18(%rbx), %rax
movq %rax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
movq %r15, %rsi
jmp 0x9ca3
movq (%r15), %rsi
movq (%rsi), %rax
movq %rax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
addq $0x8, %rsi
callq 0x7a04
movq (%r15), %rsi
movq 0x10(%rsi), %rax
movq %rax, 0x18(%rbx)
leaq 0x20(%rbx), %rdi
addq $0x18, %rsi
callq 0x7a04
jmp 0x9db7
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 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rcx
callq 0xa024
addq 0x40(%rbx), %r15
movq 0x48(%rbx), %rdx
leaq 0x58(%rsp), %rdi
leaq 0x40(%rsp), %rcx
movq %r15, %rsi
callq 0xa024
leaq 0x48(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x40(%rsp), %rsi
leaq 0x37(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x3c(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0xb596
movapd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x40(%rsp)
movq 0x10(%rbx), %rdi
movupd %xmm0, 0x8(%rbx)
testq %rdi, %rdi
je 0x9d39
callq 0x7768
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x9d48
callq 0x7768
leaq 0x48(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x40(%rsp), %rsi
leaq 0x37(%rsp), %rdx
leaq 0x58(%rsp), %rcx
leaq 0x3c(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0xb596
movapd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x40(%rsp)
movq 0x20(%rbx), %rdi
movupd %xmm0, 0x18(%rbx)
testq %rdi, %rdi
je 0x9d94
callq 0x7768
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x9da3
callq 0x7768
leaq 0x58(%rsp), %rdi
callq 0x7720
leaq 0x70(%rsp), %rdi
callq 0x7720
leaq 0x8(%rbx), %rax
movq (%rax), %rdi
movss 0x3c(%rsp), %xmm0
movss 0x38(%rsp), %xmm1
movq (%rdi), %rax
leaq 0x70(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
je 0x9df8
movq 0x18(%rbx), %rdi
movss 0x3c(%rsp), %xmm0
movss 0x38(%rsp), %xmm1
movq (%rdi), %rax
leaq 0x58(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
jne 0x9e10
movq 0x451e1(%rip), %rdi     # 0x4efe0
leaq 0x325b2(%rip), %rsi     # 0x3c3b8
movl $0x29, %edx
callq 0x6470
movq 0x68(%rsp), %rax
movq %rax, 0x28(%rsp)
movups 0x58(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x70(%rsp), %xmm0
movupd %xmm0, (%rsp)
leaq 0x40(%rsp), %rdi
callq 0xc52e
movq 0x50(%rsp), %rax
movq %rax, 0x38(%rbx)
movups 0x40(%rsp), %xmm0
movups %xmm0, 0x28(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9e6c
movq %rax, %r15
leaq 0x58(%rsp), %rdi
callq 0x7720
jmp 0x9e7e
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x7720
jmp 0x9e95
jmp 0x9e92
jmp 0x9e92
jmp 0x9e92
jmp 0x9e92
movq %rax, %r15
movq %r14, %rdi
callq 0x7720
movq 0x20(%rbx), %rdi
testq %rdi, %rdi
je 0x9eab
callq 0x7768
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x9eb9
callq 0x7768
movq %r15, %rdi
callq 0x66e0
nop