// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/apps/conf2img/conf2img.cpp
// Function: CaptureDepth(gaps::R2Grid&)
static int 
CaptureDepth(R2Grid& image)
{
  // Get viewport dimensions
  GLint viewport[4];
  glGetIntegerv(GL_VIEWPORT, viewport);

  // Get modelview  matrix
  static GLdouble modelview_matrix[16];
  // glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
  for (int i = 0; i < 16; i++) modelview_matrix[i] = 0;
  modelview_matrix[0] = 1.0;
  modelview_matrix[5] = 1.0;
  modelview_matrix[10] = 1.0;
  modelview_matrix[15] = 1.0;
  
  // Get projection matrix
  GLdouble projection_matrix[16];
  glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);

  // Get viewpoint matrix
  GLint viewport_matrix[16];
  glGetIntegerv(GL_VIEWPORT, viewport_matrix);

  // Allocate pixels
  float *pixels = new float [ image.NEntries() ];

  // Read pixels from frame buffer 
  glReadPixels(0, 0, viewport[2], viewport[3], GL_DEPTH_COMPONENT, GL_FLOAT, pixels); 

  // Clear image
  image.Clear(0.0);
  
  // Convert pixels to depths
  int ix, iy;
  double x, y, z;
  for (int i = 0; i < image.NEntries(); i++) {
    if (RNIsEqual(pixels[i], 1.0)) continue;
    if (RNIsNegativeOrZero(pixels[i])) continue;
    image.IndexToIndices(i, ix, iy);
    gluUnProject(ix, iy, pixels[i], modelview_matrix, projection_matrix, viewport_matrix, &x, &y, &z);
    image.SetGridValue(i, -z);
  }

  // Delete pixels
  delete [] pixels;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
leaq 0x20(%rsp), %r15
movl $0xba2, %edi            # imm = 0xBA2
movq %r15, %rsi
callq 0x23880
xorpd %xmm0, %xmm0
movapd %xmm0, 0x1517c0(%rip)   # 0x17cae0
movapd %xmm0, 0x1517d8(%rip)   # 0x17cb00
movapd %xmm0, 0x151800(%rip)   # 0x17cb30
movapd %xmm0, 0x151818(%rip)   # 0x17cb50
movapd %xmm0, 0x151800(%rip)   # 0x17cb40
movapd %xmm0, 0x1517d8(%rip)   # 0x17cb20
movapd %xmm0, 0x1517c0(%rip)   # 0x17cb10
movapd %xmm0, 0x151798(%rip)   # 0x17caf0
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x151777(%rip)    # 0x17cae0
movq %rax, 0x151798(%rip)    # 0x17cb08
movq %rax, 0x1517b9(%rip)    # 0x17cb30
movq %rax, 0x1517da(%rip)    # 0x17cb58
leaq 0x70(%rsp), %rsi
movl $0xba7, %edi            # imm = 0xBA7
callq 0x23a60
leaq 0x30(%rsp), %rsi
movl $0xba2, %edi            # imm = 0xBA2
callq 0x23880
movslq 0xcc(%rbx), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x23040
movq %rax, %r14
movl 0x8(%r15), %edx
movl 0xc(%r15), %ecx
movq %rax, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl $0x1902, %r8d           # imm = 0x1902
movl $0x1406, %r9d           # imm = 0x1406
callq 0x233a0
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x9fcca
cmpl $0x0, 0xcc(%rbx)
jle 0x2b4cc
xorl %r15d, %r15d
leaq 0x14cca3(%rip), %r12    # 0x1780a8
movsd 0xdcc63(%rip), %xmm4    # 0x108070
movapd 0xdcc6b(%rip), %xmm5    # 0x108080
leaq 0x10(%rsp), %r13
leaq 0x8(%rsp), %rbp
xorps %xmm2, %xmm2
cvtss2sd (%r14,%r15,4), %xmm2
movsd (%r12), %xmm0
movaps %xmm2, %xmm1
addsd %xmm4, %xmm1
ucomisd %xmm2, %xmm0
jae 0x2b4b9
movapd %xmm1, %xmm3
cmpnlepd %xmm0, %xmm3
xorpd %xmm5, %xmm0
cmpnlepd %xmm1, %xmm0
orpd %xmm3, %xmm0
movd %xmm0, %eax
testb $0x1, %al
je 0x2b4b9
movl %r15d, %eax
cltd
idivl 0xc8(%rbx)
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
leaq 0x151668(%rip), %rdi    # 0x17cae0
leaq 0x70(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r13, %r8
movq %rbp, %r9
callq 0x24230
movapd 0xdcbe6(%rip), %xmm5    # 0x108080
movsd 0xdcbce(%rip), %xmm4    # 0x108070
movsd 0x8(%rsp), %xmm0
xorpd %xmm5, %xmm0
movq 0xb8(%rbx), %rax
movlpd %xmm0, (%rax,%r15,8)
incq %r15
movslq 0xcc(%rbx), %rax
cmpq %rax, %r15
jl 0x2b41f
movq %r14, %rdi
callq 0x23c40
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq