#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 */
unsigned int Log2(unsigned int);
int /*<<< orphan*/ assert(int);
unsigned bits_bin_code(unsigned value, unsigned maxval) {
  unsigned k;
  unsigned r;
  assert(maxval && value <= maxval);
  k = Log2(maxval + 1);
  r = (maxval + 1) % (1 << k);
  return value < maxval + 1 - 2 * r ? k : k + 1;
}


x86 assembly (clang, -O0):
<bits_bin_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x11(%rbp)
je     1149 <bits_bin_code+0x29>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
setbe  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
call   1030 <Log2@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    -0xc(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%edx
shl    $0x1,%edx
sub    %edx,%ecx
cmp    %ecx,%eax
jae    11a1 <bits_bin_code+0x81>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11aa <bits_bin_code+0x8a>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bits_bin_code>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%r14d
test   %esi,%esi
setne  %al
cmp    %esi,%edi
setbe  %cl
and    %al,%cl
movzbl %cl,%edi
call   1040 <assert@plt>
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
call   1030 <Log2@plt>
mov    $0xffffffff,%edx
mov    %eax,%ecx
shl    %cl,%edx
not    %edx
and    %ebp,%edx
add    %edx,%edx
neg    %edx
lea    (%rbx,%rdx,1),%ecx
add    $0x1,%ecx
cmp    %ecx,%r14d
sbb    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @bits_bin_code(i32 %0, i32 %1) {
%3 = icmp ne i32 %1, 0
%4 = icmp ule i32 %0, %1
%5 = and i1 %3, %4
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = add i32 %1, 1
%9 = tail call i32 @Log2(i32 %8)
%10 = shl nsw i32 -1, %9
%11 = xor i32 %10, -1
%12 = and i32 %8, %11
%13 = mul i32 %12, -2
%14 = add i32 %13, %8
%15 = icmp ule i32 %14, %0
%16 = zext i1 %15 to i32
%17 = add i32 %9, %16
ret i32 %17
}