#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 */
struct bitstream_writer_t {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/
bitstream_writer_write_repeated_u8(struct bitstream_writer_t *, int, int);
int /*<<< orphan*/ bitstream_writer_write_u64_bits(struct bitstream_writer_t *,
                                                   int, int);
void bitstream_writer_write_repeated_bit(struct bitstream_writer_t *self_p,
                                         int value, int length) {
  int rest;
  if (value != 0) {
    value = 0xff;
  }
  rest = (length % 8);
  bitstream_writer_write_u64_bits(self_p, value & ((1 << rest) - 1), rest);
  bitstream_writer_write_repeated_u8(self_p, value, length / 8);
}


x86 assembly (clang, -O0):
<bitstream_writer_write_repeated_bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1143 <bitstream_writer_write_repeated_bit+0x23>
movl   $0xff,-0xc(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
and    %eax,%esi
mov    -0x14(%rbp),%edx
call   1030 <bitstream_writer_write_u64_bits@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1040 <bitstream_writer_write_repeated_u8@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitstream_writer_write_repeated_bit>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
test   %esi,%esi
mov    $0xff,%eax
cmovne %eax,%ebx
lea    0x7(%rdx),%ebp
test   %edx,%edx
cmovns %edx,%ebp
mov    %ebp,%eax
and    $0xfffffff8,%eax
sub    %eax,%edx
mov    $0xffffffff,%esi
mov    %edx,%ecx
shl    %cl,%esi
mov    %rdi,%r14
not    %esi
and    %ebx,%esi
call   1030 <bitstream_writer_write_u64_bits@plt>
sar    $0x3,%ebp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <bitstream_writer_write_repeated_u8@plt>

LLVM IR (-O0):
%struct.bitstream_writer_t = type { i32 }
define void @bitstream_writer_write_repeated_bit(%struct.bitstream_writer_t* %0, i32 %1, i32 %2) {
%4 = alloca %struct.bitstream_writer_t*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.bitstream_writer_t* %0, %struct.bitstream_writer_t** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %3
store i32 255, i32* %5
br label %11
11: ; preds = %10, %3
%12 = load i32, i32* %6
%13 = srem i32 %12, 8
store i32 %13, i32* %7
%14 = load %struct.bitstream_writer_t*, %struct.bitstream_writer_t** %4
%15 = load i32, i32* %5
%16 = load i32, i32* %7
%17 = shl i32 1, %16
%18 = sub nsw i32 %17, 1
%19 = and i32 %15, %18
%20 = load i32, i32* %7
%21 = call i32 @bitstream_writer_write_u64_bits(%struct.bitstream_writer_t* %14, i32 %19, i32 %20)
%22 = load %struct.bitstream_writer_t*, %struct.bitstream_writer_t** %4
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = sdiv i32 %24, 8
%26 = call i32 @bitstream_writer_write_repeated_u8(%struct.bitstream_writer_t* %22, i32 %23, i32 %25)
ret void
}

LLVM IR (-O2):
%struct.bitstream_writer_t = type { i32 }
define void @bitstream_writer_write_repeated_bit(%struct.bitstream_writer_t* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, 0
%5 = select i1 %4, i32 0, i32 255
%6 = srem i32 %2, 8
%7 = shl nsw i32 -1, %6
%8 = xor i32 %7, -1
%9 = and i32 %5, %8
%10 = tail call i32 @bitstream_writer_write_u64_bits(%struct.bitstream_writer_t* %0, i32 %9, i32 %6)
%11 = sdiv i32 %2, 8
%12 = tail call i32 @bitstream_writer_write_repeated_u8(%struct.bitstream_writer_t* %0, i32 %5, i32 %11)
ret void
}