// File: marxn[P]pupp/3rdparty/gmp/gen-trialdivtab.c
// Function: main
int
main (int argc, char *argv[])
{
  unsigned long t, p;
  mpz_t ppp, acc, inv, gmp_numb_max, tmp, Bhalf;
  mpz_t pre[7];
  int i;
  int start_p, end_p, interval_start, interval_end, omitted_p;
  const char *endtok;
  int stop;
  int np, start_idx;

  if (argc < 2)
    {
      fprintf (stderr, "usage: %s bits endprime\n", argv[0]);
      exit (1);
    }

  limb_bits = atoi (argv[1]);

  end_p = 1290;			/* default end prime */
  if (argc == 3)
    end_p = atoi (argv[2]);

  printf ("#if GMP_LIMB_BITS != %d\n", limb_bits);
  printf ("#error This table is for GMP_LIMB_BITS = %d\n", limb_bits);
  printf ("#endif\n\n");

  printf ("#if GMP_NAIL_BITS != 0\n");
  printf ("#error This table does not support nails\n");
  printf ("#endif\n\n");

  for (i = 0; i < 7; i++)
    mpz_init (pre[i]);

  mpz_init_set_ui (gmp_numb_max, 1);
  mpz_mul_2exp (gmp_numb_max, gmp_numb_max, limb_bits);
  mpz_sub_ui (gmp_numb_max, gmp_numb_max, 1);

  mpz_init (tmp);
  mpz_init (inv);

  mpz_init_set_ui (B, 1);  mpz_mul_2exp (B, B, limb_bits);
  mpz_init_set_ui (Bhalf, 1);  mpz_mul_2exp (Bhalf, Bhalf, limb_bits - 1);

  start_p = 3;

  mpz_init_set_ui (ppp, 1);
  mpz_init (acc);
  interval_start = start_p;
  omitted_p = 3;
  interval_end = 0;

/*  printf ("static struct gmp_primes_dtab gmp_primes_dtab[] = {\n"); */

  printf ("#ifdef WANT_dtab\n");

  for (t = start_p; t <= end_p; t += 2)
    {
      if (! isprime (t))
	continue;

      mpz_mul_ui (acc, ppp, t);
      stop = mpz_cmp (acc, Bhalf) >= 0;
      if (!stop)
	{
	  mpn_mod_1s_4p_cps (pre, acc);
	  stop = sumspills (acc, pre + 2, 5);
	}

      if (stop)
	{
	  for (p = interval_start; p <= interval_end; p += 2)
	    {
	      if (! isprime (p))
		continue;

	      printf ("  P(%d,", (int) p);
	      mpz_invert_ui_2exp (inv, p, limb_bits);
	      printf ("CNST_LIMB(0x");  mpz_out_str (stdout, 16, inv);  printf ("),");

	      mpz_tdiv_q_ui (tmp, gmp_numb_max, p);
	      printf ("CNST_LIMB(0x");  mpz_out_str (stdout, 16, tmp);
	      printf (")),\n");
	    }
	  mpz_set_ui (ppp, t);
	  interval_start = t;
	  omitted_p = t;
	}
      else
	{
	  mpz_set (ppp, acc);
	}
      interval_end = t;
    }
  printf ("#define SMALLEST_OMITTED_PRIME %d\n", (int) omitted_p);
  printf ("#endif\n");

  printf ("#ifdef WANT_ptab\n");

/*  printf ("static struct gmp_primes_ptab gmp_primes_ptab[] = {\n"); */

  endtok = "";

  mpz_set_ui (ppp, 1);
  interval_start = start_p;
  interval_end = 0;
  np = 0;
  start_idx = 0;
  for (t = start_p; t <= end_p; t += 2)
    {
      if (! isprime (t))
	continue;

      mpz_mul_ui (acc, ppp, t);

      stop = mpz_cmp (acc, Bhalf) >= 0;
      if (!stop)
	{
	  mpn_mod_1s_4p_cps (pre, acc);
	  stop = sumspills (acc, pre + 2, 5);
	}

      if (stop)
	{
	  mpn_mod_1s_4p_cps (pre, ppp);
	  printf ("%s", endtok);
	  printf ("  {CNST_LIMB(0x");  mpz_out_str (stdout, 16, ppp);
	  printf ("),{CNST_LIMB(0x");  mpz_out_str (stdout, 16, pre[0]);
	  printf ("),%d", (int) PTR(pre[1])[0]);
	  for (i = 0; i < 5; i++)
	    {
	      printf (",");
	      printf ("CNST_LIMB(0x");  mpz_out_str (stdout, 16, pre[2 + i]);
	      printf (")");
	    }
	  printf ("},");
	  printf ("%d,", start_idx);
	  printf ("%d}", np - start_idx);

	  endtok = ",\n";
	  mpz_set_ui (ppp, t);
	  interval_start = t;
	  start_idx = np;
	}
      else
	{
	  mpz_set (ppp, acc);
	}
      interval_end = t;
      np++;
    }

  printf ("\n");
  printf ("#endif\n");

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0xb848
movq 0x77d2(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x332d(%rip), %rsi      # 0xeb64
movb $0x0, %al
callq 0x1080
movl $0x1, %edi
callq 0x10c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10b0
movl %eax, 0x7855(%rip)      # 0x130b0
movl $0x50a, -0xfc(%rbp)     # imm = 0x50A
cmpl $0x3, -0x8(%rbp)
jne 0xb87e
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10b0
movl %eax, -0xfc(%rbp)
movl 0x782c(%rip), %esi      # 0x130b0
leaq 0x32f2(%rip), %rdi      # 0xeb7d
movb $0x0, %al
callq 0x1060
movl 0x7818(%rip), %esi      # 0x130b0
leaq 0x32f7(%rip), %rdi      # 0xeb96
movb $0x0, %al
callq 0x1060
leaq 0x3316(%rip), %rdi      # 0xebc3
movb $0x0, %al
callq 0x1060
leaq 0x3311(%rip), %rdi      # 0xebcc
movb $0x0, %al
callq 0x1060
leaq 0x331b(%rip), %rdi      # 0xebe4
movb $0x0, %al
callq 0x1060
leaq 0x32ec(%rip), %rdi      # 0xebc3
movb $0x0, %al
callq 0x1060
movl $0x0, -0xf4(%rbp)
cmpl $0x7, -0xf4(%rbp)
jge 0xb91c
movslq -0xf4(%rbp), %rax
leaq -0xf0(%rbp), %rdi
shlq $0x4, %rax
addq %rax, %rdi
callq 0x31e0
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0xb8e8
leaq -0x60(%rbp), %rdi
movl $0x1, %esi
callq 0x3520
leaq -0x60(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movslq 0x7777(%rip), %rdx      # 0x130b0
callq 0x5000
leaq -0x60(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x1, %edx
callq 0x47c0
leaq -0x70(%rbp), %rdi
callq 0x31e0
leaq -0x50(%rbp), %rdi
callq 0x31e0
leaq 0x7757(%rip), %rdi      # 0x130c0
movl $0x1, %esi
callq 0x3520
movslq 0x7736(%rip), %rdx      # 0x130b0
leaq 0x773f(%rip), %rsi      # 0x130c0
movq %rsi, %rdi
callq 0x5000
leaq -0x80(%rbp), %rdi
movl $0x1, %esi
callq 0x3520
leaq -0x80(%rbp), %rdi
leaq -0x80(%rbp), %rsi
movl 0x770b(%rip), %eax      # 0x130b0
subl $0x1, %eax
movslq %eax, %rdx
callq 0x5000
movl $0x3, -0xf8(%rbp)
leaq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x3520
leaq -0x40(%rbp), %rdi
callq 0x31e0
movl -0xf8(%rbp), %eax
movl %eax, -0x100(%rbp)
movl $0x3, -0x108(%rbp)
movl $0x0, -0x104(%rbp)
leaq 0x3216(%rip), %rdi      # 0xec0e
movb $0x0, %al
callq 0x1060
movslq -0xf8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movslq -0xfc(%rbp), %rcx
cmpq %rcx, %rax
ja 0xbbd4
movq -0x18(%rbp), %rdi
callq 0xb3b0
cmpl $0x0, %eax
jne 0xba31
jmp 0xbbc3
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x4d50
leaq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x41b0
cmpl $0x0, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x114(%rbp)
cmpl $0x0, -0x114(%rbp)
jne 0xba98
leaq -0xf0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xbed0
leaq -0x40(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
addq $0x20, %rsi
movl $0x5, %edx
callq 0xc1a0
movl %eax, -0x114(%rbp)
cmpl $0x0, -0x114(%rbp)
je 0xbbac
movslq -0x100(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movslq -0x104(%rbp), %rcx
cmpq %rcx, %rax
ja 0xbb89
movq -0x20(%rbp), %rdi
callq 0xb3b0
cmpl $0x0, %eax
jne 0xbad7
jmp 0xbb78
movq -0x20(%rbp), %rax
movl %eax, %esi
leaq 0x313c(%rip), %rdi      # 0xec20
movb $0x0, %al
callq 0x1060
leaq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq 0x75b6(%rip), %rdx      # 0x130b0
callq 0xb7b0
leaq 0x316d(%rip), %rdi      # 0xec73
movb $0x0, %al
callq 0x1060
movq 0x74c4(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
leaq -0x50(%rbp), %rdx
movl $0x10, %esi
callq 0xacd0
leaq 0x30fc(%rip), %rdi      # 0xec28
movb $0x0, %al
callq 0x1060
leaq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x6510
leaq 0x3128(%rip), %rdi      # 0xec73
movb $0x0, %al
callq 0x1060
movq 0x747f(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
leaq -0x70(%rbp), %rdx
movl $0x10, %esi
callq 0xacd0
leaq 0x30ba(%rip), %rdi      # 0xec2b
movb $0x0, %al
callq 0x1060
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
jmp 0xbab0
leaq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3350
movq -0x18(%rbp), %rax
movl %eax, -0x100(%rbp)
movq -0x18(%rbp), %rax
movl %eax, -0x108(%rbp)
jmp 0xbbb9
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3390
movq -0x18(%rbp), %rax
movl %eax, -0x104(%rbp)
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x18(%rbp)
jmp 0xba0a
movl -0x108(%rbp), %esi
leaq 0x304f(%rip), %rdi      # 0xec30
movb $0x0, %al
callq 0x1060
leaq 0x3064(%rip), %rdi      # 0xec53
movb $0x0, %al
callq 0x1060
leaq 0x305e(%rip), %rdi      # 0xec5b
movb $0x0, %al
callq 0x1060
leaq 0x2fc0(%rip), %rax      # 0xebcb
movq %rax, -0x110(%rbp)
leaq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x3350
movl -0xf8(%rbp), %eax
movl %eax, -0x100(%rbp)
movl $0x0, -0x104(%rbp)
movl $0x0, -0x118(%rbp)
movl $0x0, -0x11c(%rbp)
movslq -0xf8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movslq -0xfc(%rbp), %rcx
cmpq %rcx, %rax
ja 0xbe9f
movq -0x18(%rbp), %rdi
callq 0xb3b0
cmpl $0x0, %eax
jne 0xbc7c
jmp 0xbe8e
leaq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x4d50
leaq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x41b0
cmpl $0x0, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x114(%rbp)
cmpl $0x0, -0x114(%rbp)
jne 0xbce3
leaq -0xf0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xbed0
leaq -0x40(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
addq $0x20, %rsi
movl $0x5, %edx
callq 0xc1a0
movl %eax, -0x114(%rbp)
cmpl $0x0, -0x114(%rbp)
je 0xbe68
leaq -0xf0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0xbed0
movq -0x110(%rbp), %rsi
leaq 0x2f5f(%rip), %rdi      # 0xec6d
movb $0x0, %al
callq 0x1060
leaq 0x2f54(%rip), %rdi      # 0xec70
movb $0x0, %al
callq 0x1060
movq 0x72ae(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
leaq -0x30(%rbp), %rdx
movl $0x10, %esi
callq 0xacd0
leaq 0x2f3e(%rip), %rdi      # 0xec80
movb $0x0, %al
callq 0x1060
movq 0x7288(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
leaq -0xf0(%rbp), %rdx
movl $0x10, %esi
callq 0xacd0
movq -0xd8(%rbp), %rax
movq (%rax), %rax
movl %eax, %esi
leaq 0x2f19(%rip), %rdi      # 0xec90
movb $0x0, %al
callq 0x1060
movl $0x0, -0xf4(%rbp)
cmpl $0x5, -0xf4(%rbp)
jge 0xbdf9
leaq 0x2e91(%rip), %rdi      # 0xec29
movb $0x0, %al
callq 0x1060
leaq 0x2ecd(%rip), %rdi      # 0xec73
movb $0x0, %al
callq 0x1060
movq 0x7224(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
movl -0xf4(%rbp), %eax
addl $0x2, %eax
cltq
leaq -0xf0(%rbp), %rdx
shlq $0x4, %rax
addq %rax, %rdx
movl $0x10, %esi
callq 0xacd0
leaq 0x2be3(%rip), %rdi      # 0xe9c4
movb $0x0, %al
callq 0x1060
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0xbd88
leaq 0x2e95(%rip), %rdi      # 0xec95
movb $0x0, %al
callq 0x1060
movl -0x11c(%rbp), %esi
leaq 0x2e10(%rip), %rdi      # 0xec24
movb $0x0, %al
callq 0x1060
movl -0x118(%rbp), %esi
subl -0x11c(%rbp), %esi
leaq 0x2e6a(%rip), %rdi      # 0xec98
movb $0x0, %al
callq 0x1060
leaq 0x2df1(%rip), %rax      # 0xec2d
movq %rax, -0x110(%rbp)
leaq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3350
movq -0x18(%rbp), %rax
movl %eax, -0x100(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x11c(%rbp)
jmp 0xbe75
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3390
movq -0x18(%rbp), %rax
movl %eax, -0x104(%rbp)
movl -0x118(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x118(%rbp)
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x18(%rbp)
jmp 0xbc55
leaq 0x2d24(%rip), %rdi      # 0xebca
movb $0x0, %al
callq 0x1060
leaq 0x2d9f(%rip), %rdi      # 0xec53
movb $0x0, %al
callq 0x1060
xorl %eax, %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)