// File: /workspace/github_cpp_makefile_star2_O1/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 $0x178, %rsp            # imm = 0x178
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 0xc0(%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
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, 0xb8(%rsp)
callq 0x20f0
movsd %xmm0, 0x130(%rsp)
callq 0x20f0
movsd %xmm0, 0x38(%rsp)
callq 0x20f0
movsd %xmm0, 0xb0(%rsp)
callq 0x20f0
movsd %xmm0, 0x120(%rsp)
movq 0x58(%rsp), %rsi
movq %rsi, %rbp
subq 0x50(%rsp), %rbp
cmpq 0x60(%rsp), %rsi
je 0x24f7
movss (%rsp), %xmm0
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2502
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x2e62
xorps %xmm0, %xmm0
cvtsd2ss 0x40(%rsp), %xmm0
mulss 0x6af1(%rip), %xmm0     # 0x9004
addss (%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x2539
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2544
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2fa2
xorps %xmm0, %xmm0
cvtsd2ss 0x38(%rsp), %xmm0
mulss 0x6aaf(%rip), %xmm0     # 0x9004
addss (%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x257b
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x58(%rsp)
jmp 0x2586
movq %rbx, %rdi
movq %r12, %rdx
callq 0x2fa2
movq 0x98(%rsp), %rsi
cmpq 0xa0(%rsp), %rsi
je 0x25b0
movss 0x4(%rsp), %xmm0
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x25bd
movq %r14, %rdi
leaq 0x4(%rsp), %rdx
callq 0x2e62
xorps %xmm0, %xmm0
cvtsd2ss 0xb8(%rsp), %xmm0
mulss 0x6a33(%rip), %xmm0     # 0x9004
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x98(%rsp), %rsi
cmpq 0xa0(%rsp), %rsi
je 0x2601
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x260c
movq %r14, %rdi
movq %r12, %rdx
callq 0x2fa2
xorps %xmm0, %xmm0
cvtsd2ss 0xb0(%rsp), %xmm0
mulss 0x69e4(%rip), %xmm0     # 0x9004
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x98(%rsp), %rsi
cmpq 0xa0(%rsp), %rsi
je 0x2650
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x98(%rsp)
jmp 0x265b
movq %r14, %rdi
movq %r12, %rdx
callq 0x2fa2
movq 0x78(%rsp), %rsi
cmpq 0x80(%rsp), %rsi
je 0x267f
movss 0x8(%rsp), %xmm0
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x268c
movq %r15, %rdi
leaq 0x8(%rsp), %rdx
callq 0x2e62
xorps %xmm0, %xmm0
cvtsd2ss 0x130(%rsp), %xmm0
mulss 0x6964(%rip), %xmm0     # 0x9004
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x78(%rsp), %rsi
cmpq 0x80(%rsp), %rsi
je 0x26ca
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x26d5
movq %r15, %rdi
movq %r12, %rdx
callq 0x2fa2
xorps %xmm0, %xmm0
cvtsd2ss 0x120(%rsp), %xmm0
mulss 0x691b(%rip), %xmm0     # 0x9004
addss 0x8(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x78(%rsp), %rsi
cmpq 0x80(%rsp), %rsi
je 0x2713
movss %xmm0, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x78(%rsp)
jmp 0x271e
movq %r15, %rdi
movq %r12, %rdx
callq 0x2fa2
shrq $0x2, %rbp
movl %ebp, 0x20(%rsp)
movq 0xc8(%rsp), %rsi
cmpq 0xd0(%rsp), %rsi
je 0x2748
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xc8(%rsp)
jmp 0x2758
leaq 0xc0(%rsp), %rdi
movq %r12, %rdx
callq 0x3082
leal 0x1(%rbp), %eax
movl %eax, 0x20(%rsp)
movq 0x108(%rsp), %rsi
cmpq 0x110(%rsp), %rsi
je 0x2781
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x108(%rsp)
jmp 0x2791
leaq 0x100(%rsp), %rdi
movq %r12, %rdx
callq 0x3082
addl $0x2, %ebp
movl %ebp, 0x20(%rsp)
movq 0xe8(%rsp), %rsi
cmpq 0xf0(%rsp), %rsi
je 0x27ba
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0xe8(%rsp)
jmp 0x27ca
leaq 0xe0(%rsp), %rdi
movq %r12, %rdx
callq 0x3082
decl %r13d
jne 0x243d
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 0x6815(%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 0x67da(%rip), %xmm9     # 0x9008
ucomiss %xmm9, %xmm7
movaps %xmm9, %xmm8
subss %xmm13, %xmm8
jb 0x2848
movaps %xmm9, %xmm1
subss %xmm0, %xmm1
movaps %xmm1, %xmm0
movslq %r15d, %rax
movq 0xc0(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
movq 0x50(%rsp), %rdx
movss (%rdx,%rcx,4), %xmm4
movq 0x90(%rsp), %rsi
movss (%rsi,%rcx,4), %xmm5
movq 0x70(%rsp), %rdi
movss (%rdi,%rcx,4), %xmm6
movq 0x100(%rsp), %rcx
movslq (%rcx,%rax,4), %rcx
movss (%rdx,%rcx,4), %xmm3
movss (%rsi,%rcx,4), %xmm2
movss (%rdi,%rcx,4), %xmm1
movq 0xe0(%rsp), %rcx
movslq (%rcx,%rax,4), %rax
movss (%rdx,%rax,4), %xmm11
movss (%rsi,%rax,4), %xmm10
movss (%rdi,%rax,4), %xmm12
cmpnless %xmm7, %xmm9
movaps %xmm9, %xmm7
andnps %xmm8, %xmm7
andps %xmm13, %xmm9
orps %xmm7, %xmm9
subss %xmm4, %xmm3
subss %xmm5, %xmm2
subss %xmm6, %xmm1
mulss %xmm9, %xmm3
mulss %xmm9, %xmm2
mulss %xmm9, %xmm1
addss %xmm4, %xmm3
addss %xmm5, %xmm2
addss %xmm6, %xmm1
subss %xmm4, %xmm11
subss %xmm5, %xmm10
subss %xmm6, %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, 0xb8(%rsp)
callq 0x20f0
mulsd 0x66d8(%rip), %xmm0     # 0x9018
mulsd 0x66d8(%rip), %xmm0     # 0x9020
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xb0(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x130(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, 0x120(%rsp)
callq 0x20f0
cvtsd2ss %xmm0, %xmm0
movaps 0x130(%rsp), %xmm1
movss 0xb0(%rsp), %xmm2
mulss %xmm2, %xmm1
movaps 0x120(%rsp), %xmm3
mulss %xmm2, %xmm3
mulss %xmm2, %xmm0
addss 0xb8(%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 0x29eb
movl 0x28(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x20(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x8(%rsp)
jmp 0x29f6
movq %rbx, %rdi
movq %r14, %rdx
callq 0x31be
decl %ebp
jne 0x27ee
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 0x2dba
leaq 0x160(%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 0x2dfe
xorpd %xmm0, %xmm0
leaq 0x140(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x2, %rsi
imulq %rbx, %rsi
callq 0x2e30
callq 0x2040
movq %rax, %r15
movq 0x50(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x100(%rsp), %r9
movq 0xc0(%rsp), %r8
movq 0xc8(%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 0x36b4
addq $0x20, %rsp
movq %rax, %rbx
callq 0x2040
movq %rax, %r14
movq 0x20(%rsp), %rsi
leaq 0x4(%rsi), %rdx
leaq 0x8(%rsi), %rcx
movq 0x140(%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 0x190(%rsp)
pushq %r12
callq 0x3cdc
addq $0x40, %rsp
callq 0x2040
movq %rax, %r12
movq 0x948a(%rip), %rdi      # 0xbfd8
leaq 0x64eb(%rip), %rsi      # 0x9040
movl $0x13, %edx
callq 0x2130
movq %r14, %rax
subq %r15, %rax
cvtsi2sd %rax, %xmm0
divsd 0x64b6(%rip), %xmm0     # 0x9028
movq 0x945f(%rip), %rdi      # 0xbfd8
callq 0x2160
movq %rax, %r15
leaq 0x64cc(%rip), %rsi      # 0x9054
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r15, %rdi
callq 0x2060
movq 0x9434(%rip), %rdi      # 0xbfd8
leaq 0x64ab(%rip), %rsi      # 0x9056
movl $0xe, %edx
callq 0x2130
movq 0x941c(%rip), %rdi      # 0xbfd8
movl $0xf4240, %esi          # imm = 0xF4240
callq 0x2190
movq %rax, %r15
leaq 0x6495(%rip), %rsi      # 0x9065
movl $0x9, %edx
movq %rax, %rdi
callq 0x2130
subq %r14, %r12
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
divsd 0x6438(%rip), %xmm0     # 0x9028
movq %r15, %rdi
movsd %xmm0, 0x40(%rsp)
callq 0x2160
movq %rax, %r14
leaq 0x644c(%rip), %rsi      # 0x9054
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq 0x93b4(%rip), %rdi      # 0xbfd8
leaq 0x6444(%rip), %rsi      # 0x906f
movl $0x9, %edx
callq 0x2130
movsd 0x40(%rsp), %xmm0
divsd 0x63cd(%rip), %xmm0     # 0x9010
movq 0x938e(%rip), %rdi      # 0xbfd8
callq 0x2160
movq %rax, %r14
leaq 0x6420(%rip), %rsi      # 0x9079
movl $0xe, %edx
movq %rax, %rdi
callq 0x2130
movq %r14, %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x3fe9
jmp 0x2c7c
jmp 0x2c7c
jmp 0x2c7c
movq %rax, %rbx
movq 0x140(%rsp), %rdi
testq %rdi, %rdi
je 0x2ca1
movq 0x150(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2ca1
movq %rax, %rbx
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x2cc8
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2cc8
movq %rax, %rbx
jmp 0x2cdf
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2cdf
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2d0c
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
jmp 0x2d0c
jmp 0x2d09
jmp 0x2d09
jmp 0x2d09
jmp 0x2d09
jmp 0x2d09
jmp 0x2d09
jmp 0x2d09
jmp 0x2d09
jmp 0x2d09
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x2d29
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x2d46
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x2d63
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x2d7d
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x2d9a
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2db1
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21a0
nop