// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
leaq 0x7e453(%rip), %rax     # 0x170c68
testb $0x2, (%rax)
jne 0xf2aed
movl %ecx, %r12d
movl %edx, %ebx
movapd %xmm0, %xmm1
leaq 0x21b012(%rip), %rax    # 0x30d840
movsd (%rax), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0xf2847
jp 0xf2847
movapd %xmm1, %xmm0
callq 0xe4462
movsd 0x16819(%rip), %xmm1    # 0x109068
mulsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
leaq 0x39ffd(%rip), %rcx     # 0x12c860
addsd (%rcx,%rax,8), %xmm0
movsd 0x16800(%rip), %xmm1    # 0x109070
movsd %xmm1, -0x30(%rbp)
ucomisd %xmm1, %xmm0
ja 0xf2893
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm0, -0x30(%rbp)
jbe 0xf2893
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
testl %r12d, %r12d
jne 0xf28de
movl 0x2874b2(%rip), %eax    # 0x379d50
addl -0x38(%rbp), %eax
leaq 0x14c708(%rip), %rcx    # 0x23efb0
movl %eax, 0xcd6b0(%rcx)
movl 0x2874a0(%rip), %eax    # 0x379d54
addl -0x40(%rbp), %eax
movl %eax, 0xcd6b4(%rcx)
movl 0x287495(%rip), %eax    # 0x379d58
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
sarl %edx
movl %edx, 0xcd6b8(%rcx)
movl 0x77d68(%rip), %eax     # 0x16a640
movl %eax, 0xcd6bc(%rcx)
movl 0x287474(%rip), %ecx    # 0x379d58
cmpl $-0x1, %ecx
jl 0xf2aed
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 %ebx, %ebx
movl %ecx, %r15d
imull %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm0
ucomisd 0x167b9(%rip), %xmm0    # 0x109118
jb 0xf2967
sqrtsd %xmm0, %xmm0
jmp 0xf296c
callq 0x9270
cvttsd2si %xmm0, %eax
leal (%rax,%rax), %ecx
movl %eax, %edx
negl %edx
movsd -0x30(%rbp), %xmm1
movsd 0x166ec(%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 0x2873a7(%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 0x219de1(%rip), %rcx    # 0x30c7a8
cmpb $0x0, (%rcx)
jne 0xf29f1
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 0x28734e(%rip), %r14d   # 0x379d54
addl %r15d, %r14d
cmpl -0x50(%rbp), %r14d
jle 0xf2a62
movzbl 0x77c29(%rip), %r8d     # 0x16a640
movl %r13d, %edi
movl %r14d, %esi
movl %r12d, %edx
movl %r14d, %ecx
callq 0xe3c3
leal -0x2(%r13), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
leal -0x1(%r13), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
leal 0x1(%r12), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
leal 0x2(%r12), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
movq -0x40(%rbp), %rax
leal (%rax,%rbx), %r14d
addl 0x2872e3(%rip), %r14d   # 0x379d54
cmpl -0x60(%rbp), %r14d
jge 0xf2ace
movzbl 0x77bc1(%rip), %r8d     # 0x16a640
movl %r13d, %edi
movl %r14d, %esi
movl %r12d, %edx
movl %r14d, %ecx
callq 0xe3c3
leal -0x2(%r13), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
decl %r13d
movl %r13d, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
leal 0x1(%r12), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
addl $0x2, %r12d
movl %r12d, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe6a0
leal 0x1(%rbx), %ecx
movl 0x287281(%rip), %edx    # 0x379d58
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
decl %r15d
cmpl %eax, %ebx
movl %ecx, %ebx
jl 0xf293e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq