// File: /workspace/github_cpp_makefile_star2_O3/ingowald[P]closestSurfacePointQueries/examples/soaExample.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<float>   vertex_x;
    std::vector<float>   vertex_y;
    std::vector<float>   vertex_z;
    std::vector<int32_t> index_x;
    std::vector<int32_t> index_y;
    std::vector<int32_t> index_z;
    
    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_x.size();
      vertex_x.push_back(P.x);
      vertex_x.push_back(P.x+e0.x);
      vertex_x.push_back(P.x+e1.x);

      vertex_y.push_back(P.y);
      vertex_y.push_back(P.y+e0.y);
      vertex_y.push_back(P.y+e1.y);

      vertex_z.push_back(P.z);
      vertex_z.push_back(P.z+e0.z);
      vertex_z.push_back(P.z+e1.z);

      index_x.push_back(vIdx+0);
      index_y.push_back(vIdx+1);
      index_z.push_back(vIdx+2);
    }
    
    // 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_x[index_x[triID]],
                    vertex_y[index_x[triID]],
                    vertex_z[index_x[triID]]);
      const vec3f B(vertex_x[index_y[triID]],
                    vertex_y[index_y[triID]],
                    vertex_z[index_y[triID]]);
      const vec3f C(vertex_x[index_z[triID]],
                    vertex_y[index_z[triID]],
                    vertex_z[index_z[triID]]);
      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_x[0],&vertex_y[0],&vertex_z[0],1,
                              &index_x[0],&index_y[0],&index_z[0],1,
                              index_x.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 $0x1c8, %rsp            # imm = 0x1C8
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 0x90(%rsp), %r14
movq %rax, 0x10(%r14)
movaps %xmm0, (%r14)
leaq 0x70(%rsp), %r15
movq %rax, 0x10(%r15)
movaps %xmm0, (%r15)
leaq 0xb0(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps %xmm0, (%rcx)
leaq 0x100(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps %xmm0, (%rcx)
leaq 0xe0(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps %xmm0, (%rcx)
movl $0xf4240, %r13d         # imm = 0xF4240
leaq 0x20(%rsp), %r12
callq 0x20f0
movaps %xmm0, 0x40(%rsp)
callq 0x20f0
movaps %xmm0, 0xd0(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movapd 0x40(%rsp), %xmm1
unpcklpd 0xd0(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
movlpd %xmm1, (%rsp)
movss %xmm0, 0x8(%rsp)
callq 0x20f0
movsd %xmm0, 0x40(%rsp)
callq 0x20f0
movsd %xmm0, 0x150(%rsp)
callq 0x20f0
movsd %xmm0, 0x130(%rsp)
callq 0x20f0
movsd %xmm0, 0xd0(%rsp)
callq 0x20f0
movsd %xmm0, 0x140(%rsp)
callq 0x20f0
movsd %xmm0, 0x120(%rsp)
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rax
movq %rsi, %rbp
subq 0x50(%rsp), %rbp
cmpq %rax, %rsi
je 0x24e7
movss (%rsp), %xmm0
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2501
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x2e86
movss (%rsp), %xmm0
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rax
xorps %xmm1, %xmm1
cvtsd2ss 0x40(%rsp), %xmm1
mulss 0x5af2(%rip), %xmm1     # 0x8004
addss %xmm0, %xmm1
movss %xmm1, 0x20(%rsp)
cmpq %rax, %rsi
je 0x2530
movss %xmm1, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2545
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2fc6
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rax
xorps %xmm0, %xmm0
cvtsd2ss 0xd0(%rsp), %xmm0
mulss 0x5aab(%rip), %xmm0     # 0x8004
addss (%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
cmpq %rax, %rsi
je 0x2578
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2583
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2fc6
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rax
cmpq %rax, %rsi
je 0x25b0
movss 0x4(%rsp), %xmm0
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x25d3
movq %r14, %rdi
leaq 0x4(%rsp), %rdx
callq 0x2e86
movss 0x4(%rsp), %xmm0
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rax
xorps %xmm1, %xmm1
cvtsd2ss 0x150(%rsp), %xmm1
mulss 0x5a1d(%rip), %xmm1     # 0x8004
addss %xmm0, %xmm1
movss %xmm1, 0x20(%rsp)
cmpq %rax, %rsi
je 0x2608
movss %xmm1, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x2623
movq %r14, %rdi
movq %r12, %rdx
callq 0x2fc6
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
cvtsd2ss 0x140(%rsp), %xmm0
mulss 0x59cd(%rip), %xmm0     # 0x8004
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
cmpq %rax, %rsi
je 0x265a
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x2665
movq %r14, %rdi
movq %r12, %rdx
callq 0x2fc6
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rax
cmpq %rax, %rsi
je 0x268c
movss 0x8(%rsp), %xmm0
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x26ac
movq %r15, %rdi
leaq 0x8(%rsp), %rdx
callq 0x2e86
movss 0x8(%rsp), %xmm0
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rax
xorps %xmm1, %xmm1
cvtsd2ss 0x130(%rsp), %xmm1
mulss 0x5944(%rip), %xmm1     # 0x8004
addss %xmm0, %xmm1
movss %xmm1, 0x20(%rsp)
cmpq %rax, %rsi
je 0x26de
movss %xmm1, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x26f6
movq %r15, %rdi
movq %r12, %rdx
callq 0x2fc6
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rax
xorps %xmm0, %xmm0
cvtsd2ss 0x120(%rsp), %xmm0
mulss 0x58fa(%rip), %xmm0     # 0x8004
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
cmpq %rax, %rsi
je 0x272a
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x2735
movq %r15, %rdi
movq %r12, %rdx
callq 0x2fc6
shrq $0x2, %rbp
movl %ebp, 0x20(%rsp)
movq 0xb8(%rsp), %rsi
cmpq 0xc0(%rsp), %rsi
je 0x275f
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xb8(%rsp)
jmp 0x276f
leaq 0xb0(%rsp), %rdi
movq %r12, %rdx
callq 0x30a6
leal 0x1(%rbp), %eax
movl %eax, 0x20(%rsp)
movq 0x108(%rsp), %rsi
cmpq 0x110(%rsp), %rsi
je 0x2798
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x108(%rsp)
jmp 0x27a8
leaq 0x100(%rsp), %rdi
movq %r12, %rdx
callq 0x30a6
addl $0x2, %ebp
movl %ebp, 0x20(%rsp)
movq 0xe8(%rsp), %rsi
cmpq 0xf0(%rsp), %rsi
je 0x27d1
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xe8(%rsp)
jmp 0x27e1
leaq 0xe0(%rsp), %rdi
movq %r12, %rdx
callq 0x30a6
decl %r13d
jne 0x242d
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 0x57fe(%rip), %xmm0     # 0x8010
cvttsd2si %xmm0, %r15d
callq 0x20f0
movapd %xmm0, 0x40(%rsp)
callq 0x20f0
movslq %r15d, %rsi
movq 0xb0(%rsp), %rax
movslq (%rax,%rsi,4), %rdx
movq 0x50(%rsp), %rax
movq 0x90(%rsp), %rcx
movq 0x70(%rsp), %r8
movss (%r8,%rdx,4), %xmm1
movq 0x100(%rsp), %rdi
movslq (%rdi,%rsi,4), %rdi
movq 0xe0(%rsp), %r9
movapd 0x40(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
cvtpd2ps %xmm2, %xmm0
movslq (%r9,%rsi,4), %rsi
movapd %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
addss %xmm0, %xmm2
xorl %r9d, %r9d
ucomiss 0x577e(%rip), %xmm2     # 0x8008
setb %r9b
movaps 0x579b(%rip), %xmm2     # 0x8030
subps %xmm0, %xmm2
movd %r9d, %xmm3
pshufd $0x50, %xmm3, %xmm3     # xmm3 = xmm3[0,0,1,1]
pslld $0x1f, %xmm3
psrad $0x1f, %xmm3
andps %xmm3, %xmm0
pandn %xmm2, %xmm3
por %xmm0, %xmm3
movdqa %xmm3, 0x40(%rsp)
movss (%r8,%rsi,4), %xmm0
movss (%r8,%rdi,4), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movaps %xmm1, %xmm0
shufps $0x0, %xmm1, %xmm0      # xmm0 = xmm0[0,0],xmm1[0,0]
subps %xmm0, %xmm2
mulps %xmm3, %xmm2
addss %xmm2, %xmm1
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
addss %xmm1, %xmm2
movaps %xmm2, 0xd0(%rsp)
movss (%rax,%rdi,4), %xmm1
movss (%rcx,%rdi,4), %xmm0
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x150(%rsp)
movss (%rax,%rdx,4), %xmm1
movss (%rcx,%rdx,4), %xmm0
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x130(%rsp)
movss (%rax,%rsi,4), %xmm1
movss (%rcx,%rsi,4), %xmm0
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd %xmm1, 0x140(%rsp)
callq 0x20f0
mulsd 0x56de(%rip), %xmm0     # 0x8018
mulsd 0x56de(%rip), %xmm0     # 0x8020
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x1a0(%rsp)
callq 0x20f0
movaps %xmm0, 0x120(%rsp)
callq 0x20f0
movaps %xmm0, 0x1b0(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps 0x1a0(%rsp), %xmm5
mulss %xmm5, %xmm0
addss 0xd0(%rsp), %xmm0
movaps 0x130(%rsp), %xmm3
movaps 0x150(%rsp), %xmm4
subps %xmm3, %xmm4
movdqa 0x40(%rsp), %xmm2
pshufd $0x0, %xmm2, %xmm1      # xmm1 = xmm2[0,0,0,0]
mulps %xmm4, %xmm1
addps %xmm3, %xmm1
movaps 0x140(%rsp), %xmm4
subps %xmm3, %xmm4
pshufd $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
mulps %xmm4, %xmm2
addps %xmm1, %xmm2
movapd 0x120(%rsp), %xmm1
unpcklpd 0x1b0(%rsp), %xmm1      # xmm1 = xmm1[0],mem[0]
cvtpd2ps %xmm1, %xmm1
movaps %xmm5, %xmm3
unpcklps %xmm5, %xmm3            # xmm3 = xmm3[0],xmm5[0],xmm3[1],xmm5[1]
mulps %xmm1, %xmm3
addps %xmm2, %xmm3
movlps %xmm3, 0x20(%rsp)
movss %xmm0, 0x28(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x2a10
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rsp)
jmp 0x2a1b
movq %rbx, %rdi
movq %r14, %rdx
callq 0x31e2
decl %ebp
jne 0x2805
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 0x2dde
leaq 0x180(%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 0x2e22
xorpd %xmm0, %xmm0
leaq 0x160(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x2, %rsi
imulq %rbx, %rsi
callq 0x2e54
callq 0x2040
movq %rax, %r15
movq 0x50(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x100(%rsp), %r9
movq 0xb0(%rsp), %r8
movq 0xb8(%rsp), %rax
subq %r8, %rax
sarq $0x2, %rax
subq $0x8, %rsp
movl $0x1, %r12d
movl $0x1, %ecx
pushq %rax
pushq $0x1
pushq 0xf8(%rsp)
callq 0x36ec
addq $0x20, %rsp
movq %rax, %rbx
callq 0x2040
movq %rax, %r14
movq 0x20(%rsp), %rsi
leaq 0x4(%rsi), %rdx
leaq 0x8(%rsi), %rcx
movq 0x160(%rsp), %r9
movq (%rsp), %rax
leaq 0x4(%rax), %r10
leaq 0x8(%rax), %r11
movl $0x3, %r8d
movq %rbx, %rdi
pushq $0xf4240                # imm = 0xF4240
pushq $0x3
pushq %r11
pushq %r10
pushq %rax
pushq %r12
pushq 0x1b0(%rsp)
pushq %r12
callq 0x3c84
addq $0x40, %rsp
callq 0x2040
movq %rax, %r12
movq 0x8465(%rip), %rdi      # 0xafd8
leaq 0x54d6(%rip), %rsi      # 0x8050
movl $0x13, %edx
callq 0x2130
movq %r14, %rax
subq %r15, %rax
cvtsi2sd %rax, %xmm0
divsd 0x5491(%rip), %xmm0     # 0x8028
movq 0x843a(%rip), %rdi      # 0xafd8
callq 0x2160
movq %rax, %r15
leaq 0x54b7(%rip), %rsi      # 0x8064
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r15, %rdi
callq 0x2060
movq 0x840f(%rip), %rdi      # 0xafd8
leaq 0x5496(%rip), %rsi      # 0x8066
movl $0xe, %edx
callq 0x2130
movq 0x83f7(%rip), %rdi      # 0xafd8
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x2190
movq %rax, %r15
leaq 0x5480(%rip), %rsi      # 0x8075
movl $0x9, %edx
movq %rax, %rdi
callq 0x2130
subq %r14, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
divsd 0x5413(%rip), %xmm0     # 0x8028
movq %r15, %rdi
movsd %xmm0, 0x40(%rsp)
callq 0x2160
movq %rax, %r14
leaq 0x5437(%rip), %rsi      # 0x8064
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq 0x838f(%rip), %rdi      # 0xafd8
leaq 0x542f(%rip), %rsi      # 0x807f
movl $0x9, %edx
callq 0x2130
movsd 0x40(%rsp), %xmm0
divsd 0x53a8(%rip), %xmm0     # 0x8010
movq 0x8369(%rip), %rdi      # 0xafd8
callq 0x2160
movq %rax, %r14
leaq 0x540b(%rip), %rsi      # 0x8089
movl $0xe, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x3f89
jmp 0x2ca1
jmp 0x2ca1
jmp 0x2ca1
movq %rax, %rbx
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2cc6
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2cc6
movq %rax, %rbx
movq 0x180(%rsp), %rdi
testq %rdi, %rdi
je 0x2ced
movq 0x190(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2ced
movq %rax, %rbx
jmp 0x2d04
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2d04
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2d31
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2d31
jmp 0x2d2e
jmp 0x2d2e
jmp 0x2d2e
jmp 0x2d2e
jmp 0x2d2e
jmp 0x2d2e
jmp 0x2d2e
jmp 0x2d2e
jmp 0x2d2e
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x2d4e
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x2d6b
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x2d88
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x2da2
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2dbf
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2dd6
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21a0