#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 AKMemoryGetBit(unsigned char *, int);
int /*<<< orphan*/ AKMemorySetBit(unsigned char *, int, int);
unsigned char *calloc(int, int);
unsigned char *AKMemoryReverseBytesCopy(unsigned char *in, int len, int step) {
  int numOfBytes = (len - 1) / 8 + 1;
  unsigned char *out = calloc(numOfBytes, sizeof(unsigned char));
  int i;
  for (i = 0; i < len; i++) {
    int val = AKMemoryGetBit(in, (i + step) % len);
    AKMemorySetBit(out, i, val);
  }
  return out;
}


x86 assembly (clang, -O0):
<AKMemoryReverseBytesCopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11af <AKMemoryReverseBytesCopy+0x7f>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    -0x10(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,%esi
call   1030 <AKMemoryGetBit@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1050 <AKMemorySetBit@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    116e <AKMemoryReverseBytesCopy+0x3e>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AKMemoryReverseBytesCopy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%r13d
mov    %rdi,%r15
lea    -0x1(%r13),%eax
lea    0x6(%r13),%edi
test   %eax,%eax
cmovns %eax,%edi
sar    $0x3,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%r12
test   %r13d,%r13d
jle    1196 <AKMemoryReverseBytesCopy+0x66>
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    (%r14,%rbx,1),%eax
cltd
idiv   %r13d
mov    %r15,%rdi
mov    %edx,%esi
call   1030 <AKMemoryGetBit@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <AKMemorySetBit@plt>
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    1170 <AKMemoryReverseBytesCopy+0x40>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @AKMemoryReverseBytesCopy(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %5
%12 = sub nsw i32 %11, 1
%13 = sdiv i32 %12, 8
%14 = add nsw i32 %13, 1
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = call i8* @calloc(i32 %15, i32 1)
store i8* %16, i8** %8
store i32 0, i32* %9
br label %17
17: ; preds = %33, %3
%18 = load i32, i32* %9
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %36
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = load i32, i32* %9
%24 = load i32, i32* %6
%25 = add nsw i32 %23, %24
%26 = load i32, i32* %5
%27 = srem i32 %25, %26
%28 = call i32 @AKMemoryGetBit(i8* %22, i32 %27)
store i32 %28, i32* %10
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = load i32, i32* %10
%32 = call i32 @AKMemorySetBit(i8* %29, i32 %30, i32 %31)
br label %33
33: ; preds = %21
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %17
36: ; preds = %17
%37 = load i8*, i8** %8
ret i8* %37
}

LLVM IR (-O2):
define i8* @AKMemoryReverseBytesCopy(i8* %0, i32 %1, i32 %2) {
%4 = add nsw i32 %1, -1
%5 = sdiv i32 %4, 8
%6 = add nsw i32 %5, 1
%7 = tail call i8* @calloc(i32 %6, i32 1)
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %17
9: ; preds = %3, %9
%10 = phi i32 [ %15, %9 ], [ 0, %3 ]
%11 = add nsw i32 %10, %2
%12 = srem i32 %11, %1
%13 = tail call i32 @AKMemoryGetBit(i8* %0, i32 %12)
%14 = tail call i32 @AKMemorySetBit(i8* %7, i32 %10, i32 %13)
%15 = add nuw nsw i32 %10, 1
%16 = icmp eq i32 %15, %1
br i1 %16, label %17, label %9
17: ; preds = %9, %3
ret i8* %7
}