#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*/ heap_permute(char **, int, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ permute(char *, int);
int /*<<< orphan*/ permute_iterative_size_3(char *);
int /*<<< orphan*/ printf(char *);
int main() {
  int size = 4;
  char *str = (char *)malloc(size * sizeof(char));
  char *tmp = "hat";
  int c;
  for (c = 0; c < size; c++)
    str[c] = tmp[c];
  printf("heap permute:\n \n");
  heap_permute(&str, size, size);
  printf(" \n");
  size = 4;
  tmp = "four";
  for (c = 0; c < size; c++)
    str[c] = tmp[c];
  heap_permute(&str, size, size);
  printf("\ndirect:\n");
  size = 4;
  tmp = "0123";
  for (c = 0; c < size; c++)
    str[c] = tmp[c];
  permute(str, size);
  printf("\niterative:\n");
  permute_iterative_size_3("abc");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x4,-0x8(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    0xe80(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11bb <main+0x6b>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118b <main+0x3b>
lea    0xe42(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    -0x10(%rbp),%rdi
call   1040 <heap_permute@plt>
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
movl   $0x4,-0x8(%rbp)
lea    0xe25(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    122b <main+0xdb>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11fb <main+0xab>
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    -0x10(%rbp),%rdi
call   1040 <heap_permute@plt>
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
movl   $0x4,-0x8(%rbp)
lea    0xdd0(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    128f <main+0x13f>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    125f <main+0x10f>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <permute@plt>
lea    0xd87(%rip),%rdi
call   1050 <printf@plt>
lea    0xd88(%rip),%rdi
call   1060 <permute_iterative_size_3@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    $0x4,%edi
call   1070 <malloc@plt>
mov    %rax,0x8(%rsp)
movb   $0x68,(%rax)
mov    0x8(%rsp),%rax
movb   $0x61,0x1(%rax)
mov    0x8(%rsp),%rax
movb   $0x74,0x2(%rax)
mov    0x8(%rsp),%rax
movb   $0x0,0x3(%rax)
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x4,%edx
call   1050 <heap_permute@plt>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rax
movb   $0x66,(%rax)
mov    0x8(%rsp),%rax
movb   $0x6f,0x1(%rax)
mov    0x8(%rsp),%rax
movb   $0x75,0x2(%rax)
mov    0x8(%rsp),%rax
movb   $0x72,0x3(%rax)
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x4,%edx
call   1050 <heap_permute@plt>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rax
movb   $0x30,(%rax)
mov    0x8(%rsp),%rax
movb   $0x31,0x1(%rax)
mov    0x8(%rsp),%rax
movb   $0x32,0x2(%rax)
mov    0x8(%rsp),%rax
movb   $0x33,0x3(%rax)
mov    0x8(%rsp),%rdi
mov    $0x4,%esi
call   1040 <permute@plt>
lea    0xdf2(%rip),%rdi
call   1030 <puts@plt>
lea    0xdc9(%rip),%rdi
call   1060 <permute_iterative_size_3@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str.7 = [4 x i8] c"abc\00"
@str = [16 x i8] c"heap permute:\0A \00"
@str.8 = [2 x i8] c" \00"
@str.9 = [9 x i8] c"\0Adirect:\00"
@str.10 = [12 x i8] c"\0Aiterative:\00"
define i32 @main() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call i64 @malloc(i32 4)
%4 = inttoptr i64 %3 to i8*
store i8* %4, i8** %1
store i8 104, i8* %4
%5 = load i8*, i8** %1
%6 = getelementptr inbounds i8, i8* %5, i64 1
store i8 97, i8* %6
%7 = load i8*, i8** %1
%8 = getelementptr inbounds i8, i8* %7, i64 2
store i8 116, i8* %8
%9 = load i8*, i8** %1
%10 = getelementptr inbounds i8, i8* %9, i64 3
store i8 0, i8* %10
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%12 = call i32 @heap_permute(i8** %1, i32 4, i32 4)
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.8, i64 0, i64 0))
%14 = load i8*, i8** %1
store i8 102, i8* %14
%15 = load i8*, i8** %1
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8 111, i8* %16
%17 = load i8*, i8** %1
%18 = getelementptr inbounds i8, i8* %17, i64 2
store i8 117, i8* %18
%19 = load i8*, i8** %1
%20 = getelementptr inbounds i8, i8* %19, i64 3
store i8 114, i8* %20
%21 = call i32 @heap_permute(i8** %1, i32 4, i32 4)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.9, i64 0, i64 0))
%23 = load i8*, i8** %1
store i8 48, i8* %23
%24 = load i8*, i8** %1
%25 = getelementptr inbounds i8, i8* %24, i64 1
store i8 49, i8* %25
%26 = load i8*, i8** %1
%27 = getelementptr inbounds i8, i8* %26, i64 2
store i8 50, i8* %27
%28 = load i8*, i8** %1
%29 = getelementptr inbounds i8, i8* %28, i64 3
store i8 51, i8* %29
%30 = load i8*, i8** %1
%31 = call i32 @permute(i8* %30, i32 4)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
%33 = call i32 @permute_iterative_size_3(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}