// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rsi, %r14
movq %rdi, %rbx
movl 0x13133b(%rip), %esi    # 0x15c974
movl 0x131339(%rip), %edx    # 0x15c978
leaq 0x60(%rsp), %rdi
callq 0x88284
movl 0x131329(%rip), %eax    # 0x15c978
imull 0x13131e(%rip), %eax    # 0x15c974
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
testl %eax, %eax
je 0x2bae3
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0xc69a1(%rip), %xmm0    # 0xf2018
cvttsd2si %xmm0, %eax
testl %eax, %eax
je 0x2bae3
movl %eax, 0x34(%rsp)
movq (%r14), %r12
cmpb $0x1, 0x1361e3(%rip)    # 0x161874
movq %r14, 0x28(%rsp)
jne 0x2b819
testb $0x1, 0x1361cd(%rip)    # 0x161870
jne 0x2b819
leaq 0x60(%rsp), %rdi
movsd 0xc69ea(%rip), %xmm0    # 0xf20a0
callq 0x8a404
movl 0x120(%rsp), %ecx
movl 0x124(%rsp), %r8d
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4606e
leaq 0x130(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq %rbx, %rsi
callq 0x439a8
movq %r13, 0x40(%rsp)
movl %r15d, 0x30(%rsp)
movl 0x124(%rsp), %ecx
testl %ecx, %ecx
jle 0x2b7fd
xorl %edx, %edx
movl 0x120(%rsp), %eax
leaq 0x1d8(%rsp), %r13
leaq 0x130(%rsp), %rbp
leaq 0x38(%rsp), %rbx
testl %eax, %eax
jle 0x2b7f3
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rdx, 0x48(%rsp)
movslq %edx, %r14
xorl %r15d, %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movq %r13, %rdi
movq %rbp, %rsi
movsd 0x10(%rsp), %xmm1
callq 0x44816
movq $0x0, 0x38(%rsp)
leaq 0x13195b(%rip), %rax    # 0x15d0c8
movsd (%rax), %xmm1
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
xorpd %xmm0, %xmm0
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x42576
testl %eax, %eax
setne %cl
movq 0x38(%rsp), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2b7cf
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rax), %xmm0
movq 0x118(%rsp), %rax
movslq 0x128(%rsp), %rcx
imulq %r14, %rcx
addq %r15, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x120(%rsp), %eax
incq %r15
cmpl %eax, %r15d
jl 0x2b744
movl 0x124(%rsp), %ecx
movq 0x28(%rsp), %r14
movq 0x48(%rsp), %rdx
incl %edx
cmpl %ecx, %edx
jl 0x2b724
leaq 0x130(%rsp), %rdi
callq 0x43a1c
movl 0x30(%rsp), %r15d
movq 0x40(%rsp), %r13
jmp 0x2b995
movss 0xc68ef(%rip), %xmm3    # 0xf2110
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x22df0
movl $0x4100, %edi           # imm = 0x4100
callq 0x221c0
movl $0x1700, %edi           # imm = 0x1700
callq 0x22c80
callq 0x22c10
movq %rbx, %rdi
xorl %esi, %esi
callq 0x45dc4
movl 0x131119(%rip), %edx    # 0x15c974
movl 0x131117(%rip), %ecx    # 0x15c978
xorl %edi, %edi
xorl %esi, %esi
callq 0x221e0
movl $0xb71, %edi            # imm = 0xB71
callq 0x227e0
leaq 0x13c4a5(%rip), %rdi    # 0x167d20
xorl %esi, %esi
callq 0x495c2
movq %r12, %rdi
callq 0x2bb31
leaq 0x13c48f(%rip), %rdi    # 0x167d20
xorl %esi, %esi
callq 0x495c2
movl 0x1310d6(%rip), %ebp    # 0x15c974
testb $0x3, %bpl
je 0x2b8b9
movl $0xd05, %edi            # imm = 0xD05
movl $0x1, %esi
callq 0x223e0
movl 0x1310bb(%rip), %ebp    # 0x15c974
movl 0x1310b8(%rip), %r12d   # 0x15c978
movl %ebp, %eax
imull %r12d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x22040
movq %rax, %rbx
movq %rax, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl %ebp, %edx
movl %r12d, %ecx
movl $0x1907, %r8d           # imm = 0x1907
movl $0x1401, %r9d           # imm = 0x1401
callq 0x22390
movslq 0x13107f(%rip), %rax    # 0x15c978
testq %rax, %rax
jle 0x2b965
movl 0x131070(%rip), %ecx    # 0x15c974
movq 0x118(%rsp), %rdx
movslq 0x128(%rsp), %rsi
shlq $0x3, %rsi
xorl %edi, %edi
movq %rbx, %r8
testl %ecx, %ecx
jle 0x2b95a
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 0x2b924
incq %rdi
addq %rsi, %rdx
cmpq %rax, %rdi
jne 0x2b91d
movq %rbx, %rdi
callq 0x22bf0
leaq 0x60(%rsp), %rdi
movsd 0xc6726(%rip), %xmm1    # 0xf20a0
xorpd %xmm0, %xmm0
callq 0x8cb54
leaq 0x60(%rsp), %rdi
movsd 0xc6708(%rip), %xmm0    # 0xf2098
callq 0x8cc1e
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 0x2b9c9
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x22460
movslq 0x12c(%rsp), %rax
testq %rax, %rax
jle 0x2ba25
movq 0x118(%rsp), %rcx
xorl %edx, %edx
movsd 0xc66b8(%rip), %xmm0    # 0xf20a0
movsd 0xc6618(%rip), %xmm1    # 0xf2008
movsd (%rcx,%rdx,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x2b9fd
jnp 0x2ba1d
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %esi
testl %esi, %esi
sets %dil
cmpl %esi, %r14d
setle %r8b
orb %dil, %r8b
jne 0x2ba1d
movl %esi, %esi
incl (%r12,%rsi,4)
incq %rdx
cmpq %rdx, %rax
jne 0x2b9f0
testl %r14d, %r14d
jle 0x2babc
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
movq (%rax,%r14,8), %rbx
testl %r15d, %r15d
je 0x2ba5b
movq %rbx, %rdi
callq 0x2b50e
testl %eax, %eax
je 0x2ba9c
testq %r13, %r13
je 0x2ba6f
movq %rbx, %rdi
movq %r13, %rsi
callq 0x41f52
testl %eax, %eax
je 0x2ba9c
movl (%r12,%r14,4), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jle 0x2ba9c
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 0x28(%rsp), %rcx
movslq 0x14(%rcx), %rax
cmpq %rax, %r14
jl 0x2ba42
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movapd 0x10(%rsp), %xmm1
jmp 0x2bac4
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd 0x130ee4(%rip), %xmm2   # 0x15c9b0
cmpltsd %xmm0, %xmm2
andpd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
movq %r12, %rdi
callq 0x22bf0
leaq 0x60(%rsp), %rdi
callq 0x88eb8
movaps 0x10(%rsp), %xmm0
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2bb08
jmp 0x2bb08
movq %rax, %rbx
jmp 0x2bb1f
jmp 0x2bb0f
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x43a1c
leaq 0x60(%rsp), %rdi
callq 0x88eb8
movq %rbx, %rdi
callq 0x23090