// File: /workspace/github_cpp_makefile_star2_O2/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 0x7d127(%rip), %rax     # 0x15ac68
testb $0x2, (%rax)
jne 0xdddfd
movl %ecx, %r14d
movl %edx, %ebx
movapd %xmm0, %xmm1
leaq 0x219ce6(%rip), %rax    # 0x2f7840
movsd (%rax), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
jne 0xddb73
jp 0xddb73
movapd %xmm1, %xmm0
callq 0xd18b7
movsd 0x154ed(%rip), %xmm1    # 0xf3068
mulsd %xmm1, %xmm0
xorl %eax, %eax
ucomisd %xmm0, %xmm1
seta %al
leaq 0x38ce1(%rip), %rcx     # 0x116870
addsd (%rcx,%rax,8), %xmm0
movsd 0x154d4(%rip), %xmm1    # 0xf3070
movsd %xmm1, -0x30(%rbp)
ucomisd %xmm1, %xmm0
ja 0xddbbf
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm0, -0x30(%rbp)
jbe 0xddbbf
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
testl %r14d, %r14d
jne 0xddc07
movl 0x286186(%rip), %eax    # 0x363d50
addl -0x38(%rbp), %eax
leaq 0x14b3dc(%rip), %rcx    # 0x228fb0
movl %eax, 0xcd6b0(%rcx)
movl 0x286174(%rip), %eax    # 0x363d54
addl -0x40(%rbp), %eax
movl %eax, 0xcd6b4(%rcx)
movl 0x286169(%rip), %eax    # 0x363d58
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, 0xcd6b8(%rcx)
movl 0x76a3f(%rip), %eax     # 0x154640
movl %eax, 0xcd6bc(%rcx)
movsd -0x30(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x38(%rbp), %rax
movl %eax, %ecx
subl %ebx, %ecx
leal 0x1(%rcx), %edx
movl %edx, -0x4c(%rbp)
addl $0x2, %ecx
movq %rcx, -0x58(%rbp)
leal (%rbx,%rax), %ecx
decl %ecx
movl %ecx, -0x48(%rbp)
leal -0x2(%rbx,%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rax
movl %eax, %ecx
subl %ebx, %ecx
movl %ecx, -0x50(%rbp)
addl %eax, %ebx
movq %rbx, -0x60(%rbp)
xorl %ebx, %ebx
movl %eax, %r15d
movl 0x286103(%rip), %eax    # 0x363d58
cltd
pushq $0x2
popq %rcx
idivl %ecx
cmpl %eax, %ebx
jg 0xdddfd
imull %eax, %eax
cvtsi2sd %eax, %xmm0
cvtsi2sd %ebx, %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm0
ucomisd 0x1549a(%rip), %xmm0    # 0xf3118
jb 0xddc86
sqrtsd %xmm0, %xmm0
jmp 0xddc8b
callq 0x9260
cvttsd2si %xmm0, %eax
leal (%rax,%rax), %ecx
movl %eax, %edx
negl %edx
movsd -0x30(%rbp), %xmm1
movsd 0x153cd(%rip), %xmm0    # 0xf3070
ucomisd %xmm1, %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 0x28608b(%rip), %r12d   # 0x363d50
addl -0x38(%rbp), %r12d
leal (%r12,%rdx), %ecx
addl %esi, %r12d
cmpl %esi, %edx
movl %ecx, %eax
cmovgl %r12d, %eax
cmovgl %ecx, %r12d
leaq 0x218ac5(%rip), %rcx    # 0x2f67a8
cmpb $0x0, (%rcx)
jne 0xddd0d
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 0x286032(%rip), %r14d   # 0x363d54
movq %r15, -0x68(%rbp)
addl %r14d, %r15d
cmpl -0x50(%rbp), %r15d
jle 0xddd89
movzbl 0x76909(%rip), %r8d     # 0x154640
movl %r13d, %edi
movl %r15d, %esi
movl %r12d, %edx
movl %r15d, %ecx
callq 0xdedf
leal -0x2(%r13), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe1a7
leal -0x1(%r13), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe1a7
leal 0x1(%r12), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe1a7
leal 0x2(%r12), %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0xe1a7
movl 0x285fcb(%rip), %r14d   # 0x363d54
movq -0x40(%rbp), %rax
addl %ebx, %eax
addl %eax, %r14d
cmpl -0x60(%rbp), %r14d
jge 0xdddef
movzbl 0x768a0(%rip), %r8d     # 0x154640
movl %r13d, %edi
movl %r14d, %esi
movl %r12d, %edx
movl %r14d, %ecx
callq 0xdedf
leal -0x2(%r13), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe1a7
decl %r13d
movl %r13d, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe1a7
leal 0x1(%r12), %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe1a7
addl $0x2, %r12d
movl %r12d, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0xe1a7
incl %ebx
movq -0x68(%rbp), %r15
decl %r15d
jmp 0xddc4f
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq