// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
leaq 0x7dd96(%rip), %rax     # 0x170c68
testb $0x2, (%rax)
jne 0xf31b2
movl %ecx, %r14d
movl %edx, %ebx
movapd %xmm0, %xmm1
leaq 0x21a955(%rip), %rax    # 0x30d840
movsd (%rax), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0xf2f04
jp 0xf2f04
movapd %xmm1, %xmm0
callq 0xe5033
movsd 0x1615c(%rip), %xmm1    # 0x109068
mulsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
leaq 0x39a30(%rip), %rcx     # 0x12c950
addsd (%rcx,%rax,8), %xmm0
movsd 0x16143(%rip), %xmm1    # 0x109070
movsd %xmm1, -0x30(%rbp)
ucomisd %xmm1, %xmm0
ja 0xf2f50
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm0, -0x30(%rbp)
jbe 0xf2f50
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl 0x286e02(%rip), %ecx    # 0x379d58
testl %r14d, %r14d
jne 0xf2f9b
movl 0x286def(%rip), %eax    # 0x379d50
addl -0x38(%rbp), %eax
leaq 0x14c045(%rip), %rdx    # 0x23efb0
movl %eax, 0xcd6b0(%rdx)
movl 0x286ddd(%rip), %eax    # 0x379d54
addl -0x40(%rbp), %eax
movl %eax, 0xcd6b4(%rdx)
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, 0xcd6b8(%rdx)
movl 0x776ab(%rip), %eax     # 0x16a640
movl %eax, 0xcd6bc(%rdx)
cmpl $-0x1, %ecx
jl 0xf31b2
movsd -0x30(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movq -0x38(%rbp), %rcx
movl %ecx, %edx
subl %ebx, %edx
leal 0x1(%rdx), %esi
movl %esi, -0x4c(%rbp)
addl $0x2, %edx
movq %rdx, -0x58(%rbp)
leal (%rbx,%rcx), %edx
decl %edx
movl %edx, -0x48(%rbp)
leal -0x2(%rbx,%rcx), %ecx
movl %ecx, -0x44(%rbp)
movq -0x40(%rbp), %rcx
movl %ecx, %edx
subl %ebx, %edx
movl %edx, -0x50(%rbp)
addl %ecx, %ebx
movq %rbx, -0x60(%rbp)
xorl %r14d, %r14d
movl %ecx, %ebx
imull %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm0
ucomisd 0x16101(%rip), %xmm0    # 0x109118
jb 0xf301f
sqrtsd %xmm0, %xmm0
jmp 0xf3024
callq 0x9270
cvttsd2si %xmm0, %eax
leal (%rax,%rax), %ecx
movl %eax, %edx
negl %edx
movsd -0x30(%rbp), %xmm1
movsd 0x16034(%rip), %xmm0    # 0x109070
ucomisd %xmm1, %xmm0
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
leal (%rax,%rax,2), %esi
cmoval %eax, %esi
cmovbel %eax, %edx
subl %ecx, %esi
movl 0x286cef(%rip), %r12d   # 0x379d50
addl -0x38(%rbp), %r12d
leal (%r12,%rdx), %ecx
addl %esi, %r12d
cmpl %esi, %edx
movl %ecx, %eax
cmovgl %r12d, %eax
cmovgl %ecx, %r12d
leaq 0x219729(%rip), %rcx    # 0x30c7a8
cmpb $0x0, (%rcx)
movq %r14, -0x68(%rbp)
jne 0xf30ad
movl -0x4c(%rbp), %edx
cmpl %edx, %eax
movq -0x58(%rbp), %rcx
cmovlel %ecx, %eax
movl -0x48(%rbp), %esi
cmpl %esi, %eax
movl -0x44(%rbp), %edi
cmovgel %edi, %eax
cmpl %edx, %r12d
cmovlel %ecx, %r12d
cmpl %esi, %r12d
cmovgel %edi, %r12d
cmpl %r12d, %eax
movl %r12d, %r13d
cmovll %eax, %r13d
cmovgl %eax, %r12d
movl 0x286c92(%rip), %r14d   # 0x379d54
leal (%rbx,%r14), %r15d
cmpl -0x50(%rbp), %r15d
jle 0xf3126
movzbl 0x7756c(%rip), %r8d     # 0x16a640
movl %r13d, %edi
movl %r15d, %esi
movl %r12d, %edx
movl %r15d, %ecx
callq 0xe38a
leal -0x2(%r13), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe657
leal -0x1(%r13), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe657
leal 0x1(%r12), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe657
leal 0x2(%r12), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe657
movl 0x286c2e(%rip), %r14d   # 0x379d54
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %r15
addl %r15d, %eax
addl %eax, %r14d
cmpl -0x60(%rbp), %r14d
jge 0xf3191
movzbl 0x774fe(%rip), %r8d     # 0x16a640
movl %r13d, %edi
movl %r14d, %esi
movl %r12d, %edx
movl %r14d, %ecx
callq 0xe38a
leal -0x2(%r13), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe657
decl %r13d
movl %r13d, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe657
leal 0x1(%r12), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe657
addl $0x2, %r12d
movl %r12d, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe657
leal 0x1(%r15), %ecx
movl 0x286bbd(%rip), %edx    # 0x379d58
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
decl %ebx
cmpl %eax, %r15d
movl %ecx, %r14d
jl 0xf2ff5
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq