// File: /workspace/github_cpp_makefile_star2_O1/matthewarcus[P]mmps/stars.cpp
// Function: AddStars(char const*, Image&, Transform&, TransformParams const&)
void AddStars(const char* starlist, Image& image,
	      Transform& transform, const TransformParams& params)
{
  int width = image.Width();
  int height = image.Height();

  FILE* starfile;
  if (starlist == NULL) {
    starfile = stdin;
  } else {
    starfile = fopen(starlist, "r");
    if (starfile == NULL) {
      syserror("Can't open starlist");
    }
  }
  char linebuffer[LINESIZE];
  int i = 0;
  while (fgets(linebuffer, LINESIZE, starfile) != NULL) {
    // Read the appropriate entry in the star list
    double mag, bv, ra, dec, raproper, decproper;
    if (sscanf (linebuffer, "%lf %lf %lf %lf %lf %lf", 
		&mag, &bv, &ra, &dec,
		&raproper, &decproper) != 6) {
      syserror("sscanf failure -bad input?");
    }

    // Change right ascension and declination to sensible units (radians)
    ra = pi * ra / 12.0;
    dec = pi * dec / 180.0;

    // And put RA into to the expected range
    if (ra > pi) { ra -= 2 * pi; }

    // Apply proper motion, if required
    if (year != 0.0) {
      // Allegedly, both raproper and dec proper are in arcseconds per year
      ra += year * raproper * asecperyear;
      dec += year * decproper * asecperyear;
      // And put back into range
      if (dec > piovertwo) { dec = pi - dec; ra += pi; }
      if (dec < -piovertwo) { dec = -pi + dec; ra += pi; }
      if (ra > pi) { ra -= 2 * pi; }
      if (ra < -pi) { ra += 2 * pi; }
    }

    double x,y;
    bool show = transform.MapXY(image, params, dec, -ra, x, y);
    if (show) {
      int ix = int(floor(x));
      int iy = int(floor(y));
      if (0 <= ix && ix < width && 0 <= iy && iy < height) {
	int index = iy * width + ix;
	double value = 255 * convertmag(mag);
	Rgb current;
	image.GetRgb(index, current);
	if (value > current.r || value > current.g || value > current.b) {
	  // Adjust for BV color index
	  double r = value;
	  double g = value;
	  double b = value;
	  if (dobv) {
	    if (bv > 0) {
	      if (bv > bvfact) { bv = bvfact; }
	      g *= (bvfact - bv)/bvfact;
	      b *= (bvfact - bv)/bvfact;
	    } else if (bv < 0) {
	      if (bv < -bvfact) { bv = -bvfact; }
	      r *= (bvfact + bv)/bvfact;
	      g *= (bvfact + bv)/bvfact;
	    }
	  }
	  image.SetRgb(index, Rgb(Color(r), Color(g), Color(b)));
	}
      }
    }
    i++;
  }
  fclose(starfile);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movl (%rsi), %eax
movl %eax, 0x24(%rsp)
movl 0x8(%rsi), %eax
movl %eax, 0x28(%rsp)
testq %rdi, %rdi
je 0x46da
leaq 0x8e8d(%rip), %rsi      # 0xd549
callq 0x4240
movq %rax, %r12
testq %rax, %rax
jne 0x46e4
leaq 0x89b0(%rip), %rdi      # 0xd080
callq 0xa912
xorl %r12d, %r12d
jmp 0x46e4
movq 0xe8f7(%rip), %rax      # 0x12fd8
movq (%rax), %r12
leaq 0xa0(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdx
callq 0x42c0
testq %rax, %rax
je 0x4a7f
leaq 0xa0(%rsp), %rbp
fldl 0x88f8(%rip)            # 0xd008
fstpt 0x2c(%rsp)
movq %rbp, %rdi
leaq 0x8976(%rip), %rsi      # 0xd094
leaq 0x98(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x10(%rsp), %r9
xorl %eax, %eax
leaq 0x88(%rsp), %r10
pushq %r10
leaq 0x98(%rsp), %r10
pushq %r10
callq 0x4200
addq $0x10, %rsp
cmpl $0x6, %eax
je 0x4765
leaq 0x894c(%rip), %rdi      # 0xd0ac
callq 0xa912
fldt 0x2c(%rsp)
fld %st(0)
fmull 0x8(%rsp)
fdivs 0x88f3(%rip)            # 0xd068
fstpl 0x70(%rsp)
movsd 0x70(%rsp), %xmm1
fmull 0x10(%rsp)
movsd %xmm1, 0x8(%rsp)
fdivs 0x88dd(%rip)            # 0xd06c
fstpl 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
ucomisd 0x8861(%rip), %xmm1     # 0xd008
xorpd %xmm2, %xmm2
jbe 0x47c1
movsd %xmm1, 0x60(%rsp)
fldl 0x60(%rsp)
faddl 0x8853(%rip)            # 0xd010
fstpl 0x8(%rsp)
movsd 0xea5f(%rip), %xmm1     # 0x13228
ucomisd %xmm2, %xmm1
jne 0x47d5
jnp 0x48bf
movsd 0x90(%rsp), %xmm2
mulsd %xmm1, %xmm2
movsd 0x882e(%rip), %xmm3     # 0xd018
mulsd %xmm3, %xmm2
addsd 0x8(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
mulsd 0x88(%rsp), %xmm1
mulsd %xmm3, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd 0x8807(%rip), %xmm1     # 0xd020
jbe 0x483d
movsd %xmm1, 0x58(%rsp)
fldt 0x2c(%rsp)
fld %st(0)
fsubl 0x58(%rsp)
fstpl 0x10(%rsp)
movsd %xmm2, 0x50(%rsp)
faddl 0x50(%rsp)
fstpl 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x87dd(%rip), %xmm1     # 0xd028
ucomisd %xmm0, %xmm1
jbe 0x4873
movsd %xmm0, 0x48(%rsp)
fldl 0x48(%rsp)
faddl 0x87cf(%rip)            # 0xd030
fstpl 0x10(%rsp)
fldl 0x8(%rsp)
faddl 0x8799(%rip)            # 0xd008
fstpl 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
ucomisd 0x8787(%rip), %xmm0     # 0xd008
jbe 0x4897
movsd %xmm0, 0x40(%rsp)
fldl 0x40(%rsp)
faddl 0x877d(%rip)            # 0xd010
fstpl 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
movsd 0x878b(%rip), %xmm1     # 0xd030
ucomisd %xmm0, %xmm1
jbe 0x48bf
movsd %xmm0, 0x38(%rsp)
fldl 0x38(%rsp)
faddl 0x877d(%rip)            # 0xd038
fstpl 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
xorpd 0x879d(%rip), %xmm1     # 0xd070
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0x80(%rsp), %rcx
leaq 0x78(%rsp), %r8
callq 0x6ab0
testb %al, %al
je 0x4a66
movsd 0x80(%rsp), %xmm0
callq 0x4320
cvttsd2si %xmm0, %r13d
movsd 0x78(%rsp), %xmm0
callq 0x4320
testl %r13d, %r13d
js 0x4a66
cmpl 0x24(%rsp), %r13d
jge 0x4a66
cvttsd2si %xmm0, %eax
testl %eax, %eax
js 0x4a66
cmpl 0x28(%rsp), %eax
jge 0x4a66
imull 0x24(%rsp), %eax
movl %r13d, %r13d
movsd 0x98(%rsp), %xmm0
subsd 0xe891(%rip), %xmm0     # 0x131e8
addq %rax, %r13
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0xe886(%rip), %xmm0     # 0x131f0
callq 0x4330
mulsd 0x86c9(%rip), %xmm0     # 0xd040
movq 0x10(%r15), %rax
addq %r13, %rax
addq %r13, %rax
movzbl (%rax,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
ja 0x49bd
movzbl 0x1(%rax,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
ja 0x49bd
movzbl 0x2(%rax,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4a66
cmpb $0x1, 0xe85c(%rip)      # 0x13220
jne 0x4a44
movsd 0x18(%rsp), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x4a06
movsd 0xe81a(%rip), %xmm3     # 0x131f8
ucomisd %xmm3, %xmm1
jbe 0x49ea
movsd %xmm3, 0x18(%rsp)
movapd %xmm3, %xmm2
subsd 0x18(%rsp), %xmm2
divsd %xmm3, %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
jmp 0x4a4c
ucomisd %xmm1, %xmm2
jbe 0x4a44
movsd 0xe7e4(%rip), %xmm2     # 0x131f8
movapd %xmm2, %xmm3
xorpd 0x8650(%rip), %xmm3     # 0xd070
ucomisd %xmm1, %xmm3
jbe 0x4a2c
movsd %xmm3, 0x18(%rsp)
movsd 0x18(%rsp), %xmm1
addsd %xmm2, %xmm1
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm2
jmp 0x4a4c
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
cvttsd2si %xmm1, %ecx
cvttsd2si %xmm2, %edx
cvttsd2si %xmm0, %esi
movb %cl, (%rax,%r13)
movb %dl, 0x1(%rax,%r13)
movb %sil, 0x2(%rax,%r13)
movq %rbp, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdx
callq 0x42c0
testq %rax, %rax
jne 0x4714
movq %r12, %rdi
callq 0x4210
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq