// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/scnview/scnview.cpp
// Function: Pick(gaps::R3Scene*, int, int, gaps::R3SceneNode**, gaps::R3Point*)
static int
Pick(R3Scene *scene, int x, int y,
  R3SceneNode **hit_node = NULL, R3Point *hit_position = NULL)
{
  // Clear window
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // Set viewing transformation
  viewer->Camera().Load();

  // Set OpenGL stuff
  int pick_tolerance = 10;
  glPointSize(pick_tolerance);
  glDisable(GL_LIGHTING);
  glDisable(GL_MULTISAMPLE);

  // Draw leaf nodes with color indicating node index
  for (int i = 0; i < scene->NNodes(); i++) {
    R3SceneNode *node = scene->Node(i);
    if (node->NChildren() > 0) continue;
    R3Affine parent_transformation = node->CumulativeParentTransformation();
    parent_transformation.Push();
    int index = i + 1;
    unsigned char rgba[4];
    rgba[0] = (index >> 16) & 0xFF;
    rgba[1] = (index >> 8) & 0xFF;
    rgba[2] = index & 0xFF;
    rgba[3] = 0xFF;
    RNLoadRgba(rgba);
    node->Draw(R3_SURFACES_DRAW_FLAG);
    parent_transformation.Pop();
  }

  // Reset OpenGL stuff
  glEnable(GL_MULTISAMPLE);
  glPointSize(1);
  glFinish();

  // Read color buffer at cursor position
  unsigned char rgba[4];
  glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
  if ((rgba[0] == 0) && (rgba[1] == 0) && (rgba[2] == 0)) return 0;

  // Parse color value
  int r = rgba[0] & 0xFF;
  int g = rgba[1] & 0xFF;
  int b = rgba[2] & 0xFF;
  // int a = rgba[3] & 0xFF;
  int node_index = ((r << 16) | (g << 8) | b) - 1;
  if (node_index < 0) return 0;
  if (node_index >= scene->NNodes()) return 0;
 
  // Return hit node
  if (hit_node) {
   *hit_node = scene->Node(node_index);
  }

  // Return hit position
  if (hit_position) {
    GLfloat depth;
    GLdouble p[3];
    GLint viewport[4];
    GLdouble modelview_matrix[16];
    GLdouble projection_matrix[16];
    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);
    glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
    gluUnProject(x, y, depth, modelview_matrix, projection_matrix, viewport, &(p[0]), &(p[1]), &(p[2]));
    R3Point position(p[0], p[1], p[2]);
    *hit_position = position;
  }
  
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %r8, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movl %edx, 0x14(%rsp)
movl %esi, 0x10(%rsp)
movq %rdi, %r12
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
callq 0x21e40
movl $0x4100, %edi           # imm = 0x4100
callq 0x21200
movq 0x10ddb9(%rip), %rdi    # 0x133908
addq $0x8, %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x3c0de
movss 0x9f4ac(%rip), %xmm0    # 0xc5010
callq 0x210a0
movl $0xb50, %edi            # imm = 0xB50
callq 0x21210
movl $0x809d, %edi           # imm = 0x809D
callq 0x21210
leaq 0x100(%rsp), %r13
leaq 0x60(%rsp), %r14
pushq $0x2
popq %r15
movslq 0x14(%r12), %rax
cmpq %rax, %rbx
jge 0x25bff
movq 0x8(%r12), %rax
movq (%rax,%rbx,8), %rbp
cmpl $0x0, 0x2c(%rbp)
jle 0x25bac
incq %rbx
jmp 0x25b8e
movq %r13, %rdi
movq %rbp, %rsi
callq 0x388ea
movq %r13, %rdi
callq 0x40294
incq %rbx
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x60(%rsp)
movb %bh, 0x61(%rsp)
movb %bl, 0x62(%rsp)
movb $-0x1, 0x63(%rsp)
movq %r14, %rdi
callq 0x21410
movq %rbp, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x390e8
movq %r13, %rdi
callq 0x402d0
movq %r13, %rdi
callq 0x66856
jmp 0x25b8e
movl $0x809d, %edi           # imm = 0x809D
callq 0x21820
movss 0x9f3f3(%rip), %xmm0    # 0xc5004
callq 0x210a0
callq 0x22350
leaq 0x2c(%rsp), %rbx
movq %rbx, (%rsp)
pushq $0x1
popq %r15
movl 0x10(%rsp), %edi
movl 0x14(%rsp), %esi
movl %r15d, %edx
movl %r15d, %ecx
movl $0x1908, %r8d           # imm = 0x1908
movl $0x1401, %r9d           # imm = 0x1401
callq 0x213c0
movzbl (%rbx), %ecx
movb 0x1(%rbx), %al
movb 0x2(%rbx), %dl
movl %ecx, %esi
orb %al, %sil
xorl %ebx, %ebx
orb %dl, %sil
je 0x25d6c
movzbl %al, %eax
movzbl %dl, %edx
shll $0x10, %ecx
shll $0x8, %eax
orl %ecx, %eax
orl %edx, %eax
je 0x25d6c
cmpl 0x14(%r12), %eax
jg 0x25d6c
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
je 0x25c9a
movq 0x8(%r12), %rcx
movl %eax, %eax
movq -0x8(%rcx,%rax,8), %rax
movq %rax, (%rdx)
movl %r15d, %ebx
cmpq $0x0, 0x18(%rsp)
je 0x25d6c
leaq 0x30(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x21870
leaq 0x100(%rsp), %r12
movl $0xba6, %edi            # imm = 0xBA6
movq %r12, %rsi
callq 0x21a50
leaq 0x60(%rsp), %r13
movl $0xba7, %edi            # imm = 0xBA7
movq %r13, %rsi
callq 0x21a50
leaq 0x28(%rsp), %r14
movq %r14, (%rsp)
pushq $0x1
popq %rbx
movl 0x10(%rsp), %r15d
movl %r15d, %edi
movl 0x14(%rsp), %ebp
movl %ebp, %esi
movl %ebx, %edx
movl %ebx, %ecx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x213c0
cvtsi2sd %r15d, %xmm0
cvtsi2sd %ebp, %xmm1
cvtss2sd (%r14), %xmm2
leaq 0x48(%rsp), %r8
leaq 0x50(%rsp), %r9
leaq 0x40(%rsp), %r14
movq %r12, %rdi
movq %r13, %rsi
leaq 0x30(%rsp), %rdx
movq %r14, %rcx
callq 0x22240
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
callq 0x6b874
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x6bb5e
movl %ebx, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x25d82
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x66856
movq %rbx, %rdi
callq 0x220e0