#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 */

/* Variables and functions */
int fftw_power_mod(int, int, int);
int get_prime_factors(int, int *);
int fftw_find_generator(int p) {
  int n, i, size;
  int primef[16];
  int pm1 = p - 1;
  if (p == 2)
    return 1;
  size = get_prime_factors(pm1, primef);
  n = 2;
  for (i = 0; i < size; i++)
    if (fftw_power_mod(n, pm1 / primef[i], p) == 1) {
      i = -1;
      n++;
    }
  return n;
}


x86 assembly (clang, -O0):
<fftw_find_generator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x64(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    114a <fftw_find_generator+0x2a>
movl   $0x1,-0x4(%rbp)
jmp    11be <fftw_find_generator+0x9e>
mov    -0x64(%rbp),%edi
lea    -0x60(%rbp),%rsi
call   1040 <get_prime_factors@plt>
mov    %eax,-0x14(%rbp)
movl   $0x2,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b8 <fftw_find_generator+0x98>
mov    -0xc(%rbp),%edi
mov    -0x64(%rbp),%eax
movslq -0x10(%rbp),%rcx
cltd
idivl  -0x60(%rbp,%rcx,4)
mov    %eax,%esi
mov    -0x8(%rbp),%edx
call   1030 <fftw_power_mod@plt>
cmp    $0x1,%eax
jne    11a5 <fftw_find_generator+0x85>
movl   $0xffffffff,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11aa <fftw_find_generator+0x8a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1167 <fftw_find_generator+0x47>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fftw_find_generator>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    $0x1,%ebp
cmp    $0x2,%edi
je     119c <fftw_find_generator+0x7c>
mov    %edi,%r15d
lea    -0x1(%r15),%r14d
mov    %rsp,%rsi
mov    %r14d,%edi
call   1040 <get_prime_factors@plt>
test   %eax,%eax
jle    1197 <fftw_find_generator+0x77>
mov    %eax,%r12d
xor    %ebx,%ebx
mov    $0x2,%ebp
jmp    116b <fftw_find_generator+0x4b>
nopl   0x0(%rax,%rax,1)
mov    %al,%cl
add    %ecx,%ebp
mov    %edx,%ebx
cmp    %r12d,%edx
jge    119c <fftw_find_generator+0x7c>
movslq %ebx,%rcx
mov    %r14d,%eax
cltd
idivl  (%rsp,%rcx,4)
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15d,%edx
call   1030 <fftw_power_mod@plt>
xor    %ecx,%ecx
mov    $0x0,%edx
cmp    $0x1,%eax
sete   %al
je     1160 <fftw_find_generator+0x40>
add    $0x1,%ebx
mov    %ebx,%edx
jmp    1160 <fftw_find_generator+0x40>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fftw_find_generator(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [16 x i32]
%8 = alloca i32
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = sub nsw i32 %9, 1
store i32 %10, i32* %8
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 1, i32* %2
br label %42
14: ; preds = %1
%15 = load i32, i32* %8
%16 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%17 = call i32 @get_prime_factors(i32 %15, i32* %16)
store i32 %17, i32* %6
store i32 2, i32* %4
store i32 0, i32* %5
br label %18
18: ; preds = %37, %14
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %40
22: ; preds = %18
%23 = load i32, i32* %4
%24 = load i32, i32* %8
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = sdiv i32 %24, %28
%30 = load i32, i32* %3
%31 = call i32 @fftw_power_mod(i32 %23, i32 %29, i32 %30)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %22
store i32 -1, i32* %5
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
store i32 %35, i32* %4
br label %36
36: ; preds = %33, %22
br label %37
37: ; preds = %36
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %18
40: ; preds = %18
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %13
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
define i32 @fftw_find_generator(i32 %0) {
%2 = alloca [16 x i32]
%3 = bitcast [16 x i32]* %2 to i8*
%4 = add nsw i32 %0, -1
%5 = icmp eq i32 %0, 2
br i1 %5, label %24, label %6
6: ; preds = %1
%7 = getelementptr inbounds [16 x i32], [16 x i32]* %2, i64 0, i64 0
%8 = call i32 @get_prime_factors(i32 %4, i32* %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %6, %10
%11 = phi i32 [ %20, %10 ], [ 2, %6 ]
%12 = phi i32 [ %22, %10 ], [ 0, %6 ]
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [16 x i32], [16 x i32]* %2, i64 0, i64 %13
%15 = load i32, i32* %14
%16 = sdiv i32 %4, %15
%17 = call i32 @fftw_power_mod(i32 %11, i32 %16, i32 %0)
%18 = icmp eq i32 %17, 1
%19 = zext i1 %18 to i32
%20 = add nuw nsw i32 %11, %19
%21 = add nsw i32 %12, 1
%22 = select i1 %18, i32 0, i32 %21
%23 = icmp slt i32 %22, %8
br i1 %23, label %10, label %24
24: ; preds = %10, %6, %1
%25 = phi i32 [ 1, %1 ], [ 2, %6 ], [ %20, %10 ]
ret i32 %25
}