// File: 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
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x74e0
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7500
movl %eax, -0x28(%rbp)
cmpq $0x0, -0x8(%rbp)
jne 0x5bda
movq 0x17407(%rip), %rax     # 0x1cfd8
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x5c03
movq -0x8(%rbp), %rdi
leaq 0xf954(%rip), %rsi      # 0x15539
callq 0x5310
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x5c01
leaq 0xf474(%rip), %rdi      # 0x15070
callq 0xfee0
jmp 0x5c03
movl $0x0, -0x134(%rbp)
leaq -0x130(%rbp), %rdi
movq -0x30(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x53a0
cmpq $0x0, %rax
je 0x61dc
leaq -0x130(%rbp), %rdi
leaq 0xf44a(%rip), %rsi      # 0x15084
leaq -0x140(%rbp), %rdx
leaq -0x148(%rbp), %rcx
leaq -0x150(%rbp), %r8
leaq -0x158(%rbp), %r9
leaq -0x160(%rbp), %r10
leaq -0x168(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x5290
cmpl $0x6, %eax
je 0x5c85
leaq 0xf41c(%rip), %rdi      # 0x1509c
callq 0xfee0
fldl -0x150(%rbp)
fldl 0xf377(%rip)            # 0x15008
fmul %st, %st(1)
flds 0xf3cf(%rip)            # 0x15068
fdivrp %st, %st(2)
fxch %st(1)
fstpl -0x150(%rbp)
fldl -0x158(%rbp)
fmul %st(1), %st
flds 0xf3bb(%rip)            # 0x1506c
fdivrp %st, %st(1)
fstpl -0x158(%rbp)
fldl -0x150(%rbp)
fucompi %st(1), %st
fstp %st(0)
jbe 0x5ceb
jmp 0x5cc7
fldl -0x150(%rbp)
fldl 0xf33d(%rip)            # 0x15010
faddp %st, %st(1)
fstpl -0x1c0(%rbp)
movsd -0x1c0(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
movsd 0x175bd(%rip), %xmm0    # 0x1d2b0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x5d03
jp 0x5d03
jmp 0x5e6d
movsd 0x175a5(%rip), %xmm0    # 0x1d2b0
movsd -0x160(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd -0x150(%rbp), %xmm1
movsd 0xf2f1(%rip), %xmm2     # 0x15018
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x150(%rbp)
movsd 0x17571(%rip), %xmm0    # 0x1d2b0
movsd -0x168(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd -0x158(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x158(%rbp)
fldl -0x158(%rbp)
fldl 0xf2b1(%rip)            # 0x15020
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jbe 0x5dad
jmp 0x5d79
fldl -0x158(%rbp)
fldl 0xf283(%rip)            # 0x15008
fsub %st, %st(1)
fxch %st(1)
fstpl -0x158(%rbp)
fldl -0x150(%rbp)
faddp %st, %st(1)
fstpl -0x1c8(%rbp)
movsd -0x1c8(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
fldl -0x158(%rbp)
fldl 0xf26f(%rip)            # 0x15028
fucompi %st(1), %st
fstp %st(0)
jbe 0x5df9
jmp 0x5dc1
fldl -0x158(%rbp)
fldl 0xf263(%rip)            # 0x15030
faddp %st, %st(1)
fstpl -0x158(%rbp)
fldl -0x150(%rbp)
fldl 0xf227(%rip)            # 0x15008
faddp %st, %st(1)
fstpl -0x1d0(%rbp)
movsd -0x1d0(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
fldl -0x150(%rbp)
fldl 0xf203(%rip)            # 0x15008
fxch %st(1)
fucompi %st(1), %st
fstp %st(0)
jbe 0x5e33
jmp 0x5e0f
fldl -0x150(%rbp)
fldl 0xf1f5(%rip)            # 0x15010
faddp %st, %st(1)
fstpl -0x1d8(%rbp)
movsd -0x1d8(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
fldl -0x150(%rbp)
fldl 0xf1f1(%rip)            # 0x15030
fucompi %st(1), %st
fstp %st(0)
jbe 0x5e6b
jmp 0x5e47
fldl -0x150(%rbp)
fldl 0xf1e5(%rip)            # 0x15038
faddp %st, %st(1)
fstpl -0x1e0(%rbp)
movsd -0x1e0(%rbp), %xmm0
movsd %xmm0, -0x150(%rbp)
jmp 0x5e6d
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movsd -0x158(%rbp), %xmm0
movsd -0x150(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
leaq -0x170(%rbp), %rcx
leaq -0x178(%rbp), %r8
callq 0x94c0
andb $0x1, %al
movb %al, -0x179(%rbp)
testb $0x1, -0x179(%rbp)
je 0x61c8
movsd -0x170(%rbp), %xmm0
callq 0x5400
cvttsd2si %xmm0, %eax
movl %eax, -0x180(%rbp)
movsd -0x178(%rbp), %xmm0
callq 0x5400
cvttsd2si %xmm0, %eax
movl %eax, -0x184(%rbp)
xorl %eax, %eax
cmpl -0x180(%rbp), %eax
jg 0x61c6
movl -0x180(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x61c6
xorl %eax, %eax
cmpl -0x184(%rbp), %eax
jg 0x61c6
movl -0x184(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x61c6
movl -0x184(%rbp), %eax
imull -0x24(%rbp), %eax
addl -0x180(%rbp), %eax
movl %eax, -0x188(%rbp)
movsd -0x140(%rbp), %xmm0
callq 0x5b40
movaps %xmm0, %xmm1
movsd 0xf0e2(%rip), %xmm0     # 0x15040
mulsd %xmm1, %xmm0
movsd %xmm0, -0x190(%rbp)
leaq -0x193(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x74b0
movq -0x10(%rbp), %rdi
movslq -0x188(%rbp), %rsi
leaq -0x193(%rbp), %rdx
callq 0x7520
movsd -0x190(%rbp), %xmm0
movzbl -0x193(%rbp), %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
ja 0x5fe2
movsd -0x190(%rbp), %xmm0
movzbl -0x192(%rbp), %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
ja 0x5fe2
movsd -0x190(%rbp), %xmm0
movzbl -0x191(%rbp), %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x61c4
movsd -0x190(%rbp), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd -0x190(%rbp), %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd -0x190(%rbp), %xmm0
movsd %xmm0, -0x1b0(%rbp)
testb $0x1, 0x17292(%rip)     # 0x1d2ab
je 0x6160
movsd -0x148(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x60a7
movsd -0x148(%rbp), %xmm0
ucomisd 0x17230(%rip), %xmm0    # 0x1d270
jbe 0x6052
movsd 0x17226(%rip), %xmm0    # 0x1d270
movsd %xmm0, -0x148(%rbp)
movsd 0x17216(%rip), %xmm0    # 0x1d270
subsd -0x148(%rbp), %xmm0
divsd 0x17206(%rip), %xmm0    # 0x1d270
mulsd -0x1a8(%rbp), %xmm0
movsd %xmm0, -0x1a8(%rbp)
movsd 0x171ee(%rip), %xmm0    # 0x1d270
subsd -0x148(%rbp), %xmm0
divsd 0x171de(%rip), %xmm0    # 0x1d270
mulsd -0x1b0(%rbp), %xmm0
movsd %xmm0, -0x1b0(%rbp)
jmp 0x615e
xorps %xmm0, %xmm0
ucomisd -0x148(%rbp), %xmm0
jbe 0x615c
movsd -0x148(%rbp), %xmm1
movsd 0x171a8(%rip), %xmm0    # 0x1d270
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x610c
movsd 0x17183(%rip), %xmm0    # 0x1d270
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x148(%rbp)
movsd 0x1715c(%rip), %xmm0    # 0x1d270
addsd -0x148(%rbp), %xmm0
divsd 0x1714c(%rip), %xmm0    # 0x1d270
mulsd -0x1a0(%rbp), %xmm0
movsd %xmm0, -0x1a0(%rbp)
movsd 0x17134(%rip), %xmm0    # 0x1d270
addsd -0x148(%rbp), %xmm0
divsd 0x17124(%rip), %xmm0    # 0x1d270
mulsd -0x1a8(%rbp), %xmm0
movsd %xmm0, -0x1a8(%rbp)
jmp 0x615e
jmp 0x6160
movq -0x10(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movslq -0x188(%rbp), %rax
movq %rax, -0x1e8(%rbp)
cvttsd2si -0x1a0(%rbp), %eax
movb %al, %dl
cvttsd2si -0x1a8(%rbp), %eax
movb %al, %cl
cvttsd2si -0x1b0(%rbp), %eax
leaq -0x1b3(%rbp), %rdi
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0x74b0
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
leaq -0x1b3(%rbp), %rdx
callq 0x7590
jmp 0x61c6
jmp 0x61c8
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x5c0d
movq -0x30(%rbp), %rdi
callq 0x52a0
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
nop