#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*/ exchange(int *, int, int);
int factorial(int);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ print_array(int *, int);
int /*<<< orphan*/ printf(char *, int, int, int, int);
void permutations(int **perms, int *nums, int nums_size, int col, int row) {
  int n_perms = factorial(nums_size);
  printf("nums_size: %d, col: %d, row: %d, n_perms: %d\n", nums_size, col, row,
         n_perms);
  print_array(nums, nums_size);
  for (int i = 0; i < n_perms; i++) {
    int current_index = i * nums_size / n_perms;
    perms[i + row][col] = nums[current_index];
    if (i % (n_perms / nums_size) == 0 && nums_size - 1 > 0) {
      int *nums_cpy = malloc(nums_size * sizeof(int));
      memcpy(nums_cpy, nums, nums_size * sizeof(int));
      exchange(nums_cpy, current_index, nums_size - 1);
      permutations(perms, nums_cpy, nums_size - 1, col + 1, i + row);
      free(nums_cpy);
    }
  }
}


x86 assembly (clang, -O0):
<permutations>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <factorial@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
lea    0xe47(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <print_array@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12b4 <permutations+0x134>
mov    -0x24(%rbp),%eax
imul   -0x14(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12a1 <permutations+0x121>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cmp    $0x0,%eax
jle    12a1 <permutations+0x121>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   10a0 <exchange@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    -0x24(%rbp),%r8d
add    -0x1c(%rbp),%r8d
call   1090 <permutations@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    12a6 <permutations+0x126>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d1 <permutations+0x51>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<permutations>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8d,%r15d
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%r13
mov    %rdi,0x18(%rsp)
mov    %edx,%edi
call   1040 <factorial@plt>
mov    %eax,%ebx
lea    0xe52(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%r12d
mov    %r14d,%edx
mov    %r15d,%ecx
mov    %eax,%r8d
call   1060 <printf@plt>
mov    %r13,0x20(%rsp)
mov    %r13,%rdi
mov    %ebp,%esi
call   1050 <print_array@plt>
test   %ebx,%ebx
jle    12dc <permutations+0x15c>
mov    %ebx,%edi
movslq %r12d,%r8
mov    %edi,%eax
cltd
idiv   %ebp
mov    %eax,%r9d
lea    -0x1(%rbp),%eax
mov    %eax,0x10(%rsp)
lea    0x0(,%rbp,4),%eax
mov    %eax,0xc(%rsp)
add    $0x1,%r12d
mov    %r12d,0x14(%rsp)
movslq %r15d,%rax
mov    %r15d,%ecx
mov    %rcx,0x28(%rsp)
mov    0x18(%rsp),%rcx
lea    (%rcx,%rax,8),%rsi
xor    %r13d,%r13d
xor    %r15d,%r15d
mov    %rdi,0x40(%rsp)
mov    %rbp,0x38(%rsp)
mov    %r8,0x30(%rsp)
mov    %r9d,0x8(%rsp)
jmp    1240 <permutations+0xc0>
add    $0x1,%r15
add    %ebp,%r13d
cmp    %r15,%rdi
je     12dc <permutations+0x15c>
mov    %r13d,%eax
cltd
idiv   %edi
mov    %eax,%r14d
cltq
mov    0x20(%rsp),%rcx
mov    (%rcx,%rax,4),%eax
mov    (%rsi,%r15,8),%rcx
mov    %eax,(%rcx,%r8,4)
mov    %r15d,%eax
cltd
idiv   %r9d
test   %edx,%edx
jne    1230 <permutations+0xb0>
cmp    $0x2,%ebp
jl     1230 <permutations+0xb0>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
mov    %rsi,%rbp
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    0x20(%rsp),%rsi
mov    %ebx,%edx
call   1070 <memcpy@plt>
mov    %r12,%rdi
mov    %r14d,%esi
mov    0x10(%rsp),%ebx
mov    %ebx,%edx
call   10a0 <exchange@plt>
mov    0x28(%rsp),%rax
lea    (%rax,%r15,1),%r8d
mov    0x18(%rsp),%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    0x14(%rsp),%ecx
call   1090 <permutations@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rsi
mov    0x8(%rsp),%r9d
mov    0x40(%rsp),%rdi
mov    0x30(%rsp),%r8
mov    0x38(%rsp),%rbp
jmp    1230 <permutations+0xb0>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [46 x i8] c"nums_size: %d, col: %d, row: %d, n_perms: %d\0A\00"
define void @permutations(i32** %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
store i32** %0, i32*** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%15 = load i32, i32* %8
%16 = call i32 @factorial(i32 %15)
store i32 %16, i32* %11
%17 = load i32, i32* %8
%18 = load i32, i32* %9
%19 = load i32, i32* %10
%20 = load i32, i32* %11
%21 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18, i32 %19, i32 %20)
%22 = load i32*, i32** %7
%23 = load i32, i32* %8
%24 = call i32 @print_array(i32* %22, i32 %23)
store i32 0, i32* %12
br label %25
25: ; preds = %90, %5
%26 = load i32, i32* %12
%27 = load i32, i32* %11
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %93
29: ; preds = %25
%30 = load i32, i32* %12
%31 = load i32, i32* %8
%32 = mul nsw i32 %30, %31
%33 = load i32, i32* %11
%34 = sdiv i32 %32, %33
store i32 %34, i32* %13
%35 = load i32*, i32** %7
%36 = load i32, i32* %13
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = load i32**, i32*** %6
%41 = load i32, i32* %12
%42 = load i32, i32* %10
%43 = add nsw i32 %41, %42
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32*, i32** %40, i64 %44
%46 = load i32*, i32** %45
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 %39, i32* %49
%50 = load i32, i32* %12
%51 = load i32, i32* %11
%52 = load i32, i32* %8
%53 = sdiv i32 %51, %52
%54 = srem i32 %50, %53
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %89
56: ; preds = %29
%57 = load i32, i32* %8
%58 = sub nsw i32 %57, 1
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %89
60: ; preds = %56
%61 = load i32, i32* %8
%62 = sext i32 %61 to i64
%63 = mul i64 %62, 4
%64 = trunc i64 %63 to i32
%65 = call i32* @malloc(i32 %64)
store i32* %65, i32** %14
%66 = load i32*, i32** %14
%67 = load i32*, i32** %7
%68 = load i32, i32* %8
%69 = sext i32 %68 to i64
%70 = mul i64 %69, 4
%71 = trunc i64 %70 to i32
%72 = call i32 @memcpy(i32* %66, i32* %67, i32 %71)
%73 = load i32*, i32** %14
%74 = load i32, i32* %13
%75 = load i32, i32* %8
%76 = sub nsw i32 %75, 1
%77 = call i32 @exchange(i32* %73, i32 %74, i32 %76)
%78 = load i32**, i32*** %6
%79 = load i32*, i32** %14
%80 = load i32, i32* %8
%81 = sub nsw i32 %80, 1
%82 = load i32, i32* %9
%83 = add nsw i32 %82, 1
%84 = load i32, i32* %12
%85 = load i32, i32* %10
%86 = add nsw i32 %84, %85
%87 = load i32*, i32** %14
%88 = call i32 @free(i32* %87)
br label %89
89: ; preds = %60, %56, %29
br label %90
90: ; preds = %89
%91 = load i32, i32* %12
%92 = add nsw i32 %91, 1
store i32 %92, i32* %12
br label %25
93: ; preds = %25
ret void
}

LLVM IR (-O2):
@.str = [46 x i8] c"nums_size: %d, col: %d, row: %d, n_perms: %d\0A\00"
define void @permutations(i32** nocapture %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i32 @factorial(i32 %2)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3, i32 %4, i32 %6)
%8 = tail call i32 @print_array(i32* %1, i32 %2)
%9 = icmp sgt i32 %6, 0
br i1 %9, label %10, label %19
10: ; preds = %5
%11 = sext i32 %3 to i64
%12 = sdiv i32 %6, %2
%13 = add nsw i32 %2, -1
%14 = icmp slt i32 %2, 2
%15 = shl i32 %2, 2
%16 = add nsw i32 %3, 1
%17 = sext i32 %4 to i64
%18 = zext i32 %6 to i64
br label %20
19: ; preds = %42, %5
ret void
20: ; preds = %10, %42
%21 = phi i64 [ 0, %10 ], [ %43, %42 ]
%22 = trunc i64 %21 to i32
%23 = mul i32 %22, %2
%24 = sdiv i32 %23, %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %1, i64 %25
%27 = load i32, i32* %26
%28 = add nsw i64 %21, %17
%29 = getelementptr inbounds i32*, i32** %0, i64 %28
%30 = load i32*, i32** %29
%31 = getelementptr inbounds i32, i32* %30, i64 %11
store i32 %27, i32* %31
%32 = trunc i64 %21 to i32
%33 = srem i32 %32, %12
%34 = icmp ne i32 %33, 0
%35 = or i1 %34, %14
br i1 %35, label %42, label %36
36: ; preds = %20
%37 = tail call i32* @malloc(i32 %15)
%38 = tail call i32 @memcpy(i32* %37, i32* %1, i32 %15)
%39 = tail call i32 @exchange(i32* %37, i32 %24, i32 %13)
%40 = trunc i64 %28 to i32
tail call void @permutations(i32** %0, i32* %37, i32 %13, i32 %16, i32 %40)
%41 = tail call i32 @free(i32* %37)
br label %42
42: ; preds = %20, %36
%43 = add nuw nsw i64 %21, 1
%44 = icmp eq i64 %43, %18
br i1 %44, label %19, label %20
}