#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 */

/* Type definitions */
typedef int /*<<< orphan*/ mpz_srcptr;
typedef int /*<<< orphan*/ mpz_ptr;

/* Variables and functions */
scalar_t__ mpz_cmp(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, long);
int /*<<< orphan*/ mpz_powm(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm_ui(int /*<<< orphan*/, int /*<<< orphan*/, long,
                               int /*<<< orphan*/);
int mpi_millerrabin(mpz_srcptr n, mpz_srcptr nm1, mpz_ptr x, mpz_ptr y,
                    mpz_srcptr q, unsigned long int k) {
  unsigned long int i;
  mpz_powm(y, x, q, n);
  if (mpz_cmp_ui(y, 1L) == 0 || mpz_cmp(y, nm1) == 0)
    return 1;
  for (i = 1; i < k; i++) {
    mpz_powm_ui(y, y, 2L, n);
    if (mpz_cmp(y, nm1) == 0)
      return 1;
    if (mpz_cmp_ui(y, 1L) == 0)
      return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mpi_millerrabin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x8(%rbp),%ecx
call   1030 <mpz_powm@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
je     1199 <mpi_millerrabin+0x59>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <mpz_cmp@plt>
cmp    $0x0,%rax
jne    11a5 <mpi_millerrabin+0x65>
movl   $0x1,-0x4(%rbp)
jmp    122f <mpi_millerrabin+0xef>
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1228 <mpi_millerrabin+0xe8>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%ecx
mov    $0x2,%edx
call   1050 <mpz_powm_ui@plt>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <mpz_cmp@plt>
cmp    $0x0,%rax
jne    11ef <mpi_millerrabin+0xaf>
movl   $0x1,-0x4(%rbp)
jmp    122f <mpi_millerrabin+0xef>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jne    1212 <mpi_millerrabin+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    122f <mpi_millerrabin+0xef>
jmp    1217 <mpi_millerrabin+0xd7>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ad <mpi_millerrabin+0x6d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpi_millerrabin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r14
mov    %ecx,%ebp
mov    %esi,%r12d
mov    %edi,%r13d
mov    %ecx,%edi
mov    %edx,%esi
mov    %r8d,%edx
mov    %r13d,%ecx
call   1030 <mpz_powm@plt>
mov    $0x1,%esi
mov    %ebp,%edi
call   1040 <mpz_cmp_ui@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     11e0 <mpi_millerrabin+0xa0>
mov    %ebp,%edi
mov    %r12d,%esi
call   1060 <mpz_cmp@plt>
test   %rax,%rax
je     11e0 <mpi_millerrabin+0xa0>
cmp    $0x2,%r14
jb     11dd <mpi_millerrabin+0x9d>
mov    $0x2,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    $0x2,%edx
mov    %ebp,%edi
mov    %ebp,%esi
mov    %r13d,%ecx
call   1050 <mpz_powm_ui@plt>
mov    %ebp,%edi
mov    %r12d,%esi
call   1060 <mpz_cmp@plt>
test   %rax,%rax
je     11e0 <mpi_millerrabin+0xa0>
mov    $0x1,%esi
mov    %ebp,%edi
call   1040 <mpz_cmp_ui@plt>
test   %rax,%rax
je     11dd <mpi_millerrabin+0x9d>
lea    0x1(%rbx),%rax
cmp    %r14,%rbx
mov    %rax,%rbx
jb     11a0 <mpi_millerrabin+0x60>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mpi_millerrabin(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i64
%14 = alloca i64
store i32 %0, i32* %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i64 %5, i64* %13
%15 = load i32, i32* %11
%16 = load i32, i32* %10
%17 = load i32, i32* %12
%18 = load i32, i32* %8
%19 = call i32 @mpz_powm(i32 %15, i32 %16, i32 %17, i32 %18)
%20 = load i32, i32* %11
%21 = call i64 @mpz_cmp_ui(i32 %20, i64 1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %6
%24 = load i32, i32* %11
%25 = load i32, i32* %9
%26 = call i64 @mpz_cmp(i32 %24, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23, %6
store i32 1, i32* %7
br label %54
29: ; preds = %23
store i64 1, i64* %14
br label %30
30: ; preds = %50, %29
%31 = load i64, i64* %14
%32 = load i64, i64* %13
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %53
34: ; preds = %30
%35 = load i32, i32* %11
%36 = load i32, i32* %11
%37 = load i32, i32* %8
%38 = call i32 @mpz_powm_ui(i32 %35, i32 %36, i64 2, i32 %37)
%39 = load i32, i32* %11
%40 = load i32, i32* %9
%41 = call i64 @mpz_cmp(i32 %39, i32 %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %34
store i32 1, i32* %7
br label %54
44: ; preds = %34
%45 = load i32, i32* %11
%46 = call i64 @mpz_cmp_ui(i32 %45, i64 1)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 0, i32* %7
br label %54
49: ; preds = %44
br label %50
50: ; preds = %49
%51 = load i64, i64* %14
%52 = add i64 %51, 1
store i64 %52, i64* %14
br label %30
53: ; preds = %30
store i32 0, i32* %7
br label %54
54: ; preds = %53, %48, %43, %28
%55 = load i32, i32* %7
ret i32 %55
}

LLVM IR (-O2):
define i32 @mpi_millerrabin(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4, i64 %5) {
%7 = tail call i32 @mpz_powm(i32 %3, i32 %2, i32 %4, i32 %0)
%8 = tail call i64 @mpz_cmp_ui(i32 %3, i64 1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %6
%11 = tail call i64 @mpz_cmp(i32 %3, i32 %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %10
%14 = icmp ugt i64 %5, 1
br i1 %14, label %15, label %26
15: ; preds = %13, %20
%16 = phi i64 [ %23, %20 ], [ 1, %13 ]
%17 = tail call i32 @mpz_powm_ui(i32 %3, i32 %3, i64 2, i32 %0)
%18 = tail call i64 @mpz_cmp(i32 %3, i32 %1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %15
%21 = tail call i64 @mpz_cmp_ui(i32 %3, i64 1)
%22 = icmp ne i64 %21, 0
%23 = add nuw i64 %16, 1
%24 = icmp ult i64 %23, %5
%25 = select i1 %22, i1 %24, i1 false
br i1 %25, label %15, label %26
26: ; preds = %15, %20, %13, %6, %10
%27 = phi i32 [ 1, %10 ], [ 1, %6 ], [ 0, %13 ], [ 1, %15 ], [ 0, %20 ]
ret i32 %27
}