#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 /*<<< orphan*/ cycle_leader(int *, int, int);
int /*<<< orphan*/ right_rotate(int *, int, int);
void perfect_shuffle3(int *a, int n) {
  int n2, m, i, k, t;
  for (; n > 1;) {
    n2 = n * 2;
    for (k = 0, m = 1; n2 / m >= 3; ++k, m *= 3)
      ;
    m /= 2;
    right_rotate(a + m, m, n);
    for (i = 0, t = 1; i < k; ++i, t *= 3) {
      cycle_leader(a, t, m * 2 + 1);
    }
    a += m * 2;
    n -= m;
  }
  t = a[1];
  a[1] = a[2];
  a[2] = t;
}


x86 assembly (clang, -O0):
<perfect_shuffle3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    120e <perfect_shuffle3+0xee>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cltd
idivl  -0x14(%rbp)
cmp    $0x3,%eax
jl     117a <perfect_shuffle3+0x5a>
jmp    1165 <perfect_shuffle3+0x45>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
imul   $0x3,-0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1150 <perfect_shuffle3+0x30>
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <right_rotate@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11e6 <perfect_shuffle3+0xc6>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x14(%rbp),%edx
shl    $0x1,%edx
add    $0x1,%edx
call   1040 <cycle_leader@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
imul   $0x3,-0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11b0 <perfect_shuffle3+0x90>
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    112f <perfect_shuffle3+0xf>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x8(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perfect_shuffle3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
cmp    $0x2,%esi
jl     11d0 <perfect_shuffle3+0xb0>
mov    %esi,%ebx
nopl   0x0(%rax)
lea    (%rbx,%rbx,1),%ecx
mov    $0xffffffff,%esi
mov    $0x1,%edx
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %edx,%r14d
lea    0x0(%rbp,%rbp,2),%ebp
mov    %ecx,%eax
cltd
idiv   %ebp
add    $0x1,%esi
lea    0x1(%r14),%edx
cmp    $0x2,%eax
jg     1160 <perfect_shuffle3+0x40>
mov    %ebp,%r15d
shr    %r15d
lea    (%r12,%r15,4),%rdi
mov    %r15d,%esi
mov    %rbx,(%rsp)
mov    %ebx,%edx
call   1030 <right_rotate@plt>
mov    %ebp,%r13d
or     $0x1,%r13d
mov    $0x1,%ebx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
call   1040 <cycle_leader@plt>
lea    (%rbx,%rbx,2),%ebx
add    $0xffffffff,%r14d
jne    11a0 <perfect_shuffle3+0x80>
and    $0x7ffffffe,%ebp
lea    (%r12,%rbp,4),%r12
mov    (%rsp),%rbx
sub    %r15d,%ebx
cmp    $0x1,%ebx
jg     1140 <perfect_shuffle3+0x20>
rolq   $0x20,0x4(%r12)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @perfect_shuffle3(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
br label %10
10: ; preds = %53, %2
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %62
13: ; preds = %10
%14 = load i32, i32* %4
%15 = mul nsw i32 %14, 2
store i32 %15, i32* %5
store i32 0, i32* %8
store i32 1, i32* %6
br label %16
16: ; preds = %22, %13
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = sdiv i32 %17, %18
%20 = icmp sge i32 %19, 3
br i1 %20, label %21, label %27
21: ; preds = %16
br label %22
22: ; preds = %21
%23 = load i32, i32* %8
%24 = add nsw i32 %23, 1
store i32 %24, i32* %8
%25 = load i32, i32* %6
%26 = mul nsw i32 %25, 3
store i32 %26, i32* %6
br label %16
27: ; preds = %16
%28 = load i32, i32* %6
%29 = sdiv i32 %28, 2
store i32 %29, i32* %6
%30 = load i32*, i32** %3
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %6
%35 = load i32, i32* %4
%36 = call i32 @right_rotate(i32* %33, i32 %34, i32 %35)
store i32 0, i32* %7
store i32 1, i32* %9
br label %37
37: ; preds = %48, %27
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %53
41: ; preds = %37
%42 = load i32*, i32** %3
%43 = load i32, i32* %9
%44 = load i32, i32* %6
%45 = mul nsw i32 %44, 2
%46 = add nsw i32 %45, 1
%47 = call i32 @cycle_leader(i32* %42, i32 %43, i32 %46)
br label %48
48: ; preds = %41
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
%51 = load i32, i32* %9
%52 = mul nsw i32 %51, 3
store i32 %52, i32* %9
br label %37
53: ; preds = %37
%54 = load i32, i32* %6
%55 = mul nsw i32 %54, 2
%56 = load i32*, i32** %3
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds i32, i32* %56, i64 %57
store i32* %58, i32** %3
%59 = load i32, i32* %6
%60 = load i32, i32* %4
%61 = sub nsw i32 %60, %59
store i32 %61, i32* %4
br label %10
62: ; preds = %10
%63 = load i32*, i32** %3
%64 = getelementptr inbounds i32, i32* %63, i64 1
%65 = load i32, i32* %64
store i32 %65, i32* %9
%66 = load i32*, i32** %3
%67 = getelementptr inbounds i32, i32* %66, i64 2
%68 = load i32, i32* %67
%69 = load i32*, i32** %3
%70 = getelementptr inbounds i32, i32* %69, i64 1
store i32 %68, i32* %70
%71 = load i32, i32* %9
%72 = load i32*, i32** %3
%73 = getelementptr inbounds i32, i32* %72, i64 2
store i32 %71, i32* %73
ret void
}

LLVM IR (-O2):
define void @perfect_shuffle3(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %34
4: ; preds = %2, %28
%5 = phi i32* [ %31, %28 ], [ %0, %2 ]
%6 = phi i32 [ %32, %28 ], [ %1, %2 ]
%7 = shl nuw nsw i32 %6, 1
br label %8
8: ; preds = %4, %8
%9 = phi i32 [ %11, %8 ], [ 0, %4 ]
%10 = phi i32 [ %12, %8 ], [ 1, %4 ]
%11 = add nuw nsw i32 %9, 1
%12 = mul nuw nsw i32 %10, 3
%13 = sdiv i32 %7, %12
%14 = icmp sgt i32 %13, 2
br i1 %14, label %8, label %15
15: ; preds = %8
%16 = lshr i32 %12, 1
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %5, i64 %17
%19 = tail call i32 @right_rotate(i32* %18, i32 %16, i32 %6)
%20 = or i32 %12, 1
br label %21
21: ; preds = %15, %21
%22 = phi i32 [ 1, %15 ], [ %26, %21 ]
%23 = phi i32 [ 0, %15 ], [ %25, %21 ]
%24 = tail call i32 @cycle_leader(i32* %5, i32 %22, i32 %20)
%25 = add nuw i32 %23, 1
%26 = mul nsw i32 %22, 3
%27 = icmp eq i32 %23, %9
br i1 %27, label %28, label %21
28: ; preds = %21
%29 = and i32 %12, 2147483646
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %5, i64 %30
%32 = sub nsw i32 %6, %16
%33 = icmp sgt i32 %32, 1
br i1 %33, label %4, label %34
34: ; preds = %28, %2
%35 = phi i32* [ %0, %2 ], [ %31, %28 ]
%36 = getelementptr inbounds i32, i32* %35, i64 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds i32, i32* %35, i64 2
%39 = load i32, i32* %38
store i32 %39, i32* %36
store i32 %37, i32* %38
ret void
}