#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 */
struct bitstream_elmt_t {
  int dummy;
};

/* Variables and functions */
int _read_bits(struct bitstream_elmt_t *, int, int);
unsigned int bitstream_ue(struct bitstream_elmt_t *bs) {
  int leading_zeros = -1;
  int b;
  unsigned int code_num;
  for (b = 0; !b; leading_zeros++) {
    b = _read_bits(bs, 1, 1);
  }
  code_num = (1 << leading_zeros) - 1 + _read_bits(bs, leading_zeros, 1);
  return code_num;
}


x86 assembly (clang, -O0):
<bitstream_ue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1140 <bitstream_ue+0x30>
jmp    1161 <bitstream_ue+0x51>
mov    -0x8(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <_read_bits@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112a <bitstream_ue+0x1a>
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x1,%edx
call   1030 <_read_bits@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitstream_ue>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0xffffffff,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1030 <_read_bits@plt>
add    $0x1,%ebp
test   %eax,%eax
je     1120 <bitstream_ue+0x10>
mov    $0xffffffff,%r14d
mov    %ebp,%ecx
shl    %cl,%r14d
not    %r14d
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1030 <_read_bits@plt>
add    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.bitstream_elmt_t = type { i32 }
define i32 @bitstream_ue(%struct.bitstream_elmt_t* %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i32 [ -1, %1 ], [ %5, %2 ]
%4 = tail call i32 @_read_bits(%struct.bitstream_elmt_t* %0, i32 1, i32 1)
%5 = add nsw i32 %3, 1
%6 = icmp eq i32 %4, 0
br i1 %6, label %2, label %7
7: ; preds = %2
%8 = shl nsw i32 -1, %5
%9 = xor i32 %8, -1
%10 = tail call i32 @_read_bits(%struct.bitstream_elmt_t* %0, i32 %5, i32 1)
%11 = add nsw i32 %10, %9
ret i32 %11
}