#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 factorial(int);
int /*<<< orphan*/ printVal(int);
int /*<<< orphan*/ print_Array(int *, int);
int /*<<< orphan*/ printf(char *, char *);
int *swap(int *, int, int);
void permute(int *toPermute, int elementCount) {
  int ttLoops = factorial(elementCount);
  printVal(ttLoops);
  { printf("%s", "\n"); };
  int currentLoop = 0;
  int currentIndx = elementCount - 1;
  while (currentLoop < ttLoops) {
    toPermute = swap(toPermute, currentIndx, elementCount - 1);
    print_Array(toPermute, elementCount - 1);
    { printf("%s", "\n"); };
    currentLoop++;
    currentIndx = currentIndx == 0 ? elementCount - 1 : currentIndx - 1;
  }
}


x86 assembly (clang, -O0):
<permute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <factorial@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <printVal@plt>
lea    0xe87(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    120e <permute+0xbe>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1070 <swap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1030 <print_Array@plt>
lea    0xe33(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11fa <permute+0xaa>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1203 <permute+0xb3>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1195 <permute+0x45>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<permute>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r13
mov    %esi,%edi
call   1040 <factorial@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <printVal@plt>
lea    0xe88(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1050 <printf@plt>
test   %ebp,%ebp
jle    11d2 <permute+0x82>
add    $0xffffffff,%r14d
lea    0xe6d(%rip),%r15
lea    0xe69(%rip),%r12
mov    %r14d,%ebx
nopl   (%rax)
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
call   1070 <swap@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14d,%esi
call   1030 <print_Array@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
sub    $0x1,%ebx
cmovb  %r14d,%ebx
add    $0xffffffff,%ebp
jne    11a0 <permute+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @permute(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = call i32 @factorial(i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = call i32 @printVal(i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %7
br label %15
15: ; preds = %40, %2
%16 = load i32, i32* %6
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %42
19: ; preds = %15
%20 = load i32*, i32** %3
%21 = load i32, i32* %7
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = call i32* @swap(i32* %20, i32 %21, i32 %23)
store i32* %24, i32** %3
%25 = load i32*, i32** %3
%26 = load i32, i32* %4
%27 = sub nsw i32 %26, 1
%28 = call i32 @print_Array(i32* %25, i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %19
%35 = load i32, i32* %4
%36 = sub nsw i32 %35, 1
br label %40
37: ; preds = %19
%38 = load i32, i32* %7
%39 = sub nsw i32 %38, 1
br label %40
40: ; preds = %37, %34
%41 = phi i32 [ %36, %34 ], [ %39, %37 ]
store i32 %41, i32* %7
br label %15
42: ; preds = %15
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @permute(i32* %0, i32 %1) {
%3 = tail call i32 @factorial(i32 %1)
%4 = tail call i32 @printVal(i32 %3)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = add nsw i32 %1, -1
%7 = icmp sgt i32 %3, 0
br i1 %7, label %8, label %20
8: ; preds = %2, %8
%9 = phi i32 [ %18, %8 ], [ %6, %2 ]
%10 = phi i32 [ %15, %8 ], [ 0, %2 ]
%11 = phi i32* [ %12, %8 ], [ %0, %2 ]
%12 = tail call i32* @swap(i32* %11, i32 %9, i32 %6)
%13 = tail call i32 @print_Array(i32* %12, i32 %6)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = add nuw nsw i32 %10, 1
%16 = icmp eq i32 %9, 0
%17 = add nsw i32 %9, -1
%18 = select i1 %16, i32 %6, i32 %17
%19 = icmp eq i32 %15, %3
br i1 %19, label %20, label %8
20: ; preds = %8, %2
ret void
}