#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 */
typedef int uint64_t;
typedef int int64_t;

/* Variables and functions */
int readBit();
uint64_t readEliasDelta() {
  int qq = 0;
  while (readBit() == 0) {
    qq++;
  }
  int64_t q = 1;
  for (int i = qq; i > 0; i--) {
    q = (q << 1) | readBit();
  }
  uint64_t x = 1;
  for (int64_t i = q - 2; i >= 0; i--) {
    x = (x << 1) | readBit();
  }
  return x;
}


x86 assembly (clang, -O0):
<readEliasDelta>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <readBit@plt>
cmp    $0x0,%eax
jne    113d <readEliasDelta+0x2d>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    111f <readEliasDelta+0xf>
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    117c <readEliasDelta+0x6c>
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <readBit@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    114a <readEliasDelta+0x3a>
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11be <readEliasDelta+0xae>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <readBit@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
or     %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    118c <readEliasDelta+0x7c>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readEliasDelta>:
push   %rbp
push   %r14
push   %rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <readBit@plt>
add    $0x1,%ebp
test   %eax,%eax
je     1120 <readEliasDelta+0x10>
mov    $0x1,%eax
cmp    $0x1,%ebp
je     1182 <readEliasDelta+0x72>
mov    $0x1,%ebx
nopl   (%rax)
mov    %ebx,%r14d
add    %ebx,%r14d
xor    %eax,%eax
call   1030 <readBit@plt>
mov    %eax,%ebx
or     %r14d,%ebx
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
jg     1140 <readEliasDelta+0x30>
mov    $0x1,%eax
cmp    $0x2,%ebx
jl     1182 <readEliasDelta+0x72>
add    $0xffffffff,%ebx
mov    $0x1,%ebp
nopl   0x0(%rax)
add    %ebp,%ebp
xor    %eax,%eax
call   1030 <readBit@plt>
or     %ebp,%eax
mov    %eax,%ebp
add    $0xffffffff,%ebx
jne    1170 <readEliasDelta+0x60>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readEliasDelta() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %9, %0
%7 = call i32 (...) @readBit()
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = load i32, i32* %1
%11 = add nsw i32 %10, 1
store i32 %11, i32* %1
br label %6
12: ; preds = %6
store i32 1, i32* %2
%13 = load i32, i32* %1
store i32 %13, i32* %3
br label %14
14: ; preds = %22, %12
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i32, i32* %2
%19 = shl i32 %18, 1
%20 = call i32 (...) @readBit()
%21 = or i32 %19, %20
store i32 %21, i32* %2
br label %22
22: ; preds = %17
%23 = load i32, i32* %3
%24 = add nsw i32 %23, -1
store i32 %24, i32* %3
br label %14
25: ; preds = %14
store i32 1, i32* %4
%26 = load i32, i32* %2
%27 = sub nsw i32 %26, 2
store i32 %27, i32* %5
br label %28
28: ; preds = %36, %25
%29 = load i32, i32* %5
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %39
31: ; preds = %28
%32 = load i32, i32* %4
%33 = shl i32 %32, 1
%34 = call i32 (...) @readBit()
%35 = or i32 %33, %34
store i32 %35, i32* %4
br label %36
36: ; preds = %31
%37 = load i32, i32* %5
%38 = add nsw i32 %37, -1
store i32 %38, i32* %5
br label %28
39: ; preds = %28
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
define i32 @readEliasDelta() {
br label %1
1: ; preds = %1, %0
%2 = phi i32 [ 0, %0 ], [ %5, %1 ]
%3 = tail call i32 (...) @readBit()
%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 eq i32 %2, 0
br i1 %7, label %20, label %12
8: ; preds = %12
%9 = icmp sgt i32 %17, 1
br i1 %9, label %10, label %20
10: ; preds = %8
%11 = add nsw i32 %17, -2
br label %22
12: ; preds = %6, %12
%13 = phi i32 [ %18, %12 ], [ %2, %6 ]
%14 = phi i32 [ %17, %12 ], [ 1, %6 ]
%15 = shl i32 %14, 1
%16 = tail call i32 (...) @readBit()
%17 = or i32 %16, %15
%18 = add nsw i32 %13, -1
%19 = icmp sgt i32 %13, 1
br i1 %19, label %12, label %8
20: ; preds = %22, %6, %8
%21 = phi i32 [ 1, %8 ], [ 1, %6 ], [ %27, %22 ]
ret i32 %21
22: ; preds = %10, %22
%23 = phi i32 [ %28, %22 ], [ %11, %10 ]
%24 = phi i32 [ %27, %22 ], [ 1, %10 ]
%25 = shl i32 %24, 1
%26 = tail call i32 (...) @readBit()
%27 = or i32 %26, %25
%28 = add nsw i32 %23, -1
%29 = icmp eq i32 %23, 0
br i1 %29, label %20, label %22
}