// File: /workspace/github_cpp_makefile_star2_O2/marxn[P]pupp/3rdparty/gmp/mpn/dcpi1_divappr_q.c
// Function: _gmpn_dcpi1_divappr_q
mp_limb_t
mpn_dcpi1_divappr_q (mp_ptr qp, mp_ptr np, mp_size_t nn,
		     mp_srcptr dp, mp_size_t dn, gmp_pi1_t *dinv)
{
  mp_size_t qn;
  mp_limb_t qh, cy, qsave;
  mp_ptr tp;
  TMP_DECL;

  TMP_MARK;

  ASSERT (dn >= 6);
  ASSERT (nn > dn);
  ASSERT (dp[dn-1] & GMP_NUMB_HIGHBIT);

  qn = nn - dn;
  qp += qn;
  np += nn;
  dp += dn;

  if (qn >= dn)
    {
      qn++;			/* pretend we'll need an extra limb */
      /* Reduce qn mod dn without division, optimizing small operations.  */
      do
	qn -= dn;
      while (qn > dn);

      qp -= qn;			/* point at low limb of next quotient block */
      np -= qn;			/* point in the middle of partial remainder */

      tp = TMP_SALLOC_LIMBS (dn);

      /* Perform the typically smaller block first.  */
      if (qn == 1)
	{
	  mp_limb_t q, n2, n1, n0, d1, d0;

	  /* Handle qh up front, for simplicity. */
	  qh = mpn_cmp (np - dn + 1, dp - dn, dn) >= 0;
	  if (qh)
	    ASSERT_NOCARRY (mpn_sub_n (np - dn + 1, np - dn + 1, dp - dn, dn));

	  /* A single iteration of schoolbook: One 3/2 division,
	     followed by the bignum update and adjustment. */
	  n2 = np[0];
	  n1 = np[-1];
	  n0 = np[-2];
	  d1 = dp[-1];
	  d0 = dp[-2];

	  ASSERT (n2 < d1 || (n2 == d1 && n1 <= d0));

	  if (UNLIKELY (n2 == d1) && n1 == d0)
	    {
	      q = GMP_NUMB_MASK;
	      cy = mpn_submul_1 (np - dn, dp - dn, dn, q);
	      ASSERT (cy == n2);
	    }
	  else
	    {
	      udiv_qr_3by2 (q, n1, n0, n2, n1, n0, d1, d0, dinv->inv32);

	      if (dn > 2)
		{
		  mp_limb_t cy, cy1;
		  cy = mpn_submul_1 (np - dn, dp - dn, dn - 2, q);

		  cy1 = n0 < cy;
		  n0 = (n0 - cy) & GMP_NUMB_MASK;
		  cy = n1 < cy1;
		  n1 = (n1 - cy1) & GMP_NUMB_MASK;
		  np[-2] = n0;

		  if (UNLIKELY (cy != 0))
		    {
		      n1 += d1 + mpn_add_n (np - dn, np - dn, dp - dn, dn - 1);
		      qh -= (q == 0);
		      q = (q - 1) & GMP_NUMB_MASK;
		    }
		}
	      else
		np[-2] = n0;

	      np[-1] = n1;
	    }
	  qp[0] = q;
	}
      else
	{
	  if (qn == 2)
	    qh = mpn_divrem_2 (qp, 0L, np - 2, 4, dp - 2);
	  else if (BELOW_THRESHOLD (qn, DC_DIV_QR_THRESHOLD))
	    qh = mpn_sbpi1_div_qr (qp, np - qn, 2 * qn, dp - qn, qn, dinv->inv32);
	  else
	    qh = mpn_dcpi1_div_qr_n (qp, np - qn, dp - qn, qn, dinv, tp);

	  if (qn != dn)
	    {
	      if (qn > dn - qn)
		mpn_mul (tp, qp, qn, dp - dn, dn - qn);
	      else
		mpn_mul (tp, dp - dn, dn - qn, qp, qn);

	      cy = mpn_sub_n (np - dn, np - dn, tp, dn);
	      if (qh != 0)
		cy += mpn_sub_n (np - dn + qn, np - dn + qn, dp - dn, dn - qn);

	      while (cy != 0)
		{
		  qh -= mpn_sub_1 (qp, qp, qn, 1);
		  cy -= mpn_add_n (np - dn, np - dn, dp - dn, dn);
		}
	    }
	}
      qn = nn - dn - qn + 1;
      while (qn > dn)
	{
	  qp -= dn;
	  np -= dn;
	  mpn_dcpi1_div_qr_n (qp, np - dn, dp - dn, dn, dinv, tp);
	  qn -= dn;
	}

      /* Since we pretended we'd need an extra quotient limb before, we now
	 have made sure the code above left just dn-1=qn quotient limbs to
	 develop.  Develop that plus a guard limb. */
      qn--;
      qp -= qn;
      np -= dn;
      qsave = qp[qn];
      mpn_dcpi1_divappr_q_n (qp, np - dn, dp - dn, dn, dinv, tp);
      MPN_COPY_INCR (qp, qp + 1, qn);
      qp[qn] = qsave;
    }
  else    /* (qn < dn) */
    {
      mp_ptr q2p;
#if 0				/* not possible since we demand nn > dn */
      if (qn == 0)
	{
	  qh = mpn_cmp (np - dn, dp - dn, dn) >= 0;
	  if (qh)
	    mpn_sub_n (np - dn, np - dn, dp - dn, dn);
	  TMP_FREE;
	  return qh;
	}
#endif

      qp -= qn;			/* point at low limb of next quotient block */
      np -= qn;			/* point in the middle of partial remainder */

      q2p = TMP_SALLOC_LIMBS (qn + 1);
      /* Should we at all check DC_DIVAPPR_Q_THRESHOLD here, or reply on
	 callers not to be silly?  */
      if (BELOW_THRESHOLD (qn, DC_DIVAPPR_Q_THRESHOLD))
	{
	  qh = mpn_sbpi1_divappr_q (q2p, np - qn - 2, 2 * (qn + 1),
				    dp - (qn + 1), qn + 1, dinv->inv32);
	}
      else
	{
	  /* It is tempting to use qp for recursive scratch and put quotient in
	     tp, but the recursive scratch needs one limb too many.  */
	  tp = TMP_SALLOC_LIMBS (qn + 1);
	  qh = mpn_dcpi1_divappr_q_n (q2p, np - qn - 2, dp - (qn + 1), qn + 1, dinv, tp);
	}
      MPN_COPY (qp, q2p + 1, qn);
    }

  TMP_FREE;
  return qh;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, -0x68(%rbp)
movq %r8, %r15
movq %rsi, %r8
movq %rdi, %r12
movq %rdx, %r14
subq %r15, %r14
leaq (%rcx,%r15,8), %rsi
cmpq %r15, %r14
jge 0x279ca
leaq (%r8,%rdx,8), %rax
movq %r14, %rdx
negq %rdx
leaq (,%r14,8), %rcx
subq %rcx, %rax
leaq 0x1(%r14), %r8
movq %rsp, %rbx
leaq 0x17(,%r14,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rbx
movq %rbx, %rsp
movq %r14, %rcx
notq %rcx
leaq (%rsi,%rcx,8), %rcx
cmpq $0x107, %r14            # imm = 0x107
jle 0x27bed
leaq 0x8(,%r14,8), %rsi
movq %rsp, %r9
addq $0xf, %rsi
andq $-0x10, %rsi
subq %rsi, %r9
movq %r9, %rsp
leaq (%rax,%rdx,8), %rsi
addq $-0x10, %rsi
movq %rbx, %rdi
movq %rcx, %rdx
movq %r8, %rcx
movq -0x68(%rbp), %r8
callq 0x27744
jmp 0x27c08
movq %rsi, -0x50(%rbp)
movq %r12, -0x60(%rbp)
incq %r14
movq %r15, %rax
shlq $0x4, %rax
leaq (,%r15,8), %r9
leaq (,%rdx,8), %rdi
movq %rdi, %r11
subq %rax, %r11
addq $0x10, %r11
xorl %r13d, %r13d
xorl %ebx, %ebx
addq %r9, %rbx
subq %r15, %r13
subq %rax, %r11
leaq (%r14,%r13), %r12
cmpq %r15, %r12
jg 0x279fb
movq -0x60(%rbp), %rsi
addq %rbx, %rsi
addq $-0x8, %rsi
movq %rsi, -0x40(%rbp)
leaq (%r8,%r9), %r10
movq %rsp, %r14
leaq 0xf(,%r15,8), %rsi
andq $-0x10, %rsi
subq %rsi, %r14
movq %r14, %rsp
cmpq $0x2, %r12
movq %rcx, -0x30(%rbp)
movq %r9, -0x48(%rbp)
movq %r8, -0x70(%rbp)
movq %r14, -0x78(%rbp)
je 0x27c22
cmpq $0x1, %r12
jne 0x27c46
movq %r10, -0x38(%rbp)
leaq (%r8,%rbx), %r14
movq %r14, %rdi
movq %rcx, %rsi
movq %r15, %rdx
movq %rcx, %r12
callq 0x1ac5c
xorl %ecx, %ecx
testl %eax, %eax
setns %cl
movq %rcx, -0x58(%rbp)
js 0x27a90
movq %r14, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x17ed0
movq -0x70(%rbp), %rax
leaq (%rax,%rbx), %rdi
addq $-0x8, %rdi
movq -0x38(%rbp), %r9
movq -0x8(%r9,%rbx), %r8
movq -0x18(%rbx,%r9), %r12
movq -0x10(%rbx,%r9), %r14
movq -0x50(%rbp), %rax
movq -0x10(%rax), %rcx
movq -0x8(%rax), %r11
cmpq %r11, %r8
je 0x27e57
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
movq %r8, %rax
mulq -0xe8(%rbp)
movq %rax, %rsi
movq %rdx, %r10
movq %r8, -0xe0(%rbp)
movq %r14, -0xd8(%rbp)
addq -0xd8(%rbp), %rsi
adcq -0xe0(%rbp), %r10
movq %r10, %rax
imulq %r11, %rax
subq %rax, %r14
movq %r11, -0xd0(%rbp)
movq %rcx, -0xc8(%rbp)
subq -0xc8(%rbp), %r12
sbbq -0xd0(%rbp), %r14
movq %r10, -0xc0(%rbp)
movq %rcx, %rax
mulq -0xc0(%rbp)
movq %rdx, -0xb8(%rbp)
movq %rax, -0xb0(%rbp)
subq -0xb0(%rbp), %r12
sbbq -0xb8(%rbp), %r14
cmpq %rsi, %r14
adcq $-0x1, %r10
xorl %eax, %eax
cmpq %rsi, %r14
movl $0x0, %edx
cmovaeq %r11, %rdx
cmovaeq %rcx, %rax
incq %r10
movq %rdx, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
addq -0xa0(%rbp), %r12
adcq -0xa8(%rbp), %r14
cmpq %r11, %r14
movq -0x30(%rbp), %r8
movq -0x48(%rbp), %rsi
jae 0x27e89
cmpq $0x3, %r15
jl 0x27d8a
movq %r11, -0x88(%rbp)
leaq -0x2(%r15), %rdx
movq %r8, %rsi
movq %r10, -0x50(%rbp)
movq %r10, %rcx
movq %rdi, -0x80(%rbp)
callq 0x29f90
movq -0x38(%rbp), %r9
subq %rax, %r12
sbbq $0x0, %r14
movq %r12, -0x18(%r9,%rbx)
jb 0x27ebe
movq -0x60(%rbp), %rax
movq -0x30(%rbp), %r8
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %r10
jmp 0x27d93
leaq (%rax,%rdx,8), %rsi
addq $-0x10, %rsi
leaq (%r8,%r8), %rdx
movq -0x68(%rbp), %rax
movq (%rax), %r9
movq %rbx, %rdi
callq 0x265b0
movq %rax, %r15
addq $0x8, %rbx
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x290c6
jmp 0x27e45
movq %rdx, -0x38(%rbp)
leaq (%rbx,%r10), %rdx
addq $-0x18, %rdx
movq -0x50(%rbp), %r8
addq $-0x10, %r8
pushq $0x4
popq %rcx
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x18920
jmp 0x27ca1
movq %rdx, -0x38(%rbp)
subq %r11, %r8
cmpq $0x32, %r12
jle 0x27c79
subq %rdi, %rax
addq %rcx, %rax
leaq (%rbx,%rax), %rdx
addq $-0x8, %rdx
movq -0x40(%rbp), %rdi
movq %r8, %rsi
movq %r12, %rcx
movq -0x68(%rbp), %r8
movq %r14, %r9
callq 0x26ef0
jmp 0x27ca1
leaq (%r12,%r12), %rdx
subq %rdi, %rax
addq %rcx, %rax
leaq (%rbx,%rax), %rcx
addq $-0x8, %rcx
movq -0x68(%rbp), %rax
movq (%rax), %r9
movq -0x40(%rbp), %rdi
movq %r8, %rsi
movq %r12, %r8
callq 0x26308
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
notq %rax
leaq (%rax,%r15,2), %rax
subq %r13, %rax
jne 0x27cc8
movq %r15, %rcx
negq %rcx
movq -0x30(%rbp), %r8
movq -0x48(%rbp), %rsi
jmp 0x27da3
cmpq %rax, %r12
movq -0x30(%rbp), %rcx
movq %rax, -0x38(%rbp)
movq %r14, %rdi
jle 0x27ce4
movq -0x40(%rbp), %rsi
movq %r12, %rdx
movq %rax, %r8
jmp 0x27cf1
movq %rcx, %rsi
movq %rax, %rdx
movq -0x40(%rbp), %rcx
movq %r12, %r8
callq 0x18a58
movq %r14, %rdx
movq -0x70(%rbp), %rax
leaq (%rax,%rbx), %r14
addq $-0x8, %r14
movq %r14, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x17ed0
movq %r14, %rcx
movq %rax, %r14
cmpq $0x0, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
je 0x27d3b
leaq (%rcx,%r12,8), %rdi
movq %rdi, %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x17ed0
addq %rax, %r14
movq -0x48(%rbp), %rsi
movq %r15, %rax
negq %rax
movq %rax, -0x38(%rbp)
testq %r14, %r14
je 0x27d80
movq -0x40(%rbp), %rdi
movq %rdi, %rsi
movq %r12, %rdx
pushq $0x1
popq %rcx
callq 0x17e44
subq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq %rdi, %rsi
movq -0x30(%rbp), %rdx
movq %r15, %rcx
callq 0x17cf0
movq -0x48(%rbp), %rsi
subq %rax, %r14
jmp 0x27d49
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %rcx
jmp 0x27da3
movq %r12, -0x18(%r9,%rbx)
movq -0x60(%rbp), %rax
movq %r14, -0x10(%r9,%rbx)
movq %r15, %rcx
negq %rcx
movq %r10, -0x8(%rax,%rbx)
notq %r13
leaq (,%rcx,8), %r14
leaq (%rsi,%rcx,8), %r12
shlq $0x4, %rcx
movq -0x70(%rbp), %rax
leaq (%rax,%r15,8), %rax
addq %rcx, %rax
addq %rax, %rbx
addq $-0x8, %rbx
leaq 0x1(%r13), %rax
cmpq %r15, %rax
jle 0x27e02
movq -0x40(%rbp), %rdi
addq %r14, %rdi
movq %rdi, -0x40(%rbp)
movq %rbx, %rsi
movq %r8, %rdx
movq %r15, %rcx
movq -0x68(%rbp), %r8
movq -0x78(%rbp), %r9
callq 0x26ef0
movq -0x30(%rbp), %r8
addq %r12, -0x60(%rbp)
subq %r15, %r13
addq %r14, %rbx
jmp 0x27dc8
movq -0x40(%rbp), %r14
movq (%r14), %rax
movq %rax, -0x30(%rbp)
movq -0x60(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r8, %rdx
movq %r15, %rcx
movq -0x68(%rbp), %r8
movq -0x78(%rbp), %r9
callq 0x27744
leaq 0x8(%r12), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x290c6
movq -0x30(%rbp), %rax
movq %rax, (%r14)
movq -0x58(%rbp), %r15
movq %r15, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %rcx, %r14
jne 0x27ac4
pushq $-0x1
popq %r12
movq -0x30(%rbp), %r14
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x29f90
movq %r12, %r10
movq %r14, %r8
movq -0x60(%rbp), %rax
movq -0x48(%rbp), %rsi
jmp 0x27d98
setbe %al
cmpq %rcx, %r12
setb %dl
testb %dl, %al
jne 0x27b9a
incq %r10
movq %rcx, -0x90(%rbp)
movq %r11, -0x98(%rbp)
subq -0x90(%rbp), %r12
sbbq -0x98(%rbp), %r14
jmp 0x27b9a
leaq -0x1(%r15), %rcx
movq -0x80(%rbp), %rdi
movq %rdi, %rsi
movq -0x30(%rbp), %r12
movq %r12, %rdx
callq 0x17cf0
movq -0x38(%rbp), %r9
movq %r12, %r8
addq -0x88(%rbp), %r14
addq %rax, %r14
movq -0x50(%rbp), %r10
subq $0x1, %r10
sbbq $0x0, -0x58(%rbp)
movq -0x60(%rbp), %rax
movq -0x48(%rbp), %rsi
jmp 0x27d93