#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 */
scalar_t__ checkThePositions(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ swap(int *, int, int);
void permute(int *mat, int index, int len, int *count) {
  if (index == len) {
    if (checkThePositions(mat, len)) {
      *count = *count + 1;
      for (int i = 0; i < len; i++)
        printf("%d, ", mat[i]);
      printf("\n");
    }
    return;
  }
  for (int i = index; i < len; i++) {
    swap(mat, i, index);
    permute(mat, index + 1, len, count);
    swap(mat, i, index);
  }
}


x86 assembly (clang, -O0):
<permute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jne    11d4 <permute+0x94>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <checkThePositions@plt>
cmp    $0x0,%rax
je     11cf <permute+0x8f>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11c1 <permute+0x81>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118e <permute+0x4e>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1228 <permute+0xe8>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1228 <permute+0xe8>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1060 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1030 <permute@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1060 <swap@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11da <permute+0x9a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<permute>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %esi,%r14d
mov    %rdi,%rbx
cmp    %edx,%esi
jne    11ce <permute+0x7e>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <checkThePositions@plt>
test   %rax,%rax
je     1213 <permute+0xc3>
addl   $0x1,(%r15)
test   %r14d,%r14d
jle    11b6 <permute+0x66>
mov    %r14d,%r15d
lea    0xe71(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    11a0 <permute+0x50>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
jge    1213 <permute+0xc3>
mov    %edx,%r13d
lea    0x1(%r14),%r12d
mov    %r14d,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1070 <swap@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r13d,%edx
mov    %r15,%rcx
call   1040 <permute@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1070 <swap@plt>
add    $0x1,%ebp
cmp    %ebp,%r13d
jne    11e0 <permute+0x90>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%d, \00"
@.str.1 = [2 x i8] c"\0A\00"
define void @permute(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** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %41
14: ; preds = %4
%15 = load i32*, i32** %5
%16 = load i32, i32* %7
%17 = call i64 @checkThePositions(i32* %15, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %40
19: ; preds = %14
%20 = load i32*, i32** %8
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
%23 = load i32*, i32** %8
store i32 %22, i32* %23
store i32 0, i32* %9
br label %24
24: ; preds = %35, %19
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load i32*, i32** %5
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %28
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %24
38: ; preds = %24
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %14
br label %64
41: ; preds = %4
%42 = load i32, i32* %6
store i32 %42, i32* %10
br label %43
43: ; preds = %61, %41
%44 = load i32, i32* %10
%45 = load i32, i32* %7
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %64
47: ; preds = %43
%48 = load i32*, i32** %5
%49 = load i32, i32* %10
%50 = load i32, i32* %6
%51 = call i32 @swap(i32* %48, i32 %49, i32 %50)
%52 = load i32*, i32** %5
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
%55 = load i32, i32* %7
%56 = load i32*, i32** %8
%57 = load i32*, i32** %5
%58 = load i32, i32* %10
%59 = load i32, i32* %6
%60 = call i32 @swap(i32* %57, i32 %58, i32 %59)
br label %61
61: ; preds = %47
%62 = load i32, i32* %10
%63 = add nsw i32 %62, 1
store i32 %63, i32* %10
br label %43
64: ; preds = %40, %43
ret void
}

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