#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 /*<<< orphan*/ flush_8bits(unsigned int *, unsigned char **, int *);
void writebits(unsigned int value, int length, unsigned char **output,
               int *bitptr) {
  unsigned int mask;
  unsigned int combine = (unsigned int)**output;
  if (length >= 8)
    mask = 0xFFU << (length - 8);
  else
    mask = 0xFFU >> (8 - length);
  while (length > 8) {
    combine <<= 8;
    (*bitptr) += 8;
    combine |= (value & mask) >> (length - 8);
    flush_8bits(&combine, output, bitptr);
    length -= 8;
    mask >>= 8;
  }
  if (length) {
    combine <<= length;
    (*bitptr) += length;
    combine |= value;
    flush_8bits(&combine, output, bitptr);
  }
  **output = (unsigned char)combine;
}


x86 assembly (clang, -O0):
<writebits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movzbl (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x8,-0x8(%rbp)
jl     1152 <writebits+0x42>
mov    -0x8(%rbp),%ecx
sub    $0x8,%ecx
mov    $0xff,%eax
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
jmp    1164 <writebits+0x54>
mov    $0x8,%ecx
sub    -0x8(%rbp),%ecx
mov    $0xff,%eax
shr    %cl,%eax
mov    %eax,-0x1c(%rbp)
jmp    1169 <writebits+0x59>
cmpl   $0x8,-0x8(%rbp)
jle    11c3 <writebits+0xb3>
mov    -0x20(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x8,%ecx
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
and    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x8,%ecx
shr    %cl,%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   1030 <flush_8bits@plt>
mov    -0x8(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0x1c(%rbp)
jmp    1169 <writebits+0x59>
cmpl   $0x0,-0x8(%rbp)
je     11fd <writebits+0xed>
mov    -0x8(%rbp),%ecx
mov    -0x20(%rbp),%eax
shl    %cl,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
call   1030 <flush_8bits@plt>
mov    -0x20(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %cl,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writebits>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%r15d
mov    (%rdx),%rax
movzbl (%rax),%eax
mov    %eax,0x4(%rsp)
cmp    $0x9,%esi
jl     11b8 <writebits+0xa8>
lea    -0x8(%rbp),%r12d
mov    $0xff,%ebx
mov    %r12d,%ecx
shl    %cl,%ebx
addl   $0x8,0x0(%r13)
mov    %ebx,%edx
and    %r15d,%edx
shr    %cl,%edx
shl    $0x8,%eax
or     %eax,%edx
mov    %edx,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <flush_8bits@plt>
cmp    $0x11,%ebp
jl     11bb <writebits+0xab>
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
shr    $0x8,%ebx
mov    0x4(%rsp),%eax
addl   $0x8,0x0(%r13)
mov    %ebx,%edx
and    %r15d,%edx
lea    -0x10(%rbp),%ecx
shr    %cl,%edx
shl    $0x8,%eax
or     %eax,%edx
mov    %edx,0x4(%rsp)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <flush_8bits@plt>
add    $0xfffffff8,%ebp
cmp    $0x10,%ebp
jg     1180 <writebits+0x70>
add    $0xfffffff8,%ebp
mov    %ebp,%r12d
test   %r12d,%r12d
je     11e4 <writebits+0xd4>
mov    0x4(%rsp),%eax
mov    %r12d,%ecx
shl    %cl,%eax
add    %r12d,0x0(%r13)
or     %r15d,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <flush_8bits@plt>
mov    0x4(%rsp),%al
mov    (%r14),%rcx
mov    %al,(%rcx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @writebits(i32 %0, i32 %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32* %3, i32** %8
%11 = load i8**, i8*** %7
%12 = load i8*, i8** %11
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
store i32 %14, i32* %10
%15 = load i32, i32* %6
%16 = icmp sge i32 %15, 8
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* %6
%19 = sub nsw i32 %18, 8
%20 = shl i32 255, %19
store i32 %20, i32* %9
br label %25
21: ; preds = %4
%22 = load i32, i32* %6
%23 = sub nsw i32 8, %22
%24 = lshr i32 255, %23
store i32 %24, i32* %9
br label %25
25: ; preds = %21, %17
br label %26
26: ; preds = %29, %25
%27 = load i32, i32* %6
%28 = icmp sgt i32 %27, 8
br i1 %28, label %29, label %50
29: ; preds = %26
%30 = load i32, i32* %10
%31 = shl i32 %30, 8
store i32 %31, i32* %10
%32 = load i32*, i32** %8
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 8
store i32 %34, i32* %32
%35 = load i32, i32* %5
%36 = load i32, i32* %9
%37 = and i32 %35, %36
%38 = load i32, i32* %6
%39 = sub nsw i32 %38, 8
%40 = lshr i32 %37, %39
%41 = load i32, i32* %10
%42 = or i32 %41, %40
store i32 %42, i32* %10
%43 = load i8**, i8*** %7
%44 = load i32*, i32** %8
%45 = call i32 @flush_8bits(i32* %10, i8** %43, i32* %44)
%46 = load i32, i32* %6
%47 = sub nsw i32 %46, 8
store i32 %47, i32* %6
%48 = load i32, i32* %9
%49 = lshr i32 %48, 8
store i32 %49, i32* %9
br label %26
50: ; preds = %26
%51 = load i32, i32* %6
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %67
53: ; preds = %50
%54 = load i32, i32* %6
%55 = load i32, i32* %10
%56 = shl i32 %55, %54
store i32 %56, i32* %10
%57 = load i32, i32* %6
%58 = load i32*, i32** %8
%59 = load i32, i32* %58
%60 = add nsw i32 %59, %57
store i32 %60, i32* %58
%61 = load i32, i32* %5
%62 = load i32, i32* %10
%63 = or i32 %62, %61
store i32 %63, i32* %10
%64 = load i8**, i8*** %7
%65 = load i32*, i32** %8
%66 = call i32 @flush_8bits(i32* %10, i8** %64, i32* %65)
br label %67
67: ; preds = %53, %50
%68 = load i32, i32* %10
%69 = trunc i32 %68 to i8
%70 = load i8**, i8*** %7
%71 = load i8*, i8** %70
store i8 %69, i8* %71
ret void
}

LLVM IR (-O2):
define void @writebits(i32 %0, i32 %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = load i8*, i8** %2
%8 = load i8, i8* %7
%9 = zext i8 %8 to i32
store i32 %9, i32* %5
%10 = icmp sgt i32 %1, 8
br i1 %10, label %11, label %37
11: ; preds = %4
%12 = add nsw i32 %1, -8
%13 = shl i32 255, %12
%14 = shl nuw nsw i32 %9, 8
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 8
store i32 %16, i32* %3
%17 = and i32 %13, %0
%18 = add nsw i32 %1, -8
%19 = lshr i32 %17, %18
%20 = or i32 %14, %19
store i32 %20, i32* %5
%21 = call i32 @flush_8bits(i32* %5, i8** %2, i32* %3)
%22 = icmp sgt i32 %1, 16
br i1 %22, label %23, label %37
23: ; preds = %11, %23
%24 = phi i32 [ %32, %23 ], [ %18, %11 ]
%25 = phi i32 [ %26, %23 ], [ %13, %11 ]
%26 = lshr i32 %25, 8
%27 = load i32, i32* %5
%28 = shl i32 %27, 8
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 8
store i32 %30, i32* %3
%31 = and i32 %26, %0
%32 = add nsw i32 %24, -8
%33 = lshr i32 %31, %32
%34 = or i32 %28, %33
store i32 %34, i32* %5
%35 = call i32 @flush_8bits(i32* %5, i8** %2, i32* %3)
%36 = icmp sgt i32 %24, 16
br i1 %36, label %23, label %37
37: ; preds = %23, %11, %4
%38 = phi i32 [ %1, %4 ], [ %18, %11 ], [ %32, %23 ]
%39 = icmp eq i32 %38, 0
br i1 %39, label %47, label %40
40: ; preds = %37
%41 = load i32, i32* %5
%42 = shl i32 %41, %38
%43 = load i32, i32* %3
%44 = add nsw i32 %43, %38
store i32 %44, i32* %3
%45 = or i32 %42, %0
store i32 %45, i32* %5
%46 = call i32 @flush_8bits(i32* %5, i8** %2, i32* %3)
br label %47
47: ; preds = %40, %37
%48 = load i32, i32* %5
%49 = trunc i32 %48 to i8
%50 = load i8*, i8** %2
store i8 %49, i8* %50
ret void
}