#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);
scalar_t__ malloc(int);
int /*<<< orphan*/ permutations(int **, int *, int, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int **permute(int *nums, int nums_size, int *return_size) {
  int n_perms = factorial(nums_size);
  int **perms = (int **)malloc(n_perms * sizeof(int *));
  for (int i = 0; i < n_perms; i++)
    perms[i] = (int *)malloc(nums_size * sizeof(int));
  permutations(perms, nums, nums_size, 0, 0);
  for (int i = 0; i < n_perms; i++) {
    for (int j = 0; j < nums_size; j++)
      printf("%d ", perms[i][j]);
    printf("\n");
  }
  *return_size = n_perms;
  return perms;
}


x86 assembly (clang, -O0):
<permute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <factorial@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11b0 <permute+0x70>
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1178 <permute+0x38>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
xor    %r8d,%r8d
mov    %r8d,%ecx
call   1060 <permutations@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1237 <permute+0xf7>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    121b <permute+0xdb>
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e0 <permute+0xa0>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11cd <permute+0x8d>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<permute>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x10(%rsp)
mov    %esi,%ebp
mov    %rdi,%r13
mov    %esi,%edi
call   1040 <factorial@plt>
mov    %eax,%r12d
lea    0x0(,%r12,8),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %r12d,%r12d
mov    %r12,0x8(%rsp)
jle    122d <permute+0xdd>
mov    %rbp,(%rsp)
lea    0x0(,%rbp,4),%r15d
mov    %r12d,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
call   1060 <malloc@plt>
mov    %rax,(%rbx,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%r14
jne    11b0 <permute+0x60>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    (%rsp),%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <permutations@plt>
test   %r12d,%r12d
jle    123f <permute+0xef>
mov    (%rsp),%ebp
lea    0xe18(%rip),%r13
xor    %r15d,%r15d
jmp    1203 <permute+0xb3>
nopl   (%rax)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r15
cmp    %r14,%r15
je     123f <permute+0xef>
cmpl   $0x0,(%rsp)
jle    11f0 <permute+0xa0>
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    (%rbx,%r15,8),%rax
mov    (%rax,%r12,4),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    1210 <permute+0xc0>
jmp    11f0 <permute+0xa0>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <permutations@plt>
mov    0x10(%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %ecx,(%rax)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d \00"
@.str.1 = [2 x i8] c"\0A\00"
define i32** @permute(i32* %0, i32 %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%12 = load i32, i32* %5
%13 = call i32 @factorial(i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 8
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i32**
store i32** %19, i32*** %8
store i32 0, i32* %9
br label %20
20: ; preds = %35, %3
%21 = load i32, i32* %9
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %38
24: ; preds = %20
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 4
%28 = trunc i64 %27 to i32
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i32*
%31 = load i32**, i32*** %8
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32*, i32** %31, i64 %33
store i32* %30, i32** %34
br label %35
35: ; preds = %24
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %20
38: ; preds = %20
%39 = load i32**, i32*** %8
%40 = load i32*, i32** %4
%41 = load i32, i32* %5
%42 = call i32 @permutations(i32** %39, i32* %40, i32 %41, i32 0, i32 0)
store i32 0, i32* %10
br label %43
43: ; preds = %68, %38
%44 = load i32, i32* %10
%45 = load i32, i32* %7
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %71
47: ; preds = %43
store i32 0, i32* %11
br label %48
48: ; preds = %63, %47
%49 = load i32, i32* %11
%50 = load i32, i32* %5
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %66
52: ; preds = %48
%53 = load i32**, i32*** %8
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32*, i32** %53, i64 %55
%57 = load i32*, i32** %56
%58 = load i32, i32* %11
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
%61 = load i32, i32* %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %52
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 1
store i32 %65, i32* %11
br label %48
66: ; preds = %48
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %68
68: ; preds = %66
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %43
71: ; preds = %43
%72 = load i32, i32* %7
%73 = load i32*, i32** %6
store i32 %72, i32* %73
%74 = load i32**, i32*** %8
ret i32** %74
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d \00"
define i32** @permute(i32* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @factorial(i32 %1)
%5 = shl i32 %4, 3
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i32**
%8 = icmp sgt i32 %4, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @permutations(i32** %7, i32* %0, i32 %1, i32 0, i32 0)
br label %31
11: ; preds = %3
%12 = shl i32 %1, 2
%13 = zext i32 %4 to i64
br label %20
14: ; preds = %20
%15 = tail call i32 @permutations(i32** %7, i32* %0, i32 %1, i32 0, i32 0)
br i1 %8, label %16, label %31
16: ; preds = %14
%17 = icmp sgt i32 %1, 0
%18 = zext i32 %4 to i64
%19 = zext i32 %1 to i64
br label %27
20: ; preds = %11, %20
%21 = phi i64 [ 0, %11 ], [ %25, %20 ]
%22 = tail call i64 @malloc(i32 %12)
%23 = inttoptr i64 %22 to i32*
%24 = getelementptr inbounds i32*, i32** %7, i64 %21
store i32* %23, i32** %24
%25 = add nuw nsw i64 %21, 1
%26 = icmp eq i64 %25, %13
br i1 %26, label %14, label %20
27: ; preds = %16, %32
%28 = phi i64 [ 0, %16 ], [ %34, %32 ]
br i1 %17, label %29, label %32
29: ; preds = %27
%30 = getelementptr inbounds i32*, i32** %7, i64 %28
br label %36
31: ; preds = %32, %9, %14
store i32 %4, i32* %2
ret i32** %7
32: ; preds = %36, %27
%33 = tail call i32 @putchar(i32 10)
%34 = add nuw nsw i64 %28, 1
%35 = icmp eq i64 %34, %18
br i1 %35, label %31, label %27
36: ; preds = %29, %36
%37 = phi i64 [ 0, %29 ], [ %42, %36 ]
%38 = load i32*, i32** %30
%39 = getelementptr inbounds i32, i32* %38, i64 %37
%40 = load i32, i32* %39
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %40)
%42 = add nuw nsw i64 %37, 1
%43 = icmp eq i64 %42, %19
br i1 %43, label %32, label %36
}