// File: 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 %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0xe520
movq -0x150(%rbp), %rdi
leaq 0x1012b(%rip), %rax     # 0x21d70
movq %rax, (%rdi)
addq $0x8, %rdi
movq %rdi, -0x160(%rbp)
callq 0x121c0
movq -0x150(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x158(%rbp)
callq 0x121c0
movq -0x150(%rbp), %rdi
addq $0x28, %rdi
callq 0x4890
jmp 0x11c81
callq 0x5970
movsd %xmm0, -0x168(%rbp)
jmp 0x11c90
movsd -0x168(%rbp), %xmm0
movsd 0x54a8(%rip), %xmm1     # 0x17148
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x16c(%rbp)
testl %eax, %eax
je 0x11d00
jmp 0x11cba
movl -0x16c(%rbp), %eax
subl $0x1, %eax
je 0x11d21
jmp 0x11cc7
movl -0x16c(%rbp), %eax
subl $0x2, %eax
je 0x11d42
jmp 0x11d63
movq -0x158(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x47a0
movq -0x160(%rbp), %rdi
callq 0x47a0
jmp 0x11ff1
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x121e0
jmp 0x11d1f
jmp 0x11d65
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x12220
jmp 0x11d40
jmp 0x11d65
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x12260
jmp 0x11d61
jmp 0x11d63
jmp 0x11d65
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0x122a0
movq %rax, -0x178(%rbp)
jmp 0x11d8b
movq -0x178(%rbp), %rax
movl %eax, -0x70(%rbp)
cmpl $0x1, -0x70(%rbp)
jne 0x11dcf
leaq -0x8(%rbp), %rdi
callq 0x49b0
movq -0x150(%rbp), %rdi
movq %rax, %rsi
addq $0x18, %rdi
callq 0x122e0
movq -0x150(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rdi
callq 0x122e0
jmp 0x11ef3
cmpl $0x2, -0x70(%rbp)
jne 0x11e2a
leaq -0x8(%rbp), %rdi
callq 0x49b0
movq -0x150(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rdi
callq 0x122e0
leaq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x12310
movq %rax, -0x88(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x49b0
movq -0x150(%rbp), %rdi
movq %rax, %rsi
addq $0x18, %rdi
callq 0x122e0
jmp 0x11ef1
movl -0x70(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movslq %eax, %rsi
leaq -0x8(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x12310
movq -0x180(%rbp), %rsi
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x12350
jmp 0x11e6c
movq -0x150(%rbp), %rdi
addq $0x8, %rdi
leaq -0x98(%rbp), %rsi
movq %rsi, -0x188(%rbp)
callq 0x123d0
movq -0x188(%rbp), %rdi
callq 0x9d70
movl -0x70(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movslq %eax, %rsi
leaq -0x8(%rbp), %rdi
callq 0x12310
movq %rax, -0xb8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
leaq -0x10(%rbp), %rdx
callq 0x12400
jmp 0x11ece
movq -0x150(%rbp), %rdi
addq $0x18, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x123d0
leaq -0xb0(%rbp), %rdi
callq 0x9d70
jmp 0x11ef3
leaq -0xe8(%rbp), %rdi
callq 0x4890
jmp 0x11f01
leaq -0x118(%rbp), %rdi
callq 0x4890
jmp 0x11f0f
movq -0x150(%rbp), %rdi
addq $0x8, %rdi
callq 0x4730
movq %rax, %rdi
movq (%rdi), %rax
movq 0x8(%rax), %rax
leaq -0xe8(%rbp), %rsi
callq *%rax
movb %al, -0x189(%rbp)
jmp 0x11f3a
movb -0x189(%rbp), %al
testb $0x1, %al
jne 0x11f46
jmp 0x11f7d
movq -0x150(%rbp), %rdi
addq $0x18, %rdi
callq 0x4730
movq %rax, %rdi
movq (%rdi), %rax
movq 0x8(%rax), %rax
leaq -0x118(%rbp), %rsi
callq *%rax
movb %al, -0x18a(%rbp)
jmp 0x11f71
movb -0x18a(%rbp), %al
testb $0x1, %al
jne 0x11fb0
jmp 0x11f7d
movq 0x1006c(%rip), %rdi     # 0x21ff0
leaq 0x5584(%rip), %rsi      # 0x1750f
callq 0x3210
movq %rax, -0x198(%rbp)
jmp 0x11f99
movq -0x198(%rbp), %rdi
movq 0x10009(%rip), %rsi     # 0x21fb0
callq 0x3240
jmp 0x11fae
jmp 0x11fb0
leaq -0x148(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
leaq -0x118(%rbp), %rdx
callq 0x49d0
jmp 0x11fcc
movq -0x150(%rbp), %rdi
addq $0x28, %rdi
leaq -0x148(%rbp), %rsi
movl $0x30, %edx
callq 0x31a0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x3390
nopw (%rax,%rax)