#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;
typedef int uint32_t;

/* Variables and functions */
void bit_pack(uint32_t value, uint8_t **bits, int nof_bits) {
  int i;
  for (i = 0; i < nof_bits; i++) {
    (*bits)[i] = (value >> (nof_bits - i - 1)) & 0x1;
  }
  *bits += nof_bits;
}


x86 assembly (clang, -O0):
<bit_pack>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    114e <bit_pack+0x4e>
mov    -0x4(%rbp),%edx
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
sub    $0x1,%ecx
sar    %cl,%edx
and    $0x1,%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1115 <bit_pack+0x15>
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<bit_pack>:
push   %rbx
mov    (%rsi),%r9
test   %edx,%edx
jle    1417 <bit_pack+0x317>
mov    %edx,%r8d
cmp    $0x8,%edx
jae    111b <bit_pack+0x1b>
xor    %ebx,%ebx
jmp    13f7 <bit_pack+0x2f7>
mov    %r8d,%ebx
and    $0xfffffff8,%ebx
movd   %edx,%xmm0
pshufd $0x0,%xmm0,%xmm13
movd   %edi,%xmm0
pshufd $0x0,%xmm0,%xmm8
lea    -0x8(%rbx),%rcx
mov    %rcx,%r10
shr    $0x3,%r10
add    $0x1,%r10
test   %rcx,%rcx
je     1423 <bit_pack+0x323>
mov    %r10,%r11
and    $0xfffffffffffffffe,%r11
movdqa 0xea3(%rip),%xmm15
xor    %ecx,%ecx
movdqa 0xea8(%rip),%xmm9
movaps 0xeb0(%rip),%xmm14
movdqa 0xeb7(%rip),%xmm10
movdqa 0xebe(%rip),%xmm11
movdqa 0xec5(%rip),%xmm12
nopl   0x0(%rax,%rax,1)
movdqa %xmm15,%xmm2
pxor   0xec3(%rip),%xmm2
paddd  %xmm13,%xmm2
movdqa %xmm13,%xmm4
pshuflw $0xfe,%xmm2,%xmm0
movdqa %xmm8,%xmm6
psrld  %xmm0,%xmm6
psubd  %xmm15,%xmm4
movdqa %xmm4,%xmm1
pshuflw $0x54,%xmm2,%xmm5
movdqa %xmm8,%xmm0
psrld  %xmm5,%xmm0
pshufd $0xee,%xmm2,%xmm2
pshuflw $0xfe,%xmm2,%xmm5
movdqa %xmm8,%xmm3
psrld  %xmm5,%xmm3
paddd  %xmm9,%xmm1
pshuflw $0x54,%xmm2,%xmm2
movdqa %xmm8,%xmm5
psrld  %xmm2,%xmm5
punpcklqdq %xmm6,%xmm0
pshuflw $0xfe,%xmm1,%xmm2
movdqa %xmm8,%xmm6
psrld  %xmm2,%xmm6
punpckhqdq %xmm3,%xmm5
pshuflw $0x54,%xmm1,%xmm2
movdqa %xmm8,%xmm3
psrld  %xmm2,%xmm3
pshufd $0xee,%xmm1,%xmm1
pshuflw $0xfe,%xmm1,%xmm2
movdqa %xmm8,%xmm7
psrld  %xmm2,%xmm7
shufps $0xcc,%xmm5,%xmm0
pshuflw $0x54,%xmm1,%xmm1
movdqa %xmm8,%xmm2
psrld  %xmm1,%xmm2
punpcklqdq %xmm6,%xmm3
punpckhqdq %xmm7,%xmm2
shufps $0xcc,%xmm2,%xmm3
andps  %xmm14,%xmm0
andps  %xmm14,%xmm3
movups %xmm0,(%r9,%rcx,4)
movups %xmm3,0x10(%r9,%rcx,4)
movdqa %xmm4,%xmm1
paddd  %xmm10,%xmm1
paddd  %xmm11,%xmm4
pshuflw $0xfe,%xmm1,%xmm0
movdqa %xmm8,%xmm2
psrld  %xmm0,%xmm2
pshuflw $0x54,%xmm1,%xmm3
movdqa %xmm8,%xmm0
psrld  %xmm3,%xmm0
punpcklqdq %xmm2,%xmm0
pshufd $0xee,%xmm1,%xmm1
pshuflw $0xfe,%xmm1,%xmm2
movdqa %xmm8,%xmm3
psrld  %xmm2,%xmm3
pshuflw $0x54,%xmm1,%xmm1
movdqa %xmm8,%xmm2
psrld  %xmm1,%xmm2
punpckhqdq %xmm3,%xmm2
shufps $0xcc,%xmm2,%xmm0
pshuflw $0xfe,%xmm4,%xmm1
movdqa %xmm8,%xmm2
psrld  %xmm1,%xmm2
pshuflw $0x54,%xmm4,%xmm1
movdqa %xmm8,%xmm3
psrld  %xmm1,%xmm3
punpcklqdq %xmm2,%xmm3
pshufd $0xee,%xmm4,%xmm1
pshuflw $0xfe,%xmm1,%xmm2
movdqa %xmm8,%xmm4
psrld  %xmm2,%xmm4
pshuflw $0x54,%xmm1,%xmm1
movdqa %xmm8,%xmm2
psrld  %xmm1,%xmm2
punpckhqdq %xmm4,%xmm2
shufps $0xcc,%xmm2,%xmm3
andps  %xmm14,%xmm0
andps  %xmm14,%xmm3
movups %xmm0,0x20(%r9,%rcx,4)
movups %xmm3,0x30(%r9,%rcx,4)
add    $0x10,%rcx
paddd  %xmm12,%xmm15
add    $0xfffffffffffffffe,%r11
jne    1190 <bit_pack+0x90>
test   $0x1,%r10b
je     13f2 <bit_pack+0x2f2>
pcmpeqd %xmm0,%xmm0
pxor   %xmm15,%xmm0
paddd  %xmm13,%xmm0
psubd  %xmm15,%xmm13
paddd  0xcc9(%rip),%xmm13
pshuflw $0xfe,%xmm0,%xmm1
movdqa %xmm8,%xmm2
psrld  %xmm1,%xmm2
pshuflw $0x54,%xmm0,%xmm3
movdqa %xmm8,%xmm1
psrld  %xmm3,%xmm1
pshufd $0xee,%xmm0,%xmm0
pshuflw $0xfe,%xmm0,%xmm3
movdqa %xmm8,%xmm4
psrld  %xmm3,%xmm4
pshuflw $0x54,%xmm0,%xmm0
movdqa %xmm8,%xmm3
psrld  %xmm0,%xmm3
punpcklqdq %xmm2,%xmm1
pshuflw $0xfe,%xmm13,%xmm0
movdqa %xmm8,%xmm2
psrld  %xmm0,%xmm2
punpckhqdq %xmm4,%xmm3
pshuflw $0x54,%xmm13,%xmm0
movdqa %xmm8,%xmm4
psrld  %xmm0,%xmm4
pshufd $0xee,%xmm13,%xmm0
pshuflw $0xfe,%xmm0,%xmm5
movdqa %xmm8,%xmm6
psrld  %xmm5,%xmm6
shufps $0xcc,%xmm3,%xmm1
pshuflw $0x54,%xmm0,%xmm0
psrld  %xmm0,%xmm8
punpcklqdq %xmm2,%xmm4
punpckhqdq %xmm6,%xmm8
shufps $0xcc,%xmm8,%xmm4
movaps 0xc3f(%rip),%xmm0
andps  %xmm0,%xmm1
andps  %xmm0,%xmm4
movups %xmm1,(%r9,%rcx,4)
movups %xmm4,0x10(%r9,%rcx,4)
cmp    %r8,%rbx
je     1417 <bit_pack+0x317>
mov    %ebx,%ecx
not    %ecx
add    %edx,%ecx
nopl   (%rax)
mov    %edi,%eax
shr    %cl,%eax
and    $0x1,%eax
mov    %eax,(%r9,%rbx,4)
add    $0x1,%rbx
add    $0xffffffff,%ecx
cmp    %rbx,%r8
jne    1400 <bit_pack+0x300>
movslq %edx,%rax
lea    (%r9,%rax,4),%rax
mov    %rax,(%rsi)
pop    %rbx
ret
movdqa 0xbd4(%rip),%xmm15
xor    %ecx,%ecx
test   $0x1,%r10b
jne    132b <bit_pack+0x22b>
jmp    13f2 <bit_pack+0x2f2>

