// File: /workspace/github_cpp_makefile_star2_O3/marxn[P]pupp/3rdparty/gmp/mpn/invertappr.c
// Function: _gmpn_ni_invertappr
mp_limb_t
mpn_ni_invertappr (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
{
  mp_limb_t cy;
  mp_size_t rn, mn;
  mp_size_t sizes[NPOWS], *sizp;
  mp_ptr tp;
  TMP_DECL;
#define xp scratch

  ASSERT (n > 4);
  ASSERT (dp[n-1] & GMP_NUMB_HIGHBIT);
  ASSERT (! MPN_OVERLAP_P (ip, n, dp, n));
  ASSERT (! MPN_OVERLAP_P (ip, n, scratch, mpn_invertappr_itch(n)));
  ASSERT (! MPN_OVERLAP_P (dp, n, scratch, mpn_invertappr_itch(n)));

  /* Compute the computation precisions from highest to lowest, leaving the
     base case size in 'rn'.  */
  sizp = sizes;
  rn = n;
  do {
    *sizp = rn;
    rn = (rn >> 1) + 1;
    ++sizp;
  } while (ABOVE_THRESHOLD (rn, INV_NEWTON_THRESHOLD));

  /* We search the inverse of 0.{dp,n}, we compute it as 1.{ip,n} */
  dp += n;
  ip += n;

  /* Compute a base value of rn limbs. */
  mpn_bc_invertappr (ip - rn, dp - rn, rn, scratch);

  TMP_MARK;

  if (ABOVE_THRESHOLD (n, INV_MULMOD_BNM1_THRESHOLD))
    {
      mn = mpn_mulmod_bnm1_next_size (n + 1);
      tp = TMP_ALLOC_LIMBS (mpn_mulmod_bnm1_itch (mn, n, (n >> 1) + 1));
    }
  /* Use Newton's iterations to get the desired precision.*/

  while (1) {
    n = *--sizp;
    /*
      v    n  v
      +----+--+
      ^ rn ^
    */

    /* Compute i_jd . */
    if (BELOW_THRESHOLD (n, INV_MULMOD_BNM1_THRESHOLD)
	|| ((mn = mpn_mulmod_bnm1_next_size (n + 1)) > (n + rn))) {
      /* FIXME: We do only need {xp,n+1}*/
      mpn_mul (xp, dp - n, n, ip - rn, rn);
      mpn_add_n (xp + rn, xp + rn, dp - n, n - rn + 1);
      cy = CNST_LIMB(1); /* Remember we truncated, Mod B^(n+1) */
      /* We computed (truncated) {xp,n+1} <- 1.{ip,rn} * 0.{dp,n} */
    } else { /* Use B^mn-1 wraparound */
      mpn_mulmod_bnm1 (xp, mn, dp - n, n, ip - rn, rn, tp);
      /* We computed {xp,mn} <- {ip,rn} * {dp,n} mod (B^mn-1) */
      /* We know that 2*|ip*dp + dp*B^rn - B^{rn+n}| < B^mn-1 */
      /* Add dp*B^rn mod (B^mn-1) */
      ASSERT (n >= mn - rn);
      cy = mpn_add_n (xp + rn, xp + rn, dp - n, mn - rn);
      cy = mpn_add_nc (xp, xp, dp - (n - (mn - rn)), n - (mn - rn), cy);
      /* Subtract B^{rn+n}, maybe only compensate the carry*/
      xp[mn] = CNST_LIMB (1); /* set a limit for DECR_U */
      MPN_DECR_U (xp + rn + n - mn, 2 * mn + 1 - rn - n, CNST_LIMB (1) - cy);
      MPN_DECR_U (xp, mn, CNST_LIMB (1) - xp[mn]); /* if DECR_U eroded xp[mn] */
      cy = CNST_LIMB(0); /* Remember we are working Mod B^mn-1 */
    }

    if (xp[n] < CNST_LIMB (2)) { /* "positive" residue class */
      cy = xp[n]; /* 0 <= cy <= 1 here. */
#if HAVE_NATIVE_mpn_sublsh1_n
      if (cy++) {
	if (mpn_cmp (xp, dp - n, n) > 0) {
	  mp_limb_t chk;
	  chk = mpn_sublsh1_n (xp, xp, dp - n, n);
	  ASSERT (chk == xp[n]);
	  ++ cy;
	} else
	  ASSERT_CARRY (mpn_sub_n (xp, xp, dp - n, n));
      }
#else /* no mpn_sublsh1_n*/
      if (cy++ && !mpn_sub_n (xp, xp, dp - n, n)) {
	ASSERT_CARRY (mpn_sub_n (xp, xp, dp - n, n));
	++cy;
      }
#endif
      /* 1 <= cy <= 3 here. */
#if HAVE_NATIVE_mpn_rsblsh1_n
      if (mpn_cmp (xp, dp - n, n) > 0) {
	ASSERT_NOCARRY (mpn_rsblsh1_n (xp + n, xp, dp - n, n));
	++cy;
      } else
	ASSERT_NOCARRY (mpn_sub_nc (xp + 2 * n - rn, dp - rn, xp + n - rn, rn, mpn_cmp (xp, dp - n, n - rn) > 0));
#else /* no mpn_rsblsh1_n*/
      if (mpn_cmp (xp, dp - n, n) > 0) {
	ASSERT_NOCARRY (mpn_sub_n (xp, xp, dp - n, n));
	++cy;
      }
      ASSERT_NOCARRY (mpn_sub_nc (xp + 2 * n - rn, dp - rn, xp + n - rn, rn, mpn_cmp (xp, dp - n, n - rn) > 0));
#endif
      MPN_DECR_U(ip - rn, rn, cy); /* 1 <= cy <= 4 here. */
    } else { /* "negative" residue class */
      ASSERT (xp[n] >= GMP_NUMB_MAX - CNST_LIMB(1));
      MPN_DECR_U(xp, n + 1, cy);
      if (xp[n] != GMP_NUMB_MAX) {
	MPN_INCR_U(ip - rn, rn, CNST_LIMB (1));
	ASSERT_CARRY (mpn_add_n (xp, xp, dp - n, n));
      }
      mpn_com (xp + 2 * n - rn, xp + n - rn, rn);
    }

    /* Compute x_ju_j. FIXME:We need {xp+rn,rn}, mulhi? */
    mpn_mul_n (xp, xp + 2 * n - rn, ip - rn, rn);
    cy = mpn_add_n (xp + rn, xp + rn, xp + 2 * n - rn, 2 * rn - n);
    cy = mpn_add_nc (ip - n, xp + 3 * rn - n, xp + n + rn, n - rn, cy);
    MPN_INCR_U (ip - rn, rn, cy);
    if (sizp == sizes) { /* Get out of the cycle */
      /* Check for possible carry propagation from below. */
      cy = xp[3 * rn - n - 1] > GMP_NUMB_MAX - CNST_LIMB (7); /* Be conservative. */
      /*    cy = mpn_add_1 (xp + rn, xp + rn, 2*rn - n, 4); */
      break;
    }
    rn = n;
  }
  TMP_FREE;

  return cy;
#undef xp
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
leaq -0x1c0(%rbp), %rcx
movq %rdx, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, (%rcx)
sarq %rax
leaq 0x1(%rax), %rdx
movq %rdx, -0x30(%rbp)
addq $0x8, %rcx
cmpq $0xf4, %rax
jg 0x321a0
movq %rcx, -0x50(%rbp)
leaq (%r12,%r15,8), %rcx
leaq (%rdi,%r15,8), %rdx
notq %rax
movq %rdx, -0x40(%rbp)
leaq (%rdx,%rax,8), %rdi
movq %rcx, -0x38(%rbp)
leaq (%rcx,%rax,8), %rsi
movq -0x30(%rbp), %rdx
movq %rbx, %rcx
callq 0x32653
xorl %r13d, %r13d
movq %r13, -0x70(%rbp)
leaq 0x1(%r15), %rdi
callq 0x2ab71
movq %r15, %rcx
sarq %rcx
movq %rax, %rdx
sarq %rdx
cmpq %rcx, %rdx
movq %rax, %rcx
cmovgq %rdx, %rcx
cmpq %r15, %rdx
cmovgeq %r13, %rcx
addq %rax, %rcx
leaq 0x20(,%rcx,8), %rsi
cmpq $0x7f00, %rsi           # imm = 0x7F00
ja 0x3263a
movq %rsp, %rax
addq $0xf, %rsi
andq $-0x10, %rsi
subq %rsi, %rax
movq %rax, -0x68(%rbp)
movq %rax, %rsp
leaq (%r12,%r15,8), %rax
addq $-0x8, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %r15
movq -0x8(%r15), %r14
leaq 0x1(%r14), %rdi
callq 0x2ab71
movq %rax, %r12
movq -0x30(%rbp), %rcx
leaq (%r14,%rcx), %rax
leaq (%rbx,%rcx,8), %r13
cmpq %rax, %r12
movq %r15, -0x50(%rbp)
movq %r13, -0x60(%rbp)
leaq (,%r14,8), %rax
jle 0x322d0
movq -0x38(%rbp), %r15
subq %rax, %r15
movq -0x30(%rbp), %r12
leaq (,%r12,8), %rax
movq -0x40(%rbp), %rcx
subq %rax, %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %r8
callq 0x1c838
movq %r14, %rcx
subq %r12, %rcx
incq %rcx
movq %r13, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x1ba80
movl $0x1, %eax
jmp 0x3238e
movq -0x38(%rbp), %r13
subq %rax, %r13
movq -0x30(%rbp), %r9
leaq (,%r9,8), %rax
movq -0x40(%rbp), %r8
subq %rax, %r8
subq $0x8, %rsp
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
pushq -0x68(%rbp)
callq 0x2a51a
addq $0x10, %rsp
movq %r12, %r15
subq -0x30(%rbp), %r15
movq -0x60(%rbp), %rdi
movq %rdi, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x1ba80
movq %r14, %rcx
subq %r15, %rcx
leaq (,%rcx,8), %rsi
movq -0x38(%rbp), %rdx
subq %rsi, %rdx
movq %rbx, %rdi
movq %rbx, %rsi
movq %rax, %r8
callq 0x1ba60
movq %r12, %rcx
shlq $0x3, %rcx
movl $0x1, %edx
movq %rdx, (%rbx,%r12,8)
subq %rax, %rdx
movq -0x60(%rbp), %rax
leaq (%rax,%r14,8), %rax
subq %rcx, %rax
subq %rdx, (%rax)
jae 0x32370
subq $0x1, 0x8(%rax)
leaq 0x8(%rax), %rax
jb 0x32365
movl $0x1, %eax
subq (%rbx,%r12,8), %rax
movq %rbx, %rcx
subq %rax, (%rcx)
jae 0x3238c
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x32381
xorl %eax, %eax
movq %r14, %r15
leaq (%rbx,%r14,8), %r14
movq (%r14), %r13
cmpq $0x1, %r13
movq %r14, -0x78(%rbp)
ja 0x32403
movq %r15, %rcx
negq %rcx
testq %r13, %r13
movq %rcx, -0x48(%rbp)
je 0x32479
leaq 0x1(%r13), %r12
movq -0x38(%rbp), %rax
leaq (%rax,%rcx,8), %rdx
movq -0x58(%rbp), %rax
movq %r15, %rcx
testq %rcx, %rcx
jle 0x32465
leaq -0x1(%rcx), %rsi
movq (%rax), %rdi
addq $-0x8, %rax
cmpq %rdi, -0x8(%rbx,%rcx,8)
movq %rsi, %rcx
je 0x323c8
jbe 0x32465
movq %rbx, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x2f300
movq -0x48(%rbp), %rcx
orq $0x2, %r13
movq %r13, %r12
jmp 0x3247f
movq %rbx, %rcx
subq %rax, (%rcx)
jae 0x32416
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x3240b
movq -0x30(%rbp), %r13
negq %r13
cmpq $-0x1, (%r14)
movq -0x40(%rbp), %rax
je 0x3254d
leaq (%rax,%r13,8), %rax
addq $0x1, (%rax)
leaq 0x8(%rax), %rax
jb 0x3242f
movq %r15, %rax
negq %rax
movq %rax, -0x48(%rbp)
leaq (,%r15,8), %rax
movq -0x38(%rbp), %rdx
subq %rax, %rdx
movq %rbx, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x1ba80
jmp 0x32557
movq %rbx, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x1bca0
movq -0x48(%rbp), %rcx
jmp 0x3247f
movl $0x1, %r12d
movq -0x38(%rbp), %rax
leaq (%rax,%rcx,8), %rdx
movq -0x58(%rbp), %rax
movq %r15, %rcx
testq %rcx, %rcx
jle 0x324bd
leaq -0x1(%rcx), %rsi
movq (%rax), %rdi
addq $-0x8, %rax
cmpq %rdi, -0x8(%rbx,%rcx,8)
movq %rsi, %rcx
je 0x3248e
jbe 0x324bd
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x2f420
incq %r12
jmp 0x3251f
movq %r15, %rdi
shlq $0x4, %rdi
addq %rbx, %rdi
movq -0x30(%rbp), %rax
leaq (,%rax,8), %r8
subq %r8, %rdi
movq -0x38(%rbp), %rsi
subq %r8, %rsi
movq %r14, %rdx
subq %r8, %rdx
movq %r15, %rcx
subq %rax, %rcx
movq -0x58(%rbp), %rax
subq %r8, %rax
testq %rcx, %rcx
jle 0x32513
leaq -0x1(%rcx), %r8
movq (%rax), %r9
addq $-0x8, %rax
cmpq %r9, -0x8(%rbx,%rcx,8)
movq %r8, %rcx
je 0x324f0
seta %al
movzbl %al, %r8d
jmp 0x32516
xorl %r8d, %r8d
movq -0x30(%rbp), %rcx
callq 0x1bc80
movq -0x30(%rbp), %rax
movq %rax, %r13
negq %r13
leaq (,%rax,8), %rax
movq -0x40(%rbp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
subq %r12, (%rcx)
jae 0x3254b
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x32540
jmp 0x32576
movq %r15, %rax
negq %rax
movq %rax, -0x48(%rbp)
movq %r15, %rax
shlq $0x4, %rax
addq %rbx, %rax
leaq (%rax,%r13,8), %rdi
leaq (%r14,%r13,8), %rsi
movq -0x30(%rbp), %rdx
callq 0x1bde0
movq -0x40(%rbp), %rdx
addq $-0x8, -0x50(%rbp)
movq %r15, %rax
shlq $0x4, %rax
addq %rbx, %rax
leaq (%rax,%r13,8), %r12
leaq (%rdx,%r13,8), %r13
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq -0x30(%rbp), %r14
movq %r14, %rcx
callq 0x1d348
leaq (%r14,%r14), %rcx
subq %r15, %rcx
movq -0x60(%rbp), %rdi
movq %rdi, %rsi
movq %r12, %rdx
callq 0x1ba80
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq (%rcx,%rdx,8), %rdi
leaq (%r14,%r14,2), %rcx
leaq (%rbx,%rcx,8), %r12
leaq (%r12,%rdx,8), %rsi
movq -0x78(%rbp), %rcx
leaq (%rcx,%r14,8), %rdx
movq %r15, %rcx
subq %r14, %rcx
movq %rax, %r8
callq 0x1ba60
movq %r15, %rcx
addq %rax, (%r13)
jae 0x325fa
addq $0x1, 0x8(%r13)
leaq 0x8(%r13), %r13
jb 0x325ef
movq -0x50(%rbp), %r15
movq %rcx, -0x30(%rbp)
leaq -0x1c0(%rbp), %rax
cmpq %rax, %r15
jne 0x32253
notq %rcx
movq (%r12,%rcx,8), %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
jne 0x3264c
xorl %eax, %eax
cmpq $-0x7, %rbx
setae %al
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x2fdb0
movq %rax, -0x68(%rbp)
jmp 0x32243
callq 0x2fde3
jmp 0x32622