// File: /workspace/github_cpp_makefile_star2_O2/philipperemy[P]digital-setting-circles/Calc.cpp
// Function: Calc::optimizeZ3()
bool Calc::optimizeZ3()
{

  double maxGuess;
  double upperGuess, lowerGuess;
  double upperError, lowerError;

  // Our residual error threshold
  const double maxErr = 0.01*radPerDeg;

  int ntries;

  // Calculate the true angular separation of the two stars,
  // based on their RA/Dec.  This need be calculated only once.
  const double trueSep = calcSeparation(alignRA[1], alignDec[1], alignRA[2], alignDec[2]);

  if (!MathLib::finite(trueSep)) {
    printDebugMsg("Can't calculate star separation!");
    return false;
  }

  if (trueSep < 5.0*radPerDeg) {
    printDebugMsg("Stars too close together!");
    return false;
  }

  maxGuess = 10.0*radPerDeg;
  if (maxGuess > trueSep/2)
    maxGuess = trueSep/2;


  Z3 = upperGuess = maxGuess;
  upperError = calcApparentSeparation() - trueSep;
  if (MathLib::fabs(upperError) <= maxErr) {
    printDebugMsg("Lucky pos. first guess");
    return true;  // Lucky guess!
  }

  Z3 = lowerGuess = -maxGuess;
  lowerError = calcApparentSeparation() - trueSep;
  if (MathLib::fabs(lowerError) <= maxErr) {
    printDebugMsg("Lucky neg. first guess");
    return true;  // Lucky guess!
  }

  // For the interpolation to succeed, the upper and
  // lower errors must be of opposite sign.
  if (upperError * lowerError > 0.0) {
    printDebugMsg("Correct Z3 out of range");
    return false;
  }

  ntries = 0;
  for (;;) {
    double newGuess, newError;
    ntries++;

    // Interpolate between the upper and lower guesses to
    // get a new guess.

    Z3 = newGuess = lowerGuess +
	((lowerError/(lowerError-upperError))*(upperGuess-lowerGuess));

    newError = calcApparentSeparation() - trueSep;

    if (MathLib::fabs(newError) <= maxErr) {
        std::cout << "Converged after " << ntries << " tries: Z3= " << Z3*degPerRad << std::endl;
      return true;  // We have converged!  Z3 is set.
    }

    if (ntries > 25) {
      printDebugMsg("No convergence after 25 tries");
      return false;  // Failure to converge
    }

    // NewGuess replaces either lowerGuess or upperGuess, depending
    // on the sign of the newError.

    if (lowerError * newError > 0) {
      // newError has the same sign as lowerError
      lowerGuess = newGuess;
      lowerError = newError;
    } else {
      upperGuess = newGuess;
      upperError = newError;
    }
  }

  // We should never get here!
  printDebugMsg("Broke out of loop!?!");

  return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
movq %rdi, %rbx
movsd 0x2e8(%rdi), %xmm1
movsd 0x2d0(%rdi), %xmm0
movsd 0x2d8(%rdi), %xmm2
movsd 0x2f0(%rdi), %xmm3
callq 0x3e2e
movsd %xmm0, -0x20(%rbp)
callq 0x56d4
testl %eax, %eax
je 0x3f67
movsd 0x1c108(%rip), %xmm0    # 0x20038
movsd -0x20(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3f9d
leaq 0x1c17f(%rip), %rsi     # 0x200c1
leaq -0x110(%rbp), %rdi
leaq -0x11(%rbp), %rdx
callq 0x4ce2
leaq -0x110(%rbp), %rsi
callq 0x38a6
leaq -0x110(%rbp), %rdi
jmp 0x3f91
leaq 0x1c132(%rip), %rsi     # 0x200a0
leaq -0x130(%rbp), %rdi
leaq -0x11(%rbp), %rdx
callq 0x4ce2
leaq -0x130(%rbp), %rsi
callq 0x38a6
leaq -0x130(%rbp), %rdi
callq 0x3100
xorl %eax, %eax
jmp 0x40d2
movapd %xmm1, %xmm0
movsd 0x1c097(%rip), %xmm1    # 0x20040
mulsd %xmm0, %xmm1
minsd 0x1c093(%rip), %xmm1    # 0x20048
movapd %xmm1, -0x40(%rbp)
movsd %xmm1, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3d96
subsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
callq 0x5690
movsd 0x1c06f(%rip), %xmm1    # 0x20050
ucomisd %xmm0, %xmm1
jae 0x4075
movapd 0x1c09d(%rip), %xmm0    # 0x20090
xorpd -0x40(%rbp), %xmm0
movapd %xmm0, -0x60(%rbp)
movlpd %xmm0, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3d96
subsd -0x20(%rbp), %xmm0
movapd %xmm0, -0x50(%rbp)
callq 0x5690
movsd 0x1c02c(%rip), %xmm1    # 0x20050
ucomisd %xmm0, %xmm1
jae 0x40a1
movsd -0x28(%rbp), %xmm0
movapd -0x50(%rbp), %xmm2
mulsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x40de
leaq 0x1c0bc(%rip), %rsi     # 0x20109
leaq -0xb0(%rbp), %rdi
leaq -0x11(%rbp), %rdx
callq 0x4ce2
leaq -0xb0(%rbp), %rsi
callq 0x38a6
leaq -0xb0(%rbp), %rdi
jmp 0x3f91
leaq 0x1c05f(%rip), %rsi     # 0x200db
leaq -0xf0(%rbp), %rdi
leaq -0x11(%rbp), %rdx
callq 0x4ce2
leaq -0xf0(%rbp), %rsi
callq 0x38a6
leaq -0xf0(%rbp), %rdi
jmp 0x40cb
leaq 0x1c04a(%rip), %rsi     # 0x200f2
leaq -0xd0(%rbp), %rdi
leaq -0x11(%rbp), %rdx
callq 0x4ce2
leaq -0xd0(%rbp), %rsi
callq 0x38a6
leaq -0xd0(%rbp), %rdi
callq 0x3100
movb $0x1, %al
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r14
popq %rbp
retq
movapd %xmm2, %xmm1
pushq $-0x1
popq %r14
movapd -0x40(%rbp), %xmm2
movapd %xmm1, %xmm0
subsd -0x28(%rbp), %xmm0
movapd %xmm1, -0x50(%rbp)
divsd %xmm0, %xmm1
movapd %xmm2, -0x40(%rbp)
movapd -0x60(%rbp), %xmm0
subsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, -0x30(%rbp)
movsd %xmm2, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3d96
subsd -0x20(%rbp), %xmm0
movapd %xmm0, -0x70(%rbp)
callq 0x5690
movsd 0x1bf11(%rip), %xmm1    # 0x20050
ucomisd %xmm0, %xmm1
jae 0x41a5
cmpl $-0x1a, %r14d
je 0x41fa
movapd -0x50(%rbp), %xmm3
movapd %xmm3, %xmm0
movapd -0x70(%rbp), %xmm4
mulsd %xmm4, %xmm0
ucomisd 0x1beb7(%rip), %xmm0    # 0x20020
ja 0x4170
movsd %xmm4, -0x28(%rbp)
movapd -0x40(%rbp), %xmm2
ja 0x417e
movsd -0x30(%rbp), %xmm2
jmp 0x4188
movsd -0x30(%rbp), %xmm1
movapd %xmm1, -0x60(%rbp)
xorpd %xmm1, %xmm1
cmpltsd %xmm0, %xmm1
andpd %xmm1, %xmm4
andnpd %xmm3, %xmm1
orpd %xmm4, %xmm1
decl %r14d
jmp 0x40eb
movq 0x29e1c(%rip), %rdi     # 0x2dfc8
leaq 0x1bf6e(%rip), %rsi     # 0x20121
callq 0x32a0
negl %r14d
movq %rax, %rdi
movl %r14d, %esi
callq 0x3460
leaq 0x1bf65(%rip), %rsi     # 0x20132
movq %rax, %rdi
callq 0x32a0
movsd 0x308(%rbx), %xmm0
mulsd 0x1be73(%rip), %xmm0    # 0x20058
movq %rax, %rdi
callq 0x33e0
movq %rax, %rdi
callq 0x3110
jmp 0x40d0
leaq 0x1bf3e(%rip), %rsi     # 0x2013f
leaq -0x90(%rbp), %rdi
leaq -0x11(%rbp), %rdx
callq 0x4ce2
leaq -0x90(%rbp), %rsi
callq 0x38a6
leaq -0x90(%rbp), %rdi
jmp 0x3f91
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
jmp 0x4279
jmp 0x4280
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
jmp 0x4279
jmp 0x4280
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
jmp 0x4279
jmp 0x4280
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
jmp 0x4279
jmp 0x4280
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
jmp 0x4279
jmp 0x4280
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x3100
jmp 0x4283
movq %rax, %rbx
movq %rbx, %rdi
callq 0x34a0
nop