#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 /*<<< orphan*/ swap(char *, char *);
void generate_permutations(char *a, int n) {
  if (n == 0) {
    printf("%s\n", a);
  } else {
    for (int i = 0; i < n; i++) {
      generate_permutations(a, n - 1);
      swap(&a[n % 2 == 0 ? 0 : i], &a[n]);
    }
    generate_permutations(a, n - 1);
  }
}


x86 assembly (clang, -O0):
<generate_permutations>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    115e <generate_permutations+0x2e>
mov    -0x8(%rbp),%rsi
lea    0xeac(%rip),%rdi
call   1030 <printf@plt>
jmp    11e5 <generate_permutations+0xb5>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d6 <generate_permutations+0xa6>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1040 <generate_permutations@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11a6 <generate_permutations+0x76>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ac <generate_permutations+0x7c>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
call   1050 <swap@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1165 <generate_permutations+0x35>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1040 <generate_permutations@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_permutations>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
test   %esi,%esi
je     119d <generate_permutations+0x6d>
movslq %esi,%r13
jmp    1158 <generate_permutations+0x28>
nopw   0x0(%rax,%rax,1)
mov    %r14,%r13
test   %r14d,%r14d
je     119d <generate_permutations+0x6d>
lea    -0x1(%r13),%r14
test   %r13,%r13
jle    1150 <generate_permutations+0x20>
mov    %r13,%r15
add    %r12,%r15
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r13d,%ebx
and    $0x1,%ebx
neg    %ebx
mov    %r12,%rdi
mov    %r14d,%esi
call   1040 <generate_permutations@plt>
and    %ebp,%ebx
add    %r12,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <swap@plt>
add    $0x1,%ebp
cmp    %ebp,%r13d
jne    1170 <generate_permutations+0x40>
jmp    1150 <generate_permutations+0x20>
lea    0xe5c(%rip),%rdi
mov    %r12,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

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

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