#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(int *);
int /*<<< orphan*/ generateCandidates(int *, int, int, int *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printPermutation(int *, int);
void backtrackPermutations(int A[], int k, int n) {
  if (k == n) {
    printPermutation(A, n);
  } else {
    int *candidate = (int *)malloc((n - k) * sizeof(int));
    int numbOfCandidates;
    generateCandidates(A, k, n, candidate, &numbOfCandidates);
    int i;
    for (i = 0; i < numbOfCandidates; i++) {
      A[k] = candidate[i];
      backtrackPermutations(A, k + 1, n);
    }
    free(candidate);
  }
}


x86 assembly (clang, -O0):
<backtrackPermutations>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    117f <backtrackPermutations+0x2f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <printPermutation@plt>
jmp    11ff <backtrackPermutations+0xaf>
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1040 <generateCandidates@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11f6 <backtrackPermutations+0xa6>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
call   1050 <backtrackPermutations@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11b4 <backtrackPermutations+0x64>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backtrackPermutations>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rdi,%rbx
mov    %edx,%edi
sub    %esi,%edi
jne    1180 <backtrackPermutations+0x30>
mov    %rbx,%rdi
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printPermutation@plt>
mov    %esi,%r12d
shl    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
lea    0x4(%rsp),%r8
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1040 <generateCandidates@plt>
cmpl   $0x0,0x4(%rsp)
jle    11e4 <backtrackPermutations+0x94>
movslq %r12d,%r13
add    $0x1,%r12d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15,%rbp,4),%eax
mov    %eax,(%rbx,%r13,4)
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
call   1050 <backtrackPermutations@plt>
add    $0x1,%rbp
movslq 0x4(%rsp),%rax
cmp    %rax,%rbp
jl     11c0 <backtrackPermutations+0x70>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @backtrackPermutations(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = icmp eq i32 %2, %1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @printPermutation(i32* %0, i32 %2)
br label %31
8: ; preds = %3
%9 = sub nsw i32 %2, %1
%10 = shl i32 %9, 2
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i32*
%13 = bitcast i32* %4 to i8*
%14 = call i32 @generateCandidates(i32* %0, i32 %1, i32 %2, i32* %12, i32* %4)
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %8
%18 = sext i32 %1 to i64
%19 = getelementptr inbounds i32, i32* %0, i64 %18
%20 = add nsw i32 %1, 1
br label %21
21: ; preds = %17, %21
%22 = phi i64 [ 0, %17 ], [ %25, %21 ]
%23 = getelementptr inbounds i32, i32* %12, i64 %22
%24 = load i32, i32* %23
store i32 %24, i32* %19
%25 = add nuw nsw i64 %22, 1
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = icmp slt i64 %25, %27
br i1 %28, label %21, label %29
29: ; preds = %21, %8
%30 = call i32 @free(i32* %12)
br label %31
31: ; preds = %29, %6
ret void
}