#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_6__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ mpz_t;
typedef TYPE_1__ *mpz_srcptr;
typedef int /*<<< orphan*/ gmp_randstate_t;
struct TYPE_6__ {
  int _mp_size;
};

/* Variables and functions */
int /*<<< orphan*/ gmp_randclear(int /*<<< orphan*/);
int /*<<< orphan*/ gmp_randinit_default(int /*<<< orphan*/);
int mpi_millerrabin(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/,
                    int /*<<< orphan*/, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ mpz_add_ui(int /*<<< orphan*/, int /*<<< orphan*/, long);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, long);
int /*<<< orphan*/ mpz_init2(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_powm(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, TYPE_1__ *);
unsigned long mpz_scan1(int /*<<< orphan*/, long);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, long);
int /*<<< orphan*/ mpz_sub_ui(int /*<<< orphan*/, TYPE_1__ *, long);
int /*<<< orphan*/ mpz_tdiv_q_2exp(int /*<<< orphan*/, int /*<<< orphan*/,
                                   unsigned long);
int /*<<< orphan*/ mpz_urandomm(int /*<<< orphan*/, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int mpi_mpz_millerrabin(mpz_srcptr n, int reps) {
  int r;
  mpz_t nm1, nm3, x, y, q;
  unsigned long int k;
  gmp_randstate_t rstate;
  int is_prime;
  mpz_init2(nm1, n->_mp_size);
  mpz_sub_ui(nm1, n, 1L);
  mpz_init2(x, n->_mp_size + 1);
  mpz_init2(y, 2 * n->_mp_size);
  mpz_set_ui(x, 210L);
  mpz_powm(y, x, nm1, n);
  if (mpz_cmp_ui(y, 1L) != 0) {
    return 0;
  }
  mpz_init2(q, n->_mp_size);
  k = mpz_scan1(nm1, 0L);
  mpz_tdiv_q_2exp(q, nm1, k);
  mpz_init2(nm3, n->_mp_size + 1);
  mpz_sub_ui(nm3, n, 3L);
  gmp_randinit_default(rstate);
  is_prime = 1;
  for (r = 0; r < reps && is_prime; r++) {
    mpz_urandomm(x, rstate, nm3);
    mpz_add_ui(x, x, 2L);
    is_prime = mpi_millerrabin(n, nm1, x, y, q, k);
  }
  gmp_randclear(rstate);
  return is_prime;
}


x86 assembly (clang, -O0):
<mpi_mpz_millerrabin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   10c0 <mpz_init2@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1060 <mpz_sub_ui@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
add    $0x1,%esi
call   10c0 <mpz_init2@plt>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
shl    $0x1,%esi
call   10c0 <mpz_init2@plt>
mov    -0x24(%rbp),%edi
mov    $0xd2,%esi
call   1090 <mpz_set_ui@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rcx
call   1030 <mpz_powm@plt>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1080 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
je     1252 <mpi_mpz_millerrabin+0x92>
movl   $0x0,-0x4(%rbp)
jmp    1339 <mpi_mpz_millerrabin+0x179>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   10c0 <mpz_init2@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <mpz_scan1@plt>
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x38(%rbp),%rdx
call   1070 <mpz_tdiv_q_2exp@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
add    $0x1,%esi
call   10c0 <mpz_init2@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1060 <mpz_sub_ui@plt>
mov    -0x3c(%rbp),%edi
call   10d0 <gmp_randinit_default@plt>
movl   $0x1,-0x40(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x41(%rbp)
jge    12d2 <mpi_mpz_millerrabin+0x112>
cmpl   $0x0,-0x40(%rbp)
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    12e2 <mpi_mpz_millerrabin+0x122>
jmp    132b <mpi_mpz_millerrabin+0x16b>
mov    -0x24(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   10e0 <mpz_urandomm@plt>
mov    -0x24(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    $0x2,%edx
call   1050 <mpz_add_ui@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x38(%rbp),%r9
call   10a0 <mpi_millerrabin@plt>
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12b7 <mpi_mpz_millerrabin+0xf7>
mov    -0x3c(%rbp),%edi
call   1040 <gmp_randclear@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpi_mpz_millerrabin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r12
mov    (%rdi),%esi
call   10c0 <mpz_init2@plt>
mov    $0x1,%edx
mov    %r12,%rsi
call   1060 <mpz_sub_ui@plt>
mov    (%r12),%esi
add    $0x1,%esi
call   10c0 <mpz_init2@plt>
mov    (%r12),%esi
add    %esi,%esi
call   10c0 <mpz_init2@plt>
mov    $0xd2,%esi
call   1090 <mpz_set_ui@plt>
mov    %r12,%rcx
call   1030 <mpz_powm@plt>
mov    $0x1,%esi
call   1080 <mpz_cmp_ui@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1294 <mpi_mpz_millerrabin+0xd4>
mov    (%r12),%esi
call   10c0 <mpz_init2@plt>
xor    %esi,%esi
call   10b0 <mpz_scan1@plt>
mov    %rax,%r15
mov    %rax,%rdx
call   1070 <mpz_tdiv_q_2exp@plt>
mov    (%r12),%esi
add    $0x1,%esi
call   10c0 <mpz_init2@plt>
mov    $0x3,%edx
mov    %r12,%rsi
call   1060 <mpz_sub_ui@plt>
call   10d0 <gmp_randinit_default@plt>
test   %r14d,%r14d
jle    128a <mpi_mpz_millerrabin+0xca>
mov    $0x1,%ebx
nop
call   10e0 <mpz_urandomm@plt>
mov    $0x2,%edx
call   1050 <mpz_add_ui@plt>
mov    %r12,%rdi
mov    %r15,%r9
call   10a0 <mpi_millerrabin@plt>
mov    %eax,%ebp
cmp    %r14d,%ebx
jge    128f <mpi_mpz_millerrabin+0xcf>
add    $0x1,%ebx
test   %ebp,%ebp
jne    1260 <mpi_mpz_millerrabin+0xa0>
jmp    128f <mpi_mpz_millerrabin+0xcf>
mov    $0x1,%ebp
call   1040 <gmp_randclear@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_6__ = type { i32 }
define i32 @mpi_mpz_millerrabin(%struct.TYPE_6__* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.TYPE_6__*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
store %struct.TYPE_6__* %0, %struct.TYPE_6__** %4
store i32 %1, i32* %5
%15 = load i32, i32* %7
%16 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%17 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @mpz_init2(i32 %15, i32 %18)
%20 = load i32, i32* %7
%21 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%22 = call i32 @mpz_sub_ui(i32 %20, %struct.TYPE_6__* %21, i64 1)
%23 = load i32, i32* %9
%24 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%25 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = add nsw i32 %26, 1
%28 = call i32 @mpz_init2(i32 %23, i32 %27)
%29 = load i32, i32* %10
%30 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%31 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = mul nsw i32 2, %32
%34 = call i32 @mpz_init2(i32 %29, i32 %33)
%35 = load i32, i32* %9
%36 = call i32 @mpz_set_ui(i32 %35, i64 210)
%37 = load i32, i32* %10
%38 = load i32, i32* %9
%39 = load i32, i32* %7
%40 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%41 = call i32 @mpz_powm(i32 %37, i32 %38, i32 %39, %struct.TYPE_6__* %40)
%42 = load i32, i32* %10
%43 = call i64 @mpz_cmp_ui(i32 %42, i64 1)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %2
store i32 0, i32* %3
br label %100
46: ; preds = %2
%47 = load i32, i32* %11
%48 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%49 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @mpz_init2(i32 %47, i32 %50)
%52 = load i32, i32* %7
%53 = call i64 @mpz_scan1(i32 %52, i64 0)
store i64 %53, i64* %12
%54 = load i32, i32* %11
%55 = load i32, i32* %7
%56 = load i64, i64* %12
%57 = call i32 @mpz_tdiv_q_2exp(i32 %54, i32 %55, i64 %56)
%58 = load i32, i32* %8
%59 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%60 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %59, i32 0, i32 0
%61 = load i32, i32* %60
%62 = add nsw i32 %61, 1
%63 = call i32 @mpz_init2(i32 %58, i32 %62)
%64 = load i32, i32* %8
%65 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%66 = call i32 @mpz_sub_ui(i32 %64, %struct.TYPE_6__* %65, i64 3)
%67 = load i32, i32* %13
%68 = call i32 @gmp_randinit_default(i32 %67)
store i32 1, i32* %14
store i32 0, i32* %6
br label %69
69: ; preds = %93, %46
%70 = load i32, i32* %6
%71 = load i32, i32* %5
%72 = icmp slt i32 %70, %71
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i32, i32* %14
%75 = icmp ne i32 %74, 0
br label %76
76: ; preds = %73, %69
%77 = phi i1 [ false, %69 ], [ %75, %73 ]
br i1 %77, label %78, label %96
78: ; preds = %76
%79 = load i32, i32* %9
%80 = load i32, i32* %13
%81 = load i32, i32* %8
%82 = call i32 @mpz_urandomm(i32 %79, i32 %80, i32 %81)
%83 = load i32, i32* %9
%84 = load i32, i32* %9
%85 = call i32 @mpz_add_ui(i32 %83, i32 %84, i64 2)
%86 = load %struct.TYPE_6__*, %struct.TYPE_6__** %4
%87 = load i32, i32* %7
%88 = load i32, i32* %9
%89 = load i32, i32* %10
%90 = load i32, i32* %11
%91 = load i64, i64* %12
%92 = call i32 @mpi_millerrabin(%struct.TYPE_6__* %86, i32 %87, i32 %88, i32 %89, i32 %90, i64 %91)
store i32 %92, i32* %14
br label %93
93: ; preds = %78
%94 = load i32, i32* %6
%95 = add nsw i32 %94, 1
store i32 %95, i32* %6
br label %69
96: ; preds = %76
%97 = load i32, i32* %13
%98 = call i32 @gmp_randclear(i32 %97)
%99 = load i32, i32* %14
store i32 %99, i32* %3
br label %100
100: ; preds = %96, %45
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
%struct.TYPE_6__ = type { i32 }
define i32 @mpi_mpz_millerrabin(%struct.TYPE_6__* %0, i32 %1) {
%3 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = tail call i32 @mpz_init2(i32 undef, i32 %4)
%6 = tail call i32 @mpz_sub_ui(i32 undef, %struct.TYPE_6__* %0, i64 1)
%7 = load i32, i32* %3
%8 = add nsw i32 %7, 1
%9 = tail call i32 @mpz_init2(i32 undef, i32 %8)
%10 = load i32, i32* %3
%11 = shl nsw i32 %10, 1
%12 = tail call i32 @mpz_init2(i32 undef, i32 %11)
%13 = tail call i32 @mpz_set_ui(i32 undef, i64 210)
%14 = tail call i32 @mpz_powm(i32 undef, i32 undef, i32 undef, %struct.TYPE_6__* %0)
%15 = tail call i64 @mpz_cmp_ui(i32 undef, i64 1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %2
%18 = load i32, i32* %3
%19 = tail call i32 @mpz_init2(i32 undef, i32 %18)
%20 = tail call i64 @mpz_scan1(i32 undef, i64 0)
%21 = tail call i32 @mpz_tdiv_q_2exp(i32 undef, i32 undef, i64 %20)
%22 = load i32, i32* %3
%23 = add nsw i32 %22, 1
%24 = tail call i32 @mpz_init2(i32 undef, i32 %23)
%25 = tail call i32 @mpz_sub_ui(i32 undef, %struct.TYPE_6__* %0, i64 3)
%26 = tail call i32 @gmp_randinit_default(i32 undef)
%27 = icmp sgt i32 %1, 0
br i1 %27, label %28, label %37
28: ; preds = %17, %28
%29 = phi i32 [ %33, %28 ], [ 0, %17 ]
%30 = tail call i32 @mpz_urandomm(i32 undef, i32 undef, i32 undef)
%31 = tail call i32 @mpz_add_ui(i32 undef, i32 undef, i64 2)
%32 = tail call i32 @mpi_millerrabin(%struct.TYPE_6__* %0, i32 undef, i32 undef, i32 undef, i32 undef, i64 %20)
%33 = add nuw nsw i32 %29, 1
%34 = icmp slt i32 %33, %1
%35 = icmp ne i32 %32, 0
%36 = select i1 %34, i1 %35, i1 false
br i1 %36, label %28, label %37
37: ; preds = %28, %17
%38 = phi i32 [ 1, %17 ], [ %32, %28 ]
%39 = tail call i32 @gmp_randclear(i32 undef)
br label %40
40: ; preds = %2, %37
%41 = phi i32 [ %38, %37 ], [ 0, %2 ]
ret i32 %41
}