// File: /workspace/github_cpp_makefile_star2_O2/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), %r14
testq %r14, %r14
je 0x2827e
subl %edx, %ecx
incl %ecx
je 0x2827e
movq %rdi, %r12
movl %ecx, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movq %r14, %rax
shlq $0x3, %rax
xorl %r15d, %r15d
testl %r14d, %r14d
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
movl $0x0, %r13d
cmovgl %r14d, %r13d
callq 0x21040
movq %rax, %rbx
cmpq %r15, %r13
je 0x280c7
andq $0x0, (%rbx,%r15,8)
incq %r15
jmp 0x280b8
leaq 0x18(%r12), %r13
xorl %r15d, %r15d
leaq 0x10(%rsp), %rbp
movslq %r14d, %rax
cmpq %rax, %r15
jge 0x281b2
movq (%r13), %rax
movq (%rax,%r15,8), %rsi
movq (%rsi), %rax
movq %rbp, %rdi
movsd 0x9cf6a(%rip), %xmm0    # 0xc5060
callq *0x50(%rax)
movsd 0x30(%rsp), %xmm0
leaq 0x106ff2(%rip), %rax    # 0x12f0f8
movsd (%rax), %xmm1
movapd %xmm1, %xmm2
xorpd 0x9d41a(%rip), %xmm2    # 0xc5530
ucomisd %xmm2, %xmm0
jb 0x28122
ucomisd %xmm0, %xmm1
jae 0x2819d
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x61a94
movq %r14, %rdi
movq (%rsp), %rsi
callq 0x483ab
leaq 0x106fb3(%rip), %rax    # 0x12f0f8
movsd (%rax), %xmm1
movapd %xmm0, %xmm2
cmpnlesd %xmm1, %xmm2
movapd %xmm2, %xmm3
andnpd %xmm1, %xmm2
xorpd 0x9d3ce(%rip), %xmm1    # 0xc5530
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 0x9ce87(%rip), %xmm1    # 0xc5018
ucomisd %xmm0, %xmm1
ja 0x2819d
movsd %xmm0, (%rbx,%r15,8)
movq %rbp, %rdi
callq 0x6499a
incq %r15
movl 0x24(%r12), %r14d
jmp 0x280d4
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x7c1ce
pushq $0x1
popq %r13
xorl %r15d, %r15d
movl 0xc(%r14), %eax
movslq %eax, %rcx
cmpq %rcx, %r15
jge 0x28234
leaq 0x1(%r15), %rbp
movq %r13, %r12
cmpl %r12d, %eax
jle 0x2822c
movsd (%rbx,%r12,8), %xmm0
movsd (%rbx,%r15,8), %xmm1
movsd %xmm0, (%rsp)
movsd %xmm1, 0x38(%rsp)
ucomisd %xmm0, %xmm1
jbe 0x28227
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x7c470
movsd (%rsp), %xmm0
movsd %xmm0, (%rbx,%r15,8)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rbx,%r12,8)
movl 0x1c(%rsp), %eax
incq %r12
jmp 0x281dc
incq %r13
movq %rbp, %r15
jmp 0x281cd
leaq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x7c410
movl 0x8(%rsp), %r14d
xorl %r15d, %r15d
movslq 0x1c(%rsp), %rax
cmpq %rax, %r15
jge 0x2826c
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rdi
movq (%rdi), %rax
leal (%r14,%r15), %esi
callq *0x70(%rax)
incq %r15
jmp 0x2824a
movq %rbx, %rdi
callq 0x21c20
leaq 0x10(%rsp), %rdi
callq 0x7c234
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x282a3
jmp 0x282a3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6499a
jmp 0x282b0
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7c234
movq %rbx, %rdi
callq 0x220e0