#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;
typedef int /*<<< orphan*/ gmp_randstate_t;

/* Variables and functions */
scalar_t__ atoi(char *);
int /*<<< orphan*/ gmp_randinit_default(int /*<<< orphan*/);
int /*<<< orphan*/ gmp_randseed_ui(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ miller_rabin(int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mpz_even_p(int /*<<< orphan*/);
scalar_t__ mpz_fdiv_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set_str(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ mpz_init_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_mul_2exp(int /*<<< orphan*/, int /*<<< orphan*/,
                                scalar_t__);
int /*<<< orphan*/ mpz_out_str(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_urandomm(int /*<<< orphan*/, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  mpz_t n, max, two, p;
  gmp_randstate_t rand_state;
  gmp_randinit_default(rand_state);
  gmp_randseed_ui(rand_state, time(NULL));
  if (strcmp(argv[1], "test") == 0) {
    mpz_init_set_str(n, argv[2], 10);
    puts(miller_rabin(n, rand_state) == 1 ? "PRIME" : "COMPOSITE");
  } else if (strcmp(argv[1], "genprime") == 0) {
    mpz_init(max);
    mpz_init_set_ui(two, 2);
    mpz_mul_2exp(max, two, atoi(argv[2]) + 1);
    mpz_init(p);
    do {
      mpz_urandomm(p, rand_state, max);
      if (mpz_even_p(p))
        continue;
      if (mpz_fdiv_ui(p, 3) == 0)
        continue;
      if (mpz_fdiv_ui(p, 5) == 0)
        continue;
      if (mpz_fdiv_ui(p, 7) == 0)
        continue;
    } while (miller_rabin(p, rand_state) == 0);
    mpz_out_str(stdout, 10, p);
    puts("");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x24(%rbp),%edi
call   10f0 <gmp_randinit_default@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
call   10d0 <gmp_randseed_ui@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdca(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1288 <main+0x98>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    $0xa,%edx
call   1040 <mpz_init_set_str@plt>
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1100 <miller_rabin@plt>
mov    %rax,%rcx
lea    0xd9c(%rip),%rdi
lea    0xd8f(%rip),%rax
cmp    $0x1,%rcx
cmove  %rax,%rdi
call   1030 <puts@plt>
jmp    13a8 <main+0x1b8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd7e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    13a3 <main+0x1b3>
mov    -0x18(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   10b0 <mpz_init_set_ui@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    %rax,%rdx
add    $0x1,%rdx
call   1070 <mpz_mul_2exp@plt>
mov    -0x20(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1110 <mpz_urandomm@plt>
mov    -0x20(%rbp),%edi
call   10a0 <mpz_even_p@plt>
cmp    $0x0,%rax
je     1313 <main+0x123>
jmp    136c <main+0x17c>
mov    -0x20(%rbp),%edi
mov    $0x3,%esi
call   10c0 <mpz_fdiv_ui@plt>
cmp    $0x0,%rax
jne    132f <main+0x13f>
jmp    136c <main+0x17c>
mov    -0x20(%rbp),%edi
mov    $0x5,%esi
call   10c0 <mpz_fdiv_ui@plt>
cmp    $0x0,%rax
jne    134b <main+0x15b>
jmp    136c <main+0x17c>
mov    -0x20(%rbp),%edi
mov    $0x7,%esi
call   10c0 <mpz_fdiv_ui@plt>
cmp    $0x0,%rax
jne    1367 <main+0x177>
jmp    136c <main+0x17c>
jmp    136c <main+0x17c>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1100 <miller_rabin@plt>
cmp    $0x0,%rax
je     12ee <main+0xfe>
mov    0x2c58(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
mov    $0xa,%esi
call   1090 <mpz_out_str@plt>
lea    0xc6c(%rip),%rdi
call   1030 <puts@plt>
jmp    13a8 <main+0x1b8>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
call   1100 <gmp_randinit_default@plt>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%esi
call   10e0 <gmp_randseed_ui@plt>
mov    0x8(%rbx),%rdi
lea    0xdde(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     12c3 <main+0xc3>
mov    0x8(%rbx),%rdi
lea    0xdda(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1310 <main+0x110>
call   1060 <mpz_init@plt>
mov    $0x2,%esi
call   10c0 <mpz_init_set_ui@plt>
mov    0x10(%rbx),%rdi
call   10f0 <atoi@plt>
lea    0x1(%rax),%rdx
call   1080 <mpz_mul_2exp@plt>
call   1060 <mpz_init@plt>
jmp    128a <main+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
call   1110 <miller_rabin@plt>
test   %rax,%rax
jne    12f3 <main+0xf3>
call   1120 <mpz_urandomm@plt>
call   10b0 <mpz_even_p@plt>
test   %rax,%rax
jne    1280 <main+0x80>
mov    $0x3,%esi
call   10d0 <mpz_fdiv_ui@plt>
test   %rax,%rax
je     1280 <main+0x80>
mov    $0x5,%esi
call   10d0 <mpz_fdiv_ui@plt>
test   %rax,%rax
je     1280 <main+0x80>
mov    $0x7,%esi
call   10d0 <mpz_fdiv_ui@plt>
jmp    1280 <main+0x80>
mov    0x10(%rbx),%rsi
mov    $0xa,%edx
call   1050 <mpz_init_set_str@plt>
call   1110 <miller_rabin@plt>
cmp    $0x1,%rax
lea    0xd24(%rip),%rax
lea    0xd23(%rip),%rdi
cmove  %rax,%rdi
call   1040 <puts@plt>
jmp    1310 <main+0x110>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
mov    $0xa,%esi
call   10a0 <mpz_out_str@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"test\00"
@.str.1 = [6 x i8] c"PRIME\00"
@.str.2 = [10 x i8] c"COMPOSITE\00"
@.str.3 = [9 x i8] c"genprime\00"
@stdout = global i32 0
@.str.4 = [1 x i8] zeroinitializer
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %10
%12 = call i32 @gmp_randinit_default(i32 %11)
%13 = load i32, i32* %10
%14 = call i32 @time(i32* null)
%15 = call i32 @gmp_randseed_ui(i32 %13, i32 %14)
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %2
%22 = load i32, i32* %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @mpz_init_set_str(i32 %22, i8* %25, i32 10)
%27 = load i32, i32* %6
%28 = load i32, i32* %10
%29 = call i64 @miller_rabin(i32 %27, i32 %28)
%30 = icmp eq i64 %29, 1
%31 = zext i1 %30 to i64
%32 = select i1 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0)
%33 = call i32 @puts(i8* %32)
br label %91
34: ; preds = %2
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %90
40: ; preds = %34
%41 = load i32, i32* %7
%42 = call i32 @mpz_init(i32 %41)
%43 = load i32, i32* %8
%44 = call i32 @mpz_init_set_ui(i32 %43, i32 2)
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
%50 = call i64 @atoi(i8* %49)
%51 = add nsw i64 %50, 1
%52 = call i32 @mpz_mul_2exp(i32 %45, i32 %46, i64 %51)
%53 = load i32, i32* %9
%54 = call i32 @mpz_init(i32 %53)
br label %55
55: ; preds = %80, %40
%56 = load i32, i32* %9
%57 = load i32, i32* %10
%58 = load i32, i32* %7
%59 = call i32 @mpz_urandomm(i32 %56, i32 %57, i32 %58)
%60 = load i32, i32* %9
%61 = call i64 @mpz_even_p(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %55
br label %80
64: ; preds = %55
%65 = load i32, i32* %9
%66 = call i64 @mpz_fdiv_ui(i32 %65, i32 3)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %64
br label %80
69: ; preds = %64
%70 = load i32, i32* %9
%71 = call i64 @mpz_fdiv_ui(i32 %70, i32 5)
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %69
br label %80
74: ; preds = %69
%75 = load i32, i32* %9
%76 = call i64 @mpz_fdiv_ui(i32 %75, i32 7)
%77 = icmp eq i64 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %74
br label %80
79: ; preds = %74
br label %80
80: ; preds = %79, %78, %73, %68, %63
%81 = load i32, i32* %9
%82 = load i32, i32* %10
%83 = call i64 @miller_rabin(i32 %81, i32 %82)
%84 = icmp eq i64 %83, 0
br i1 %84, label %55, label %85
85: ; preds = %80
%86 = load i32, i32* @stdout
%87 = load i32, i32* %9
%88 = call i32 @mpz_out_str(i32 %86, i32 10, i32 %87)
%89 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
br label %90
90: ; preds = %85, %34
br label %91
91: ; preds = %90, %21
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"test\00"
@.str.1 = [6 x i8] c"PRIME\00"
@.str.2 = [10 x i8] c"COMPOSITE\00"
@.str.3 = [9 x i8] c"genprime\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @gmp_randinit_default(i32 undef)
%4 = tail call i32 @time(i32* null)
%5 = tail call i32 @gmp_randseed_ui(i32 undef, i32 %4)
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @mpz_init_set_str(i32 undef, i8* %12, i32 10)
%14 = tail call i64 @miller_rabin(i32 undef, i32 undef)
%15 = icmp eq i64 %14, 1
%16 = select i1 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0)
%17 = tail call i32 @puts(i8* dereferenceable(1) %16)
br label %50
18: ; preds = %2
%19 = load i8*, i8** %6
%20 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %50
22: ; preds = %18
%23 = tail call i32 @mpz_init(i32 undef)
%24 = tail call i32 @mpz_init_set_ui(i32 undef, i32 2)
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i64 @atoi(i8* %26)
%28 = add nsw i64 %27, 1
%29 = tail call i32 @mpz_mul_2exp(i32 undef, i32 undef, i64 %28)
%30 = tail call i32 @mpz_init(i32 undef)
br label %31
31: ; preds = %43, %22
%32 = tail call i32 @mpz_urandomm(i32 undef, i32 undef, i32 undef)
%33 = tail call i64 @mpz_even_p(i32 undef)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %31
%36 = tail call i64 @mpz_fdiv_ui(i32 undef, i32 3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %35
%39 = tail call i64 @mpz_fdiv_ui(i32 undef, i32 5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = tail call i64 @mpz_fdiv_ui(i32 undef, i32 7)
br label %43
43: ; preds = %41, %38, %35, %31
%44 = tail call i64 @miller_rabin(i32 undef, i32 undef)
%45 = icmp eq i64 %44, 0
br i1 %45, label %31, label %46
46: ; preds = %43
%47 = load i32, i32* @stdout
%48 = tail call i32 @mpz_out_str(i32 %47, i32 10, i32 undef)
%49 = tail call i32 @putchar(i32 10)
br label %50
50: ; preds = %18, %46, %10
ret i32 0
}