#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int bsr(int);
uint32_t convert_dist_to_dist_sym(uint32_t dist) {
  assert(dist <= 32768 && dist > 0);
  if (dist <= 32768) {
    uint32_t msb = dist > 4 ? bsr(dist - 1) - 2 : 0;
    return (msb * 2) + ((dist - 1) >> msb);
  } else {
    return ~0;
  }
}


x86 assembly (clang, -O0):
<convert_dist_to_dist_sym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x8000,-0x8(%rbp)
mov    %al,-0xd(%rbp)
jg     1147 <convert_dist_to_dist_sym+0x27>
cmpl   $0x0,-0x8(%rbp)
setg   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x8000,-0x8(%rbp)
jg     11ae <convert_dist_to_dist_sym+0x8e>
cmpl   $0x4,-0x8(%rbp)
jle    1181 <convert_dist_to_dist_sym+0x61>
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
call   1040 <bsr@plt>
sub    $0x2,%eax
mov    %eax,-0x14(%rbp)
jmp    118b <convert_dist_to_dist_sym+0x6b>
xor    %eax,%eax
mov    %eax,-0x14(%rbp)
jmp    118b <convert_dist_to_dist_sym+0x6b>
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    -0x8(%rbp),%edx
sub    $0x1,%edx
mov    -0xc(%rbp),%ecx
sar    %cl,%edx
mov    %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <convert_dist_to_dist_sym+0x95>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_dist_to_dist_sym>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x1(%rbx),%r14d
xor    %edi,%edi
cmp    $0x8000,%r14d
setb   %dil
call   1030 <assert@plt>
mov    $0xffffffff,%eax
cmp    $0x8000,%ebx
jg     1164 <convert_dist_to_dist_sym+0x44>
xor    %ecx,%ecx
cmp    $0x5,%ebx
jl     115d <convert_dist_to_dist_sym+0x3d>
mov    %r14d,%edi
call   1040 <bsr@plt>
mov    %eax,%ecx
add    $0xfffffffe,%ecx
sar    %cl,%r14d
lea    (%r14,%rcx,2),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @convert_dist_to_dist_sym(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 32768
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 0
br label %10
10: ; preds = %7, %1
%11 = phi i1 [ false, %1 ], [ %9, %7 ]
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %3
%15 = icmp sle i32 %14, 32768
br i1 %15, label %16, label %34
16: ; preds = %10
%17 = load i32, i32* %3
%18 = icmp sgt i32 %17, 4
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* %3
%21 = sub nsw i32 %20, 1
%22 = call i32 @bsr(i32 %21)
%23 = sub nsw i32 %22, 2
br label %25
24: ; preds = %16
br label %25
25: ; preds = %24, %19
%26 = phi i32 [ %23, %19 ], [ 0, %24 ]
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = mul nsw i32 %27, 2
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 1
%31 = load i32, i32* %4
%32 = ashr i32 %30, %31
%33 = add nsw i32 %28, %32
store i32 %33, i32* %2
br label %35
34: ; preds = %10
store i32 -1, i32* %2
br label %35
35: ; preds = %34, %25
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
define i32 @convert_dist_to_dist_sym(i32 %0) {
%2 = icmp slt i32 %0, 32769
%3 = add i32 %0, -1
%4 = icmp ult i32 %3, 32768
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
br i1 %2, label %7, label %17
7: ; preds = %1
%8 = icmp sgt i32 %0, 4
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 @bsr(i32 %3)
%11 = add nsw i32 %10, -2
br label %12
12: ; preds = %7, %9
%13 = phi i32 [ %11, %9 ], [ 0, %7 ]
%14 = shl nsw i32 %13, 1
%15 = ashr i32 %3, %13
%16 = add nsw i32 %14, %15
br label %17
17: ; preds = %1, %12
%18 = phi i32 [ %16, %12 ], [ -1, %1 ]
ret i32 %18
}