#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_t;

/* Variables and functions */
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, int);
scalar_t__ mpz_even_p(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_fdiv_q_2exp(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm_ui(int /*<<< orphan*/, int /*<<< orphan*/, int,
                               int /*<<< orphan*/);
int /*<<< orphan*/ mpz_sub_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int miller_rabin_pass(mpz_t a, mpz_t n) {
  int i, s, result;
  mpz_t a_to_power, d, n_minus_one;
  mpz_init(n_minus_one);
  mpz_sub_ui(n_minus_one, n, 1);
  s = 0;
  mpz_init_set(d, n_minus_one);
  while (mpz_even_p(d)) {
    mpz_fdiv_q_2exp(d, d, 1);
    s++;
  }
  mpz_init(a_to_power);
  mpz_powm(a_to_power, a, d, n);
  if (mpz_cmp_ui(a_to_power, 1) == 0) {
    result = 1;
    goto exit;
  }
  for (i = 0; i < s - 1; i++) {
    if (mpz_cmp(a_to_power, n_minus_one) == 0) {
      result = 1;
      goto exit;
    }
    mpz_powm_ui(a_to_power, a_to_power, 2, n);
  }
  if (mpz_cmp(a_to_power, n_minus_one) == 0) {
    result = 1;
    goto exit;
  }
  result = 0;
exit:
  mpz_clear(a_to_power);
  mpz_clear(d);
  mpz_clear(n_minus_one);
  return result;
}


x86 assembly (clang, -O0):
<miller_rabin_pass>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x1,%edx
call   1060 <mpz_sub_ui@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   10c0 <mpz_init_set@plt>
mov    -0x1c(%rbp),%edi
call   1080 <mpz_even_p@plt>
cmp    $0x0,%rax
je     1208 <miller_rabin_pass+0x68>
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1050 <mpz_fdiv_q_2exp@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11d8 <miller_rabin_pass+0x38>
mov    -0x18(%rbp),%edi
call   1040 <mpz_init@plt>
mov    -0x18(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%ecx
call   1030 <mpz_powm@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jne    1244 <miller_rabin_pass+0xa4>
movl   $0x1,-0x14(%rbp)
jmp    12c6 <miller_rabin_pass+0x126>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    129e <miller_rabin_pass+0xfe>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
call   10a0 <mpz_cmp@plt>
cmp    $0x0,%rax
jne    127d <miller_rabin_pass+0xdd>
movl   $0x1,-0x14(%rbp)
jmp    12c6 <miller_rabin_pass+0x126>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x8(%rbp),%ecx
mov    $0x2,%edx
call   1090 <mpz_powm_ui@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    124b <miller_rabin_pass+0xab>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%esi
call   10a0 <mpz_cmp@plt>
cmp    $0x0,%rax
jne    12bf <miller_rabin_pass+0x11f>
movl   $0x1,-0x14(%rbp)
jmp    12c6 <miller_rabin_pass+0x126>
movl   $0x0,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   10b0 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <mpz_clear@plt>
mov    -0x20(%rbp),%edi
call   10b0 <mpz_clear@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<miller_rabin_pass>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%r15d
call   1040 <mpz_init@plt>
mov    $0x1,%r14d
mov    %ebp,%esi
mov    $0x1,%edx
call   1060 <mpz_sub_ui@plt>
call   10c0 <mpz_init_set@plt>
call   1080 <mpz_even_p@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11f7 <miller_rabin_pass+0x57>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x1,%edx
call   1050 <mpz_fdiv_q_2exp@plt>
add    $0x1,%ebx
call   1080 <mpz_even_p@plt>
test   %rax,%rax
jne    11e0 <miller_rabin_pass+0x40>
call   1040 <mpz_init@plt>
mov    %r15d,%esi
mov    %ebp,%ecx
call   1030 <mpz_powm@plt>
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
test   %rax,%rax
je     124a <miller_rabin_pass+0xaa>
cmp    $0x2,%ebx
jl     123b <miller_rabin_pass+0x9b>
add    $0xffffffff,%ebx
nopl   (%rax)
call   10a0 <mpz_cmp@plt>
test   %rax,%rax
je     124a <miller_rabin_pass+0xaa>
mov    $0x2,%edx
mov    %ebp,%ecx
call   1090 <mpz_powm_ui@plt>
add    $0xffffffff,%ebx
jne    1220 <miller_rabin_pass+0x80>
call   10a0 <mpz_cmp@plt>
xor    %r14d,%r14d
test   %rax,%rax
sete   %r14b
call   10b0 <mpz_clear@plt>
call   10b0 <mpz_clear@plt>
call   10b0 <mpz_clear@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @miller_rabin_pass(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = load i32, i32* %10
%12 = call i32 @mpz_init(i32 %11)
%13 = load i32, i32* %10
%14 = load i32, i32* %4
%15 = call i32 @mpz_sub_ui(i32 %13, i32 %14, i32 1)
store i32 0, i32* %6
%16 = load i32, i32* %9
%17 = load i32, i32* %10
%18 = call i32 @mpz_init_set(i32 %16, i32 %17)
br label %19
19: ; preds = %23, %2
%20 = load i32, i32* %9
%21 = call i64 @mpz_even_p(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* %9
%25 = load i32, i32* %9
%26 = call i32 @mpz_fdiv_q_2exp(i32 %24, i32 %25, i32 1)
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %19
29: ; preds = %19
%30 = load i32, i32* %8
%31 = call i32 @mpz_init(i32 %30)
%32 = load i32, i32* %8
%33 = load i32, i32* %3
%34 = load i32, i32* %9
%35 = load i32, i32* %4
%36 = call i32 @mpz_powm(i32 %32, i32 %33, i32 %34, i32 %35)
%37 = load i32, i32* %8
%38 = call i64 @mpz_cmp_ui(i32 %37, i32 1)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %29
store i32 1, i32* %7
br label %68
41: ; preds = %29
store i32 0, i32* %5
br label %42
42: ; preds = %58, %41
%43 = load i32, i32* %5
%44 = load i32, i32* %6
%45 = sub nsw i32 %44, 1
%46 = icmp slt i32 %43, %45
br i1 %46, label %47, label %61
47: ; preds = %42
%48 = load i32, i32* %8
%49 = load i32, i32* %10
%50 = call i64 @mpz_cmp(i32 %48, i32 %49)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 1, i32* %7
br label %68
53: ; preds = %47
%54 = load i32, i32* %8
%55 = load i32, i32* %8
%56 = load i32, i32* %4
%57 = call i32 @mpz_powm_ui(i32 %54, i32 %55, i32 2, i32 %56)
br label %58
58: ; preds = %53
%59 = load i32, i32* %5
%60 = add nsw i32 %59, 1
store i32 %60, i32* %5
br label %42
61: ; preds = %42
%62 = load i32, i32* %8
%63 = load i32, i32* %10
%64 = call i64 @mpz_cmp(i32 %62, i32 %63)
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %61
store i32 1, i32* %7
br label %68
67: ; preds = %61
store i32 0, i32* %7
br label %68
68: ; preds = %67, %66, %52, %40
%69 = load i32, i32* %8
%70 = call i32 @mpz_clear(i32 %69)
%71 = load i32, i32* %9
%72 = call i32 @mpz_clear(i32 %71)
%73 = load i32, i32* %10
%74 = call i32 @mpz_clear(i32 %73)
%75 = load i32, i32* %7
ret i32 %75
}

LLVM IR (-O2):
define i32 @miller_rabin_pass(i32 %0, i32 %1) {
%3 = tail call i32 @mpz_init(i32 undef)
%4 = tail call i32 @mpz_sub_ui(i32 undef, i32 %1, i32 1)
%5 = tail call i32 @mpz_init_set(i32 undef, i32 undef)
%6 = tail call i64 @mpz_even_p(i32 undef)
%7 = icmp eq i64 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %2, %8
%9 = phi i32 [ %11, %8 ], [ 0, %2 ]
%10 = tail call i32 @mpz_fdiv_q_2exp(i32 undef, i32 undef, i32 1)
%11 = add nuw nsw i32 %9, 1
%12 = tail call i64 @mpz_even_p(i32 undef)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %8
14: ; preds = %8, %2
%15 = phi i32 [ 0, %2 ], [ %11, %8 ]
%16 = tail call i32 @mpz_init(i32 undef)
%17 = tail call i32 @mpz_powm(i32 undef, i32 %0, i32 undef, i32 %1)
%18 = tail call i64 @mpz_cmp_ui(i32 undef, i32 1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %14
%21 = add i32 %15, -1
%22 = icmp sgt i32 %15, 1
br i1 %22, label %23, label %31
23: ; preds = %20, %27
%24 = phi i32 [ %29, %27 ], [ 0, %20 ]
%25 = tail call i64 @mpz_cmp(i32 undef, i32 undef)
%26 = icmp eq i64 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %23
%28 = tail call i32 @mpz_powm_ui(i32 undef, i32 undef, i32 2, i32 %1)
%29 = add nuw nsw i32 %24, 1
%30 = icmp eq i32 %29, %21
br i1 %30, label %31, label %23
31: ; preds = %27, %20
%32 = tail call i64 @mpz_cmp(i32 undef, i32 undef)
%33 = icmp eq i64 %32, 0
%34 = zext i1 %33 to i32
br label %35
35: ; preds = %23, %31, %14
%36 = phi i32 [ 1, %14 ], [ %34, %31 ], [ 1, %23 ]
%37 = tail call i32 @mpz_clear(i32 undef)
%38 = tail call i32 @mpz_clear(i32 undef)
%39 = tail call i32 @mpz_clear(i32 undef)
ret i32 %36
}