// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::LoadLights(gaps::R3Box const&, int, int) const
int R3Scene::
LoadLights(const R3Box& world_bbox, int min_index, int max_index) const
{
  // Determine the maximum number of lights
  if (NLights() == 0) return 1;
  int max_lights = max_index - min_index + 1;
  if (max_lights == 0) return 1;

  // Allocate scores for lights
  RNScalar *scores = new RNScalar [ NLights() ];
  for (int i = 0; i < NLights(); i++) scores[i] = 0;
  
  // Score lights
  for (int i = 0; i < NLights(); i++) {
    R3Light *light = Light(i);
    R3Sphere sphere = light->SphereOfInfluence(1E-3);
    if (RNIsZero(sphere.Radius())) continue; 
    RNScalar d = R3Distance(sphere.Centroid(), world_bbox);
    if (RNIsZero(d)) d = RN_EPSILON;
    RNScalar score = sphere.Radius() / d;
    if (score < 1.0) continue; 
    scores[i] = score;
  }

  // Sort lights based on score
  RNArray<R3Light *> sorted_lights = lights;
  for (int i = 0; i < sorted_lights.NEntries(); i++) {
    for (int j = i+1; j < sorted_lights.NEntries(); j++) {
      if (scores[j] < scores[i]) {
        sorted_lights.Swap(i, j);
        RNScalar swap = scores[i];
        scores[i] = scores[j];
        scores[j] = swap;
      }    
    }
  }

  // Load top lights
  sorted_lights.Truncate(max_lights);
  for (int i = 0; i < sorted_lights.NEntries(); i++) {
    R3Light *light = sorted_lights.Kth(i);
    light->Draw(min_index + i);
  }

  // Delete scores
  delete [] scores;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, (%rsp)
movslq 0x24(%rdi), %r13
testq %r13, %r13
je 0x2933e
subl %edx, %ecx
incl %ecx
je 0x2933e
movq %rdi, %r15
movl %ecx, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movl %r13d, %ebp
shlq $0x3, %r13
testl %ebp, %ebp
movq $-0x1, %rdi
cmovnsq %r13, %rdi
callq 0x21040
movq %rax, %rbx
testl %ebp, %ebp
jle 0x2925b
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21490
leaq 0x10(%rsp), %r13
leaq 0x114f48(%rip), %r12    # 0x13e0d8
leaq 0x40(%rsp), %rbp
movq 0x18(%r15), %rax
movq (%rax,%r14,8), %rsi
movq (%rsi), %rax
movq %r13, %rdi
movsd 0xb0eb5(%rip), %xmm0    # 0xda060
callq *0x50(%rax)
movsd 0x30(%rsp), %xmm0
movsd (%r12), %xmm1
movapd %xmm1, %xmm2
xorpd 0xb137a(%rip), %xmm2    # 0xda540
ucomisd %xmm2, %xmm0
jb 0x291d2
ucomisd %xmm0, %xmm1
jae 0x29243
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6a85c
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0x4e35f
movsd (%r12), %xmm1
movapd %xmm0, %xmm2
cmpnlesd %xmm1, %xmm2
movapd %xmm2, %xmm3
andnpd %xmm1, %xmm2
xorpd 0xb1338(%rip), %xmm1    # 0xda540
cmpnlesd %xmm0, %xmm1
andpd %xmm0, %xmm3
orpd %xmm3, %xmm2
movapd %xmm1, %xmm3
andnpd %xmm2, %xmm3
andpd %xmm0, %xmm1
orpd %xmm3, %xmm1
movsd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd 0xb0de1(%rip), %xmm1    # 0xda018
ucomisd %xmm0, %xmm1
ja 0x29243
movsd %xmm0, (%rbx,%r14,8)
movq %r13, %rdi
callq 0x6dafc
incq %r14
movslq 0x24(%r15), %rax
cmpq %rax, %r14
jl 0x29195
addq $0x18, %r15
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x87594
movl 0xc(%r12), %eax
testl %eax, %eax
jle 0x292ef
movl $0x1, %r14d
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movq %rbp, %r12
incq %rbp
movslq %eax, %rcx
cmpq %rcx, %rbp
jge 0x292e7
movq %r14, %r13
movsd (%rbx,%r13,8), %xmm0
movsd (%rbx,%r12,8), %xmm1
movsd %xmm0, (%rsp)
movsd %xmm1, 0x38(%rsp)
ucomisd %xmm0, %xmm1
jbe 0x292dc
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x878c0
movsd (%rsp), %xmm0
movsd %xmm0, (%rbx,%r12,8)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rbx,%r13,8)
movl 0x1c(%rsp), %eax
incq %r13
cmpl %r13d, %eax
jg 0x29296
movslq %eax, %rcx
incq %r14
cmpq %rcx, %rbp
jl 0x29285
leaq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x87824
cmpl $0x0, 0x1c(%rsp)
jle 0x2932c
movl 0x8(%rsp), %r14d
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rdi
movq (%rdi), %rax
leal (%r14,%r15), %esi
callq *0x70(%rax)
incq %r15
movslq 0x1c(%rsp), %rax
cmpq %rax, %r15
jl 0x2930c
movq %rbx, %rdi
callq 0x21c20
leaq 0x10(%rsp), %rdi
callq 0x875f6
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29365
jmp 0x29365
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6dafc
jmp 0x29372
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x875f6
movq %rbx, %rdi
callq 0x220c0