// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rcx, -0x48(%rbp)
movq %rdx, %r15
movq %rsi, %r12
leaq -0x1d0(%rbp), %rcx
movq %rdx, (%rcx)
movq %rdx, %rax
sarq %rax
leaq 0x1(%rax), %rdx
addq $0x8, %rcx
cmpq $0xf4, %rax
jg 0x3254d
movq %rcx, -0x58(%rbp)
leaq (%r12,%r15,8), %rcx
leaq (%rdi,%r15,8), %rsi
notq %rax
movq %rsi, -0x40(%rbp)
leaq (%rsi,%rax,8), %rdi
movq %rcx, -0x38(%rbp)
leaq (%rcx,%rax,8), %rsi
movq %rdx, %r14
movq -0x48(%rbp), %rcx
callq 0x32a2d
xorl %r13d, %r13d
movq %r13, -0x80(%rbp)
leaq 0x1(%r15), %rdi
callq 0x2aee3
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 0x32a14
movq %rsp, %rax
addq $0xf, %rsi
andq $-0x10, %rsi
subq %rsi, %rax
movq %rax, -0x70(%rbp)
movq %rax, %rsp
leaq (%r12,%r15,8), %rax
addq $-0x8, %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %r15
movq %r15, -0x58(%rbp)
movq -0x8(%r15), %rbx
leaq 0x1(%rbx), %rdi
callq 0x2aee3
movq %rax, %r12
leaq (%rbx,%r14), %rax
movq -0x48(%rbp), %rdi
leaq (%rdi,%r14,8), %r13
cmpq %rax, %r12
movq %r14, -0x50(%rbp)
movq %r13, -0x78(%rbp)
leaq (,%rbx,8), %rax
jle 0x3267d
movq -0x38(%rbp), %r15
subq %rax, %r15
leaq (,%r14,8), %rax
movq -0x40(%rbp), %rcx
subq %rax, %rcx
movq -0x48(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r14, %r8
callq 0x1cd58
movq %rbx, %rcx
subq %r14, %rcx
incq %rcx
movq %r13, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq -0x48(%rbp), %r15
callq 0x1bfa0
movl $0x1, %eax
jmp 0x32739
movq -0x38(%rbp), %r13
subq %rax, %r13
leaq (,%r14,8), %rax
movq -0x40(%rbp), %r8
subq %rax, %r8
subq $0x8, %rsp
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r14, %r9
pushq -0x70(%rbp)
callq 0x2a886
addq $0x10, %rsp
movq %r12, %r15
subq %r14, %r15
movq -0x78(%rbp), %rdi
movq %rdi, %rsi
movq %rdi, %r14
movq %r13, %rdx
movq %r15, %rcx
callq 0x1bfa0
movq %rbx, %rcx
subq %r15, %rcx
movq -0x48(%rbp), %r15
leaq (,%rcx,8), %rsi
movq -0x38(%rbp), %rdx
subq %rsi, %rdx
movq %r15, %rdi
movq %r15, %rsi
movq %rax, %r8
callq 0x1bf80
movq %r12, %rcx
shlq $0x3, %rcx
movl $0x1, %edx
movq %rdx, (%r15,%r12,8)
subq %rax, %rdx
leaq (%r14,%rbx,8), %rax
subq %rcx, %rax
subq %rdx, (%rax)
jae 0x3271b
subq $0x1, 0x8(%rax)
leaq 0x8(%rax), %rax
jb 0x32710
movl $0x1, %eax
subq (%r15,%r12,8), %rax
movq %r15, %rcx
subq %rax, (%rcx)
jae 0x32737
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x3272c
xorl %eax, %eax
movq %rbx, -0x30(%rbp)
leaq (%r15,%rbx,8), %r12
movq (%r12), %r13
cmpq $0x1, %r13
movq %r12, -0x88(%rbp)
ja 0x327bd
leaq 0x1(%r13), %rax
movq %rax, -0x60(%rbp)
testq %r13, %r13
je 0x3285c
movq -0x30(%rbp), %rcx
leaq (,%rcx,8), %rax
movq -0x38(%rbp), %rdx
subq %rax, %rdx
movq -0x68(%rbp), %rax
testq %rcx, %rcx
jle 0x32840
leaq -0x1(%rcx), %rsi
movq (%rax), %rdi
addq $-0x8, %rax
cmpq %rdi, -0x8(%r15,%rcx,8)
movq %rsi, %rcx
je 0x3277a
jbe 0x32840
movq %r15, %rdi
movq %r15, %rsi
movq -0x30(%rbp), %rbx
movq %rbx, %rcx
callq 0x2f6c0
orq $0x2, %r13
movq %r13, -0x60(%rbp)
jmp 0x32852
movq %r15, %rcx
subq %rax, (%rcx)
jae 0x327d0
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x327c5
cmpq $-0x1, (%r12)
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r13
movq -0x30(%rbp), %rbx
je 0x32818
leaq (,%r13,8), %rax
movq %r14, %rcx
subq %rax, %rcx
addq $0x1, (%rcx)
leaq 0x8(%rcx), %rcx
jb 0x327f1
leaq (,%rbx,8), %rax
movq -0x38(%rbp), %rdx
subq %rax, %rdx
movq %r15, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x1bfa0
movq %rbx, %rdi
shlq $0x4, %rdi
addq %r15, %rdi
leaq (,%r13,8), %rax
subq %rax, %rdi
movq %r12, %rsi
subq %rax, %rsi
movq %r13, %rdx
callq 0x1c300
jmp 0x32930
movq %r15, %rdi
movq %r15, %rsi
movq -0x30(%rbp), %rbx
movq %rbx, %rcx
callq 0x1c1c0
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r13
jmp 0x32868
movq -0x40(%rbp), %r14
movq -0x50(%rbp), %r13
movq -0x30(%rbp), %rbx
leaq (,%rbx,8), %rax
movq -0x38(%rbp), %rdx
subq %rax, %rdx
movq -0x68(%rbp), %rax
movq %rbx, %rcx
testq %rcx, %rcx
jle 0x328b1
leaq -0x1(%rcx), %rsi
movq (%rax), %rdi
addq $-0x8, %rax
cmpq %rdi, -0x8(%r15,%rcx,8)
movq %rsi, %rcx
je 0x3287e
jbe 0x328b1
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x2f7e0
movq -0x60(%rbp), %rdx
incq %rdx
jmp 0x32912
movq %rbx, %rdi
shlq $0x4, %rdi
addq %r15, %rdi
leaq (,%r13,8), %r8
subq %r8, %rdi
movq -0x38(%rbp), %rsi
subq %r8, %rsi
movq %r12, %rdx
subq %r8, %rdx
movq %rbx, %rcx
subq %r13, %rcx
movq -0x68(%rbp), %rax
subq %r8, %rax
testq %rcx, %rcx
jle 0x32903
leaq -0x1(%rcx), %r8
movq (%rax), %r9
addq $-0x8, %rax
cmpq %r9, -0x8(%r15,%rcx,8)
movq %r8, %rcx
je 0x328e0
seta %al
movzbl %al, %r8d
jmp 0x32906
xorl %r8d, %r8d
movq %r13, %rcx
callq 0x1c1a0
movq -0x60(%rbp), %rdx
leaq (,%r13,8), %rax
movq %r14, %rcx
subq %rax, %rcx
subq %rdx, (%rcx)
jae 0x32930
subq $0x1, 0x8(%rcx)
leaq 0x8(%rcx), %rcx
jb 0x32925
addq $-0x8, -0x58(%rbp)
movq %rbx, %r12
shlq $0x4, %r12
movq -0x48(%rbp), %r15
addq %r15, %r12
leaq (,%r13,8), %rax
subq %rax, %r12
movq %r14, %rbx
subq %rax, %rbx
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x1d864
leaq (,%r13,2), %rcx
subq -0x30(%rbp), %rcx
movq -0x78(%rbp), %rdi
movq %rdi, %rsi
movq %r12, %rdx
callq 0x1bfa0
movq -0x30(%rbp), %rcx
leaq (,%rcx,8), %rcx
subq %rcx, %r14
leaq (,%r13,2), %rdx
addq %r13, %rdx
leaq (%r15,%rdx,8), %r12
movq %r12, %rsi
subq %rcx, %rsi
movq -0x88(%rbp), %rcx
leaq (%rcx,%r13,8), %rdx
movq -0x30(%rbp), %rcx
subq %r13, %rcx
movq %r14, %rdi
movq %rax, %r8
callq 0x1bf80
movq -0x58(%rbp), %r15
addq %rax, (%rbx)
jae 0x329d5
addq $0x1, 0x8(%rbx)
leaq 0x8(%rbx), %rbx
jb 0x329ca
movq -0x30(%rbp), %rcx
movq %rcx, %r14
leaq -0x1d0(%rbp), %rax
cmpq %rax, %r15
jne 0x325fb
notq %rcx
movq (%r12,%rcx,8), %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
jne 0x32a26
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 -0x80(%rbp), %rdi
callq 0x30170
movq %rax, -0x70(%rbp)
jmp 0x325eb
callq 0x301a3
jmp 0x329fc