#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 */
unsigned int get_bit(unsigned char *, unsigned long);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, unsigned long);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, unsigned long);
unsigned long numbytes_from_numbits(unsigned long);
void reverse_bits(unsigned char *bits, unsigned long numbits) {
  unsigned long numbytes = numbytes_from_numbits(numbits);
  unsigned char tmp[numbytes];
  unsigned long curbit;
  long curbyte = 0;
  memset(tmp, 0, numbytes);
  for (curbit = 0; curbit < numbits; ++curbit) {
    unsigned int bitpos = curbit % 8;
    if (curbit > 0 && curbit % 8 == 0)
      ++curbyte;
    tmp[curbyte] |= (get_bit(bits, numbits - curbit - 1) << bitpos);
  }
  memcpy(bits, tmp, numbytes);
}


x86 assembly (clang, -O0):
<reverse_bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <numbytes_from_numbits@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    121f <reverse_bits+0xdf>
mov    -0x30(%rbp),%rax
and    $0x7,%rax
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11e1 <reverse_bits+0xa1>
mov    -0x30(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
jne    11e1 <reverse_bits+0xa1>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x30(%rbp),%rsi
sub    $0x1,%rsi
call   1060 <get_bit@plt>
mov    %eax,%esi
mov    -0x48(%rbp),%rax
mov    -0x3c(%rbp),%ecx
shl    %cl,%esi
mov    -0x38(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
or     %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    119f <reverse_bits+0x5f>
mov    -0x48(%rbp),%rsi
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_bits>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %rdi,-0x30(%rbp)
mov    %rsi,%rdi
call   1050 <numbytes_from_numbits@plt>
mov    %rax,%rdx
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
xor    %esi,%esi
mov    %rdx,-0x38(%rbp)
call   1030 <memset@plt>
test   %r13,%r13
je     11ca <reverse_bits+0x8a>
add    $0xffffffffffffffff,%r13
xor    %ebx,%ebx
xor    %r14d,%r14d
mov    %ebx,%r15d
and    $0x7,%r15d
test   %rbx,%rbx
setne  %al
test   %r15,%r15
sete   %cl
and    %al,%cl
movzbl %cl,%eax
add    %rax,%r14
mov    -0x30(%rbp),%rdi
mov    %r13,%rsi
call   1060 <get_bit@plt>
mov    %r15d,%ecx
shl    %cl,%eax
or     %al,(%r12,%r14,1)
add    $0x1,%rbx
add    $0xffffffffffffffff,%r13
jb     1190 <reverse_bits+0x50>
mov    -0x30(%rbp),%rdi
mov    %r12,%rsi
mov    -0x38(%rbp),%rdx
call   1040 <memcpy@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @reverse_bits(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%11 = load i64, i64* %4
%12 = call i64 @numbytes_from_numbits(i64 %11)
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
store i64 0, i64* %9
%16 = load i64, i64* %5
%17 = call i32 @memset(i8* %15, i32 0, i64 %16)
store i64 0, i64* %8
br label %18
18: ; preds = %50, %2
%19 = load i64, i64* %8
%20 = load i64, i64* %4
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %53
22: ; preds = %18
%23 = load i64, i64* %8
%24 = urem i64 %23, 8
%25 = trunc i64 %24 to i32
store i32 %25, i32* %10
%26 = load i64, i64* %8
%27 = icmp ugt i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %22
%29 = load i64, i64* %8
%30 = urem i64 %29, 8
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i64, i64* %9
%34 = add nsw i64 %33, 1
store i64 %34, i64* %9
br label %35
35: ; preds = %32, %28, %22
%36 = load i8*, i8** %3
%37 = load i64, i64* %4
%38 = load i64, i64* %8
%39 = sub i64 %37, %38
%40 = sub i64 %39, 1
%41 = call i32 @get_bit(i8* %36, i64 %40)
%42 = load i32, i32* %10
%43 = shl i32 %41, %42
%44 = load i64, i64* %9
%45 = getelementptr inbounds i8, i8* %15, i64 %44
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = or i32 %47, %43
%49 = trunc i32 %48 to i8
store i8 %49, i8* %45
br label %50
50: ; preds = %35
%51 = load i64, i64* %8
%52 = add i64 %51, 1
store i64 %52, i64* %8
br label %18
53: ; preds = %18
%54 = load i8*, i8** %3
%55 = load i64, i64* %5
%56 = call i32 @memcpy(i8* %54, i8* %15, i64 %55)
%57 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
define void @reverse_bits(i8* %0, i64 %1) {
%3 = tail call i64 @numbytes_from_numbits(i64 %1)
%4 = alloca i8, i64 %3
%5 = call i32 @memset(i8* %4, i32 0, i64 %3)
%6 = icmp eq i64 %1, 0
br i1 %6, label %27, label %7
7: ; preds = %2, %7
%8 = phi i64 [ %25, %7 ], [ 0, %2 ]
%9 = phi i64 [ %16, %7 ], [ 0, %2 ]
%10 = and i64 %8, 7
%11 = trunc i64 %10 to i32
%12 = icmp ne i64 %8, 0
%13 = icmp eq i64 %10, 0
%14 = and i1 %12, %13
%15 = zext i1 %14 to i64
%16 = add nuw nsw i64 %9, %15
%17 = xor i64 %8, -1
%18 = add i64 %17, %1
%19 = call i32 @get_bit(i8* %0, i64 %18)
%20 = shl i32 %19, %11
%21 = getelementptr inbounds i8, i8* %4, i64 %16
%22 = load i8, i8* %21
%23 = trunc i32 %20 to i8
%24 = or i8 %22, %23
store i8 %24, i8* %21
%25 = add nuw i64 %8, 1
%26 = icmp eq i64 %25, %1
br i1 %26, label %27, label %7
27: ; preds = %7, %2
%28 = call i32 @memcpy(i8* %0, i8* %4, i64 %3)
ret void
}