#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 uint64_t;

/* Variables and functions */
int powmod(int, int, int);
int spsp(uint64_t n, uint64_t p) {
  uint64_t x;
  uint64_t r = n - 1;
  int k = 0;
  while ((r & 1) == 0) {
    k++;
    r >>= 1;
  }
  x = powmod(p, r, n);
  if (x == 1)
    return (1);
  while (k > 0) {
    if (x == n - 1)
      return (1);
    x = powmod(x, 2, n);
    k--;
  }
  return (0);
}


x86 assembly (clang, -O0):
<spsp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    1154 <spsp+0x44>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    112e <spsp+0x1e>
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1030 <powmod@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    117b <spsp+0x6b>
movl   $0x1,-0x4(%rbp)
jmp    11cf <spsp+0xbf>
jmp    1180 <spsp+0x70>
cmpl   $0x0,-0x18(%rbp)
jle    11c8 <spsp+0xb8>
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11a7 <spsp+0x97>
movl   $0x1,-0x4(%rbp)
jmp    11cf <spsp+0xbf>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    $0x2,%esi
call   1030 <powmod@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1180 <spsp+0x70>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spsp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
lea    -0x1(%r15),%ebp
test   $0x1,%bpl
jne    1180 <spsp+0x70>
mov    $0x1,%ebx
mov    %ebp,%eax
nopl   0x0(%rax,%rax,1)
mov    %eax,%ecx
sar    %eax
add    $0x1,%ebx
test   $0x2,%cl
je     1130 <spsp+0x20>
mov    %esi,%edi
mov    %eax,%esi
mov    %r15d,%edx
call   1030 <powmod@plt>
mov    $0x1,%r14d
cmp    $0x1,%eax
je     1196 <spsp+0x86>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %ebp,%eax
je     1196 <spsp+0x86>
mov    %eax,%edi
mov    $0x2,%esi
mov    %r15d,%edx
call   1030 <powmod@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jg     1160 <spsp+0x50>
xor    %r14d,%r14d
jmp    1196 <spsp+0x86>
mov    %esi,%edi
mov    %ebp,%esi
mov    %r15d,%edx
call   1030 <powmod@plt>
xor    %r14d,%r14d
cmp    $0x1,%eax
sete   %r14b
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @spsp(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
store i32 %10, i32* %7
store i32 0, i32* %8
br label %11
11: ; preds = %15, %2
%12 = load i32, i32* %7
%13 = and i32 %12, 1
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
store i32 %17, i32* %8
%18 = load i32, i32* %7
%19 = ashr i32 %18, 1
store i32 %19, i32* %7
br label %11
20: ; preds = %11
%21 = load i32, i32* %5
%22 = load i32, i32* %7
%23 = load i32, i32* %4
%24 = call i32 @powmod(i32 %21, i32 %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %20
store i32 1, i32* %3
br label %45
28: ; preds = %20
br label %29
29: ; preds = %38, %28
%30 = load i32, i32* %8
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %29
%33 = load i32, i32* %6
%34 = load i32, i32* %4
%35 = sub nsw i32 %34, 1
%36 = icmp eq i32 %33, %35
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 1, i32* %3
br label %45
38: ; preds = %32
%39 = load i32, i32* %6
%40 = load i32, i32* %4
%41 = call i32 @powmod(i32 %39, i32 2, i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %8
%43 = add nsw i32 %42, -1
store i32 %43, i32* %8
br label %29
44: ; preds = %29
store i32 0, i32* %3
br label %45
45: ; preds = %44, %37, %27
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
define i32 @spsp(i32 %0, i32 %1) {
%3 = add nsw i32 %0, -1
%4 = and i32 %3, 1
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %16
6: ; preds = %2, %6
%7 = phi i32 [ %9, %6 ], [ 0, %2 ]
%8 = phi i32 [ %10, %6 ], [ %3, %2 ]
%9 = add nuw nsw i32 %7, 1
%10 = ashr i32 %8, 1
%11 = and i32 %8, 2
%12 = icmp eq i32 %11, 0
br i1 %12, label %6, label %13
13: ; preds = %6
%14 = tail call i32 @powmod(i32 %1, i32 %10, i32 %0)
%15 = icmp eq i32 %14, 1
br i1 %15, label %28, label %20
16: ; preds = %2
%17 = tail call i32 @powmod(i32 %1, i32 %3, i32 %0)
%18 = icmp eq i32 %17, 1
%19 = zext i1 %18 to i32
br label %28
20: ; preds = %13, %24
%21 = phi i32 [ %26, %24 ], [ %9, %13 ]
%22 = phi i32 [ %25, %24 ], [ %14, %13 ]
%23 = icmp eq i32 %22, %3
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = tail call i32 @powmod(i32 %22, i32 2, i32 %0)
%26 = add nsw i32 %21, -1
%27 = icmp sgt i32 %21, 1
br i1 %27, label %20, label %28
28: ; preds = %20, %24, %16, %13
%29 = phi i32 [ 1, %13 ], [ %19, %16 ], [ 1, %20 ], [ 0, %24 ]
ret i32 %29
}