#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 *);
int /*<<< orphan*/ printf(char *, char *);
char *remove_at(char *, unsigned int);
unsigned int strlen(char *);
void permute(char *cs, char *buf, int last) {
  if (strlen(cs) == 0) {
    buf[last] = '\0';
    printf("%s\n", buf);
  } else {
    for (unsigned i = 0; i < strlen(cs); i++) {
      buf[last] = cs[i];
      char *rest = remove_at(cs, i);
      permute(rest, buf, last + 1);
      free(rest);
    }
  }
}


x86 assembly (clang, -O0):
<permute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jne    1196 <permute+0x46>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1060 <printf@plt>
jmp    120d <permute+0xbd>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jae    1208 <permute+0xb8>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1070 <remove_at@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1040 <permute@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119d <permute+0x4d>
jmp    120d <permute+0xbd>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<permute>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
test   %eax,%eax
je     11de <permute+0x8e>
mov    %r12,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
je     11cf <permute+0x7f>
movslq %ebp,%r13
lea    0x1(%r13),%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%rbp,1),%eax
mov    %al,(%r14,%r13,1)
mov    %r12,%rdi
mov    %ebp,%esi
call   1070 <remove_at@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <permute@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1,%rbp
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbp
jb     1190 <permute+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
lea    0xe13(%rip),%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>

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

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