void write_elias_gamma(int value) {
  int bits = 0, rvalue = 0;
  while (value > 1)
    ++bits, rvalue <<= 1, rvalue |= value & 1, value >>= 1;
  while (bits--)
    write_bit(0), write_bit(rvalue & 1), rvalue >>= 1;
  write_bit(1);
}


x86 assembly (clang, -O0):
<write_elias_gamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x1,-0x4(%rbp)
jle    115f <write_elias_gamma+0x4f>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1129 <write_elias_gamma+0x19>
jmp    1164 <write_elias_gamma+0x54>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     119c <write_elias_gamma+0x8c>
xor    %edi,%edi
mov    $0x0,%al
call   1030 <write_bit@plt>
mov    -0xc(%rbp),%edi
and    $0x1,%edi
mov    $0x0,%al
call   1030 <write_bit@plt>
mov    -0xc(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1164 <write_elias_gamma+0x54>
mov    $0x1,%edi
mov    $0x0,%al
call   1030 <write_bit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_elias_gamma>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     115b <write_elias_gamma+0x4b>
xor    %ebp,%ebp
xor    %ebx,%ebx
mov    %edi,%eax
xchg   %ax,%ax
mov    %edi,%ecx
and    $0x1,%ecx
lea    (%rcx,%rbx,2),%ebx
shr    %eax
add    $0xffffffff,%ebp
cmp    $0x3,%edi
mov    %eax,%edi
ja     1120 <write_elias_gamma+0x10>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
xor    %eax,%eax
call   1030 <write_bit@plt>
mov    %ebx,%edi
and    $0x1,%edi
xor    %eax,%eax
call   1030 <write_bit@plt>
sar    %ebx
inc    %ebp
jne    1140 <write_elias_gamma+0x30>
mov    $0x1,%edi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <write_bit@plt>

LLVM IR (-O0):
define void @write_elias_gamma(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %8, %1
%6 = load i32, i32* %2
%7 = icmp sgt i32 %6, 1
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = load i32, i32* %3
%10 = add nsw i32 %9, 1
store i32 %10, i32* %3
%11 = load i32, i32* %4
%12 = shl i32 %11, 1
store i32 %12, i32* %4
%13 = load i32, i32* %2
%14 = and i32 %13, 1
%15 = load i32, i32* %4
%16 = or i32 %15, %14
store i32 %16, i32* %4
%17 = load i32, i32* %2
%18 = ashr i32 %17, 1
store i32 %18, i32* %2
br label %5
19: ; preds = %5
br label %20
20: ; preds = %24, %19
%21 = load i32, i32* %3
%22 = add nsw i32 %21, -1
store i32 %22, i32* %3
%23 = icmp ne i32 %21, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = call i32 (i32, ...) bitcast (i32 (...)* @write_bit to i32 (i32, ...)*)(i32 0)
%26 = load i32, i32* %4
%27 = and i32 %26, 1
%28 = call i32 (i32, ...) bitcast (i32 (...)* @write_bit to i32 (i32, ...)*)(i32 %27)
%29 = load i32, i32* %4
%30 = ashr i32 %29, 1
store i32 %30, i32* %4
br label %20
31: ; preds = %20
%32 = call i32 (i32, ...) bitcast (i32 (...)* @write_bit to i32 (i32, ...)*)(i32 1)
ret void
}

LLVM IR (-O2):
define void @write_elias_gamma(i32 %0) {
%2 = icmp sgt i32 %0, 1
br i1 %2, label %3, label %22
3: ; preds = %1, %3
%4 = phi i32 [ %10, %3 ], [ 0, %1 ]
%5 = phi i32 [ %7, %3 ], [ 0, %1 ]
%6 = phi i32 [ %11, %3 ], [ %0, %1 ]
%7 = add nuw nsw i32 %5, 1
%8 = shl i32 %4, 1
%9 = and i32 %6, 1
%10 = or i32 %8, %9
%11 = lshr i32 %6, 1
%12 = icmp ugt i32 %6, 3
br i1 %12, label %3, label %13
13: ; preds = %3, %13
%14 = phi i32 [ %20, %13 ], [ %10, %3 ]
%15 = phi i32 [ %16, %13 ], [ %7, %3 ]
%16 = add nsw i32 %15, -1
%17 = tail call i32 (i32, ...) bitcast (i32 (...)* @write_bit to i32 (i32, ...)*)(i32 0)
%18 = and i32 %14, 1
%19 = tail call i32 (i32, ...) bitcast (i32 (...)* @write_bit to i32 (i32, ...)*)(i32 %18)
%20 = ashr i32 %14, 1
%21 = icmp eq i32 %16, 0
br i1 %21, label %22, label %13
22: ; preds = %13, %1
%23 = tail call i32 (i32, ...) bitcast (i32 (...)* @write_bit to i32 (i32, ...)*)(i32 1)
ret void
}