// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
movsd 0x2e8(%rdi), %xmm1
movsd 0x2d0(%rdi), %xmm0
movsd 0x2d8(%rdi), %xmm2
movsd 0x2f0(%rdi), %xmm3
callq 0x3f26
movsd %xmm0, -0x20(%rbp)
callq 0x59a8
testl %eax, %eax
je 0x407e
movsd 0x2800e(%rip), %xmm0    # 0x2c038
movsd -0x20(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x40db
leaq -0x110(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x28076(%rip), %rsi     # 0x2c0c1
leaq 0x28088(%rip), %rdx     # 0x2c0da
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f28
movq %rbx, %rsi
callq 0x38e6
movq -0x120(%rbp), %rdi
cmpq %r14, %rdi
je 0x40c9
movq -0x110(%rbp), %rsi
jmp 0x40c1
leaq -0x130(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x28010(%rip), %rsi     # 0x2c0a0
leaq 0x28029(%rip), %rdx     # 0x2c0c0
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f28
movq %rbx, %rsi
callq 0x38e6
movq -0x140(%rbp), %rdi
cmpq %r14, %rdi
je 0x40c9
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x32e0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movapd %xmm1, %xmm0
movsd 0x27f59(%rip), %xmm1    # 0x2c040
mulsd %xmm0, %xmm1
minsd 0x27f55(%rip), %xmm1    # 0x2c048
movapd %xmm1, -0x50(%rbp)
movsd %xmm1, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3e1a
subsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
callq 0x5964
movsd 0x27f31(%rip), %xmm1    # 0x2c050
ucomisd %xmm0, %xmm1
jae 0x41d8
movapd 0x27f5f(%rip), %xmm0    # 0x2c090
xorpd -0x50(%rbp), %xmm0
movapd %xmm0, -0x60(%rbp)
movlpd %xmm0, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3e1a
subsd -0x20(%rbp), %xmm0
movapd %xmm0, -0x40(%rbp)
callq 0x5964
movsd 0x27eee(%rip), %xmm1    # 0x2c050
ucomisd %xmm0, %xmm1
jae 0x4223
movsd -0x28(%rbp), %xmm2
movapd %xmm2, %xmm0
movapd -0x40(%rbp), %xmm3
mulsd %xmm3, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4279
leaq -0xb0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x27f6b(%rip), %rsi     # 0x2c109
leaq 0x27f7b(%rip), %rdx     # 0x2c120
leaq -0xc0(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f28
movq %rbx, %rsi
callq 0x38e6
movq -0xc0(%rbp), %rdi
cmpq %r14, %rdi
je 0x40c9
movq -0xb0(%rbp), %rsi
jmp 0x40c1
leaq -0xf0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x27ef1(%rip), %rsi     # 0x2c0db
leaq 0x27f00(%rip), %rdx     # 0x2c0f1
leaq -0x100(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f28
movq %rbx, %rsi
callq 0x38e6
movq -0x100(%rbp), %rdi
movb $0x1, %bl
cmpq %r14, %rdi
je 0x40cb
movq -0xf0(%rbp), %rsi
jmp 0x426c
leaq -0xd0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x27ebd(%rip), %rsi     # 0x2c0f2
leaq 0x27ecc(%rip), %rdx     # 0x2c108
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f28
movq %rbx, %rsi
callq 0x38e6
movq -0xe0(%rbp), %rdi
movb $0x1, %bl
cmpq %r14, %rdi
je 0x40cb
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x32e0
jmp 0x40cb
movapd %xmm3, %xmm1
movapd %xmm3, %xmm0
subsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movapd -0x50(%rbp), %xmm0
addsd %xmm0, %xmm0
mulsd %xmm1, %xmm0
addsd -0x60(%rbp), %xmm0
movapd %xmm0, -0x80(%rbp)
movsd %xmm0, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3e1a
subsd -0x20(%rbp), %xmm0
movapd %xmm0, -0x70(%rbp)
callq 0x5964
movl $0x1, %r14d
movsd 0x27d83(%rip), %xmm1    # 0x2c050
ucomisd %xmm0, %xmm1
jae 0x43a9
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movapd -0x40(%rbp), %xmm5
cmpl $-0x1a, %r14d
je 0x442f
movapd %xmm5, %xmm0
movapd -0x70(%rbp), %xmm6
mulsd %xmm6, %xmm0
ucomisd 0x27d1f(%rip), %xmm0    # 0x2c020
movsd -0x28(%rbp), %xmm2
ja 0x430c
movapd %xmm6, %xmm2
movapd -0x50(%rbp), %xmm1
movapd -0x60(%rbp), %xmm3
movapd -0x80(%rbp), %xmm4
ja 0x4324
movapd %xmm4, -0x50(%rbp)
jmp 0x432c
movapd %xmm4, %xmm3
movapd %xmm1, %xmm4
xorpd %xmm1, %xmm1
cmpltsd %xmm0, %xmm1
andpd %xmm1, %xmm6
andnpd %xmm5, %xmm1
orpd %xmm6, %xmm1
movapd %xmm1, -0x40(%rbp)
movapd %xmm1, %xmm0
movsd %xmm2, -0x28(%rbp)
subsd %xmm2, %xmm0
divsd %xmm0, %xmm1
subsd %xmm3, %xmm4
mulsd %xmm1, %xmm4
movapd %xmm3, -0x60(%rbp)
addsd %xmm3, %xmm4
movapd %xmm4, -0x80(%rbp)
movsd %xmm4, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3e1a
subsd -0x20(%rbp), %xmm0
movapd %xmm0, -0x70(%rbp)
callq 0x5964
decl %r14d
movsd 0x27cb9(%rip), %xmm1    # 0x2c050
ucomisd %xmm0, %xmm1
movapd -0x40(%rbp), %xmm5
jb 0x42e2
negl %r14d
movq 0x35c18(%rip), %r15     # 0x39fc8
leaq 0x27d6a(%rip), %rsi     # 0x2c121
movl $0x10, %edx
movq %r15, %rdi
callq 0x3310
movq %r15, %rdi
movl %r14d, %esi
callq 0x3470
movq %rax, %r14
leaq 0x27d59(%rip), %rsi     # 0x2c132
movl $0xc, %edx
movq %rax, %rdi
callq 0x3310
movsd 0x308(%rbx), %xmm0
mulsd 0x27c62(%rip), %xmm0    # 0x2c058
movq %r14, %rdi
callq 0x33f0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31b0
movb $0x1, %bl
jmp 0x40cb
leaq -0x90(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x27cfe(%rip), %rsi     # 0x2c13f
leaq 0x27d14(%rip), %rdx     # 0x2c15c
leaq -0xa0(%rbp), %rbx
movq %rbx, %rdi
callq 0x4f28
movq %rbx, %rsi
callq 0x38e6
movq -0xa0(%rbp), %rdi
cmpq %r14, %rdi
je 0x40c9
movq -0x90(%rbp), %rsi
jmp 0x40c1
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
jmp 0x44c1
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
jmp 0x44c1
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
jmp 0x44c1
movq %rax, %rbx
movq -0x100(%rbp), %rdi
jmp 0x44c1
movq %rax, %rbx
movq -0x120(%rbp), %rdi
jmp 0x44c1
movq %rax, %rbx
movq -0x140(%rbp), %rdi
cmpq %r14, %rdi
je 0x44d1
movq (%r14), %rsi
incq %rsi
callq 0x32e0
movq %rbx, %rdi
callq 0x34b0
nop