#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 BitsLog2Floor(int);
void PrefixEncode(int distance, int *const code, int *const extra_bits_count,
                  int *const extra_bits_value) {
  if (distance > 2) {
    const int highest_bit = BitsLog2Floor(--distance);
    const int second_highest_bit = (distance >> (highest_bit - 1)) & 1;
    *extra_bits_count = highest_bit - 1;
    *extra_bits_value = distance & ((1 << *extra_bits_count) - 1);
    *code = 2 * highest_bit + second_highest_bit;
  } else {
    *extra_bits_count = 0;
    *extra_bits_value = 0;
    *code = (distance == 2) ? 1 : 0;
  }
}


x86 assembly (clang, -O0):
<PrefixEncode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x2,-0x4(%rbp)
jle    1194 <PrefixEncode+0x84>
mov    -0x4(%rbp),%edi
add    $0xffffffff,%edi
mov    %edi,-0x4(%rbp)
call   1030 <BitsLog2Floor@plt>
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
sar    %cl,%eax
and    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%eax
and    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
shl    $0x1,%ecx
add    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11be <PrefixEncode+0xae>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x4(%rbp),%edx
xor    %ecx,%ecx
mov    $0x1,%eax
cmp    $0x2,%edx
cmove  %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PrefixEncode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
cmp    $0x3,%edi
jl     1150 <PrefixEncode+0x40>
add    $0xffffffff,%ebp
mov    %ebp,%edi
call   1030 <BitsLog2Floor@plt>
lea    -0x1(%rax),%ecx
mov    %ebp,%edx
shr    %cl,%edx
and    $0x1,%edx
mov    %ecx,(%rbx)
mov    $0xffffffff,%esi
shl    %cl,%esi
not    %esi
and    %ebp,%esi
mov    %esi,(%r15)
lea    (%rdx,%rax,2),%eax
jmp    1165 <PrefixEncode+0x55>
movl   $0x0,(%rbx)
movl   $0x0,(%r15)
xor    %eax,%eax
cmp    $0x2,%ebp
sete   %al
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @PrefixEncode(i32 %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 2
br i1 %12, label %13, label %37
13: ; preds = %4
%14 = load i32, i32* %5
%15 = add nsw i32 %14, -1
store i32 %15, i32* %5
%16 = call i32 @BitsLog2Floor(i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %5
%18 = load i32, i32* %9
%19 = sub nsw i32 %18, 1
%20 = ashr i32 %17, %19
%21 = and i32 %20, 1
store i32 %21, i32* %10
%22 = load i32, i32* %9
%23 = sub nsw i32 %22, 1
%24 = load i32*, i32** %7
store i32 %23, i32* %24
%25 = load i32, i32* %5
%26 = load i32*, i32** %7
%27 = load i32, i32* %26
%28 = shl i32 1, %27
%29 = sub nsw i32 %28, 1
%30 = and i32 %25, %29
%31 = load i32*, i32** %8
store i32 %30, i32* %31
%32 = load i32, i32* %9
%33 = mul nsw i32 2, %32
%34 = load i32, i32* %10
%35 = add nsw i32 %33, %34
%36 = load i32*, i32** %6
store i32 %35, i32* %36
br label %45
37: ; preds = %4
%38 = load i32*, i32** %7
store i32 0, i32* %38
%39 = load i32*, i32** %8
store i32 0, i32* %39
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, 2
%42 = zext i1 %41 to i64
%43 = select i1 %41, i32 1, i32 0
%44 = load i32*, i32** %6
store i32 %43, i32* %44
br label %45
45: ; preds = %37, %13
ret void
}

LLVM IR (-O2):
define void @PrefixEncode(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = icmp sgt i32 %0, 2
br i1 %5, label %6, label %17
6: ; preds = %4
%7 = add nsw i32 %0, -1
%8 = tail call i32 @BitsLog2Floor(i32 %7)
%9 = add nsw i32 %8, -1
%10 = lshr i32 %7, %9
%11 = and i32 %10, 1
store i32 %9, i32* %2
%12 = shl nsw i32 -1, %9
%13 = xor i32 %12, -1
%14 = and i32 %7, %13
store i32 %14, i32* %3
%15 = shl nsw i32 %8, 1
%16 = or i32 %11, %15
br label %20
17: ; preds = %4
store i32 0, i32* %2
store i32 0, i32* %3
%18 = icmp eq i32 %0, 2
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %17, %6
%21 = phi i32 [ %19, %17 ], [ %16, %6 ]
store i32 %21, i32* %1
ret void
}