#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(char *);
char *malloc(int);
int /*<<< orphan*/ save_perm(int);
int /*<<< orphan*/ strncat(char *, char, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
permu(char str[], int len) {
  int i;
  char *str1;
  str1 = malloc(len);
  for (i = 0; i < len; i++) {
    strncpy(str1, str, i);
    strncat(str1, str[i + 1], len - i - 1);
    save_perm(str[i] + permu(str1, len - 1));
  }
  free(str1);
}


x86 assembly (clang, -O0):
<permu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f9 <permu+0x99>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
sub    $0x1,%edx
movsbl %al,%esi
call   1060 <strncat@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
call   1080 <permu@plt>
mov    -0x24(%rbp),%edi
add    %eax,%edi
call   1050 <save_perm@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1182 <permu+0x22>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<permu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    %esi,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    11c9 <permu+0x69>
mov    %r14d,%r13d
add    $0xffffffff,%r14d
xor    %ebx,%ebx
mov    %r14d,%ebp
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
movsbl 0x1(%r15,%rbx,1),%esi
add    $0x1,%rbx
mov    %r12,%rdi
mov    %ebp,%edx
call   1060 <strncat@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1080 <permu@plt>
call   1050 <save_perm@plt>
add    $0xffffffff,%ebp
cmp    %rbx,%r13
jne    1190 <permu+0x30>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

LLVM IR (-O2):
define i32 @permu(i8* %0, i32 %1) {
%3 = tail call i8* @malloc(i32 %1)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %21
5: ; preds = %2
%6 = add nsw i32 %1, -1
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %5, %8
%9 = phi i64 [ 0, %5 ], [ %12, %8 ]
%10 = trunc i64 %9 to i32
%11 = tail call i32 @strncpy(i8* %3, i8* %0, i32 %10)
%12 = add nuw nsw i64 %9, 1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = xor i32 %10, -1
%16 = add i32 %15, %1
%17 = tail call i32 @strncat(i8* %3, i8 signext %14, i32 %16)
%18 = tail call i32 @permu(i8* %3, i32 %6)
%19 = tail call i32 @save_perm(i32 undef)
%20 = icmp eq i64 %12, %7
br i1 %20, label %21, label %8
21: ; preds = %8, %2
%22 = tail call i32 @free(i8* %3)
ret i32 undef
}