#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 from_xp(uint8_t *bytes, uint8_t *xp, int offset, int nbits) {
  int bit;
  for (bit = 0; bit < nbits; bit++) {
    int xp_bit_value = (xp[bit / 8] >> (bit % 8)) & 0x01;
    int header_bit = offset + nbits - 1 - bit;
    bytes[header_bit / 8] &= ~(1 << (7 - (header_bit % 8)));
    bytes[header_bit / 8] |= xp_bit_value << (7 - (header_bit % 8));
  }
}


x86 assembly (clang, -O0):
<from_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    1205 <from_xp+0x105>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x48(%rbp),%eax
mov    %edx,%ecx
sar    %cl,%eax
and    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
sub    $0x1,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    $0x7,%ecx
sub    %edx,%ecx
mov    $0x1,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x44(%rbp),%edx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
and    (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x38(%rbp),%eax
mov    $0x7,%ecx
sub    %edx,%ecx
shl    %cl,%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%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 <from_xp+0x19>
pop    %rbp
ret

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

LLVM IR (-O0):
define void @from_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 = %57, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %8
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %60
16: ; preds = %12
%17 = load i32*, i32** %6
%18 = load i32, i32* %9
%19 = sdiv i32 %18, 8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %17, i64 %20
%22 = load i32, i32* %21
%23 = load i32, i32* %9
%24 = srem i32 %23, 8
%25 = ashr i32 %22, %24
%26 = and i32 %25, 1
store i32 %26, i32* %10
%27 = load i32, i32* %7
%28 = load i32, i32* %8
%29 = add nsw i32 %27, %28
%30 = sub nsw i32 %29, 1
%31 = load i32, i32* %9
%32 = sub nsw i32 %30, %31
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = srem i32 %33, 8
%35 = sub nsw i32 7, %34
%36 = shl i32 1, %35
%37 = xor i32 %36, -1
%38 = load i32*, i32** %5
%39 = load i32, i32* %11
%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 = and i32 %43, %37
store i32 %44, i32* %42
%45 = load i32, i32* %10
%46 = load i32, i32* %11
%47 = srem i32 %46, 8
%48 = sub nsw i32 7, %47
%49 = shl i32 %45, %48
%50 = load i32*, i32** %5
%51 = load i32, i32* %11
%52 = sdiv i32 %51, 8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %50, i64 %53
%55 = load i32, i32* %54
%56 = or i32 %55, %49
store i32 %56, i32* %54
br label %57
57: ; preds = %16
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %12
60: ; preds = %12
ret void
}

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