#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*/ printf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ swap(char *, char *);
void permutate(char *str, int index) {
  int i = 0;
  char lastChar = 0;
  if (index == strlen(str)) {
    printf("%s\n", str);
    return;
  }
  for (i = index; i < strlen(str); i++) {
    if (lastChar == str[i]) {
      continue;
    } else {
      lastChar = str[i];
    }
    swap(&str[index], &str[i]);
    permutate(str, index + 1);
    swap(&str[index], &str[i]);
  }
}


x86 assembly (clang, -O0):
<permutate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jne    118b <permutate+0x4b>
mov    -0x8(%rbp),%rsi
lea    0xe7f(%rip),%rdi
call   1050 <printf@plt>
jmp    122b <permutate+0xeb>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    122b <permutate+0xeb>
movsbl -0x11(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x10(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    11ca <permutate+0x8a>
jmp    121d <permutate+0xdd>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
call   1060 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x1,%esi
call   1030 <permutate@plt>
mov    -0x8(%rbp),%rdi
movslq -0xc(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
call   1060 <swap@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1191 <permutate+0x51>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<permutate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r12
call   1040 <strlen@plt>
cmp    %r14d,%eax
jne    1178 <permutate+0x38>
lea    0xe9e(%rip),%rdi
mov    %r12,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    %r14d,%eax
jle    11e7 <permutate+0xa7>
movslq %r14d,%rbx
lea    (%r12,%rbx,1),%r15
add    $0x1,%r14d
xor    %ebp,%ebp
jmp    11b3 <permutate+0x73>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    11e7 <permutate+0xa7>
movzbl (%r12,%rbx,1),%r13d
cmp    %r13b,%bpl
je     11a0 <permutate+0x60>
lea    (%r12,%rbx,1),%rbp
mov    %r15,%rdi
mov    %rbp,%rsi
call   1060 <swap@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1030 <permutate@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1060 <swap@plt>
mov    %r13d,%ebp
jmp    11a0 <permutate+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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