#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*/ free(unsigned int *);
int /*<<< orphan*/ init_factorial(unsigned int *, size_t);
unsigned int *malloc(size_t);
int /*<<< orphan*/ swip(int *, unsigned int, unsigned int);
int *nth_permutation(int *data, size_t size, unsigned int n) {
  unsigned int i = 0;
  unsigned int j;
  unsigned int f;
  unsigned int *factorial = malloc(size * sizeof(int));
  init_factorial(factorial, size);
  if (n == 0)
    return NULL;
  n = n - 1;
  while (n > 0) {
    f = factorial[size - (i + 1)];
    j = n / f;
    if (0 != j)
      swip(data, i, i + j);
    n = n % f;
    i++;
  }
  free(factorial);
  return data;
}


x86 assembly (clang, -O0):
<nth_permutation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <init_factorial@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    118f <nth_permutation+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    120d <nth_permutation+0xcd>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jbe    11fc <nth_permutation+0xbc>
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%edx
add    $0x1,%edx
mov    %edx,%edx
sub    %rdx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
xor    %edx,%edx
divl   -0x28(%rbp)
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
je     11e3 <nth_permutation+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x20(%rbp),%edx
add    -0x24(%rbp),%edx
call   1060 <swip@plt>
mov    -0x1c(%rbp),%eax
xor    %edx,%edx
divl   -0x28(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1198 <nth_permutation+0x58>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nth_permutation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x0(,%rsi,4),%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <init_factorial@plt>
test   %ebp,%ebp
je     11be <nth_permutation+0x7e>
add    $0xffffffff,%ebp
je     11b4 <nth_permutation+0x74>
xor    %esi,%esi
jmp    118f <nth_permutation+0x4f>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%eax
xor    %edx,%edx
div    %ebx
mov    %r13d,%esi
mov    %edx,%ebp
test   %edx,%edx
je     11b4 <nth_permutation+0x74>
lea    0x1(%rsi),%r13d
mov    %r15,%rax
sub    %r13,%rax
mov    (%r12,%rax,4),%ebx
cmp    %ebp,%ebx
ja     1180 <nth_permutation+0x40>
mov    %ebp,%eax
xor    %edx,%edx
div    %ebx
lea    (%rax,%rsi,1),%edx
mov    %r14,%rdi
call   1060 <swip@plt>
jmp    1180 <nth_permutation+0x40>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    11c1 <nth_permutation+0x81>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @nth_permutation(i32* %0, i64 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%12 = load i64, i64* %6
%13 = mul i64 %12, 4
%14 = call i32* @malloc(i64 %13)
store i32* %14, i32** %11
%15 = load i32*, i32** %11
%16 = load i64, i64* %6
%17 = call i32 @init_factorial(i32* %15, i64 %16)
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32* null, i32** %4
br label %58
21: ; preds = %3
%22 = load i32, i32* %7
%23 = sub i32 %22, 1
store i32 %23, i32* %7
br label %24
24: ; preds = %48, %21
%25 = load i32, i32* %7
%26 = icmp ugt i32 %25, 0
br i1 %26, label %27, label %54
27: ; preds = %24
%28 = load i32*, i32** %11
%29 = load i64, i64* %6
%30 = load i32, i32* %8
%31 = add i32 %30, 1
%32 = zext i32 %31 to i64
%33 = sub i64 %29, %32
%34 = getelementptr inbounds i32, i32* %28, i64 %33
%35 = load i32, i32* %34
store i32 %35, i32* %10
%36 = load i32, i32* %7
%37 = load i32, i32* %10
%38 = udiv i32 %36, %37
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp ne i32 0, %39
br i1 %40, label %41, label %48
41: ; preds = %27
%42 = load i32*, i32** %5
%43 = load i32, i32* %8
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = add i32 %44, %45
%47 = call i32 @swip(i32* %42, i32 %43, i32 %46)
br label %48
48: ; preds = %41, %27
%49 = load i32, i32* %7
%50 = load i32, i32* %10
%51 = urem i32 %49, %50
store i32 %51, i32* %7
%52 = load i32, i32* %8
%53 = add i32 %52, 1
store i32 %53, i32* %8
br label %24
54: ; preds = %24
%55 = load i32*, i32** %11
%56 = call i32 @free(i32* %55)
%57 = load i32*, i32** %5
store i32* %57, i32** %4
br label %58
58: ; preds = %54, %20
%59 = load i32*, i32** %4
ret i32* %59
}

LLVM IR (-O2):
define i32* @nth_permutation(i32* %0, i64 %1, i32 %2) {
%4 = shl i64 %1, 2
%5 = tail call i32* @malloc(i64 %4)
%6 = tail call i32 @init_factorial(i32* %5, i64 %1)
%7 = icmp eq i32 %2, 0
br i1 %7, label %29, label %8
8: ; preds = %3
%9 = add i32 %2, -1
%10 = icmp eq i32 %9, 0
br i1 %10, label %27, label %11
11: ; preds = %8, %24
%12 = phi i32 [ %25, %24 ], [ %9, %8 ]
%13 = phi i32 [ %14, %24 ], [ 0, %8 ]
%14 = add i32 %13, 1
%15 = zext i32 %14 to i64
%16 = sub i64 %1, %15
%17 = getelementptr inbounds i32, i32* %5, i64 %16
%18 = load i32, i32* %17
%19 = icmp ugt i32 %18, %12
br i1 %19, label %24, label %20
20: ; preds = %11
%21 = udiv i32 %12, %18
%22 = add i32 %21, %13
%23 = tail call i32 @swip(i32* %0, i32 %13, i32 %22)
br label %24
24: ; preds = %20, %11
%25 = urem i32 %12, %18
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %11
27: ; preds = %24, %8
%28 = tail call i32 @free(i32* %5)
br label %29
29: ; preds = %3, %27
%30 = phi i32* [ %0, %27 ], [ null, %3 ]
ret i32* %30
}