#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_bits(unsigned char **, int);
unsigned int read_ue(unsigned char **param_ptr) {
  int b, leadingZeroBits = -1;
  unsigned char *bit_ptr = *param_ptr;
  unsigned int value = 0;
  for (b = 0; !b; leadingZeroBits++) {
    b = read_bits(&bit_ptr, 1);
  }
  value = ((1 << leadingZeroBits) - 1) + read_bits(&bit_ptr, leadingZeroBits);
  *param_ptr = bit_ptr;
  return value;
}


x86 assembly (clang, -O0):
<read_ue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1152 <read_ue+0x42>
jmp    1171 <read_ue+0x61>
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1030 <read_bits@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    113c <read_ue+0x2c>
mov    -0x10(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1030 <read_bits@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ue>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    (%rdi),%rax
mov    %rax,0x8(%rsp)
mov    $0xffffffff,%ebp
lea    0x8(%rsp),%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <read_bits@plt>
add    $0x1,%ebp
test   %eax,%eax
je     1130 <read_ue+0x20>
mov    $0xffffffff,%ebx
mov    %ebp,%ecx
shl    %cl,%ebx
not    %ebx
lea    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1030 <read_bits@plt>
add    %ebx,%eax
mov    0x8(%rsp),%rcx
mov    %rcx,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_ue(i8** nocapture %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = load i8*, i8** %0
store i8* %4, i8** %2
br label %5
5: ; preds = %1, %5
%6 = phi i32 [ -1, %1 ], [ %8, %5 ]
%7 = call i32 @read_bits(i8** %2, i32 1)
%8 = add nsw i32 %6, 1
%9 = icmp eq i32 %7, 0
br i1 %9, label %5, label %10
10: ; preds = %5
%11 = shl nsw i32 -1, %8
%12 = xor i32 %11, -1
%13 = call i32 @read_bits(i8** %2, i32 %8)
%14 = add nsw i32 %13, %12
%15 = load i8*, i8** %2
store i8* %15, i8** %0
ret i32 %14
}