#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*/ agile_bit_get(unsigned char *, int const);
int /*<<< orphan*/ agile_bit_set(unsigned char *, int, int /*<<< orphan*/);
scalar_t__ ceil(int);
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)ceil(n / 8));
  for (i = 0; i < n; ++i)
    agile_bit_set(temp, i, agile_bit_get(bits, mapping[i] - 1));
  memcpy(bits, temp, (int)ceil(n / 8));
}


x86 assembly (clang, -O0):
<permute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x1c(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <ceil@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11de <permute+0x8e>
lea    -0x1c(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
sub    $0x1,%esi
call   1060 <agile_bit_get@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
call   1070 <agile_bit_set@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1191 <permute+0x41>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x1c(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edi
call   1030 <ceil@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
add    $0x50,%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,%r13
mov    %rdi,%r14
lea    0x7(%rbp),%r15d
test   %edx,%edx
cmovns %edx,%r15d
sar    $0x3,%r15d
mov    %r15d,%edi
call   1030 <ceil@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1040 <memset@plt>
test   %ebp,%ebp
jle    11b5 <permute+0x65>
mov    %ebp,%ebx
xor    %ebp,%ebp
mov    %rsp,%r12
mov    0x0(%r13,%rbp,4),%esi
add    $0xffffffff,%esi
mov    %r14,%rdi
call   1060 <agile_bit_get@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1070 <agile_bit_set@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1190 <permute+0x40>
mov    %r15d,%edi
call   1030 <ceil@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    %eax,%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 i64 @ceil(i32 %11)
%13 = trunc i64 %12 to i32
%14 = call i32 @memset(i8* %9, i32 0, i32 %13)
store i32 0, i32* %8
br label %15
15: ; preds = %31, %3
%16 = load i32, i32* %8
%17 = load i32, i32* %6
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* %8
%22 = load i8*, i8** %4
%23 = load i32*, i32** %5
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = sub nsw i32 %27, 1
%29 = call i32 @agile_bit_get(i8* %22, i32 %28)
%30 = call i32 @agile_bit_set(i8* %20, i32 %21, i32 %29)
br label %31
31: ; preds = %19
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %15
34: ; preds = %15
%35 = load i8*, i8** %4
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %6
%38 = sdiv i32 %37, 8
%39 = call i64 @ceil(i32 %38)
%40 = trunc i64 %39 to i32
%41 = call i32 @memcpy(i8* %35, i8* %36, i32 %40)
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 = tail call i64 @ceil(i32 %6)
%8 = trunc i64 %7 to i32
%9 = call i32 @memset(i8* %5, i32 0, i32 %8)
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %23
11: ; preds = %3
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ 0, %11 ], [ %21, %13 ]
%15 = getelementptr inbounds i32, i32* %1, i64 %14
%16 = load i32, i32* %15
%17 = add nsw i32 %16, -1
%18 = call i32 @agile_bit_get(i8* %0, i32 %17)
%19 = trunc i64 %14 to i32
%20 = call i32 @agile_bit_set(i8* %5, i32 %19, i32 %18)
%21 = add nuw nsw i64 %14, 1
%22 = icmp eq i64 %21, %12
br i1 %22, label %23, label %13
23: ; preds = %13, %3
%24 = call i64 @ceil(i32 %6)
%25 = trunc i64 %24 to i32
%26 = call i32 @memcpy(i8* %0, i8* %5, i32 %25)
ret void
}