#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 */
scalar_t__ mpz_cmp_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_gcd_ui(int /*<<< orphan*/, int /*<<< orphan*/,
                              unsigned long);
int /*<<< orphan*/ mpz_init(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_powm(int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_set_ui(int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ mpz_sub_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int fermat(mpz_t n) {
  unsigned long int i, a;
  mpz_t g, fer, base, exp1;
  i = 0;
  a = 2;
  mpz_init(g);
  mpz_init(fer);
  mpz_init(base);
  mpz_init(exp1);
  while (i < 10) {
    mpz_gcd_ui(g, n, a);
    if (mpz_cmp_ui(g, 1) == 0) {
      i++;
      mpz_set_ui(base, a);
      mpz_sub_ui(exp1, n, 1);
      mpz_powm(fer, base, exp1, n);
      if (mpz_cmp_ui(fer, 1) != 0) {
        return 0;
      }
    }
    a++;
  }
  if (mpz_cmp_ui(fer, 1) == 0) {
    return 1;
  }
}


x86 assembly (clang, -O0):
<fermat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x2,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x20(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x24(%rbp),%edi
call   1050 <mpz_init@plt>
mov    -0x28(%rbp),%edi
call   1050 <mpz_init@plt>
cmpq   $0xa,-0x10(%rbp)
jae    123e <fermat+0xde>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1030 <mpz_gcd_ui@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jne    122d <fermat+0xcd>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1080 <mpz_set_ui@plt>
mov    -0x28(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x1,%edx
call   1060 <mpz_sub_ui@plt>
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x8(%rbp),%ecx
call   1040 <mpz_powm@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
je     1228 <fermat+0xc8>
movl   $0x0,-0x4(%rbp)
jmp    125c <fermat+0xfc>
jmp    122d <fermat+0xcd>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    119b <fermat+0x3b>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
cmp    $0x0,%rax
jne    125c <fermat+0xfc>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fermat>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
call   1050 <mpz_init@plt>
mov    $0x2,%ebx
xor    %ebp,%ebp
jmp    119a <fermat+0x3a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
cmp    $0xa,%rbp
jae    11e6 <fermat+0x86>
mov    %r14d,%esi
mov    %rbx,%rdx
call   1030 <mpz_gcd_ui@plt>
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
test   %rax,%rax
jne    1190 <fermat+0x30>
mov    %rbx,%rsi
call   1080 <mpz_set_ui@plt>
mov    %r14d,%esi
mov    $0x1,%edx
call   1060 <mpz_sub_ui@plt>
mov    %r14d,%ecx
call   1040 <mpz_powm@plt>
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
test   %rax,%rax
jne    11f7 <fermat+0x97>
add    $0x1,%rbp
jmp    1190 <fermat+0x30>
mov    $0x1,%ebx
mov    $0x1,%esi
call   1070 <mpz_cmp_ui@plt>
jmp    11f9 <fermat+0x99>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fermat(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i64 0, i64* %4
store i64 2, i64* %5
%10 = load i32, i32* %6
%11 = call i32 @mpz_init(i32 %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)
br label %18
18: ; preds = %48, %1
%19 = load i64, i64* %4
%20 = icmp ult i64 %19, 10
br i1 %20, label %21, label %51
21: ; preds = %18
%22 = load i32, i32* %6
%23 = load i32, i32* %3
%24 = load i64, i64* %5
%25 = call i32 @mpz_gcd_ui(i32 %22, i32 %23, i64 %24)
%26 = load i32, i32* %6
%27 = call i64 @mpz_cmp_ui(i32 %26, i32 1)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %21
%30 = load i64, i64* %4
%31 = add i64 %30, 1
store i64 %31, i64* %4
%32 = load i32, i32* %8
%33 = load i64, i64* %5
%34 = call i32 @mpz_set_ui(i32 %32, i64 %33)
%35 = load i32, i32* %9
%36 = load i32, i32* %3
%37 = call i32 @mpz_sub_ui(i32 %35, i32 %36, i32 1)
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = load i32, i32* %9
%41 = load i32, i32* %3
%42 = call i32 @mpz_powm(i32 %38, i32 %39, i32 %40, i32 %41)
%43 = load i32, i32* %7
%44 = call i64 @mpz_cmp_ui(i32 %43, i32 1)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %29
store i32 0, i32* %2
br label %56
47: ; preds = %29
br label %48
48: ; preds = %47, %21
%49 = load i64, i64* %5
%50 = add i64 %49, 1
store i64 %50, i64* %5
br label %18
51: ; preds = %18
%52 = load i32, i32* %7
%53 = call i64 @mpz_cmp_ui(i32 %52, i32 1)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %51
store i32 1, i32* %2
br label %56
56: ; preds = %46, %55, %51
%57 = load i32, i32* %2
ret i32 %57
}

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