LLVM IR (-O0):
define void @bit_pack(i32 %0, i32** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32** %1, i32*** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %25, %3
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = load i32, i32* %4
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = sub nsw i32 %14, %15
%17 = sub nsw i32 %16, 1
%18 = ashr i32 %13, %17
%19 = and i32 %18, 1
%20 = load i32**, i32*** %5
%21 = load i32*, i32** %20
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
store i32 %19, i32* %24
br label %25
25: ; preds = %12
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
store i32 %27, i32* %7
br label %8
28: ; preds = %8
%29 = load i32, i32* %6
%30 = load i32**, i32*** %5
%31 = load i32*, i32** %30
%32 = sext i32 %29 to i64
%33 = getelementptr inbounds i32, i32* %31, i64 %32
store i32* %33, i32** %30
ret void
}

LLVM IR (-O2):
define void @bit_pack(i32 %0, i32** nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
%5 = load i32*, i32** %1
br i1 %4, label %6, label %90
6: ; preds = %3
%7 = zext i32 %2 to i64
%8 = icmp ult i32 %2, 8
br i1 %8, label %78, label %9
9: ; preds = %6
%10 = and i64 %7, 4294967288
%11 = insertelement <4 x i32> poison, i32 %2, i64 0
%12 = shufflevector <4 x i32> %11, <4 x i32> poison, <4 x i32> zeroinitializer
%13 = insertelement <4 x i32> poison, i32 %2, i64 0
%14 = shufflevector <4 x i32> %13, <4 x i32> poison, <4 x i32> zeroinitializer
%15 = insertelement <4 x i32> poison, i32 %0, i64 0
%16 = shufflevector <4 x i32> %15, <4 x i32> poison, <4 x i32> zeroinitializer
%17 = insertelement <4 x i32> poison, i32 %0, i64 0
%18 = shufflevector <4 x i32> %17, <4 x i32> poison, <4 x i32> zeroinitializer
%19 = add nsw i64 %10, -8
%20 = lshr exact i64 %19, 3
%21 = add nuw nsw i64 %20, 1
%22 = and i64 %21, 1
%23 = icmp eq i64 %19, 0
br i1 %23, label %59, label %24
24: ; preds = %9
%25 = and i64 %21, 4611686018427387902
br label %26
26: ; preds = %26, %24
%27 = phi i64 [ 0, %24 ], [ %55, %26 ]
%28 = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %24 ], [ %56, %26 ]
%29 = phi i64 [ 0, %24 ], [ %57, %26 ]
%30 = xor <4 x i32> %28, <i32 -1, i32 -1, i32 -1, i32 -1>
%31 = sub <4 x i32> <i32 -5, i32 -5, i32 -5, i32 -5>, %28
%32 = add <4 x i32> %12, %30
%33 = add <4 x i32> %31, %14
%34 = lshr <4 x i32> %16, %32
%35 = lshr <4 x i32> %18, %33
%36 = and <4 x i32> %34, <i32 1, i32 1, i32 1, i32 1>
%37 = and <4 x i32> %35, <i32 1, i32 1, i32 1, i32 1>
%38 = getelementptr inbounds i32, i32* %5, i64 %27
%39 = bitcast i32* %38 to <4 x i32>*
store <4 x i32> %36, <4 x i32>* %39
%40 = getelementptr inbounds i32, i32* %38, i64 4
%41 = bitcast i32* %40 to <4 x i32>*
store <4 x i32> %37, <4 x i32>* %41
%42 = or i64 %27, 8
%43 = sub <4 x i32> <i32 -9, i32 -9, i32 -9, i32 -9>, %28
%44 = sub <4 x i32> <i32 -13, i32 -13, i32 -13, i32 -13>, %28
%45 = add <4 x i32> %12, %43
%46 = add <4 x i32> %44, %14
%47 = lshr <4 x i32> %16, %45
%48 = lshr <4 x i32> %18, %46
%49 = and <4 x i32> %47, <i32 1, i32 1, i32 1, i32 1>
%50 = and <4 x i32> %48, <i32 1, i32 1, i32 1, i32 1>
%51 = getelementptr inbounds i32, i32* %5, i64 %42
%52 = bitcast i32* %51 to <4 x i32>*
store <4 x i32> %49, <4 x i32>* %52
%53 = getelementptr inbounds i32, i32* %51, i64 4
%54 = bitcast i32* %53 to <4 x i32>*
store <4 x i32> %50, <4 x i32>* %54
%55 = add nuw i64 %27, 16
%56 = add <4 x i32> %28, <i32 16, i32 16, i32 16, i32 16>
%57 = add i64 %29, 2
%58 = icmp eq i64 %57, %25
br i1 %58, label %59, label %26
59: ; preds = %26, %9
%60 = phi i64 [ 0, %9 ], [ %55, %26 ]
%61 = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %9 ], [ %56, %26 ]
%62 = icmp eq i64 %22, 0
br i1 %62, label %76, label %63
63: ; preds = %59
%64 = xor <4 x i32> %61, <i32 -1, i32 -1, i32 -1, i32 -1>
%65 = sub <4 x i32> <i32 -5, i32 -5, i32 -5, i32 -5>, %61
%66 = add <4 x i32> %12, %64
%67 = add <4 x i32> %65, %14
%68 = lshr <4 x i32> %16, %66
%69 = lshr <4 x i32> %18, %67
%70 = and <4 x i32> %68, <i32 1, i32 1, i32 1, i32 1>
%71 = and <4 x i32> %69, <i32 1, i32 1, i32 1, i32 1>
%72 = getelementptr inbounds i32, i32* %5, i64 %60
%73 = bitcast i32* %72 to <4 x i32>*
store <4 x i32> %70, <4 x i32>* %73
%74 = getelementptr inbounds i32, i32* %72, i64 4
%75 = bitcast i32* %74 to <4 x i32>*
store <4 x i32> %71, <4 x i32>* %75
br label %76
76: ; preds = %59, %63
%77 = icmp eq i64 %10, %7
br i1 %77, label %90, label %78
78: ; preds = %6, %76
%79 = phi i64 [ 0, %6 ], [ %10, %76 ]
br label %80
80: ; preds = %78, %80
%81 = phi i64 [ %88, %80 ], [ %79, %78 ]
%82 = trunc i64 %81 to i32
%83 = xor i32 %82, -1
%84 = add i32 %83, %2
%85 = lshr i32 %0, %84
%86 = and i32 %85, 1
%87 = getelementptr inbounds i32, i32* %5, i64 %81
store i32 %86, i32* %87
%88 = add nuw nsw i64 %81, 1
%89 = icmp eq i64 %88, %7
br i1 %89, label %90, label %80
90: ; preds = %80, %76, %3
%91 = sext i32 %2 to i64
%92 = getelementptr inbounds i32, i32* %5, i64 %91
store i32* %92, i32** %1
ret void
}