// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
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 0x3eae
movsd %xmm0, -0x30(%rbp)
callq 0x5884
testl %eax, %eax
je 0x3ffb
movsd 0x28082(%rip), %xmm0    # 0x2c038
movsd -0x30(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4040
leaq -0x120(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x280ed(%rip), %rsi     # 0x2c0c1
leaq 0x280ff(%rip), %rdx     # 0x2c0da
leaq -0x130(%rbp), %rbx
movq %rbx, %rdi
callq 0x4e0a
movq %rbx, %rsi
callq 0x38f6
movq -0x130(%rbp), %rdi
jmp 0x4033
leaq -0x140(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x28092(%rip), %rsi     # 0x2c0a0
leaq 0x280ab(%rip), %rdx     # 0x2c0c0
leaq -0x150(%rbp), %rbx
movq %rbx, %rdi
callq 0x4e0a
movq %rbx, %rsi
callq 0x38f6
movq -0x150(%rbp), %rdi
xorl %ebx, %ebx
movl $0x0, %r15d
jmp 0x41a1
movapd %xmm1, %xmm0
movsd 0x27ff4(%rip), %xmm1    # 0x2c040
mulsd %xmm0, %xmm1
minsd 0x27ff0(%rip), %xmm1    # 0x2c048
movapd %xmm1, -0x40(%rbp)
movsd %xmm1, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3e16
subsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
callq 0x5840
movsd 0x27fcc(%rip), %xmm1    # 0x2c050
ucomisd %xmm0, %xmm1
jae 0x412a
movapd 0x27ffa(%rip), %xmm0    # 0x2c090
xorpd -0x40(%rbp), %xmm0
movapd %xmm0, -0x80(%rbp)
movlpd %xmm0, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3e16
subsd -0x30(%rbp), %xmm0
movapd %xmm0, -0x60(%rbp)
callq 0x5840
movsd 0x27f89(%rip), %xmm1    # 0x2c050
ucomisd %xmm0, %xmm1
jae 0x4164
movsd -0x48(%rbp), %xmm0
movapd -0x60(%rbp), %xmm2
mulsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x41ce
leaq -0xc0(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x28009(%rip), %rsi     # 0x2c109
leaq 0x28019(%rip), %rdx     # 0x2c120
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
callq 0x4e0a
movq %rbx, %rsi
callq 0x38f6
movq -0xd0(%rbp), %rdi
jmp 0x4033
leaq -0x100(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x27f9e(%rip), %rsi     # 0x2c0db
leaq 0x27fad(%rip), %rdx     # 0x2c0f1
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
callq 0x4e0a
movq %rbx, %rsi
callq 0x38f6
movq -0x110(%rbp), %rdi
jmp 0x419c
leaq -0xe0(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x27f7b(%rip), %rsi     # 0x2c0f2
leaq 0x27f8a(%rip), %rdx     # 0x2c108
leaq -0xf0(%rbp), %rbx
movq %rbx, %rdi
callq 0x4e0a
movq %rbx, %rsi
callq 0x38f6
movq -0xf0(%rbp), %rdi
movb $0x1, %bl
movb $0x1, %r15b
cmpq %r12, %rdi
je 0x41b5
movq (%r12), %rsi
incq %rsi
callq 0x32e0
movl %ebx, %r15d
andb $0x1, %r15b
movl %r15d, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movapd %xmm2, %xmm1
xorl %r14d, %r14d
leaq -0x90(%rbp), %r12
movapd -0x40(%rbp), %xmm2
movl %r14d, %r13d
leal 0x1(%r13), %r14d
movapd %xmm1, %xmm0
subsd -0x48(%rbp), %xmm0
movapd %xmm1, -0x60(%rbp)
divsd %xmm0, %xmm1
movapd %xmm2, -0x40(%rbp)
movapd -0x80(%rbp), %xmm0
subsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, -0x68(%rbp)
movsd %xmm2, 0x308(%rbx)
movq %rbx, %rdi
callq 0x3e16
subsd -0x30(%rbp), %xmm0
movapd %xmm0, -0xb0(%rbp)
callq 0x5840
movsd 0x27e11(%rip), %xmm1    # 0x2c050
ucomisd %xmm0, %xmm1
jae 0x42a0
cmpl $0x19, %r13d
jb 0x4330
movq %r12, -0xa0(%rbp)
leaq -0xa0(%rbp), %r15
movq %r15, %rdi
leaq 0x27ed8(%rip), %rsi     # 0x2c13f
leaq 0x27eee(%rip), %rdx     # 0x2c15c
callq 0x4e0a
movq %r15, %rsi
callq 0x38f6
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0x4296
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x32e0
xorl %eax, %eax
xorl %r15d, %r15d
jmp 0x4324
movl $0x10, %edx
movq 0x35d1c(%rip), %r15     # 0x39fc8
movq %r15, %rdi
leaq 0x27e6b(%rip), %rsi     # 0x2c121
callq 0x3310
movq %r15, %rdi
movl %r14d, %esi
callq 0x3470
movq %rax, %r15
movl $0xc, %edx
movq %rax, %rdi
leaq 0x27e5a(%rip), %rsi     # 0x2c132
callq 0x3310
movsd 0x308(%rbx), %xmm0
mulsd 0x27d6b(%rip), %xmm0    # 0x2c058
movq %r15, %rdi
callq 0x33f0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31b0
movb $0x1, %r15b
xorl %eax, %eax
movapd -0x40(%rbp), %xmm2
movapd -0x60(%rbp), %xmm1
jmp 0x4383
movapd -0x60(%rbp), %xmm3
movapd %xmm3, %xmm0
movapd -0xb0(%rbp), %xmm4
mulsd %xmm4, %xmm0
ucomisd 0x27ccb(%rip), %xmm0    # 0x2c018
ja 0x4354
movsd %xmm4, -0x48(%rbp)
movapd -0x40(%rbp), %xmm2
ja 0x4362
movsd -0x68(%rbp), %xmm2
jmp 0x436c
movsd -0x68(%rbp), %xmm1
movapd %xmm1, -0x80(%rbp)
xorpd %xmm1, %xmm1
cmpltsd %xmm0, %xmm1
andpd %xmm1, %xmm4
andnpd %xmm3, %xmm1
orpd %xmm4, %xmm1
movb $0x1, %al
testb %al, %al
jne 0x41e1
jmp 0x41b5
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
jmp 0x43d6
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
jmp 0x43d6
movq %rax, %rbx
movq -0x110(%rbp), %rdi
jmp 0x43d6
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
jmp 0x43d6
movq %rax, %rbx
movq -0x130(%rbp), %rdi
jmp 0x43d6
movq %rax, %rbx
movq -0x150(%rbp), %rdi
cmpq %r12, %rdi
je 0x43e7
movq (%r12), %rsi
incq %rsi
callq 0x32e0
movq %rbx, %rdi
callq 0x34b0
nop