// File: jkingan[P]ladyheather/heathmsc.cpp
// Function: draw_moon(double, int, int, int, int)
void draw_moon(double jd, int x0,int y0, int r, int watch)
{
int Xpos, Ypos, Rpos;
int Xpos1, Xpos2;
int y, x1,x2;
int t;
double age;
double synod;  // length of the synodic month
int moon_r;

    // This routine draws an image of the moon phase on the analog watch display
    // This code derived from code by Mostafa Kaisoun.
    if(MOON_PRN == 0) return;
    if(no_sun_moon & 0x02) return;
//  if(zoom_screen == 'I') return;

    if(0) {
       age = moon_age(jd);           // calc the moon phase age in days since last new moon

//     synod = (jd - JD2000) / 36525.0;
//     synod = 29.5305888531 + 0.00000021621*synod - 3.64E-10*synod*synod;  // average length of synodic month
       synod = moon_synod(jd);
sprintf(plot_title, "synod %f: %f  calc:%f age:%f  mage2:%f",
jd, synod, moon_synod(jd), age, age/synod);
       if(synod) age = age / synod;  // normalize moon age to 0.0 .. 1.0
       else age = 0.0;
                                                                         // ... which may be +/- 6 hours of the actual value
    }
    else if(MoonPhase) {   // we got MoonPhase from moon_info() call in primary_misc()
       age = MoonPhase / 2.0;
    }
    else {
       age = moon_phase(jd) / 2.0;
    }

    if(age < 0.5) age = age + 0.50;
    else                age = age - 0.50;
    if(age > 1.0) age = 1.0;
    else if(age < 0.0) age = 0.0;
    age *= 2.0;

//  x0 = aclock_x;
//  y0 = aclock_y;
//  r = ACLOCK_SIZE;

    if(watch == 0) {
       sat[MOON_PRN].plot_x = x0 + moon_x;
       sat[MOON_PRN].plot_y = y0 + moon_y;
       sat[MOON_PRN].plot_r = MOON_SIZE;
       sat[MOON_PRN].plot_color = moon_color;
    }

    moon_r = 0;
    for(Ypos=0; Ypos<=MOON_SIZE; Ypos++) {  // draw the moon        
       Xpos = (int)(sqrt((MOON_SIZE*MOON_SIZE) - ((double) Ypos * (double) Ypos)));

       // determine the edges of the lighted part of the moon       
       Rpos = 2 * Xpos;
       if(age < 1.0) {
          Xpos1 = 0 - Xpos;
          Xpos2 = (int)(Rpos - Xpos - (int) (age*(double)Rpos));
       }
       else {
          Xpos1 = Xpos;
          Xpos2 = (int)(Rpos + Xpos - (int) (age*(double)Rpos));
       }       

       // Draw the lighted part of the moon       

       x1 = x0+moon_x+Xpos1;
       x2 = x0+moon_x+Xpos2;
       if(x1 > x2) { 
          t = x1;
          x1 = x2;
          x2 = t;
       }
       if(zoom_screen) ;
       else {  // clip off any part outside the drawing area
          if(x1 <= (x0-r+1)) x1 = x0-r+2;
          if(x1 >= (x0+r-1)) x1 = x0+r-2;
          if(x2 <= (x0-r+1)) x2 = x0-r+2;
          if(x2 >= (x0+r-1)) x2 = x0+r-2;
       }

       if(x1 > x2) {
          t = x1;
          x1 = x2;
          x2 = t;
       }

       y = y0+moon_y-Ypos;
       if(y > (y0-r)) {
          line(x1,y, x2,y, moon_color);
          dot(x1-2,y, BLACK);   // outline moon in black
          dot(x1-1,y, BLACK);
          dot(x2+1,y, BLACK);
          dot(x2+2,y, BLACK);
       }

       y = y0+moon_y+Ypos;
       if(y < (y0+r)) {
          line(x1,y, x2,y, moon_color);
          dot(x1-2,y, BLACK);   // outline moon in black
          dot(x1-1,y, BLACK);
          dot(x2+1,y, BLACK);
          dot(x2+2,y, BLACK);
       }
    }    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movsd %xmm0, -0x8(%rbp)
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
leaq 0x81c28(%rip), %rax     # 0x1cfe48
movl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x14e22f
jmp 0x14e623
leaq 0x21e7ea(%rip), %rax    # 0x36ca20
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x14e247
jp 0x14e247
jmp 0x14e265
leaq 0x21e7d2(%rip), %rax    # 0x36ca20
movsd (%rax), %xmm0
movsd 0x20ef6(%rip), %xmm1    # 0x16f150
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x14e280
movsd -0x8(%rbp), %xmm0
callq 0x138e00
movsd 0x20ed9(%rip), %xmm1    # 0x16f150
divsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd 0x20e08(%rip), %xmm0    # 0x16f090
ucomisd -0x48(%rbp), %xmm0
jbe 0x14e2a3
movsd 0x20df9(%rip), %xmm0    # 0x16f090
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x14e2b9
movsd -0x48(%rbp), %xmm0
movsd 0x20de0(%rip), %xmm1    # 0x16f090
subsd %xmm1, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
movsd 0x20da2(%rip), %xmm1    # 0x16f068
ucomisd %xmm1, %xmm0
jbe 0x14e2db
movsd 0x20d94(%rip), %xmm0    # 0x16f068
movsd %xmm0, -0x48(%rbp)
jmp 0x14e2ef
xorps %xmm0, %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x14e2ed
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x14e2ef
movsd 0x20e59(%rip), %xmm0    # 0x16f150
mulsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x14e363
movl -0xc(%rbp), %ecx
addl 0x28ac80(%rip), %ecx    # 0x3d8f90
leaq 0x14fe79(%rip), %rax    # 0x29e190
movl %ecx, 0xcd6b0(%rax)
movl -0x10(%rbp), %ecx
addl 0x28ac6e(%rip), %ecx    # 0x3d8f94
leaq 0x14fe63(%rip), %rax    # 0x29e190
movl %ecx, 0xcd6b4(%rax)
movl 0x28ac5f(%rip), %eax    # 0x3d8f98
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
leaq 0x14fe46(%rip), %rax    # 0x29e190
movl %ecx, 0xcd6b8(%rax)
movl 0x7b2ea(%rip), %ecx     # 0x1c9640
leaq 0x14fe33(%rip), %rax    # 0x29e190
movl %ecx, 0xcd6bc(%rax)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x58(%rbp)
movl 0x28ac1b(%rip), %eax    # 0x3d8f98
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x58(%rbp), %eax
cmpl %ecx, %eax
jg 0x14e623
movl 0x28ac00(%rip), %eax    # 0x3d8f98
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x5c(%rbp)
movl 0x28abef(%rip), %eax    # 0x3d8f98
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x5c(%rbp), %eax
imull %ecx, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl -0x20(%rbp), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x9280
cvttsd2si %xmm0, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
movsd 0x20c66(%rip), %xmm0    # 0x16f068
ucomisd -0x48(%rbp), %xmm0
jbe 0x14e430
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
subl -0x1c(%rbp), %eax
movsd -0x48(%rbp), %xmm0
cvtsi2sdl -0x24(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0x14e453
movl -0x1c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
addl -0x1c(%rbp), %eax
movsd -0x48(%rbp), %xmm0
cvtsi2sdl -0x24(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0xc(%rbp), %eax
addl 0x28ab34(%rip), %eax    # 0x3d8f90
addl -0x28(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0xc(%rbp), %eax
addl 0x28ab25(%rip), %eax    # 0x3d8f90
addl -0x2c(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jle 0x14e48b
movl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x38(%rbp)
leaq 0x21d4f6(%rip), %rax    # 0x36b988
cmpb $0x0, (%rax)
je 0x14e499
jmp 0x14e50b
movl -0x34(%rbp), %eax
movl -0xc(%rbp), %ecx
subl -0x14(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x14e4b5
movl -0xc(%rbp), %eax
subl -0x14(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0xc(%rbp), %ecx
addl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jl 0x14e4d1
movl -0xc(%rbp), %eax
addl -0x14(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
movl -0xc(%rbp), %ecx
subl -0x14(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jg 0x14e4ed
movl -0xc(%rbp), %eax
subl -0x14(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl -0xc(%rbp), %ecx
addl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jl 0x14e509
movl -0xc(%rbp), %eax
addl -0x14(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x38(%rbp)
jmp 0x14e50b
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jle 0x14e525
movl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x10(%rbp), %eax
addl 0x28aa66(%rip), %eax    # 0x3d8f94
subl -0x20(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl -0x10(%rbp), %ecx
subl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jle 0x14e59c
movl -0x34(%rbp), %edi
movl -0x30(%rbp), %esi
movl -0x38(%rbp), %edx
movl -0x30(%rbp), %ecx
movl 0x7b0ed(%rip), %eax     # 0x1c9640
movzbl %al, %r8d
callq 0x109c0
movl -0x34(%rbp), %edi
subl $0x2, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
movl -0x34(%rbp), %edi
subl $0x1, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
movl -0x38(%rbp), %edi
addl $0x1, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
movl -0x38(%rbp), %edi
addl $0x2, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
movl -0x10(%rbp), %eax
addl 0x28a9ef(%rip), %eax    # 0x3d8f94
addl -0x20(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl -0x10(%rbp), %ecx
addl -0x14(%rbp), %ecx
cmpl %ecx, %eax
jge 0x14e613
movl -0x34(%rbp), %edi
movl -0x30(%rbp), %esi
movl -0x38(%rbp), %edx
movl -0x30(%rbp), %ecx
movl 0x7b076(%rip), %eax     # 0x1c9640
movzbl %al, %r8d
callq 0x109c0
movl -0x34(%rbp), %edi
subl $0x2, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
movl -0x34(%rbp), %edi
subl $0x1, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
movl -0x38(%rbp), %edi
addl $0x1, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
movl -0x38(%rbp), %edi
addl $0x2, %edi
movl -0x30(%rbp), %esi
xorl %edx, %edx
callq 0x10d70
jmp 0x14e615
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x14e371
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)