// File: /workspace/github_cpp_makefile_star2_O1/ingowald[P]closestSurfacePointQueries/examples/aosExample.cpp
// Function: main
int main(int ac, char **av)
  {
    srand48(290374);

    int numTriangles = 1000000;
    float maxEdgeLen = 1.f/(powf(numTriangles,1.f/3.f));
    int numPoints    = 1000000;
    
    // the triangles we're querying to
    std::vector<vec3f> vertex;
    std::vector<vec3i> index;

    for (int i=0;i<numTriangles;i++) {
      const vec3f P(drand48(),drand48(),drand48());
      const vec3f e0 = maxEdgeLen * vec3f(drand48(),drand48(),drand48());
      const vec3f e1 = maxEdgeLen * vec3f(drand48(),drand48(),drand48());
      const int vIdx = vertex.size();
      vertex.push_back(P);
      vertex.push_back(P+e0);
      vertex.push_back(P+e1);
      index.push_back(vec3i(vIdx+0,vIdx+1,vIdx+2));
    }
    // loadTriangleMesh(vertex,index,av[1]);
    
    // the points we're query'ing
    std::vector<vec3f> queryPoint;
    for (int i=0;i<numPoints;i++) {
      int triID = int(drand48()*numTriangles);
      float u = drand48();
      float v = drand48();
      const vec3f A = vertex[index[triID].x];
      const vec3f B = vertex[index[triID].y];
      const vec3f C = vertex[index[triID].z];
      if (u+v >= 1.f) { u = 1-u; v = 1-v; }
      const vec3f pointOnTri = A+u*(B-A)+v*(C-A);
      queryPoint.push_back(pointOnTri + float(0.1f*drand48()*maxEdgeLen)*vec3f(drand48(),drand48(),drand48()));
    }

    // the result of our queries
    std::vector<vec3f>   result_pos;    result_pos.resize(queryPoint.size());
    std::vector<int32_t> result_primID; result_primID.resize(queryPoint.size());
    std::vector<float>   result_dist;   result_dist.resize(queryPoint.size());

    auto begin = std::chrono::system_clock::now();
    // create the actual scene:
    distance_query_scene scene
      = rtdqNewTriangleMeshfi(&vertex[0].x,&vertex[0].y,&vertex[0].z,3,
                              &index[0].x,&index[0].y,&index[0].z,3,
                              index.size());
    auto done_build = std::chrono::system_clock::now();

    // perform the queries - all together, in a single thread
    rtdqComputeClosestPointsfi(scene,
                             &result_pos[0].x,&result_pos[0].y,&result_pos[0].z,3,
                             &result_dist[0],1,
                             &result_primID[0],1,
                             &queryPoint[0].x,&queryPoint[0].y,&queryPoint[0].z,3,
                             numPoints);
    auto done_all = std::chrono::system_clock::now();

    std::chrono::duration<double> buildTime = done_build - begin;
    std::chrono::duration<double> queryTime = done_all   - done_build;
    std::cout << "time to build tree " << buildTime.count() << "s" << std::endl;
    std::cout << "time to query " << numPoints << " points: " << queryTime.count() << "s" << std::endl;
    std::cout << "(this is " << (queryTime.count()/numPoints) << " seconds/prim)" << std::endl;

    rtdqDestroy(scene);
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl $0x46e46, %edi          # imm = 0x46E46
callq 0x2180
xorl %eax, %eax
leaq 0x50(%rsp), %rbx
movq %rax, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
leaq 0x80(%rsp), %r14
movq %rax, 0x10(%r14)
movaps %xmm0, (%r14)
movl $0xf4240, %ebp          # imm = 0xF4240
movq %rsp, %r15
leaq 0x20(%rsp), %r12
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x40(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x38(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movss 0x40(%rsp), %xmm1
movss %xmm1, (%rsp)
movss 0x38(%rsp), %xmm1
movss %xmm1, 0x4(%rsp)
movss %xmm0, 0x8(%rsp)
callq 0x20f0
movsd %xmm0, 0x40(%rsp)
callq 0x20f0
movsd %xmm0, 0x38(%rsp)
callq 0x20f0
movsd %xmm0, 0x78(%rsp)
callq 0x20f0
movsd %xmm0, 0x70(%rsp)
callq 0x20f0
movsd %xmm0, 0xb0(%rsp)
callq 0x20f0
movsd %xmm0, 0xa0(%rsp)
movq 0x58(%rsp), %rsi
movq %rsi, %r13
subq 0x50(%rsp), %r13
cmpq 0x60(%rsp), %rsi
je 0x24db
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsi)
movq (%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x58(%rsp)
jmp 0x24e6
movq %rbx, %rdi
movq %r15, %rdx
callq 0x2c38
xorps %xmm0, %xmm0
cvtsd2ss 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss 0x38(%rsp), %xmm1
cvtsd2ss 0x78(%rsp), %xmm2
movss 0x6afe(%rip), %xmm3     # 0x9004
mulss %xmm3, %xmm0
mulss %xmm3, %xmm1
mulss %xmm3, %xmm2
addss (%rsp), %xmm0
addss 0x4(%rsp), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
addss 0x8(%rsp), %xmm2
movlps %xmm0, 0x20(%rsp)
movss %xmm2, 0x28(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x2554
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x58(%rsp)
jmp 0x255f
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2dae
xorps %xmm0, %xmm0
cvtsd2ss 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss 0xb0(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtsd2ss 0xa0(%rsp), %xmm2
movss 0x6a7c(%rip), %xmm3     # 0x9004
mulss %xmm3, %xmm0
mulss %xmm3, %xmm1
mulss %xmm3, %xmm2
addss (%rsp), %xmm0
addss 0x4(%rsp), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
addss 0x8(%rsp), %xmm2
movlps %xmm0, 0x20(%rsp)
movss %xmm2, 0x28(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x25d6
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x58(%rsp)
jmp 0x25e1
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2dae
shrq $0x2, %r13
imull $0xaaaaaaab, %r13d, %eax # imm = 0xAAAAAAAB
leal 0x1(%rax), %ecx
movl %eax, 0x20(%rsp)
addl $0x2, %eax
movl %ecx, 0x24(%rsp)
movl %eax, 0x28(%rsp)
movq 0x88(%rsp), %rsi
cmpq 0x90(%rsp), %rsi
je 0x262a
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x88(%rsp)
jmp 0x2635
movq %r14, %rdi
movq %r12, %rdx
callq 0x2eac
decl %ebp
jne 0x2425
xorps %xmm0, %xmm0
movq %rsp, %rbx
movaps %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movl $0xf4240, %ebp          # imm = 0xF4240
leaq 0x20(%rsp), %r14
callq 0x20f0
mulsd 0x69ab(%rip), %xmm0     # 0x9010
cvttsd2si %xmm0, %r15d
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
callq 0x20f0
movaps 0x40(%rsp), %xmm13
cvtsd2ss %xmm0, %xmm0
movaps %xmm13, %xmm7
addss %xmm0, %xmm7
movss 0x6970(%rip), %xmm9     # 0x9008
ucomiss %xmm9, %xmm7
movaps %xmm9, %xmm8
subss %xmm13, %xmm8
jb 0x26b2
movaps %xmm9, %xmm1
subss %xmm0, %xmm1
movaps %xmm1, %xmm0
movslq %r15d, %rax
movq 0x80(%rsp), %rcx
leaq (%rax,%rax,2), %rax
movslq (%rcx,%rax,4), %rdx
movq 0x50(%rsp), %rsi
leaq (%rdx,%rdx,2), %rdx
movss (%rsi,%rdx,4), %xmm6
movss 0x4(%rsi,%rdx,4), %xmm5
movss 0x8(%rsi,%rdx,4), %xmm4
movslq 0x4(%rcx,%rax,4), %rdx
leaq (%rdx,%rdx,2), %rdx
movss (%rsi,%rdx,4), %xmm3
movss 0x4(%rsi,%rdx,4), %xmm2
movss 0x8(%rsi,%rdx,4), %xmm1
movslq 0x8(%rcx,%rax,4), %rax
leaq (%rax,%rax,2), %rax
movss (%rsi,%rax,4), %xmm11
movss 0x4(%rsi,%rax,4), %xmm10
movss 0x8(%rsi,%rax,4), %xmm12
cmpnless %xmm7, %xmm9
movaps %xmm9, %xmm7
andnps %xmm8, %xmm7
andps %xmm13, %xmm9
orps %xmm7, %xmm9
subss %xmm6, %xmm3
subss %xmm5, %xmm2
subss %xmm4, %xmm1
mulss %xmm9, %xmm3
mulss %xmm9, %xmm2
mulss %xmm9, %xmm1
addss %xmm6, %xmm3
addss %xmm5, %xmm2
addss %xmm4, %xmm1
subss %xmm6, %xmm11
subss %xmm5, %xmm10
subss %xmm4, %xmm12
mulss %xmm0, %xmm11
addss %xmm3, %xmm11
movss %xmm11, 0x40(%rsp)
mulss %xmm0, %xmm10
addss %xmm2, %xmm10
movss %xmm10, 0x38(%rsp)
mulss %xmm0, %xmm12
addss %xmm1, %xmm12
movss %xmm12, 0x78(%rsp)
callq 0x20f0
mulsd 0x6876(%rip), %xmm0     # 0x9018
mulsd 0x6876(%rip), %xmm0     # 0x9020
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x70(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps 0xb0(%rsp), %xmm1
movss 0x70(%rsp), %xmm2
mulss %xmm2, %xmm1
movaps 0xa0(%rsp), %xmm3
mulss %xmm2, %xmm3
mulss %xmm2, %xmm0
addss 0x78(%rsp), %xmm0
addss 0x40(%rsp), %xmm1
addss 0x38(%rsp), %xmm3
unpcklps %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movlps %xmm1, 0x20(%rsp)
movss %xmm0, 0x28(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x2844
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rsp)
jmp 0x284f
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2dae
decl %ebp
jne 0x2658
xorpd %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movapd %xmm0, (%rdi)
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
movq $0x0, 0x10(%rdi)
sarq $0x2, %rsi
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rbx, %rsi
callq 0x2b90
leaq 0xe0(%rsp), %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x2, %rsi
imulq %rbx, %rsi
callq 0x2bd4
xorpd %xmm0, %xmm0
leaq 0xc0(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x2, %rsi
imulq %rbx, %rsi
callq 0x2c06
callq 0x2040
movq %rax, %r15
movq 0x50(%rsp), %rdi
leaq 0x4(%rdi), %rsi
leaq 0x8(%rdi), %rdx
movq 0x80(%rsp), %r8
movq 0x88(%rsp), %rax
leaq 0x4(%r8), %r9
leaq 0x8(%r8), %r10
subq %r8, %rax
sarq $0x2, %rax
imulq %rbx, %rax
subq $0x8, %rsp
movl $0x3, %ecx
pushq %rax
pushq $0x3
pushq %r10
callq 0x3464
addq $0x20, %rsp
movq %rax, %rbx
callq 0x2040
movq %rax, %r14
movq 0x20(%rsp), %rsi
leaq 0x4(%rsi), %rdx
leaq 0x8(%rsi), %rcx
movq 0xc0(%rsp), %r9
movq (%rsp), %rax
leaq 0x4(%rax), %r10
leaq 0x8(%rax), %r11
movl $0x1, %r12d
movl $0x3, %r8d
movq %rbx, %rdi
pushq $0xf4240                # imm = 0xF4240
pushq $0x3
pushq %r11
pushq %r10
pushq %rax
pushq %r12
pushq 0x110(%rsp)
pushq %r12
callq 0x3a8c
addq $0x40, %rsp
callq 0x2040
movq %rax, %r12
movq 0x9637(%rip), %rdi      # 0xbfd8
leaq 0x6698(%rip), %rsi      # 0x9040
movl $0x13, %edx
callq 0x2130
movq %r14, %rax
subq %r15, %rax
cvtsi2sd %rax, %xmm0
divsd 0x6663(%rip), %xmm0     # 0x9028
movq 0x960c(%rip), %rdi      # 0xbfd8
callq 0x2160
movq %rax, %r15
leaq 0x6679(%rip), %rsi      # 0x9054
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r15, %rdi
callq 0x2060
movq 0x95e1(%rip), %rdi      # 0xbfd8
leaq 0x6658(%rip), %rsi      # 0x9056
movl $0xe, %edx
callq 0x2130
movq 0x95c9(%rip), %rdi      # 0xbfd8
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x2190
movq %rax, %r15
leaq 0x6642(%rip), %rsi      # 0x9065
movl $0x9, %edx
movq %rax, %rdi
callq 0x2130
subq %r14, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
divsd 0x65e5(%rip), %xmm0     # 0x9028
movq %r15, %rdi
movsd %xmm0, 0x40(%rsp)
callq 0x2160
movq %rax, %r14
leaq 0x65f9(%rip), %rsi      # 0x9054
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq 0x9561(%rip), %rdi      # 0xbfd8
leaq 0x65f1(%rip), %rsi      # 0x906f
movl $0x9, %edx
callq 0x2130
movsd 0x40(%rsp), %xmm0
divsd 0x657a(%rip), %xmm0     # 0x9010
movq 0x953b(%rip), %rdi      # 0xbfd8
callq 0x2160
movq %rax, %r14
leaq 0x65cd(%rip), %rsi      # 0x9079
movl $0xe, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x3d99
jmp 0x2acf
jmp 0x2acf
jmp 0x2acf
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x2af4
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2af4
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x2b1b
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2b1b
movq %rax, %rbx
jmp 0x2b32
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2b32
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2b53
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2b53
jmp 0x2b50
jmp 0x2b50
jmp 0x2b50
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x2b70
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2b87
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21a0
nop