// File: /workspace/github_cpp_makefile_star2_O2/2TallTim[P]modern-raytracing/bvh.cpp
// Function: bvh_node::bvh_node(__gnu_cxx::__normal_iterator<std::shared_ptr<hitable>*, std::vector<std::shared_ptr<hitable>, std::allocator<std::shared_ptr<hitable>>>>, __gnu_cxx::__normal_iterator<std::shared_ptr<hitable>*, std::vector<std::shared_ptr<hitable>, std::allocator<std::shared_ptr<hitable>>>>)
bvh_node::bvh_node(std::vector<hitable::Ptr>::iterator begin,
                   std::vector<hitable::Ptr>::iterator end) {
    int axis = int(rng::gen01()*3);
    switch (axis)
    {
    case 0:
        std::sort(begin,end,box_compare<0>());
        break;
    case 1:
        std::sort(begin,end,box_compare<1>());
        break;
    case 2:
        std::sort(begin,end,box_compare<2>());
    default:
        break;
    }
    int dist = std::distance(begin,end);
    if(dist == 1){
        left = right = *begin;
    }else if(dist == 2){
        left = *begin;
        right = *(begin+1);
    }else{
        left = std::make_shared<bvh_node>(begin,begin+dist/2);
        right = std::make_shared<bvh_node>(begin+dist/2,end);
    }

    aabb box_left, box_right;
    if(!left->bounding_box(box_left) || !right->bounding_box(box_right))
        std::cerr << "no bounding box in bvh_node constructor!" << std::endl;
    box = aabb::surrounding_box(box_left,box_right);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x80, %rsp
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
leaq 0x861e(%rip), %rax      # 0xfd80
movq %rax, (%rdi)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rdi)
movupd %xmm0, 0x18(%rdi)
movupd %xmm0, 0x28(%rdi)
movupd %xmm0, 0x38(%rdi)
movupd %xmm0, 0x48(%rdi)
callq 0x3d54
mulsd 0x2de1(%rip), %xmm0     # 0xa570
cvttsd2si %xmm0, %eax
testl %eax, %eax
je 0x77bb
cmpl $0x1, %eax
je 0x77ae
cmpl $0x2, %eax
jne 0x77c6
movq %r12, %rdi
movq %r13, %rsi
callq 0x8b91
jmp 0x77c6
movq %r12, %rdi
movq %r13, %rsi
callq 0x8365
jmp 0x77c6
movq %r12, %rdi
movq %r13, %rsi
callq 0x7ac8
leaq 0x8(%rbx), %r15
leaq 0x18(%rbx), %r14
subq %r12, %r13
shrq $0x4, %r13
cmpl $0x2, %r13d
je 0x77f4
cmpl $0x1, %r13d
jne 0x7810
movq %r14, %rdi
movq %r12, %rsi
callq 0x7aac
movq %r15, %rdi
movq %r14, %rsi
jmp 0x7809
movq %r15, %rdi
movq %r12, %rsi
callq 0x7aac
addq $0x10, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x7aac
jmp 0x7884
pushq $0x2
popq %rcx
movl %r13d, %eax
cltd
idivl %ecx
movslq %eax, %r13
shlq $0x4, %r13
addq %r13, %r12
leaq 0x50(%rsp), %rdx
movq %r12, (%rdx)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7a6a
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x9498
leaq 0x28(%rsp), %rdi
callq 0x3b4a
addq 0x8(%rsp), %r13
leaq 0x50(%rsp), %rsi
movq %r13, (%rsi)
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x7a8b
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x9498
leaq 0x28(%rsp), %rdi
callq 0x3b4a
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rsi
movapd %xmm0, 0x20(%rsi)
movapd %xmm0, 0x10(%rsi)
movapd %xmm0, (%rsi)
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movq (%r15), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
testb %al, %al
je 0x78cc
movq (%r14), %rdi
movq (%rdi), %rax
leaq 0x50(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
jne 0x78e7
movq 0x871d(%rip), %rdi      # 0xfff0
leaq 0x2c9e(%rip), %rsi      # 0xa578
callq 0x31d0
movq %rax, %rdi
callq 0x3090
movsd 0x30(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
callq 0x30c0
movsd %xmm0, 0x10(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
callq 0x3220
movapd 0x20(%rsp), %xmm1
movapd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm3
minpd %xmm1, %xmm3
cmpunordpd %xmm1, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm3, %xmm1
orpd %xmm2, %xmm1
movupd %xmm1, 0x28(%rbx)
movsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x38(%rbx)
movupd 0x38(%rsp), %xmm1
movupd 0x68(%rsp), %xmm2
movapd %xmm2, %xmm3
maxpd %xmm1, %xmm3
cmpunordpd %xmm1, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm3, %xmm1
orpd %xmm2, %xmm1
movupd %xmm1, 0x40(%rbx)
movsd %xmm0, 0x50(%rbx)
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x798a
jmp 0x798a
jmp 0x798a
movq %rax, %r14
leaq 0x20(%rbx), %rdi
callq 0x3b4a
addq $0x10, %rbx
movq %rbx, %rdi
callq 0x3b4a
movq %r14, %rdi
callq 0x3300