// File: /workspace/github_cpp_makefile_star2_O2/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 0x456a
leaq 0x8025(%rip), %rsi      # 0xc571
callq 0x4240
movq %rax, %r12
testq %rax, %rax
jne 0x4574
leaq 0x7b20(%rip), %rdi      # 0xc080
callq 0x9e44
xorl %r12d, %r12d
jmp 0x4574
movq 0xda67(%rip), %rax      # 0x11fd8
movq (%rax), %r12
leaq 0xe0(%rsp), %r13
fldl 0x7a86(%rip)            # 0xc008
fstpt 0x2c(%rsp)
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdx
callq 0x42e0
testq %rax, %rax
je 0x4958
movq %r13, %rdi
leaq 0x7aeb(%rip), %rsi      # 0xc094
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 0x4200
popq %rcx
popq %rdx
cmpl $0x6, %eax
je 0x45ee
leaq 0x7ac3(%rip), %rdi      # 0xc0ac
callq 0x9e44
fldt 0x2c(%rsp)
fld %st(0)
fmull 0x8(%rsp)
fdivs 0x7a6a(%rip)            # 0xc068
fstpl 0xb0(%rsp)
movsd 0xb0(%rsp), %xmm1
fmull 0x10(%rsp)
movsd %xmm1, 0x8(%rsp)
fdivs 0x7a4e(%rip)            # 0xc06c
fstpl 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm3
movsd %xmm3, 0x10(%rsp)
ucomisd 0x79cc(%rip), %xmm1     # 0xc008
jbe 0x466a
movsd %xmm1, 0xa0(%rsp)
fldl 0xa0(%rsp)
faddl 0x79bc(%rip)            # 0xc010
fstpl 0x98(%rsp)
movsd 0x98(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
xorpd %xmm2, %xmm2
movsd 0xdbc2(%rip), %xmm0     # 0x12238
ucomisd %xmm2, %xmm0
jne 0x4682
jnp 0x47ba
movsd 0xd0(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd 0x7981(%rip), %xmm4     # 0xc018
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 0x795c(%rip), %xmm0     # 0xc020
jbe 0x4712
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 0x790e(%rip), %xmm1     # 0xc028
ucomisd %xmm0, %xmm1
jbe 0x4760
movsd %xmm0, 0x70(%rsp)
fldl 0x70(%rsp)
faddl 0x7900(%rip)            # 0xc030
fstpl 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm2, 0x68(%rsp)
fldl 0x68(%rsp)
faddl 0x78b8(%rip)            # 0xc008
fstpl 0x58(%rsp)
movsd 0x58(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
ucomisd 0x78a0(%rip), %xmm2     # 0xc008
jbe 0x478a
movsd %xmm2, 0x50(%rsp)
fldl 0x50(%rsp)
faddl 0x7896(%rip)            # 0xc010
fstpl 0x48(%rsp)
movsd 0x48(%rsp), %xmm2
movsd %xmm2, 0x8(%rsp)
movsd 0x789e(%rip), %xmm1     # 0xc030
ucomisd %xmm2, %xmm1
jbe 0x47c0
movsd %xmm2, 0x40(%rsp)
fldl 0x40(%rsp)
faddl 0x7890(%rip)            # 0xc038
fstpl 0x38(%rsp)
movsd 0x38(%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
jmp 0x47c4
movapd %xmm3, %xmm0
jmp 0x47c4
movapd %xmm2, %xmm1
xorpd 0x78a4(%rip), %xmm1     # 0xc070
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
leaq 0xc0(%rsp), %rcx
leaq 0xb8(%rsp), %r8
callq 0x6670
testb %al, %al
je 0x4586
movsd 0xc0(%rsp), %xmm0
callq 0x4340
cvttsd2si %xmm0, %ebp
movsd 0xb8(%rsp), %xmm0
callq 0x4340
testl %ebp, %ebp
js 0x4586
cmpl 0x1c(%rsp), %ebp
jge 0x4586
cvttsd2si %xmm0, %eax
testl %eax, %eax
js 0x4586
cmpl 0x28(%rsp), %eax
jge 0x4586
imull 0x1c(%rsp), %eax
movl %ebp, %ebp
movsd 0xd8(%rsp), %xmm0
subsd 0xd9a6(%rip), %xmm0     # 0x121f8
addq %rax, %rbp
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0xd99b(%rip), %xmm0     # 0x12200
callq 0x4350
mulsd 0x77ce(%rip), %xmm0     # 0xc040
movq 0x10(%r15), %rax
addq %rbp, %rax
addq %rbp, %rax
movzbl (%rax,%rbp), %ecx
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
ja 0x48ac
movzbl 0x1(%rax,%rbp), %ecx
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
ja 0x48ac
movzbl 0x2(%rax,%rbp), %ecx
cvtsi2sd %ecx, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4586
cmpb $0x1, 0xd97d(%rip)      # 0x12230
jne 0x4933
movsd 0x20(%rsp), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
jbe 0x48f7
movsd 0xd93b(%rip), %xmm3     # 0x12208
ucomisd %xmm3, %xmm1
jbe 0x48dd
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 0x493b
ucomisd %xmm1, %xmm2
jbe 0x4933
movsd 0xd903(%rip), %xmm2     # 0x12208
movapd %xmm2, %xmm3
xorpd 0x775f(%rip), %xmm3     # 0xc070
ucomisd %xmm1, %xmm3
jbe 0x4921
movsd %xmm3, 0x20(%rsp)
movapd %xmm3, %xmm1
addsd %xmm2, %xmm1
divsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm2
jmp 0x493b
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
cvttsd2si %xmm1, %ecx
cvttsd2si %xmm2, %edx
cvttsd2si %xmm0, %esi
movb %cl, (%rax,%rbp)
movb %dl, 0x1(%rax,%rbp)
movb %sil, 0x2(%rax,%rbp)
jmp 0x4586
movq %r12, %rdi
callq 0x4210
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq