#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 read_bit();
int read_elias_gamma() {
  int i;
  int value;
  i = 0;
  while (!read_bit()) {
    i++;
  }
  if (i > 15) {
    return -1;
  }
  value = 1;
  while (i--) {
    value = value << 1 | read_bit();
  }
  return value;
}


x86 assembly (clang, -O0):
<read_elias_gamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1030 <read_bit@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    113b <read_elias_gamma+0x2b>
jmp    1149 <read_elias_gamma+0x39>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    111f <read_elias_gamma+0xf>
cmpl   $0xf,-0x8(%rbp)
jle    115f <read_elias_gamma+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <read_elias_gamma+0x8f>
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
je     1199 <read_elias_gamma+0x89>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <read_bit@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1166 <read_elias_gamma+0x56>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_elias_gamma>:
push   %rbp
push   %rbx
push   %rax
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <read_bit@plt>
add    $0xffffffff,%ebp
test   %eax,%eax
je     1120 <read_elias_gamma+0x10>
mov    %ebp,%eax
neg    %eax
mov    $0xffffffff,%ebx
cmp    $0xf,%eax
ja     115f <read_elias_gamma+0x4f>
mov    $0x1,%ebx
test   %ebp,%ebp
je     115f <read_elias_gamma+0x4f>
cs nopw 0x0(%rax,%rax,1)
nop
add    %ebx,%ebx
xor    %eax,%eax
call   1030 <read_bit@plt>
or     %eax,%ebx
inc    %ebp
jne    1150 <read_elias_gamma+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_elias_gamma() {
br label %1
1: ; preds = %1, %0
%2 = phi i32 [ 0, %0 ], [ %5, %1 ]
%3 = tail call i32 (...) @read_bit()
%4 = icmp eq i32 %3, 0
%5 = add nuw nsw i32 %2, 1
br i1 %4, label %1, label %6
6: ; preds = %1
%7 = icmp ugt i32 %2, 15
br i1 %7, label %18, label %8
8: ; preds = %6
%9 = icmp eq i32 %2, 0
br i1 %9, label %18, label %10
10: ; preds = %8, %10
%11 = phi i32 [ %16, %10 ], [ 1, %8 ]
%12 = phi i32 [ %13, %10 ], [ %2, %8 ]
%13 = add nsw i32 %12, -1
%14 = shl i32 %11, 1
%15 = tail call i32 (...) @read_bit()
%16 = or i32 %15, %14
%17 = icmp eq i32 %13, 0
br i1 %17, label %18, label %10
18: ; preds = %10, %8, %6
%19 = phi i32 [ -1, %6 ], [ 1, %8 ], [ %16, %10 ]
ret i32 %19
}