// File: /workspace/github_cpp_makefile_star2_O3/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 $0x128, %rsp            # imm = 0x128
movl $0x46e46, %edi          # imm = 0x46E46
callq 0x2180
xorl %eax, %eax
leaq 0x40(%rsp), %rbx
movq %rax, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
leaq 0x90(%rsp), %r14
movq %rax, 0x10(%r14)
movaps %xmm0, (%r14)
movl $0xf4240, %ebp          # imm = 0xF4240
leaq 0x20(%rsp), %r15
movq %rsp, %r12
callq 0x20f0
movaps %xmm0, 0x60(%rsp)
callq 0x20f0
movaps %xmm0, 0x80(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movapd 0x60(%rsp), %xmm1
unpcklpd 0x80(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
movlpd %xmm1, 0x20(%rsp)
movss %xmm0, 0x28(%rsp)
callq 0x20f0
movaps %xmm0, 0x60(%rsp)
callq 0x20f0
movaps %xmm0, 0x80(%rsp)
callq 0x20f0
movsd %xmm0, 0xb0(%rsp)
callq 0x20f0
movaps %xmm0, 0x70(%rsp)
callq 0x20f0
movaps %xmm0, 0x110(%rsp)
callq 0x20f0
movsd %xmm0, 0xc8(%rsp)
movq 0x48(%rsp), %rsi
movq %rsi, %r13
subq 0x40(%rsp), %r13
cmpq 0x50(%rsp), %rsi
je 0x24d1
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
movq 0x48(%rsp), %rsi
addq $0xc, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x24e1
movq %rbx, %rdi
movq %r15, %rdx
callq 0x2bd6
movq 0x48(%rsp), %rsi
movapd 0x60(%rsp), %xmm0
unpcklpd 0x80(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
cvtpd2ps %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsd2ss 0xb0(%rsp), %xmm1
mulps 0x5b09(%rip), %xmm0     # 0x8010
mulss 0x5b31(%rip), %xmm1     # 0x8040
movsd 0x20(%rsp), %xmm2
addps %xmm0, %xmm2
addss 0x28(%rsp), %xmm1
movlps %xmm2, (%rsp)
movss %xmm1, 0x8(%rsp)
cmpq 0x50(%rsp), %rsi
je 0x254d
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsi)
movq (%rsp), %rax
movq %rax, (%rsi)
movq 0x48(%rsp), %rsi
addq $0xc, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x255d
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2d4c
movq 0x48(%rsp), %rsi
movapd 0x70(%rsp), %xmm0
unpcklpd 0x110(%rsp), %xmm0      # xmm0 = xmm0[0],mem[0]
cvtpd2ps %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsd2ss 0xc8(%rsp), %xmm1
mulps 0x5a8d(%rip), %xmm0     # 0x8010
mulss 0x5ab5(%rip), %xmm1     # 0x8040
movsd 0x20(%rsp), %xmm2
addps %xmm0, %xmm2
addss 0x28(%rsp), %xmm1
movlps %xmm2, (%rsp)
movss %xmm1, 0x8(%rsp)
cmpq 0x50(%rsp), %rsi
je 0x25c1
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsi)
movq (%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x48(%rsp)
jmp 0x25cc
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2d4c
shrq $0x2, %r13
imull $0xaaaaaaab, %r13d, %eax # imm = 0xAAAAAAAB
leal 0x1(%rax), %ecx
movl %eax, (%rsp)
addl $0x2, %eax
movl %ecx, 0x4(%rsp)
movl %eax, 0x8(%rsp)
movq 0x98(%rsp), %rsi
cmpq 0xa0(%rsp), %rsi
je 0x2613
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsi)
movq (%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x98(%rsp)
jmp 0x261e
movq %r14, %rdi
movq %r12, %rdx
callq 0x2e4a
decl %ebp
jne 0x2415
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rbx
movaps %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movl $0xf4240, %ebp          # imm = 0xF4240
movq %rsp, %r14
callq 0x20f0
mulsd 0x59fa(%rip), %xmm0     # 0x8048
cvttsd2si %xmm0, %r15d
callq 0x20f0
movapd %xmm0, 0x60(%rsp)
callq 0x20f0
movslq %r15d, %rax
movq 0x90(%rsp), %rsi
leaq (%rax,%rax,2), %rdi
movslq (%rsi,%rdi,4), %rcx
movq 0x40(%rsp), %rax
leaq (%rcx,%rcx,2), %rcx
movss 0x8(%rax,%rcx,4), %xmm1
movslq 0x4(%rsi,%rdi,4), %rdx
leaq (%rdx,%rdx,2), %rdx
movslq 0x8(%rsi,%rdi,4), %rsi
movapd 0x60(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
cvtpd2ps %xmm2, %xmm2
movapd %xmm2, %xmm0
shufps $0x55, %xmm2, %xmm0     # xmm0 = xmm0[1,1],xmm2[1,1]
addss %xmm2, %xmm0
xorl %edi, %edi
ucomiss 0x598e(%rip), %xmm0     # 0x8044
leaq (%rsi,%rsi,2), %rsi
setb %dil
movaps 0x595b(%rip), %xmm3     # 0x8020
subps %xmm2, %xmm3
movd %edi, %xmm0
pshufd $0x50, %xmm0, %xmm0     # xmm0 = xmm0[0,0,1,1]
pslld $0x1f, %xmm0
psrad $0x1f, %xmm0
andps %xmm0, %xmm2
pandn %xmm3, %xmm0
por %xmm2, %xmm0
movss 0x8(%rax,%rsi,4), %xmm2
movss 0x8(%rax,%rdx,4), %xmm3
unpcklps %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movaps %xmm1, %xmm2
shufps $0x0, %xmm1, %xmm2      # xmm2 = xmm2[0,0],xmm1[0,0]
subps %xmm2, %xmm3
mulps %xmm0, %xmm3
addss %xmm3, %xmm1
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
addss %xmm1, %xmm3
movaps %xmm3, 0x60(%rsp)
movsd (%rax,%rcx,4), %xmm1
movsd (%rax,%rdx,4), %xmm2
movsd (%rax,%rsi,4), %xmm3
subps %xmm1, %xmm2
pshufd $0x0, %xmm0, %xmm4      # xmm4 = xmm0[0,0,0,0]
mulps %xmm2, %xmm4
addps %xmm1, %xmm4
subps %xmm1, %xmm3
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
mulps %xmm3, %xmm0
addps %xmm4, %xmm0
movaps %xmm0, 0x80(%rsp)
callq 0x20f0
mulsd 0x58fd(%rip), %xmm0     # 0x8050
mulsd 0x58fd(%rip), %xmm0     # 0x8058
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
callq 0x20f0
movaps %xmm0, 0x70(%rsp)
callq 0x20f0
movaps 0x70(%rsp), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvtpd2ps %xmm1, %xmm0
movapd %xmm0, 0x70(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps 0xb0(%rsp), %xmm2
movaps %xmm2, %xmm1
unpcklps %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
mulps 0x70(%rsp), %xmm1
addps 0x80(%rsp), %xmm1
mulss %xmm2, %xmm0
addss 0x60(%rsp), %xmm0
movlps %xmm1, (%rsp)
movss %xmm0, 0x8(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x27e2
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsi)
movq (%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x28(%rsp)
jmp 0x27ed
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2d4c
decl %ebp
jne 0x2641
xorpd %xmm0, %xmm0
movq %rsp, %rdi
movapd %xmm0, (%rdi)
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
movq $0x0, 0x10(%rdi)
sarq $0x2, %rsi
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rbx, %rsi
callq 0x2b2e
leaq 0xf0(%rsp), %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
sarq $0x2, %rsi
imulq %rbx, %rsi
callq 0x2b72
xorpd %xmm0, %xmm0
leaq 0xd0(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
sarq $0x2, %rsi
imulq %rbx, %rsi
callq 0x2ba4
callq 0x2040
movq %rax, %r15
movq 0x40(%rsp), %rdi
leaq 0x4(%rdi), %rsi
leaq 0x8(%rdi), %rdx
movq 0x90(%rsp), %r8
movq 0x98(%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 0x3414
addq $0x20, %rsp
movq %rax, %rbx
callq 0x2040
movq %rax, %r14
movq (%rsp), %rsi
leaq 0x4(%rsi), %rdx
leaq 0x8(%rsi), %rcx
movq 0xd0(%rsp), %r9
movq 0x20(%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 0x120(%rsp)
pushq %r12
callq 0x39ac
addq $0x40, %rsp
callq 0x2040
movq %rax, %r12
movq 0x8698(%rip), %rdi      # 0xafd8
leaq 0x5721(%rip), %rsi      # 0x8068
movl $0x13, %edx
callq 0x2130
movq %r14, %rax
subq %r15, %rax
cvtsi2sd %rax, %xmm0
divsd 0x56fc(%rip), %xmm0     # 0x8060
movq 0x866d(%rip), %rdi      # 0xafd8
callq 0x2160
movq %rax, %r15
leaq 0x5702(%rip), %rsi      # 0x807c
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r15, %rdi
callq 0x2060
movq 0x8642(%rip), %rdi      # 0xafd8
leaq 0x56e1(%rip), %rsi      # 0x807e
movl $0xe, %edx
callq 0x2130
movq 0x862a(%rip), %rdi      # 0xafd8
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x2190
movq %rax, %r15
leaq 0x56cb(%rip), %rsi      # 0x808d
movl $0x9, %edx
movq %rax, %rdi
callq 0x2130
subq %r14, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
divsd 0x567e(%rip), %xmm0     # 0x8060
movq %r15, %rdi
movsd %xmm0, 0x60(%rsp)
callq 0x2160
movq %rax, %r14
leaq 0x5682(%rip), %rsi      # 0x807c
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq 0x85c2(%rip), %rdi      # 0xafd8
leaq 0x567a(%rip), %rsi      # 0x8097
movl $0x9, %edx
callq 0x2130
movsd 0x60(%rsp), %xmm0
divsd 0x5613(%rip), %xmm0     # 0x8048
movq 0x859c(%rip), %rdi      # 0xafd8
callq 0x2160
movq %rax, %r14
leaq 0x5656(%rip), %rsi      # 0x80a1
movl $0xe, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x3cb1
jmp 0x2a6e
jmp 0x2a6e
jmp 0x2a6e
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x2a93
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2a93
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x2aba
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2aba
movq %rax, %rbx
jmp 0x2ad0
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2ad0
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2af2
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2af2
jmp 0x2aef
jmp 0x2aef
jmp 0x2aef
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2b0f
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2b26
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21a0