#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 */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ gmp_randinit_default(int /*<<< orphan*/);
int /*<<< orphan*/ gmp_randseed_ui(int /*<<< orphan*/, long);
char *malloc(int);
int /*<<< orphan*/ mpz_clears(int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/, ...);
scalar_t__ mpz_divisible_p(int /*<<< orphan*/, int /*<<< orphan*/);
int mpz_get_si(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm_sec(int /*<<< orphan*/, int /*<<< orphan*/,
                                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_sub_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_urandomm(int /*<<< orphan*/, int /*<<< orphan*/,
                                int /*<<< orphan*/);
scalar_t__ time(int /*<<< orphan*/ *);
int fermat(mpz_t n) {
  int i;
  long seed;
  gmp_randstate_t init;
  mpz_t a;
  mpz_t e;
  mpz_t res;
  char *to_print;
  to_print = malloc(1024 * sizeof(char));
  mpz_init(a);
  mpz_init(e);
  mpz_init(res);
  gmp_randinit_default(init);
  mpz_sub_ui(e, n, 1);
  seed = (long)time(NULL);
  for (i = 0; i < 10; i++) {
    seed = seed++;
    gmp_randseed_ui(init, seed);
    mpz_urandomm(a, init, e);
    if (mpz_divisible_p(n, a)) {
      free(to_print);
      mpz_clears(a, e, NULL);
      return 1;
    }
    mpz_powm_sec(res, a, e, n);
    if (mpz_get_si(res) != 1) {
      mpz_clears(a, e, res, NULL);
      free(to_print);
      return 1;
    }
  }
  mpz_clears(a, e, res, NULL);
  free(to_print);
  return 0;
}


x86 assembly (clang, -O0):
<fermat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x400,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x24(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1070 <mpz_init@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <gmp_randinit_default@plt>
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x1,%edx
call   1090 <mpz_sub_ui@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    12eb <fermat+0x12b>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   10b0 <gmp_randseed_ui@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%edx
call   10e0 <mpz_urandomm@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <mpz_divisible_p@plt>
cmp    $0x0,%rax
je     128d <fermat+0xcd>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
xor    %edx,%edx
mov    $0x0,%al
call   1050 <mpz_clears@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <fermat+0x14f>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x8(%rbp),%ecx
call   10d0 <mpz_powm_sec@plt>
mov    -0x28(%rbp),%edi
call   1060 <mpz_get_si@plt>
cmp    $0x1,%eax
je     12d8 <fermat+0x118>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1050 <mpz_clears@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <fermat+0x14f>
jmp    12dd <fermat+0x11d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    121d <fermat+0x5d>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x0,%al
call   1050 <mpz_clears@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fermat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x400,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   1070 <mpz_init@plt>
call   10c0 <gmp_randinit_default@plt>
mov    %ebp,%esi
mov    $0x1,%edx
call   1090 <mpz_sub_ui@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %rax,%r15
mov    $0xa,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rsi
call   10b0 <gmp_randseed_ui@plt>
call   10e0 <mpz_urandomm@plt>
mov    %ebp,%edi
call   1040 <mpz_divisible_p@plt>
test   %rax,%rax
jne    1254 <fermat+0x94>
mov    %ebp,%ecx
call   10d0 <mpz_powm_sec@plt>
call   1060 <mpz_get_si@plt>
cmp    $0x1,%eax
jne    1267 <fermat+0xa7>
add    $0xffffffff,%ebx
jne    1210 <fermat+0x50>
xor    %ebx,%ebx
xor    %ecx,%ecx
xor    %eax,%eax
call   1050 <mpz_clears@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    127d <fermat+0xbd>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %edx,%edx
xor    %eax,%eax
call   1050 <mpz_clears@plt>
jmp    1278 <fermat+0xb8>
xor    %ecx,%ecx
xor    %eax,%eax
call   1050 <mpz_clears@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @fermat(i32 %0) {
%2 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%3 = tail call i32 @mpz_init(i32 undef)
%4 = tail call i32 @mpz_init(i32 undef)
%5 = tail call i32 @mpz_init(i32 undef)
%6 = tail call i32 @gmp_randinit_default(i32 undef)
%7 = tail call i32 @mpz_sub_ui(i32 undef, i32 %0, i32 1)
%8 = tail call i64 @time(i32* null)
br label %12
9: ; preds = %21
%10 = add nuw nsw i32 %13, 1
%11 = icmp eq i32 %10, 10
br i1 %11, label %28, label %12
12: ; preds = %1, %9
%13 = phi i32 [ 0, %1 ], [ %10, %9 ]
%14 = tail call i32 @gmp_randseed_ui(i32 undef, i64 %8)
%15 = tail call i32 @mpz_urandomm(i32 undef, i32 undef, i32 undef)
%16 = tail call i64 @mpz_divisible_p(i32 %0, i32 undef)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = tail call i32 @free(i8* %2)
%20 = tail call i32 (i32, i32, i32, ...) @mpz_clears(i32 undef, i32 undef, i32 0)
br label %31
21: ; preds = %12
%22 = tail call i32 @mpz_powm_sec(i32 undef, i32 undef, i32 undef, i32 %0)
%23 = tail call i32 @mpz_get_si(i32 undef)
%24 = icmp eq i32 %23, 1
br i1 %24, label %9, label %25
25: ; preds = %21
%26 = tail call i32 (i32, i32, i32, ...) @mpz_clears(i32 undef, i32 undef, i32 undef, i8* null)
%27 = tail call i32 @free(i8* %2)
br label %31
28: ; preds = %9
%29 = tail call i32 (i32, i32, i32, ...) @mpz_clears(i32 undef, i32 undef, i32 undef, i8* null)
%30 = tail call i32 @free(i8* %2)
br label %31
31: ; preds = %28, %25, %18
%32 = phi i32 [ 1, %18 ], [ 1, %25 ], [ 0, %28 ]
ret i32 %32
}