// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/confview/confview.cpp
// Function: Pick(int, int, gaps::RGBDSurface**, gaps::RGBDImage**, gaps::R3Point*, int)
static int
Pick(int x, int y,
  RGBDSurface **picked_surface = NULL, RGBDImage **picked_image = NULL,
  R3Point *picked_position = NULL, int pick_tolerance = 10)
{
  // Initialize pick results
  int pick_result = 0;
  if (picked_surface) *picked_surface = NULL;
  if (picked_image) *picked_image = NULL;
  if (picked_position) *picked_position = R3unknown_point;

  // Draw everything
  viewer.Load();
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPointSize(pick_tolerance);
  glLineWidth(pick_tolerance);
  if (picked_image || !picked_surface) {
    if (show_cameras) DrawCameras(RGBD_INDEX_COLOR_SCHEME);
    if (show_bboxes) DrawBBoxes(RGBD_INDEX_COLOR_SCHEME);
    if (show_points) DrawPoints(RGBD_INDEX_COLOR_SCHEME);
    if (show_surfels) DrawSurfels(RGBD_INDEX_COLOR_SCHEME);
    if (show_quads) DrawQuads(RGBD_INDEX_COLOR_SCHEME);
  }
  if (picked_surface || !picked_image) {
    if (show_faces || show_textures) DrawFaces(RGBD_INDEX_COLOR_SCHEME);
  }
  glPointSize(1.0);
  glLineWidth(1.0);
  glFinish();

  // Read color buffer at cursor position
  unsigned char rgba[4];
  glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
  int r = rgba[0] & 0xFF;
  int g = rgba[1] & 0xFF;
  int b = rgba[2] & 0xFF;
  // int a = rgba[3] & 0xFF;

  // Figure out what was picked
  if (r > 0) {
    // Picked surface
    int surface_index = (int) ((configuration.NSurfaces()*r/255.0) + 0.5) - 1;
    if ((surface_index < 0) || (surface_index >= configuration.NSurfaces())) return 0;
    if (picked_surface) *picked_surface = configuration.Surface(surface_index);
    pick_result = RGBD_SURFACE_SELECTION;
  }
  else if ((g > 0) || (b > 0)) {
    // Picked image
    int image_index = (int) ((configuration.NImages()*((g << 8) | b) / 65535.0) + 0.5) - 1;
    if ((image_index < 0) || (image_index >= configuration.NImages())) return 0;
    if (picked_image) *picked_image = configuration.Image(image_index);
    pick_result = RGBD_IMAGE_SELECTION;
  }
  else {
    // Picked background
    return RGBD_NO_SELECTION;
  }

  // Return position
  if (picked_position) {
    // Find 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]);
    *picked_position = position;
  }

  // Return success
  return pick_result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r8, %rbx
movq %rcx, %r12
movq %rdx, %r15
movl %esi, 0x14(%rsp)
movl %edi, %r14d
testq %rdx, %rdx
je 0x2803b
andq $0x0, (%r15)
testq %r12, %r12
je 0x28045
andq $0x0, (%r12)
testq %rbx, %rbx
je 0x28059
leaq 0x135fbf(%rip), %rsi    # 0x15e010
movq %rbx, %rdi
callq 0x7cb9e
testq %r15, %r15
setne %bpl
leaq 0x132969(%rip), %rdi    # 0x15a9d0
callq 0x29eec
movss 0xb9f90(%rip), %xmm3    # 0xe2004
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x23e70
movl $0x4100, %edi           # imm = 0x4100
callq 0x23200
movss 0xb9f78(%rip), %xmm0    # 0xe200c
callq 0x230a0
movss 0xb9f6b(%rip), %xmm0    # 0xe200c
callq 0x24190
testq %r12, %r12
sete %al
testb %al, %bpl
jne 0x28116
cmpl $0x0, 0x12d938(%rip)    # 0x1559f0
je 0x280c2
pushq $0x3
popq %rdi
callq 0x2730d
cmpl $0x0, 0x1329bb(%rip)    # 0x15aa84
je 0x280d3
pushq $0x3
popq %rdi
callq 0x27377
cmpl $0x0, 0x1329c2(%rip)    # 0x15aa9c
je 0x280e4
pushq $0x3
popq %rdi
callq 0x27408
cmpl $0x0, 0x1329b5(%rip)    # 0x15aaa0
je 0x280f5
pushq $0x3
popq %rdi
callq 0x27473
cmpl $0x0, 0x1329a8(%rip)    # 0x15aaa4
je 0x28106
pushq $0x3
popq %rdi
callq 0x274de
testq %r12, %r12
setne %al
testq %r15, %r15
sete %cl
testb %al, %cl
jne 0x2812c
movl 0x13298c(%rip), %eax    # 0x15aaa8
orl 0x13296e(%rip), %eax    # 0x15aa90
je 0x2812c
pushq $0x3
popq %rdi
callq 0x273c1
movss 0xb9ed0(%rip), %xmm0    # 0xe2004
callq 0x230a0
movss 0xb9ec3(%rip), %xmm0    # 0xe2004
callq 0x24190
callq 0x24390
leaq 0x18(%rsp), %rbp
movq %rbp, (%rsp)
pushq $0x1
popq %r13
movl %r14d, %edi
movl 0x14(%rsp), %esi
movl %r13d, %edx
movl %r13d, %ecx
movl $0x1908, %r8d           # imm = 0x1908
movl $0x1401, %r9d           # imm = 0x1401
callq 0x233c0
movzbl (%rbp), %eax
testl %eax, %eax
je 0x281bf
movl 0x132770(%rip), %ecx    # 0x15a8f4
imull %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd 0xb9ecd(%rip), %xmm0    # 0xe2060
addsd 0xb9e7d(%rip), %xmm0    # 0xe2018
cvttsd2si %xmm0, %eax
xorl %ebp, %ebp
testl %eax, %eax
jle 0x28301
testq %r15, %r15
je 0x28228
cmpl %eax, %ecx
jl 0x28228
pushq $0x2
popq %r13
leaq 0x13272b(%rip), %rcx    # 0x15a8e8
jmp 0x28216
movzbl 0x19(%rsp), %eax
movzbl 0x1a(%rsp), %edx
xorl %ebp, %ebp
movl %edx, %ecx
orb %al, %cl
je 0x28301
movl 0x132709(%rip), %ecx    # 0x15a8e4
shll $0x8, %eax
orl %edx, %eax
imull %ecx, %eax
cvtsi2sd %eax, %xmm0
divsd 0xb9e79(%rip), %xmm0    # 0xe2068
addsd 0xb9e21(%rip), %xmm0    # 0xe2018
cvttsd2si %xmm0, %eax
testl %eax, %eax
jle 0x28301
testq %r12, %r12
je 0x28234
cmpl %eax, %ecx
jl 0x28234
leaq 0x1326c5(%rip), %rcx    # 0x15a8d8
movq %r12, %r15
movq (%rcx), %rcx
movl %eax, %eax
movq -0x8(%rcx,%rax,8), %rax
movq %rax, (%r15)
movl %r13d, %ebp
jmp 0x2823f
cmpl %eax, %ecx
jl 0x28301
pushq $0x2
jmp 0x2823e
cmpl %eax, %ecx
jl 0x28301
pushq $0x1
popq %rbp
testq %rbx, %rbx
je 0x28301
leaq 0x20(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x23890
leaq 0x60(%rsp), %rsi
movl $0xba6, %edi            # imm = 0xBA6
callq 0x23a70
leaq 0xe0(%rsp), %r13
movl $0xba7, %edi            # imm = 0xBA7
movq %r13, %rsi
callq 0x23a70
leaq 0x1c(%rsp), %r15
movq %r15, (%rsp)
pushq $0x1
popq %rcx
movl %r14d, %edi
movl 0x14(%rsp), %r12d
movl %r12d, %esi
movl %ecx, %edx
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x233c0
cvtsi2sd %r14d, %xmm0
cvtsi2sd %r12d, %xmm1
cvtss2sd (%r15), %xmm2
leaq 0x38(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x30(%rsp), %r14
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rcx
callq 0x24270
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x7c8b4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7cb9e
movl %ebp, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq