// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1e8, %rsp            # imm = 0x1E8
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movl (%rsi), %eax
movl %eax, 0x1c(%rsp)
movl 0x8(%rsi), %eax
movl %eax, 0x28(%rsp)
testq %rdi, %rdi
je 0x46ca
leaq 0x8f05(%rip), %rsi      # 0xd5b1
callq 0x4230
movq %rax, %r12
testq %rax, %rax
jne 0x46d4
leaq 0x89c0(%rip), %rdi      # 0xd080
callq 0xab82
xorl %r12d, %r12d
jmp 0x46d4
movq 0xe907(%rip), %rax      # 0x12fd8
movq (%rax), %r12
leaq 0xe0(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdx
callq 0x42b0
testq %rax, %rax
je 0x4ae5
leaq 0xe0(%rsp), %rbp
fldl 0x8908(%rip)            # 0xd008
fstpt 0x2c(%rsp)
movq %rbp, %rdi
leaq 0x8986(%rip), %rsi      # 0xd094
leaq 0xd8(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x8(%rsp), %r8
leaq 0x10(%rsp), %r9
xorl %eax, %eax
leaq 0xc8(%rsp), %r10
pushq %r10
leaq 0xd8(%rsp), %r10
pushq %r10
callq 0x41f0
addq $0x10, %rsp
cmpl $0x6, %eax
je 0x4755
leaq 0x895c(%rip), %rdi      # 0xd0ac
callq 0xab82
fldt 0x2c(%rsp)
fld %st(0)
fmull 0x8(%rsp)
fdivs 0x8903(%rip)            # 0xd068
fstpl 0xb0(%rsp)
movsd 0xb0(%rsp), %xmm1
fmull 0x10(%rsp)
movsd %xmm1, 0x8(%rsp)
fdivs 0x88e7(%rip)            # 0xd06c
fstpl 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm3
movsd %xmm3, 0x10(%rsp)
ucomisd 0x8865(%rip), %xmm1     # 0xd008
jbe 0x47d1
movsd %xmm1, 0xa0(%rsp)
fldl 0xa0(%rsp)
faddl 0x8855(%rip)            # 0xd010
fstpl 0x98(%rsp)
movsd 0x98(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
xorpd %xmm2, %xmm2
movsd 0xea43(%rip), %xmm0     # 0x13220
ucomisd %xmm2, %xmm0
jne 0x47e9
jnp 0x4921
movsd 0xd0(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x881a(%rip), %xmm4     # 0xd018
mulsd %xmm4, %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
mulsd 0xc8(%rsp), %xmm0
mulsd %xmm4, %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rsp)
ucomisd 0x87f5(%rip), %xmm0     # 0xd020
jbe 0x4879
movsd %xmm0, 0x90(%rsp)
fldt 0x2c(%rsp)
fld %st(0)
fsubl 0x90(%rsp)
fstpl 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm2, 0x88(%rsp)
faddl 0x88(%rsp)
fstpl 0x78(%rsp)
movsd 0x78(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x87a7(%rip), %xmm1     # 0xd028
ucomisd %xmm0, %xmm1
jbe 0x48c7
movsd %xmm0, 0x70(%rsp)
fldl 0x70(%rsp)
faddl 0x8799(%rip)            # 0xd030
fstpl 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm2, 0x68(%rsp)
fldl 0x68(%rsp)
faddl 0x8751(%rip)            # 0xd008
fstpl 0x58(%rsp)
movsd 0x58(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
ucomisd 0x8739(%rip), %xmm2     # 0xd008
jbe 0x48f1
movsd %xmm2, 0x50(%rsp)
fldl 0x50(%rsp)
faddl 0x872f(%rip)            # 0xd010
fstpl 0x48(%rsp)
movsd 0x48(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x8737(%rip), %xmm1     # 0xd030
ucomisd %xmm2, %xmm1
jbe 0x4927
movsd %xmm2, 0x40(%rsp)
fldl 0x40(%rsp)
faddl 0x8729(%rip)            # 0xd038
fstpl 0x38(%rsp)
movsd 0x38(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
jmp 0x492b
movapd %xmm3, %xmm0
jmp 0x492b
movapd %xmm2, %xmm1
xorpd 0x873d(%rip), %xmm1     # 0xd070
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0xc0(%rsp), %rcx
leaq 0xb8(%rsp), %r8
callq 0x6e42
testb %al, %al
je 0x4acc
movsd 0xc0(%rsp), %xmm0
callq 0x4310
cvttsd2si %xmm0, %r13d
movsd 0xb8(%rsp), %xmm0
callq 0x4310
testl %r13d, %r13d
js 0x4acc
cmpl 0x1c(%rsp), %r13d
jge 0x4acc
cvttsd2si %xmm0, %eax
testl %eax, %eax
js 0x4acc
cmpl 0x28(%rsp), %eax
jge 0x4acc
imull 0x1c(%rsp), %eax
movl %r13d, %r13d
movsd 0xd8(%rsp), %xmm0
subsd 0xe823(%rip), %xmm0     # 0x131e0
addq %rax, %r13
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0xe818(%rip), %xmm0     # 0x131e8
callq 0x4320
mulsd 0x8663(%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 0x4a23
movzbl 0x1(%rax,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
ja 0x4a23
movzbl 0x2(%rax,%r13), %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4acc
cmpb $0x1, 0xe7ee(%rip)      # 0x13218
jne 0x4aaa
movsd 0x20(%rsp), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x4a6e
movsd 0xe7ac(%rip), %xmm3     # 0x131f0
ucomisd %xmm3, %xmm1
jbe 0x4a54
movsd %xmm3, 0x20(%rsp)
movapd %xmm3, %xmm1
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
divsd %xmm3, %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm1
movapd %xmm2, %xmm0
jmp 0x4ab2
ucomisd %xmm1, %xmm2
jbe 0x4aaa
movsd 0xe774(%rip), %xmm2     # 0x131f0
movapd %xmm2, %xmm3
xorpd 0x85e8(%rip), %xmm3     # 0xd070
ucomisd %xmm1, %xmm3
jbe 0x4a98
movsd %xmm3, 0x20(%rsp)
movapd %xmm3, %xmm1
addsd %xmm2, %xmm1
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm2
jmp 0x4ab2
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 0x42b0
testq %rax, %rax
jne 0x4704
movq %r12, %rdi
callq 0x4200
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq