// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/apps/scn2cam/scn2cam.cpp
// Function: SceneCoverageScore(gaps::R3Camera const&, gaps::R3Scene*, gaps::R3SceneNode*, int)
static RNScalar
SceneCoverageScore(const R3Camera& camera, R3Scene *scene, R3SceneNode *subtree = NULL, RNBoolean suncg = FALSE)
{
  // Allocate image for scoring
  R2Grid image(width, height);

  // Compute maximum number of pixels in image
  int max_pixel_count = width * height;
  if (max_pixel_count == 0) return 0;

  // Compute minimum number of pixels per object
  int min_pixel_count_per_object = min_visible_fraction * max_pixel_count;
  if (min_pixel_count_per_object == 0) return 0;

  // Render image
  RenderImage(image, camera, scene, scene->Root(), NULL, NODE_INDEX_IMAGE);

  // Allocate buffer for counting visible pixels of nodes
  int *node_pixel_counts = new int [ scene->NNodes() ];
  for (int i = 0; i < scene->NNodes(); i++) node_pixel_counts[i] = 0;
  
  // Log counts of pixels visible on each node
  for (int i = 0; i < image.NEntries(); i++) {      
    RNScalar value = image.GridValue(i);
    if (value == R2_GRID_UNKNOWN_VALUE) continue;
    int node_index = (int) (value + 0.5);
    if ((node_index < 0) || (node_index >= scene->NNodes())) continue;
    node_pixel_counts[node_index]++;
  }

  // Compute score
  RNScalar sum = 0;
  int node_count = 0;
  for (int i = 0; i < scene->NNodes(); i++) {
    R3SceneNode *node = scene->Node(i);
    if (suncg && !IsObject(node)) continue;
    if (subtree && !node->IsDecendent(subtree)) continue;
    if (node_pixel_counts[i] <= min_pixel_count_per_object) continue;
    sum += log(node_pixel_counts[i] / min_pixel_count_per_object);
    node_count++;
  }

  // Compute score (log of product of number of pixels visible in each object)
  RNScalar score = (node_count > min_visible_objects) ? sum : 0;

  // Delete pixel counts
  delete [] node_pixel_counts;

  // Return score
  return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl %ecx, %r15d
movq %rdx, 0x38(%rsp)
movq %rsi, %r14
movq %rdi, %r13
movl 0x1332c2(%rip), %esi    # 0x15e984
movl 0x1332c0(%rip), %edx    # 0x15e988
leaq 0x60(%rsp), %rdi
callq 0x893d8
movl 0x1332b0(%rip), %eax    # 0x15e988
imull 0x1332a5(%rip), %eax    # 0x15e984
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
testl %eax, %eax
je 0x2bb72
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0xc8918(%rip), %xmm0    # 0xf4018
cvttsd2si %xmm0, %eax
movl %eax, 0x34(%rsp)
testl %eax, %eax
je 0x2bb72
movq (%r14), %rax
movq %rax, 0x40(%rsp)
cmpb $0x1, 0x138165(%rip)    # 0x163884
movq %r14, 0x20(%rsp)
jne 0x2b8a6
testb $0x1, 0x13814f(%rip)    # 0x163880
jne 0x2b8a6
leaq 0x60(%rsp), %rdi
movsd 0xc895c(%rip), %xmm0    # 0xf40a0
callq 0x8b58a
movl 0x120(%rsp), %ecx
movl 0x124(%rsp), %r8d
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x465ca
leaq 0x130(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq %r13, %rsi
callq 0x43f48
movl %r15d, 0x30(%rsp)
cmpl $0x0, 0x124(%rsp)
jle 0x2b88a
movq $0x0, 0x28(%rsp)
leaq 0x1d8(%rsp), %r13
leaq 0x130(%rsp), %rbp
leaq 0x13392a(%rip), %r15    # 0x15f0d8
leaq 0x48(%rsp), %rbx
cmpl $0x0, 0x120(%rsp)
jle 0x2b871
movq 0x28(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
movslq %eax, %r14
xorl %r12d, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movq %r13, %rdi
movq %rbp, %rsi
movsd 0x10(%rsp), %xmm1
callq 0x44da6
movq $0x0, 0x48(%rsp)
movsd (%r15), %xmm1
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
xorpd %xmm0, %xmm0
movq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x42b06
testl %eax, %eax
setne %cl
movq 0x48(%rsp), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2b860
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rax), %xmm0
movq 0x118(%rsp), %rax
movslq 0x128(%rsp), %rcx
imulq %r14, %rcx
addq %r12, %rcx
movsd %xmm0, (%rax,%rcx,8)
incq %r12
cmpl 0x120(%rsp), %r12d
jl 0x2b7d9
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
cmpl 0x124(%rsp), %eax
jl 0x2b7b3
leaq 0x130(%rsp), %rdi
callq 0x43fbc
movl 0x30(%rsp), %r15d
movq 0x20(%rsp), %r14
jmp 0x2ba1e
movss 0xc8862(%rip), %xmm3    # 0xf4110
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x22e00
movl $0x4100, %edi           # imm = 0x4100
callq 0x221c0
movl $0x1700, %edi           # imm = 0x1700
callq 0x22c90
callq 0x22c20
movq %r13, %rdi
xorl %esi, %esi
callq 0x46320
movl 0x13309c(%rip), %edx    # 0x15e984
movl 0x13309a(%rip), %ecx    # 0x15e988
xorl %edi, %edi
xorl %esi, %esi
callq 0x221e0
movl $0xb71, %edi            # imm = 0xB71
callq 0x227e0
leaq 0x13e428(%rip), %rdi    # 0x169d30
xorl %esi, %esi
callq 0x49c84
movq 0x40(%rsp), %rdi
callq 0x2bbc0
leaq 0x13e410(%rip), %rdi    # 0x169d30
xorl %esi, %esi
callq 0x49c84
testb $0x3, 0x133056(%rip)    # 0x15e984
je 0x2b93f
movl $0xd05, %edi            # imm = 0xD05
movl $0x1, %esi
callq 0x223e0
movl 0x13303f(%rip), %ebx    # 0x15e984
movl 0x13303d(%rip), %ebp    # 0x15e988
movl %ebx, %eax
imull %ebp, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x22040
movq %rax, %r12
movq %rax, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl %ebx, %edx
movl %ebp, %ecx
movl $0x1907, %r8d           # imm = 0x1907
movl $0x1401, %r9d           # imm = 0x1401
callq 0x22390
movslq 0x133006(%rip), %rax    # 0x15e988
testq %rax, %rax
jle 0x2b9ee
movl 0x132ff7(%rip), %ecx    # 0x15e984
movq 0x118(%rsp), %rdx
movslq 0x128(%rsp), %rsi
shlq $0x3, %rsi
xorl %edi, %edi
movq %r12, %r8
testl %ecx, %ecx
jle 0x2b9e3
xorl %r9d, %r9d
movzbl (%r8), %r10d
shll $0x10, %r10d
movzbl 0x1(%r8), %r11d
shll $0x8, %r11d
orl %r10d, %r11d
movzbl 0x2(%r8), %r10d
orl %r11d, %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
addq $0x3, %r8
movsd %xmm0, (%rdx,%r9,8)
incq %r9
cmpq %r9, %rcx
jne 0x2b9ad
incq %rdi
addq %rsi, %rdx
cmpq %rax, %rdi
jne 0x2b9a6
movq %r12, %rdi
callq 0x22c00
leaq 0x60(%rsp), %rdi
movsd 0xc869d(%rip), %xmm1    # 0xf40a0
xorpd %xmm0, %xmm0
callq 0x8dd5a
leaq 0x60(%rsp), %rdi
movsd 0xc867f(%rip), %xmm0    # 0xf4098
callq 0x8de24
movslq 0x14(%r14), %r14
leaq (,%r14,4), %rbx
testq %r14, %r14
movq $-0x1, %rdi
cmovnsq %rbx, %rdi
callq 0x22040
movq %rax, %r12
testl %r14d, %r14d
jle 0x2ba52
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x22460
movslq 0x12c(%rsp), %rax
testq %rax, %rax
jle 0x2baae
movq 0x118(%rsp), %rcx
xorl %edx, %edx
movsd 0xc862f(%rip), %xmm0    # 0xf40a0
movsd 0xc858f(%rip), %xmm1    # 0xf4008
movsd (%rcx,%rdx,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x2ba86
jnp 0x2baa6
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %esi
testl %esi, %esi
sets %dil
cmpl %esi, %r14d
setle %r8b
orb %dil, %r8b
jne 0x2baa6
movl %esi, %esi
incl (%r12,%rsi,4)
incq %rdx
cmpq %rdx, %rax
jne 0x2ba79
movq 0x20(%rsp), %rcx
cmpl $0x0, 0x14(%rcx)
jle 0x2bb4b
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x8(%rcx), %rax
movq (%rax,%r14,8), %rbx
testl %r15d, %r15d
je 0x2bae5
movq %rbx, %rdi
callq 0x2b57e
testl %eax, %eax
je 0x2bb2b
cmpq $0x0, 0x38(%rsp)
je 0x2bafe
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x42542
testl %eax, %eax
je 0x2bb2b
movl (%r12,%r14,4), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jle 0x2bb2b
cltd
idivl %ecx
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x226a0
movapd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
incl %ebp
incq %r14
movq 0x20(%rsp), %rcx
movslq 0x14(%rcx), %rax
cmpq %rax, %r14
jl 0x2bacc
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movapd 0x10(%rsp), %xmm1
jmp 0x2bb53
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd 0x132e65(%rip), %xmm2   # 0x15e9c0
cmpltsd %xmm0, %xmm2
andpd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
movq %r12, %rdi
callq 0x22c00
leaq 0x60(%rsp), %rdi
callq 0x89ff8
movaps 0x10(%rsp), %xmm0
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2bb97
jmp 0x2bb97
movq %rax, %rbx
jmp 0x2bbae
jmp 0x2bb9e
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x43fbc
leaq 0x60(%rsp), %rdi
callq 0x89ff8
movq %rbx, %rdi
callq 0x230a0