#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__ isprime(int /*<<< orphan*/);
int /*<<< orphan*/ mpz_add_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_clear(int /*<<< orphan*/);
scalar_t__ mpz_cmp(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ mpz_init_set_ui(int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_mul_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ mpz_pow_ui(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int ac, char *av[]) {
  int n;
  mpz_t f, p, i;
  if (ac < 2) {
    printf("usage : fermat n\n");
    return (1);
  }
  n = strtol(av[1], NULL, 10);
  mpz_init_set_ui(p, 2);
  mpz_pow_ui(p, p, n);
  mpz_init_set_ui(i, 1);
  mpz_init_set_ui(f, 2);
  while (mpz_cmp(i, p) < 0) {
    mpz_mul_ui(f, f, 2);
    mpz_add_ui(i, i, 1);
  }
  mpz_add_ui(f, f, 1);
  if (isprime(f))
    printf("OK\n");
  else
    printf("NG\n");
  mpz_clear(f);
  mpz_clear(p);
  mpz_clear(i);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c8 <main+0x38>
lea    0xe49(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ae <main+0x11e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1070 <mpz_init_set_ui@plt>
mov    -0x1c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1080 <mpz_pow_ui@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1070 <mpz_init_set_ui@plt>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1070 <mpz_init_set_ui@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1090 <mpz_cmp@plt>
cmp    $0x0,%rax
jge    1250 <main+0xc0>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x2,%edx
call   10b0 <mpz_mul_ui@plt>
mov    -0x20(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    $0x1,%edx
call   1030 <mpz_add_ui@plt>
jmp    1216 <main+0x86>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    $0x1,%edx
call   1030 <mpz_add_ui@plt>
mov    -0x18(%rbp),%edi
call   1050 <isprime@plt>
cmp    $0x0,%rax
je     1283 <main+0xf3>
lea    0xd99(%rip),%rdi
call   1040 <printf@plt>
jmp    128f <main+0xff>
lea    0xd8c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <mpz_clear@plt>
mov    -0x20(%rbp),%edi
call   10a0 <mpz_clear@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11ae <main+0x1e>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1248 <main+0xb8>
mov    0x8(%rsi),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,%ebp
mov    $0x2,%esi
call   1070 <mpz_init_set_ui@plt>
mov    %ebp,%edx
call   1080 <mpz_pow_ui@plt>
mov    $0x1,%esi
call   1070 <mpz_init_set_ui@plt>
mov    $0x2,%esi
call   1070 <mpz_init_set_ui@plt>
nopw   0x0(%rax,%rax,1)
call   1090 <mpz_cmp@plt>
test   %rax,%rax
jns    1210 <main+0x80>
mov    $0x2,%edx
call   10b0 <mpz_mul_ui@plt>
mov    $0x1,%edx
call   1040 <mpz_add_ui@plt>
jmp    11f0 <main+0x60>
mov    $0x1,%edx
call   1040 <mpz_add_ui@plt>
call   1050 <isprime@plt>
test   %rax,%rax
lea    0xdd7(%rip),%rax
lea    0xdd3(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
call   10a0 <mpz_clear@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"usage : fermat n\0A\00"
@.str.1 = [4 x i8] c"OK\0A\00"
@.str.2 = [4 x i8] c"NG\0A\00"
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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %59
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strtol(i8* %17, i32* null, i32 10)
store i32 %18, i32* %6
%19 = load i32, i32* %8
%20 = call i32 @mpz_init_set_ui(i32 %19, i32 2)
%21 = load i32, i32* %8
%22 = load i32, i32* %8
%23 = load i32, i32* %6
%24 = call i32 @mpz_pow_ui(i32 %21, i32 %22, i32 %23)
%25 = load i32, i32* %9
%26 = call i32 @mpz_init_set_ui(i32 %25, i32 1)
%27 = load i32, i32* %7
%28 = call i32 @mpz_init_set_ui(i32 %27, i32 2)
br label %29
29: ; preds = %34, %14
%30 = load i32, i32* %9
%31 = load i32, i32* %8
%32 = call i64 @mpz_cmp(i32 %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %29
%35 = load i32, i32* %7
%36 = load i32, i32* %7
%37 = call i32 @mpz_mul_ui(i32 %35, i32 %36, i32 2)
%38 = load i32, i32* %9
%39 = load i32, i32* %9
%40 = call i32 @mpz_add_ui(i32 %38, i32 %39, i32 1)
br label %29
41: ; preds = %29
%42 = load i32, i32* %7
%43 = load i32, i32* %7
%44 = call i32 @mpz_add_ui(i32 %42, i32 %43, i32 1)
%45 = load i32, i32* %7
%46 = call i64 @isprime(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %52
50: ; preds = %41
%51 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %48
%53 = load i32, i32* %7
%54 = call i32 @mpz_clear(i32 %53)
%55 = load i32, i32* %8
%56 = call i32 @mpz_clear(i32 %55)
%57 = load i32, i32* %9
%58 = call i32 @mpz_clear(i32 %57)
store i32 0, i32* %3
br label %59
59: ; preds = %52, %12
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@str = [3 x i8] c"NG\00"
@str.3 = [3 x i8] c"OK\00"
@str.4 = [17 x i8] c"usage : fermat n\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.4, i64 0, i64 0))
br label %30
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strtol(i8* nocapture %8, i32* null, i32 10)
%10 = tail call i32 @mpz_init_set_ui(i32 undef, i32 2)
%11 = tail call i32 @mpz_pow_ui(i32 undef, i32 undef, i32 %9)
%12 = tail call i32 @mpz_init_set_ui(i32 undef, i32 1)
%13 = tail call i32 @mpz_init_set_ui(i32 undef, i32 2)
%14 = tail call i64 @mpz_cmp(i32 undef, i32 undef)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %6, %16
%17 = tail call i32 @mpz_mul_ui(i32 undef, i32 undef, i32 2)
%18 = tail call i32 @mpz_add_ui(i32 undef, i32 undef, i32 1)
%19 = tail call i64 @mpz_cmp(i32 undef, i32 undef)
%20 = icmp slt i64 %19, 0
br i1 %20, label %16, label %21
21: ; preds = %16, %6
%22 = tail call i32 @mpz_add_ui(i32 undef, i32 undef, i32 1)
%23 = tail call i64 @isprime(i32 undef)
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str.3, i64 0, i64 0)
%26 = tail call i32 @puts(i8* dereferenceable(1) %25)
%27 = tail call i32 @mpz_clear(i32 undef)
%28 = tail call i32 @mpz_clear(i32 undef)
%29 = tail call i32 @mpz_clear(i32 undef)
br label %30
30: ; preds = %21, %4
%31 = phi i32 [ 1, %4 ], [ 0, %21 ]
ret i32 %31
}