// File: /workspace/github_cpp_makefile_star2_O3/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 %rbx
subq $0x80, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movq %rdx, 0x48(%rsp)
leaq 0x6986(%rip), %rax      # 0xdd70
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 0x3ce0
mulsd 0x3189(%rip), %xmm0     # 0xa5a0
cvttsd2si %xmm0, %eax
testl %eax, %eax
je 0x7499
cmpl $0x1, %eax
je 0x7465
cmpl $0x2, %eax
jne 0x74cb
cmpq %r15, %r14
je 0x74cb
movq %r15, %rax
subq %r14, %rax
sarq $0x4, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x8a58
movq %r14, %rdi
movq %r15, %rsi
callq 0x8b0b
jmp 0x74cb
cmpq %r15, %r14
je 0x74cb
movq %r15, %rax
subq %r14, %rax
sarq $0x4, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x817f
movq %r14, %rdi
movq %r15, %rsi
callq 0x8232
jmp 0x74cb
cmpq %r15, %r14
je 0x74cb
movq %r15, %rax
subq %r14, %rax
sarq $0x4, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x7850
movq %r14, %rdi
movq %r15, %rsi
callq 0x7903
subq %r14, %r15
shrq $0x4, %r15
cmpl $0x2, %r15d
je 0x750d
cmpl $0x1, %r15d
jne 0x7689
movq (%r14), %rax
movq %rax, 0x18(%rbx)
leaq 0x20(%rbx), %r15
addq $0x8, %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x376c
movq 0x18(%rbx), %rax
movq %rax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
movq %r15, %rsi
jmp 0x7534
movq (%r14), %rax
movq %rax, 0x8(%rbx)
leaq 0x10(%rbx), %rdi
leaq 0x8(%r14), %rsi
callq 0x376c
movq 0x10(%r14), %rax
movq %rax, 0x18(%rbx)
leaq 0x20(%rbx), %rdi
addq $0x18, %r14
movq %r14, %rsi
callq 0x376c
leaq 0x8(%rbx), %rax
xorpd %xmm0, %xmm0
leaq 0x10(%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 (%rax), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
testb %al, %al
je 0x7586
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
leaq 0x50(%rsp), %rsi
callq *0x8(%rax)
testb %al, %al
jne 0x75cc
movq 0x6a63(%rip), %r14      # 0xdff0
leaq 0x3014(%rip), %rsi      # 0xa5a8
movl $0x28, %edx
movq %r14, %rdi
callq 0x31f0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x31b0
movsbl %al, %esi
movq 0x6a31(%rip), %rdi      # 0xdff0
callq 0x3030
movq %rax, %rdi
callq 0x30d0
movsd 0x20(%rsp), %xmm0
movsd 0x60(%rsp), %xmm2
movapd %xmm0, %xmm1
cmpunordsd %xmm0, %xmm1
movapd %xmm1, %xmm3
andpd %xmm2, %xmm3
minsd %xmm0, %xmm2
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
movsd 0x38(%rsp), %xmm2
movsd 0x78(%rsp), %xmm3
movapd %xmm2, %xmm0
cmpunordsd %xmm2, %xmm0
movapd %xmm0, %xmm4
andpd %xmm3, %xmm4
maxsd %xmm2, %xmm3
andnpd %xmm3, %xmm0
orpd %xmm4, %xmm0
movapd 0x10(%rsp), %xmm2
movapd 0x50(%rsp), %xmm3
movapd %xmm3, %xmm4
minpd %xmm2, %xmm4
cmpunordpd %xmm2, %xmm2
andpd %xmm2, %xmm3
andnpd %xmm4, %xmm2
orpd %xmm3, %xmm2
movupd %xmm2, 0x28(%rbx)
movlpd %xmm1, 0x38(%rbx)
movupd 0x28(%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)
movlpd %xmm0, 0x50(%rbx)
addq $0x80, %rsp
popq %rbx
popq %r14
popq %r15
retq
movl %r15d, %eax
shrl $0x1f, %eax
addl %r15d, %eax
sarl %eax
movslq %eax, %r15
shlq $0x4, %r15
addq %r15, %r14
leaq 0x50(%rsp), %r8
movq %r14, (%r8)
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0x7(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x9332
movapd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x10(%rsp)
movq 0x10(%rbx), %rdi
movupd %xmm0, 0x8(%rbx)
testq %rdi, %rdi
je 0x76f7
callq 0x37b6
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x76f7
callq 0x37b6
addq 0x8(%rsp), %r15
leaq 0x50(%rsp), %rcx
movq %r15, (%rcx)
leaq 0x18(%rsp), %rdi
movq $0x0, -0x8(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0x7(%rsp), %rdx
leaq 0x48(%rsp), %r8
callq 0x93b2
movapd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x10(%rsp)
movq 0x20(%rbx), %rdi
movupd %xmm0, 0x18(%rbx)
testq %rdi, %rdi
je 0x7539
callq 0x37b6
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x7539
callq 0x37b6
jmp 0x7539
jmp 0x7768
jmp 0x7768
jmp 0x7768
movq %rax, %r14
movq 0x20(%rbx), %rdi
testq %rdi, %rdi
je 0x7779
callq 0x37b6
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x7787
callq 0x37b6
movq %r14, %rdi
callq 0x32d0
nop