// File: /workspace/github_cpp_makefile_star2_O1/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, 0x38(%rsp)
movslq 0x24(%rdi), %r13
testq %r13, %r13
je 0x2940c
subl %edx, %ecx
incl %ecx
je 0x2940c
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 0x29250
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21490
cmpl $0x0, 0x24(%r15)
jle 0x29336
xorl %r14d, %r14d
leaq 0x10(%rsp), %r13
leaq 0x116e6e(%rip), %r12    # 0x1400d8
leaq 0x40(%rsp), %rbp
movq 0x18(%r15), %rax
movq (%rax,%r14,8), %rsi
movq (%rsi), %rax
movq %r13, %rdi
movsd 0xb2ddb(%rip), %xmm0    # 0xdc060
callq *0x50(%rax)
movsd 0x30(%rsp), %xmm0
movsd (%r12), %xmm1
movapd %xmm1, %xmm2
xorpd 0xb3290(%rip), %xmm2    # 0xdc530
ucomisd %xmm2, %xmm0
jb 0x292ac
ucomisd %xmm0, %xmm1
jae 0x2931e
movq %rbp, %rdi
movq %r13, %rsi
callq 0x6b62c
movq %rbp, %rdi
movq 0x38(%rsp), %rsi
callq 0x4eeff
movsd (%r12), %xmm1
movapd %xmm0, %xmm2
cmpnlesd %xmm1, %xmm2
movapd %xmm2, %xmm3
andnpd %xmm1, %xmm2
xorpd 0xb324d(%rip), %xmm1    # 0xdc530
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 0xb2d06(%rip), %xmm1    # 0xdc018
ucomisd %xmm0, %xmm1
ja 0x2931e
movsd %xmm0, (%rbx,%r14,8)
movq %r13, %rdi
callq 0x6e944
incq %r14
movslq 0x24(%r15), %rax
cmpq %rax, %r14
jl 0x2926f
addq $0x18, %r15
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x886c4
cmpl $0x0, 0xc(%r12)
jle 0x293bd
movl $0x1, %r14d
xorl %ebp, %ebp
leaq 0x10(%rsp), %r15
movq %rbp, %r12
incq %rbp
movslq 0x1c(%rsp), %rax
cmpq %rax, %rbp
jge 0x293b0
movq %r14, %r13
movsd (%rbx,%r12,8), %xmm0
ucomisd (%rbx,%r13,8), %xmm0
jbe 0x293a6
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x889ce
movsd (%rbx,%r12,8), %xmm0
movsd (%rbx,%r13,8), %xmm1
movsd %xmm1, (%rbx,%r12,8)
movsd %xmm0, (%rbx,%r13,8)
incq %r13
cmpl %r13d, 0x1c(%rsp)
jg 0x29372
movslq 0x1c(%rsp), %rax
incq %r14
cmpq %rax, %rbp
jl 0x2935f
leaq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x88934
cmpl $0x0, 0x1c(%rsp)
jle 0x293fa
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 0x293da
movq %rbx, %rdi
callq 0x21c30
leaq 0x10(%rsp), %rdi
callq 0x88726
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29433
jmp 0x29433
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6e944
jmp 0x29440
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x88726
movq %rbx, %rdi
callq 0x220d0