#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
int get_1bit(void *);
int get_bits(void *, int);
int parse_codenum(void *buf) {
  uint8_t leading_zero_bits = -1;
  uint8_t b;
  uint32_t code_num = 0;
  for (b = 0; !b; leading_zero_bits++) {
    b = get_1bit(buf);
  }
  code_num =
      ((uint32_t)1 << leading_zero_bits) - 1 + get_bits(buf, leading_zero_bits);
  return code_num;
}


x86 assembly (clang, -O0):
<parse_codenum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1157 <parse_codenum+0x37>
jmp    1171 <parse_codenum+0x51>
mov    -0x8(%rbp),%rdi
call   1030 <get_1bit@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1141 <parse_codenum+0x21>
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
call   1040 <get_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):
<parse_codenum>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0xffffffff,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <get_1bit@plt>
add    $0x1,%ebp
test   %eax,%eax
je     1130 <parse_codenum+0x10>
mov    $0xffffffff,%r14d
mov    %ebp,%ecx
shl    %cl,%r14d
not    %r14d
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <get_bits@plt>
add    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @parse_codenum(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
store i32 0, i32* %5
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 i8*, i8** %2
%12 = call i32 @get_1bit(i8* %11)
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 i8*, i8** %2
%21 = load i32, i32* %3
%22 = call i32 @get_bits(i8* %20, i32 %21)
%23 = add nsw i32 %19, %22
store i32 %23, i32* %5
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
define i32 @parse_codenum(i8* %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i32 [ -1, %1 ], [ %5, %2 ]
%4 = tail call i32 @get_1bit(i8* %0)
%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 @get_bits(i8* %0, i32 %5)
%11 = add nsw i32 %10, %9
ret i32 %11
}