// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3Scene.cpp
// Function: gaps::R3Scene::ReadSUNCGLightsFile(char const*)
int R3Scene::
ReadSUNCGLightsFile(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open lights file %s\n", filename);
    return 0;
  }

  // Read file
  char buffer[4096];
  int line_number = 0;
  while (fgets(buffer, 4096, fp)) {
    line_number++;
    char cmd[1024], reference_frame[1024];
    if (sscanf(buffer, "%s", cmd) != (unsigned int) 1) continue;
    if (cmd[0] == '#') continue;

    // Check cmd
    if (!strcmp(cmd, "directional_light")) {
      // Parse directional light 
      double intensity, r, g, b, dx, dy, dz;
      if (sscanf(buffer, "%s%s%lf%lf%lf%lf%lf%lf%lf", cmd, reference_frame,
        &intensity, &r, &g, &b, &dx, &dy, &dz) != (unsigned int) 9) {
        RNFail("Unable to parse directional light from line %d from %s\n", line_number, filename);
        return 0;
      }

      // Create directional light
      RNRgb color(r, g, b);
      R3Vector direction(dx, dy, dz);
      R3DirectionalLight light(direction, color, intensity);
      InsertCopiesOfLight(this, &light, reference_frame);
    }
    else if (!strcmp(cmd, "point_light")) {
      // Parse point light 
      double intensity, r, g, b, px, py, pz, ca, la, qa;
      if (sscanf(buffer, "%s%s%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", cmd, reference_frame,  
        &intensity, &r, &g, &b, &px, &py, &pz, &ca, &la, &qa) != (unsigned int) 12) {
        RNFail("Unable to parse point light from line %d from %s\n", line_number, filename);
        return 0;
      }

      // Create point light
      RNRgb color(r, g, b);
      R3Point position(px, py, pz);
      R3PointLight light(position, color, intensity, TRUE, ca, la, qa);
      InsertCopiesOfLight(this, &light, reference_frame);
    }
    else if (!strcmp(cmd, "spot_light")) {
      // Parse spot light 
      double intensity, r, g, b, px, py, pz, dx, dy, dz, sd, sc, ca, la, qa;
      if (sscanf(buffer, "%s%s%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", cmd, reference_frame,  
        &intensity, &r, &g, &b, &px, &py, &pz, &dx, &dy, &dz, &sd, &sc, &ca, &la, &qa) != (unsigned int) 17) {
        RNFail("Unable to parse spot light from line %d from %s\n", line_number, filename);
        return 0;
      }

      // Create spot light
      RNRgb color(r, g, b);
      R3Point position(px, py, pz);
      R3Vector direction(dx, dy, dz);
      R3SpotLight light(position, direction, color, sd, sc, intensity, TRUE, ca, la, qa);
      InsertCopiesOfLight(this, &light, reference_frame);
    }
    else if (!strcmp(cmd, "line_light")) {
      // Parse spot light 
      double intensity, r, g, b, px1, py1, pz1, px2, py2, pz2, dx, dy, dz, sd, sc, ca, la, qa;
      if (sscanf(buffer, "%s%s%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", cmd, reference_frame,  
        &intensity, &r, &g, &b, &px1, &py1, &pz1, &px2, &py2, &pz2, &dx, &dy, &dz, &sd, &sc, &ca, &la, &qa) != (unsigned int) 20) {
        RNFail("Unable to parse line light from line %d from %s\n", line_number, filename);
        return 0;
      }

      // Create spot light
      RNRgb color(r, g, b);
      R3Point position1(px1, py1, pz1);
      R3Point position2(px2, py2, pz2);
      R3Point position = 0.5 * (position1 + position2);
      R3Vector direction(dx, dy, dz);
      R3SpotLight light(position, direction, color, sd, sc, intensity, TRUE, ca, la, qa);
      InsertCopiesOfLight(this, &light, reference_frame);
    }
    else {
      RNFail("Unrecognized light type %s at line %d of %s\n", cmd, line_number, filename);
      return 0;
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1ec8, %rsp           # imm = 0x1EC8
movq %rdi, 0x1eb8(%rsp)
movq %rsi, 0x1eb0(%rsp)
movq 0x1eb8(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x1eb0(%rsp), %rdi
leaq 0x153fb3(%rip), %rsi    # 0x1952a3
callq 0x22c10
movq %rax, 0x1ea8(%rsp)
cmpq $0x0, 0x1ea8(%rsp)
jne 0x4132e
movq 0x1eb0(%rsp), %rsi
leaq 0x14e0ef(%rip), %rdi    # 0x18f406
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x1ec4(%rsp)
jmp 0x41dfc
movl $0x0, 0xe9c(%rsp)
leaq 0xea0(%rsp), %rdi
movq 0x1ea8(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x230f0
cmpq $0x0, %rax
je 0x41de4
movl 0xe9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe9c(%rsp)
leaq 0xea0(%rsp), %rdi
leaq 0xa90(%rsp), %rdx
leaq 0x16cec2(%rip), %rsi    # 0x1ae247
movb $0x0, %al
callq 0x229a0
cmpl $0x1, %eax
je 0x41393
jmp 0x41339
movsbl 0xa90(%rsp), %eax
cmpl $0x23, %eax
jne 0x413a2
jmp 0x41339
leaq 0xa90(%rsp), %rdi
leaq 0x14e074(%rip), %rsi    # 0x18f425
callq 0x22ec0
cmpl $0x0, %eax
jne 0x41551
leaq 0xea0(%rsp), %rdi
leaq 0xa90(%rsp), %rdx
leaq 0x690(%rsp), %rcx
leaq 0x14e059(%rip), %rsi    # 0x18f437
leaq 0x688(%rsp), %r8
leaq 0x680(%rsp), %r9
leaq 0x678(%rsp), %r14
leaq 0x670(%rsp), %rbx
leaq 0x668(%rsp), %r11
leaq 0x660(%rsp), %r10
leaq 0x658(%rsp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0x9, %eax
je 0x41467
movl 0xe9c(%rsp), %esi
movq 0x1eb0(%rsp), %rdx
leaq 0x14e001(%rip), %rdi    # 0x18f451
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x1ec4(%rsp)
jmp 0x41dfc
movsd 0x680(%rsp), %xmm0
movsd 0x678(%rsp), %xmm1
movsd 0x670(%rsp), %xmm2
leaq 0x640(%rsp), %rdi
movq %rdi, 0x168(%rsp)
callq 0xef340
movsd 0x668(%rsp), %xmm0
movsd 0x660(%rsp), %xmm1
movsd 0x658(%rsp), %xmm2
leaq 0x628(%rsp), %rdi
movq %rdi, 0x160(%rsp)
callq 0xcd9d0
movq 0x160(%rsp), %rsi
movq 0x168(%rsp), %rdx
movsd 0x688(%rsp), %xmm0
leaq 0x5c0(%rsp), %rdi
movq %rdi, 0x170(%rsp)
movl $0x1, %ecx
callq 0x62270
movq 0x178(%rsp), %rdi
movq 0x170(%rsp), %rsi
leaq 0x690(%rsp), %rdx
callq 0x41e30
jmp 0x41519
leaq 0x5c0(%rsp), %rdi
callq 0x49760
jmp 0x41ddf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5b8(%rsp)
movl %eax, 0x5b4(%rsp)
leaq 0x5c0(%rsp), %rdi
callq 0x49760
jmp 0x41e15
leaq 0xa90(%rsp), %rdi
leaq 0x14d73d(%rip), %rsi    # 0x18ec9d
callq 0x22ec0
cmpl $0x0, %eax
jne 0x41742
leaq 0xea0(%rsp), %rdi
leaq 0xa90(%rsp), %rdx
leaq 0x690(%rsp), %rcx
leaq 0x14defc(%rip), %rsi    # 0x18f489
leaq 0x5a8(%rsp), %r8
leaq 0x5a0(%rsp), %r9
leaq 0x598(%rsp), %r13
leaq 0x590(%rsp), %r12
leaq 0x588(%rsp), %r15
leaq 0x580(%rsp), %r14
leaq 0x578(%rsp), %rbx
leaq 0x570(%rsp), %r11
leaq 0x568(%rsp), %r10
leaq 0x560(%rsp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0xc, %eax
je 0x4163d
movl 0xe9c(%rsp), %esi
movq 0x1eb0(%rsp), %rdx
leaq 0x14de86(%rip), %rdi    # 0x18f4ac
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x1ec4(%rsp)
jmp 0x41dfc
movsd 0x5a0(%rsp), %xmm0
movsd 0x598(%rsp), %xmm1
movsd 0x590(%rsp), %xmm2
leaq 0x548(%rsp), %rdi
movq %rdi, 0x150(%rsp)
callq 0xef340
movsd 0x588(%rsp), %xmm0
movsd 0x580(%rsp), %xmm1
movsd 0x578(%rsp), %xmm2
leaq 0x530(%rsp), %rdi
movq %rdi, 0x148(%rsp)
callq 0xcc870
movq 0x148(%rsp), %rsi
movq 0x150(%rsp), %rdx
movsd 0x5a8(%rsp), %xmm0
movsd 0x570(%rsp), %xmm1
movsd 0x568(%rsp), %xmm2
movsd 0x560(%rsp), %xmm3
leaq 0x4b0(%rsp), %rdi
movq %rdi, 0x158(%rsp)
movl $0x1, %ecx
callq 0x61400
movq 0x178(%rsp), %rdi
movq 0x158(%rsp), %rsi
leaq 0x690(%rsp), %rdx
callq 0x41e30
jmp 0x4170a
leaq 0x4b0(%rsp), %rdi
callq 0x49770
jmp 0x41ddd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5b8(%rsp)
movl %eax, 0x5b4(%rsp)
leaq 0x4b0(%rsp), %rdi
callq 0x49770
jmp 0x41e15
leaq 0xa90(%rsp), %rdi
leaq 0x14d58d(%rip), %rsi    # 0x18ecde
callq 0x22ec0
cmpl $0x0, %eax
jne 0x41a0f
leaq 0xea0(%rsp), %rdi
leaq 0xa90(%rsp), %rdx
leaq 0x690(%rsp), %rcx
leaq 0x14dd60(%rip), %rsi    # 0x18f4de
leaq 0x4a8(%rsp), %r8
leaq 0x4a0(%rsp), %r9
leaq 0x498(%rsp), %rax
movq %rax, 0x120(%rsp)
leaq 0x490(%rsp), %rax
movq %rax, 0x128(%rsp)
leaq 0x488(%rsp), %rax
movq %rax, 0x130(%rsp)
leaq 0x480(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x478(%rsp), %rbp
leaq 0x470(%rsp), %r13
leaq 0x468(%rsp), %r12
leaq 0x460(%rsp), %r15
leaq 0x458(%rsp), %r14
leaq 0x450(%rsp), %rbx
leaq 0x448(%rsp), %r11
leaq 0x440(%rsp), %r10
leaq 0x438(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x120(%rsp), %rax
movq %rax, (%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x140(%rsp), %rax
movq %rbp, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
movq %r11, 0x50(%rsp)
movq %r10, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0x11, %eax
je 0x418bf
movl 0xe9c(%rsp), %esi
movq 0x1eb0(%rsp), %rdx
leaq 0x14dc68(%rip), %rdi    # 0x18f510
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x1ec4(%rsp)
jmp 0x41dfc
movsd 0x4a0(%rsp), %xmm0
movsd 0x498(%rsp), %xmm1
movsd 0x490(%rsp), %xmm2
leaq 0x420(%rsp), %rdi
movq %rdi, 0x110(%rsp)
callq 0xef340
movsd 0x488(%rsp), %xmm0
movsd 0x480(%rsp), %xmm1
movsd 0x478(%rsp), %xmm2
leaq 0x408(%rsp), %rdi
movq %rdi, 0x100(%rsp)
callq 0xcc870
movsd 0x470(%rsp), %xmm0
movsd 0x468(%rsp), %xmm1
movsd 0x460(%rsp), %xmm2
leaq 0x3f0(%rsp), %rdi
movq %rdi, 0x108(%rsp)
callq 0xcd9d0
movq 0x100(%rsp), %rsi
movq 0x108(%rsp), %rdx
movq 0x110(%rsp), %rcx
movsd 0x458(%rsp), %xmm0
movsd 0x450(%rsp), %xmm1
movsd 0x4a8(%rsp), %xmm2
movsd 0x448(%rsp), %xmm3
movsd 0x440(%rsp), %xmm4
movsd 0x438(%rsp), %xmm5
leaq 0x348(%rsp), %rdi
movq %rdi, 0x118(%rsp)
movl $0x1, %r8d
callq 0x60ce0
movq 0x178(%rsp), %rdi
movq 0x118(%rsp), %rsi
leaq 0x690(%rsp), %rdx
callq 0x41e30
jmp 0x419d7
leaq 0x348(%rsp), %rdi
callq 0x49780
jmp 0x41ddb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5b8(%rsp)
movl %eax, 0x5b4(%rsp)
leaq 0x348(%rsp), %rdi
callq 0x49780
jmp 0x41e15
leaq 0xa90(%rsp), %rdi
leaq 0x14db23(%rip), %rsi    # 0x18f541
callq 0x22ec0
cmpl $0x0, %eax
jne 0x41da7
leaq 0xea0(%rsp), %rdi
leaq 0xa90(%rsp), %rdx
leaq 0x690(%rsp), %rcx
leaq 0x14db01(%rip), %rsi    # 0x18f54c
leaq 0x340(%rsp), %r8
leaq 0x338(%rsp), %r9
leaq 0x330(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x328(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0x320(%rsp), %rax
movq %rax, 0xd0(%rsp)
leaq 0x318(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x310(%rsp), %rax
movq %rax, 0xe0(%rsp)
leaq 0x308(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0x300(%rsp), %rax
movq %rax, 0xf0(%rsp)
leaq 0x2f8(%rsp), %rbp
leaq 0x2f0(%rsp), %r13
leaq 0x2e8(%rsp), %r12
leaq 0x2e0(%rsp), %r15
leaq 0x2d8(%rsp), %r14
leaq 0x2d0(%rsp), %rbx
leaq 0x2c8(%rsp), %r11
leaq 0x2c0(%rsp), %r10
leaq 0x2b8(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, (%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xf8(%rsp), %rax
movq %rbp, 0x38(%rsp)
movq %r13, 0x40(%rsp)
movq %r12, 0x48(%rsp)
movq %r15, 0x50(%rsp)
movq %r14, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
movq %r11, 0x68(%rsp)
movq %r10, 0x70(%rsp)
movq %rax, 0x78(%rsp)
movb $0x0, %al
callq 0x229a0
cmpl $0x14, %eax
je 0x41be3
movl 0xe9c(%rsp), %esi
movq 0x1eb0(%rsp), %rdx
leaq 0x14d9bb(%rip), %rdi    # 0x18f587
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x1ec4(%rsp)
jmp 0x41dfc
movsd 0x338(%rsp), %xmm0
movsd 0x330(%rsp), %xmm1
movsd 0x328(%rsp), %xmm2
leaq 0x2a0(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
callq 0xef340
movsd 0x320(%rsp), %xmm0
movsd 0x318(%rsp), %xmm1
movsd 0x310(%rsp), %xmm2
leaq 0x288(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0xcc870
movsd 0x308(%rsp), %xmm0
movsd 0x300(%rsp), %xmm1
movsd 0x2f8(%rsp), %xmm2
leaq 0x270(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0xcc870
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0x240(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0xcd760
movq 0x98(%rsp), %rsi
leaq 0x258(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
movsd 0x14c368(%rip), %xmm0   # 0x18e020
callq 0x49790
movsd 0x2f0(%rsp), %xmm0
movsd 0x2e8(%rsp), %xmm1
movsd 0x2e0(%rsp), %xmm2
leaq 0x228(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0xcd9d0
movq 0xa0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0xb0(%rsp), %rcx
movsd 0x2d8(%rsp), %xmm0
movsd 0x2d0(%rsp), %xmm1
movsd 0x340(%rsp), %xmm2
movsd 0x2c8(%rsp), %xmm3
movsd 0x2c0(%rsp), %xmm4
movsd 0x2b8(%rsp), %xmm5
leaq 0x180(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
movl $0x1, %r8d
callq 0x60ce0
movq 0x178(%rsp), %rdi
movq 0xb8(%rsp), %rsi
leaq 0x690(%rsp), %rdx
callq 0x41e30
jmp 0x41d75
leaq 0x180(%rsp), %rdi
callq 0x49780
jmp 0x41dd9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5b8(%rsp)
movl %eax, 0x5b4(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x49780
jmp 0x41e15
leaq 0xa90(%rsp), %rsi
movl 0xe9c(%rsp), %edx
movq 0x1eb0(%rsp), %rcx
leaq 0x14d7f3(%rip), %rdi    # 0x18f5b8
movb $0x0, %al
callq 0xf3b00
movl $0x0, 0x1ec4(%rsp)
jmp 0x41dfc
jmp 0x41ddb
jmp 0x41ddd
jmp 0x41ddf
jmp 0x41339
movq 0x1ea8(%rsp), %rdi
callq 0x229d0
movl $0x1, 0x1ec4(%rsp)
movl 0x1ec4(%rsp), %eax
addq $0x1ec8, %rsp           # imm = 0x1EC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5b8(%rsp), %rdi
callq 0x23290
nopw %cs:(%rax,%rax)