#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 *nextPerm(int *p, int index, int size, int length) {
  int temp;
  if (0 == index % factorial(size)) {
    temp = p[length - 1];
    p[length - 1] = p[length - 1 - size];
    p[length - 1 - size] = temp;
    return (p);
  } else
    return (nextPerm(p, index, size - 1, length));
}


x86 assembly (clang, -O0):
<nextPerm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <factorial@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cltd
idiv   %ecx
xor    %eax,%eax
cmp    %edx,%eax
jne    11ae <nextPerm+0x8e>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <nextPerm+0xa7>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    $0x1,%edx
mov    -0x1c(%rbp),%ecx
call   1040 <nextPerm@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nextPerm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%r12d
mov    %rdi,%r15
lea    -0x1(%r14),%eax
cltq
movslq %edx,%rcx
sub    %rcx,%rax
lea    (%rdi,%rax,4),%rbx
add    $0xfffffffffffffffc,%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1030 <factorial@plt>
mov    %eax,%ecx
mov    %r12d,%eax
cltd
idiv   %ecx
add    $0xffffffff,%ebp
add    $0x4,%rbx
test   %edx,%edx
jne    1140 <nextPerm+0x30>
movslq %r14d,%rax
mov    -0x4(%r15,%rax,4),%ecx
mov    (%rbx),%edx
mov    %edx,-0x4(%r15,%rax,4)
mov    %ecx,(%rbx)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @nextPerm(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %7
%12 = load i32, i32* %8
%13 = call i32 @factorial(i32 %12)
%14 = srem i32 %11, %13
%15 = icmp eq i32 0, %14
br i1 %15, label %16, label %45
16: ; preds = %4
%17 = load i32*, i32** %6
%18 = load i32, i32* %9
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %17, i64 %20
%22 = load i32, i32* %21
store i32 %22, i32* %10
%23 = load i32*, i32** %6
%24 = load i32, i32* %9
%25 = sub nsw i32 %24, 1
%26 = load i32, i32* %8
%27 = sub nsw i32 %25, %26
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %23, i64 %28
%30 = load i32, i32* %29
%31 = load i32*, i32** %6
%32 = load i32, i32* %9
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %31, i64 %34
store i32 %30, i32* %35
%36 = load i32, i32* %10
%37 = load i32*, i32** %6
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, 1
%40 = load i32, i32* %8
%41 = sub nsw i32 %39, %40
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %37, i64 %42
store i32 %36, i32* %43
%44 = load i32*, i32** %6
store i32* %44, i32** %5
br label %52
45: ; preds = %4
%46 = load i32*, i32** %6
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = sub nsw i32 %48, 1
%50 = load i32, i32* %9
%51 = call i32* @nextPerm(i32* %46, i32 %47, i32 %49, i32 %50)
store i32* %51, i32** %5
br label %52
52: ; preds = %45, %16
%53 = load i32*, i32** %5
ret i32* %53
}

LLVM IR (-O2):
define i32* @nextPerm(i32* %0, i32 %1, i32 %2, i32 %3) {
br label %5
5: ; preds = %5, %4
%6 = phi i32 [ %2, %4 ], [ %10, %5 ]
%7 = tail call i32 @factorial(i32 %6)
%8 = srem i32 %1, %7
%9 = icmp eq i32 %8, 0
%10 = add nsw i32 %6, -1
br i1 %9, label %11, label %5
11: ; preds = %5
%12 = add nsw i32 %3, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = sub nsw i32 %12, %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %0, i64 %17
%19 = load i32, i32* %18
store i32 %19, i32* %14
store i32 %15, i32* %18
ret i32* %0
}