#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*/ write_bit(int);
void write_elias_gamma(int value) {
  int i;
  for (i = 2; i <= value; i <<= 1) {
    write_bit(0);
  }
  while ((i >>= 1) > 0) {
    write_bit(value & i);
  }
}


x86 assembly (clang, -O0):
<write_elias_gamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x2,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     1143 <write_elias_gamma+0x33>
xor    %edi,%edi
call   1030 <write_bit@plt>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1122 <write_elias_gamma+0x12>
jmp    1148 <write_elias_gamma+0x38>
mov    -0x8(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    116a <write_elias_gamma+0x5a>
mov    -0x4(%rbp),%edi
and    -0x8(%rbp),%edi
call   1030 <write_bit@plt>
jmp    1148 <write_elias_gamma+0x38>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_elias_gamma>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    $0x2,%ebx
cmp    $0x2,%edi
jl     1143 <write_elias_gamma+0x33>
mov    $0x2,%ebx
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
call   1030 <write_bit@plt>
add    %ebx,%ebx
cmp    %r14d,%ebx
jle    1130 <write_elias_gamma+0x20>
cmp    $0x2,%ebx
jl     1163 <write_elias_gamma+0x53>
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
sar    %ebp
mov    %ebp,%edi
and    %r14d,%edi
call   1030 <write_bit@plt>
cmp    $0x3,%ebx
mov    %ebp,%ebx
jg     1150 <write_elias_gamma+0x40>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @write_elias_gamma(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 2, i32* %3
br label %4
4: ; preds = %10, %1
%5 = load i32, i32* %3
%6 = load i32, i32* %2
%7 = icmp sle i32 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = call i32 @write_bit(i32 0)
br label %10
10: ; preds = %8
%11 = load i32, i32* %3
%12 = shl i32 %11, 1
store i32 %12, i32* %3
br label %4
13: ; preds = %4
br label %14
14: ; preds = %18, %13
%15 = load i32, i32* %3
%16 = ashr i32 %15, 1
store i32 %16, i32* %3
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* %2
%20 = load i32, i32* %3
%21 = and i32 %19, %20
%22 = call i32 @write_bit(i32 %21)
br label %14
23: ; preds = %14
ret void
}

LLVM IR (-O2):
define void @write_elias_gamma(i32 %0) {
%2 = icmp slt i32 %0, 2
br i1 %2, label %5, label %7
3: ; preds = %7
%4 = icmp sgt i32 %10, 1
br i1 %4, label %5, label %18
5: ; preds = %1, %3
%6 = phi i32 [ 2, %1 ], [ %10, %3 ]
br label %12
7: ; preds = %1, %7
%8 = phi i32 [ %10, %7 ], [ 2, %1 ]
%9 = tail call i32 @write_bit(i32 0)
%10 = shl i32 %8, 1
%11 = icmp sgt i32 %10, %0
br i1 %11, label %3, label %7
12: ; preds = %5, %12
%13 = phi i32 [ %14, %12 ], [ %6, %5 ]
%14 = ashr i32 %13, 1
%15 = and i32 %14, %0
%16 = tail call i32 @write_bit(i32 %15)
%17 = icmp sgt i32 %13, 3
br i1 %17, label %12, label %18
18: ; preds = %12, %3
ret void
}