#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*/ bit_get(unsigned char *, int const);
int /*<<< orphan*/ bit_set(unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void permute(unsigned char *bits, const int *mapping, int n) {
  unsigned char temp[8];
  int i;
  memset(temp, 0, (int)(n / 8));
  for (i = 0; i < n; i++)
    bit_set(temp, i, bit_get(bits, mapping[i] - 1));
  memcpy(bits, temp, (int)(n / 8));
  return;
}


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)
lea    -0x1c(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11bf <permute+0x7f>
lea    -0x1c(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
sub    $0x1,%esi
call   1030 <bit_get@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
call   1060 <bit_set@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1172 <permute+0x32>
mov    -0x8(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1050 <memcpy@plt>
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,%r15
mov    %rdi,%r13
lea    0x7(%rbp),%r14d
test   %edx,%edx
cmovns %edx,%r14d
sar    $0x3,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <memset@plt>
test   %ebp,%ebp
jle    11a4 <permute+0x64>
mov    %ebp,%ebx
xor    %ebp,%ebp
mov    %rsp,%r12
nopl   0x0(%rax)
mov    (%r15,%rbp,4),%esi
add    $0xffffffff,%esi
mov    %r13,%rdi
call   1030 <bit_get@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <bit_set@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1180 <permute+0x40>
mov    %rsp,%rsi
mov    %r13,%rdi
mov    %r14d,%edx
call   1050 <memcpy@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @permute(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [8 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* %6
%11 = sdiv i32 %10, 8
%12 = call i32 @memset(i8* %9, i32 0, i32 %11)
store i32 0, i32* %8
br label %13
13: ; preds = %29, %3
%14 = load i32, i32* %8
%15 = load i32, i32* %6
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %13
%18 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* %8
%20 = load i8*, i8** %4
%21 = load i32*, i32** %5
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = sub nsw i32 %25, 1
%27 = call i32 @bit_get(i8* %20, i32 %26)
%28 = call i32 @bit_set(i8* %18, i32 %19, i32 %27)
br label %29
29: ; preds = %17
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
br label %13
32: ; preds = %13
%33 = load i8*, i8** %4
%34 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%35 = load i32, i32* %6
%36 = sdiv i32 %35, 8
%37 = call i32 @memcpy(i8* %33, i8* %34, i32 %36)
ret void
}

LLVM IR (-O2):
define void @permute(i8* %0, i32* nocapture %1, i32 %2) {
%4 = alloca [8 x i8]
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%6 = sdiv i32 %2, 8
%7 = call i32 @memset(i8* %5, i32 0, i32 %6)
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %21
9: ; preds = %3
%10 = zext i32 %2 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ 0, %9 ], [ %19, %11 ]
%13 = getelementptr inbounds i32, i32* %1, i64 %12
%14 = load i32, i32* %13
%15 = add nsw i32 %14, -1
%16 = call i32 @bit_get(i8* %0, i32 %15)
%17 = trunc i64 %12 to i32
%18 = call i32 @bit_set(i8* %5, i32 %17, i32 %16)
%19 = add nuw nsw i64 %12, 1
%20 = icmp eq i64 %19, %10
br i1 %20, label %21, label %11
21: ; preds = %11, %3
%22 = call i32 @memcpy(i8* %0, i8* %5, i32 %6)
ret void
}