#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 */
typedef int uint8_t;

/* Variables and functions */
void to_xp(uint8_t *bytes, uint8_t *xp, int offset, int nbits) {
  int bit;
  for (bit = 0; bit < nbits; bit++) {
    int header_bit = offset + nbits - 1 - bit;
    int header_bit_value =
        (bytes[header_bit / 8] >> (7 - (header_bit % 8))) & 0x01;
    xp[bit / 8] |= header_bit_value << (bit % 8);
  }
}


x86 assembly (clang, -O0):
<to_xp>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11c1 <to_xp+0xc1>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
sub    $0x1,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x3c(%rbp),%eax
mov    $0x7,%ecx
sub    %edx,%ecx
sar    %cl,%eax
and    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%eax
mov    %edx,%ecx
shl    %cl,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
or     (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1119 <to_xp+0x19>
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_xp>:
test   %ecx,%ecx
jle    116a <to_xp+0x6a>
mov    %ecx,%r8d
mov    %ecx,%eax
neg    %r8d
lea    (%rax,%rdx,1),%r10d
add    $0xffffffff,%r10d
xor    %r9d,%r9d
nopw   0x0(%rax,%rax,1)
lea    0x7(%r10),%eax
test   %r10d,%r10d
cmovns %r10d,%eax
mov    %eax,%ecx
sar    $0x3,%ecx
movslq %ecx,%rcx
mov    (%rdi,%rcx,4),%edx
and    $0xfffffff8,%eax
mov    %r10d,%r11d
sub    %eax,%r11d
mov    $0x7,%cl
sub    %r11b,%cl
shr    %cl,%edx
and    $0x1,%edx
mov    %r9d,%ecx
and    $0x7,%cl
shl    %cl,%edx
mov    %r9d,%eax
shr    $0x3,%eax
or     %edx,(%rsi,%rax,4)
add    $0x1,%r9d
add    $0xffffffff,%r10d
mov    %r8d,%eax
add    %r9d,%eax
jne    1120 <to_xp+0x20>
ret

LLVM IR (-O0):
define void @to_xp(i32* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %12
12: ; preds = %45, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %8
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %48
16: ; preds = %12
%17 = load i32, i32* %7
%18 = load i32, i32* %8
%19 = add nsw i32 %17, %18
%20 = sub nsw i32 %19, 1
%21 = load i32, i32* %9
%22 = sub nsw i32 %20, %21
store i32 %22, i32* %10
%23 = load i32*, i32** %5
%24 = load i32, i32* %10
%25 = sdiv i32 %24, 8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %23, i64 %26
%28 = load i32, i32* %27
%29 = load i32, i32* %10
%30 = srem i32 %29, 8
%31 = sub nsw i32 7, %30
%32 = ashr i32 %28, %31
%33 = and i32 %32, 1
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = load i32, i32* %9
%36 = srem i32 %35, 8
%37 = shl i32 %34, %36
%38 = load i32*, i32** %6
%39 = load i32, i32* %9
%40 = sdiv i32 %39, 8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %38, i64 %41
%43 = load i32, i32* %42
%44 = or i32 %43, %37
store i32 %44, i32* %42
br label %45
45: ; preds = %16
%46 = load i32, i32* %9
%47 = add nsw i32 %46, 1
store i32 %47, i32* %9
br label %12
48: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @to_xp(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %29
6: ; preds = %4
%7 = add nsw i32 %3, %2
br label %8
8: ; preds = %6, %8
%9 = phi i32 [ 0, %6 ], [ %27, %8 ]
%10 = xor i32 %9, -1
%11 = add i32 %7, %10
%12 = sdiv i32 %11, 8
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = srem i32 %11, 8
%17 = sub nsw i32 7, %16
%18 = lshr i32 %15, %17
%19 = and i32 %18, 1
%20 = and i32 %9, 7
%21 = shl nuw nsw i32 %19, %20
%22 = lshr i32 %9, 3
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %1, i64 %23
%25 = load i32, i32* %24
%26 = or i32 %21, %25
store i32 %26, i32* %24
%27 = add nuw nsw i32 %9, 1
%28 = icmp eq i32 %27, %3
br i1 %28, label %29, label %8
29: ; preds = %8, %4
ret void
}