// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::CreateBox(gaps::R3Scene*, gaps::R3SceneNode*, double*, gaps::RNArray<gaps::R3Material*> const&)
static int
CreateBox(R3Scene *scene, R3SceneNode *node,
  RNScalar dimensions[3],
  const RNArray<R3Material *>& materials)
{
  // Create six sides of box
  for (int dir = 0; dir < 2; dir++) {
    for (int dim = 0; dim < 3; dim++) {
      int dim1 = (dim+1)%3;
      int dim2 = (dim+2)%3;

      // Compute radii
      RNScalar d0 = dimensions[dim];
      RNScalar d1 = dimensions[dim1];
      RNScalar d2 = dimensions[dim2];
      RNScalar r0 = (dir == 0) ? -0.5*d0 :  0.5*d0;
      RNScalar r1 = (dir == 0) ? -0.5*d1 : 0.5*d1;
      RNScalar r2 = 0.5*d2;

      // Compute axes
      R3Vector axis0 = r0 * R3xyz_triad.Axis(dim);
      R3Vector axis1 = r1 * R3xyz_triad.Axis(dim1);
      R3Vector axis2 = r2 * R3xyz_triad.Axis(dim2);

      // Compute vertex positions
      R3Point center(0, 0, 0);
      R3Point p00 = center + axis0 - axis1 - axis2;
      R3Point p10 = center + axis0 + axis1 - axis2;
      R3Point p11 = center + axis0 + axis1 + axis2;
      R3Point p01 = center + axis0 - axis1 + axis2;

      // Compute texture coordinates
      R2Point t00(0, 0);
      R2Point t10(d1, 0);
      R2Point t11(d1, d2);
      R2Point t01(0, d2);

      // Create two triangles
      RNArray<R3TriangleVertex *> vertices;
      R3TriangleVertex *v00 = new R3TriangleVertex(p00, t00); vertices.Insert(v00);
      R3TriangleVertex *v10 = new R3TriangleVertex(p10, t10); vertices.Insert(v10);
      R3TriangleVertex *v11 = new R3TriangleVertex(p11, t11); vertices.Insert(v11);
      R3TriangleVertex *v01 = new R3TriangleVertex(p01, t01); vertices.Insert(v01);
      RNArray<R3Triangle *> triangles;
      R3Triangle *tri0 = new R3Triangle(v00, v10, v11); triangles.Insert(tri0);
      R3Triangle *tri1 = new R3Triangle(v00, v11, v01); triangles.Insert(tri1);
      R3TriangleArray *shape = new R3TriangleArray(vertices, triangles);

      // Create scene element
      static const int material_indices[2][3] = { { 2, 1, 5 }, { 3, 0, 4 } };
      int material_index = material_indices[dir][dim];
      R3Material *material = (materials.NEntries() > material_index) ? materials[material_index] : &R3default_material;
      R3SceneElement *element = new R3SceneElement(material);
      element->InsertShape(shape);
      node->InsertElement(element);
    }
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x388, %rsp            # imm = 0x388
movq %rdi, 0x380(%rsp)
movq %rsi, 0x378(%rsp)
movq %rdx, 0x370(%rsp)
movq %rcx, 0x368(%rsp)
movl $0x0, 0x364(%rsp)
cmpl $0x2, 0x364(%rsp)
jge 0x3fe93
movl $0x0, 0x360(%rsp)
cmpl $0x3, 0x360(%rsp)
jge 0x3fe7b
movl 0x360(%rsp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, 0x35c(%rsp)
movl 0x360(%rsp), %eax
addl $0x2, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, 0x358(%rsp)
movq 0x370(%rsp), %rax
movslq 0x360(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x350(%rsp)
movq 0x370(%rsp), %rax
movslq 0x35c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x348(%rsp)
movq 0x370(%rsp), %rax
movslq 0x358(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x340(%rsp)
cmpl $0x0, 0x364(%rsp)
jne 0x3f61b
movsd 0x14efe1(%rip), %xmm0   # 0x18e5e8
mulsd 0x350(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x3f635
movsd 0x14e9fd(%rip), %xmm0   # 0x18e020
mulsd 0x350(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0x338(%rsp)
cmpl $0x0, 0x364(%rsp)
jne 0x3f66d
movsd 0x14ef8f(%rip), %xmm0   # 0x18e5e8
mulsd 0x348(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
jmp 0x3f687
movsd 0x14e9ab(%rip), %xmm0   # 0x18e020
mulsd 0x348(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0x330(%rsp)
movsd 0x340(%rsp), %xmm0
movsd 0x14e976(%rip), %xmm1   # 0x18e020
mulsd %xmm1, %xmm0
movsd %xmm0, 0x328(%rsp)
movsd 0x338(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0x360(%rsp), %esi
leaq 0x1bf4b4(%rip), %rdi    # 0x1feb88
movq %rdi, 0x58(%rsp)
callq 0x2bf30
movsd 0x48(%rsp), %xmm0
movq %rax, %rsi
leaq 0x310(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x2b630
movq 0x58(%rsp), %rdi
movsd 0x330(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x35c(%rsp), %esi
callq 0x2bf30
movsd 0x50(%rsp), %xmm0
movq %rax, %rsi
leaq 0x2f8(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
callq 0x2b630
movq 0x58(%rsp), %rdi
movsd 0x328(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movl 0x358(%rsp), %esi
callq 0x2bf30
movsd 0x60(%rsp), %xmm0
movq %rax, %rsi
leaq 0x2e0(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
callq 0x2b630
leaq 0x2c8(%rsp), %rdi
movq %rdi, 0x98(%rsp)
xorps %xmm2, %xmm2
movsd %xmm2, 0xc8(%rsp)
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xcc870
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x280(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0xcce30
movq 0x68(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x298(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0xcd7d0
movq 0x70(%rsp), %rsi
movq 0xc0(%rsp), %rdx
leaq 0x2b0(%rsp), %rdi
callq 0xcd7d0
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x238(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0xcce30
movq 0x78(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x250(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0xcce30
movq 0x80(%rsp), %rsi
movq 0xc0(%rsp), %rdx
leaq 0x268(%rsp), %rdi
callq 0xcd7d0
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x1f0(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0xcce30
movq 0x88(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x208(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0xcce30
movq 0x90(%rsp), %rsi
movq 0xc0(%rsp), %rdx
leaq 0x220(%rsp), %rdi
callq 0xcce30
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0x1a8(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0xcce30
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x1c0(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
callq 0xcd7d0
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
leaq 0x1d8(%rsp), %rdi
callq 0xcce30
movsd 0xc8(%rsp), %xmm1
leaq 0x198(%rsp), %rdi
movaps %xmm1, %xmm0
callq 0xe3d80
movsd 0xc8(%rsp), %xmm1
movsd 0x348(%rsp), %xmm0
leaq 0x188(%rsp), %rdi
callq 0xe3d80
movsd 0x348(%rsp), %xmm0
movsd 0x340(%rsp), %xmm1
leaq 0x178(%rsp), %rdi
callq 0xe3d80
movsd 0xc8(%rsp), %xmm0
movsd 0x340(%rsp), %xmm1
leaq 0x168(%rsp), %rdi
callq 0xe3d80
leaq 0x158(%rsp), %rdi
callq 0x48d30
movl $0x68, %edi
callq 0x229e0
movq %rax, 0xd0(%rsp)
jmp 0x3f9c0
movq 0xd0(%rsp), %rdi
leaq 0x2b0(%rsp), %rsi
leaq 0x198(%rsp), %rdx
callq 0xb08d0
jmp 0x3f9df
movq 0xd0(%rsp), %rax
movq %rax, 0x150(%rsp)
movq 0x150(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0x48f60
jmp 0x3fa06
movl $0x68, %edi
callq 0x229e0
movq %rax, 0x40(%rsp)
jmp 0x3fa17
movq 0x40(%rsp), %rdi
leaq 0x268(%rsp), %rsi
leaq 0x188(%rsp), %rdx
callq 0xb08d0
jmp 0x3fa33
movq 0x40(%rsp), %rax
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0x48f60
jmp 0x3fa57
movl $0x68, %edi
callq 0x229e0
movq %rax, 0x38(%rsp)
jmp 0x3fa68
movq 0x38(%rsp), %rdi
leaq 0x220(%rsp), %rsi
leaq 0x178(%rsp), %rdx
callq 0xb08d0
jmp 0x3fa84
movq 0x38(%rsp), %rax
movq %rax, 0x130(%rsp)
movq 0x130(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0x48f60
jmp 0x3faa8
movl $0x68, %edi
callq 0x229e0
movq %rax, 0x30(%rsp)
jmp 0x3fab9
movq 0x30(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
leaq 0x168(%rsp), %rdx
callq 0xb08d0
jmp 0x3fad5
movq 0x30(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0x48f60
jmp 0x3faf9
leaq 0x118(%rsp), %rdi
callq 0x49140
jmp 0x3fb08
movl $0x88, %edi
callq 0x229e0
movq %rax, 0x28(%rsp)
jmp 0x3fb19
movq 0x28(%rsp), %rdi
movq 0x150(%rsp), %rsi
movq 0x138(%rsp), %rdx
movq 0x130(%rsp), %rcx
callq 0xb1110
jmp 0x3fb3d
movq 0x28(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x49190
jmp 0x3fb61
movl $0x88, %edi
callq 0x229e0
movq %rax, 0x20(%rsp)
jmp 0x3fb72
movq 0x20(%rsp), %rdi
movq 0x150(%rsp), %rsi
movq 0x130(%rsp), %rdx
movq 0x128(%rsp), %rcx
callq 0xb1110
jmp 0x3fb96
movq 0x20(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x49190
jmp 0x3fbba
movl $0x70, %edi
callq 0x229e0
movq %rax, 0x18(%rsp)
jmp 0x3fbcb
movq 0x18(%rsp), %rdi
leaq 0x158(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0xacc40
jmp 0x3fbe7
movq 0x18(%rsp), %rax
movq %rax, 0x100(%rsp)
movslq 0x364(%rsp), %rcx
leaq 0x15005d(%rip), %rax    # 0x18fc60
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movslq 0x360(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0xfc(%rsp)
movq 0x368(%rsp), %rdi
callq 0x2b8b0
cmpl 0xfc(%rsp), %eax
jle 0x3fc4d
movq 0x368(%rsp), %rdi
movl 0xfc(%rsp), %esi
callq 0x4e650
movq %rax, 0x10(%rsp)
jmp 0x3fc5b
leaq 0x1bc8ac(%rip), %rax    # 0x1fc500
movq %rax, 0x10(%rsp)
jmp 0x3fc5b
movq 0x10(%rsp), %rax
movq %rax, 0xf0(%rsp)
movl $0x60, %edi
callq 0x229e0
movq %rax, 0x8(%rsp)
jmp 0x3fc79
movq 0x8(%rsp), %rdi
movq 0xf0(%rsp), %rsi
callq 0x58f20
jmp 0x3fc8d
movq 0x8(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
movq 0x100(%rsp), %rsi
callq 0x59370
jmp 0x3fcb1
movq 0x378(%rsp), %rdi
movq 0xe8(%rsp), %rsi
callq 0x57510
jmp 0x3fcc8
leaq 0x118(%rsp), %rdi
callq 0x492a0
leaq 0x158(%rsp), %rdi
callq 0x49340
movl 0x360(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x360(%rsp)
jmp 0x3f55b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x3fe6c
movq 0xd0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x3fe6c
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x3fe6c
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x3fe6c
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x68, %esi
callq 0x22a00
jmp 0x3fe6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x3fe5f
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x88, %esi
callq 0x22a00
jmp 0x3fe5f
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x88, %esi
callq 0x22a00
jmp 0x3fe5f
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x70, %esi
callq 0x22a00
jmp 0x3fe5f
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x60, %esi
callq 0x22a00
leaq 0x118(%rsp), %rdi
callq 0x492a0
leaq 0x158(%rsp), %rdi
callq 0x49340
jmp 0x3fea0
jmp 0x3fe7d
movl 0x364(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x364(%rsp)
jmp 0x3f542
movl $0x1, %eax
addq $0x388, %rsp            # imm = 0x388
retq
movq 0x148(%rsp), %rdi
callq 0x23290
nopl (%rax)