#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*/ memset(char *, char, int);
int /*<<< orphan*/ str_concat(char *, char *, char);
int /*<<< orphan*/ str_shift(char *, char *, int);
int strlen(char *);
void _permutate(char *prefix, char *str, int max_len,
                void (*callback)(char *, void *), void *data) {
  int len = strlen(str);
  if (len == 0) {
    callback(prefix, data);
  } else {
    for (int i = 0; i < len; i++) {
      char new_prefix[max_len];
      char new_str[max_len];
      memset(new_prefix, '\0', max_len);
      memset(new_str, '\0', max_len);
      str_concat(new_prefix, prefix, str[i]);
      str_shift(new_str, str, i);
      _permutate(new_prefix, new_str, max_len, callback, data);
    }
  }
}


x86 assembly (clang, -O0):
<_permutate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1194 <_permutate+0x44>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
jmp    125f <_permutate+0x10f>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    125a <_permutate+0x10a>
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x58(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
call   1070 <str_concat@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1060 <str_shift@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1030 <_permutate@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    119b <_permutate+0x4b>
jmp    125f <_permutate+0x10f>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_permutate>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,%r14
mov    %rcx,-0x30(%rbp)
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     1236 <_permutate+0xe6>
mov    %r15,-0x58(%rbp)
mov    %rbx,-0x50(%rbp)
jle    1227 <_permutate+0xd7>
mov    %r12d,%ecx
mov    %eax,%eax
mov    %rax,-0x38(%rbp)
xor    %ebx,%ebx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rcx,-0x40(%rbp)
mov    %r14,-0x48(%rbp)
mov    -0x50(%rbp),%r13
xchg   %ax,%ax
mov    %rsp,-0x60(%rbp)
mov    %rsp,%r14
mov    -0x40(%rbp),%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    %r14,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
movsbl 0x0(%r13,%rbx,1),%edx
mov    %r14,%rdi
mov    -0x58(%rbp),%rsi
call   1070 <str_concat@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1060 <str_shift@plt>
mov    %r14,%rdi
mov    -0x48(%rbp),%r8
mov    %r15,%rsi
mov    %r12d,%edx
mov    -0x30(%rbp),%rcx
call   1030 <_permutate@plt>
mov    -0x60(%rbp),%rsp
add    $0x1,%rbx
cmp    %rbx,-0x38(%rbp)
jne    11b0 <_permutate+0x60>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
mov    %r14,%rsi
mov    -0x30(%rbp),%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    *%rax

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

LLVM IR (-O2):
define void @_permutate(i8* %0, i8* %1, i32 %2, void (i8*, i8*)* nocapture %3, i8* %4) {
%6 = tail call i32 @strlen(i8* %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = icmp sgt i32 %6, 0
br i1 %9, label %10, label %28
10: ; preds = %8
%11 = zext i32 %2 to i64
%12 = zext i32 %6 to i64
br label %14
13: ; preds = %5
tail call void %3(i8* %0, i8* %4)
br label %28
14: ; preds = %10, %14
%15 = phi i64 [ 0, %10 ], [ %26, %14 ]
%16 = call i8* @llvm.stacksave()
%17 = alloca i8, i64 %11
%18 = alloca i8, i64 %11
%19 = call i32 @memset(i8* %17, i8 signext 0, i32 %2)
%20 = call i32 @memset(i8* %18, i8 signext 0, i32 %2)
%21 = getelementptr inbounds i8, i8* %1, i64 %15
%22 = load i8, i8* %21
%23 = call i32 @str_concat(i8* %17, i8* %0, i8 signext %22)
%24 = trunc i64 %15 to i32
%25 = call i32 @str_shift(i8* %18, i8* %1, i32 %24)
%26 = add nuw nsw i64 %15, 1
%27 = icmp eq i64 %26, %12
br i1 %27, label %28, label %14
28: ; preds = %14, %8, %13
ret void
}