#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 max(int, int /*<<< orphan*/);
int readBits(int);
int codeToDistance(int code) {
  if (code < 4) {
    return code + 1;
  }
  int bits = max((code >> 1) - 1, 0);
  int base = code % 2;
  int lower = ((2 + base) << bits) + 1;
  return lower + readBits(bits);
}


x86 assembly (clang, -O0):
<codeToDistance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1143 <codeToDistance+0x23>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    118d <codeToDistance+0x6d>
mov    -0x8(%rbp),%edi
sar    $0x1,%edi
sub    $0x1,%edi
xor    %esi,%esi
call   1040 <max@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    -0xc(%rbp),%ecx
shl    %cl,%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <readBits@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<codeToDistance>:
push   %rbx
mov    %edi,%ebx
cmp    $0x3,%edi
jg     112d <codeToDistance+0xd>
add    $0x1,%ebx
jmp    1151 <codeToDistance+0x31>
mov    %ebx,%edi
shr    %edi
add    $0xffffffff,%edi
xor    %esi,%esi
call   1040 <max@plt>
and    $0x1,%ebx
or     $0x2,%ebx
mov    %eax,%ecx
shl    %cl,%ebx
mov    %eax,%edi
call   1030 <readBits@plt>
add    %eax,%ebx
add    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @codeToDistance(i32 %0) {
%2 = icmp slt i32 %0, 4
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = add nsw i32 %0, 1
br label %15
5: ; preds = %1
%6 = lshr i32 %0, 1
%7 = add nsw i32 %6, -1
%8 = tail call i32 @max(i32 %7, i32 0)
%9 = and i32 %0, 1
%10 = or i32 %9, 2
%11 = shl i32 %10, %8
%12 = add nsw i32 %11, 1
%13 = tail call i32 @readBits(i32 %8)
%14 = add nsw i32 %12, %13
br label %15
15: ; preds = %5, %3
%16 = phi i32 [ %4, %3 ], [ %14, %5 ]
ret i32 %16
}