#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 short findLeadingBitPosition(unsigned int);
int errUnsignedToBase2Exponent(unsigned num, unsigned *exponent) {
  unsigned short bitpos, tmp2, index, exp = 0;
  unsigned tmp, remainder;
  int shift;
  if (num <= 0) {
    *exponent = ~2;
    return -2;
  }
  if (num >= 0x80000000) {
    *exponent = ~1;
    return -1;
  }
  bitpos = findLeadingBitPosition(num);
  tmp2 = bitpos;
  tmp = 1 << bitpos;
  remainder = num - tmp;
  if (remainder <= 0x0F) {
    index = remainder << 8;
  } else if (remainder <= 0xFF) {
    index = remainder << 4;
  } else if (remainder <= 0xFFF) {
    index = remainder;
  } else {
    for (shift = 11; shift >= 0; shift--) {
      bitpos--;
      if (remainder & (1 << bitpos)) {
        index |= (1 << shift);
      }
    }
  }
  index &= 0xFFF;
  index += 0xC000;
  *exponent = (tmp2 << 16) | exp;
  return 0;
}


x86 assembly (clang, -O0):
<errUnsignedToBase2Exponent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movw   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
ja     1145 <errUnsignedToBase2Exponent+0x35>
mov    -0x10(%rbp),%rax
movl   $0xfffffffd,(%rax)
movl   $0xfffffffe,-0x4(%rbp)
jmp    1285 <errUnsignedToBase2Exponent+0x175>
cmpl   $0x80000000,-0x8(%rbp)
jb     1168 <errUnsignedToBase2Exponent+0x58>
mov    -0x10(%rbp),%rax
movl   $0xfffffffe,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1285 <errUnsignedToBase2Exponent+0x175>
mov    -0x8(%rbp),%edi
call   1030 <findLeadingBitPosition@plt>
mov    %ax,-0x12(%rbp)
mov    -0x12(%rbp),%ax
mov    %ax,-0x14(%rbp)
movzwl -0x12(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xf,-0x20(%rbp)
ja     11ac <errUnsignedToBase2Exponent+0x9c>
mov    -0x20(%rbp),%eax
shl    $0x8,%eax
mov    %ax,-0x16(%rbp)
jmp    1251 <errUnsignedToBase2Exponent+0x141>
cmpl   $0xff,-0x20(%rbp)
ja     11c8 <errUnsignedToBase2Exponent+0xb8>
mov    -0x20(%rbp),%eax
shl    $0x4,%eax
mov    %ax,-0x16(%rbp)
jmp    124c <errUnsignedToBase2Exponent+0x13c>
cmpl   $0xfff,-0x20(%rbp)
ja     11e1 <errUnsignedToBase2Exponent+0xd1>
mov    -0x20(%rbp),%eax
mov    %ax,-0x16(%rbp)
jmp    1247 <errUnsignedToBase2Exponent+0x137>
movl   $0xb,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1242 <errUnsignedToBase2Exponent+0x132>
mov    -0x12(%rbp),%ax
add    $0xffff,%ax
mov    %ax,-0x12(%rbp)
mov    -0x20(%rbp),%eax
movzwl -0x12(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     122f <errUnsignedToBase2Exponent+0x11f>
mov    -0x24(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,%ecx
movzwl -0x16(%rbp),%eax
or     %ecx,%eax
mov    %ax,-0x16(%rbp)
jmp    1234 <errUnsignedToBase2Exponent+0x124>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11e8 <errUnsignedToBase2Exponent+0xd8>
jmp    1247 <errUnsignedToBase2Exponent+0x137>
jmp    124c <errUnsignedToBase2Exponent+0x13c>
jmp    1251 <errUnsignedToBase2Exponent+0x141>
movzwl -0x16(%rbp),%eax
and    $0xfff,%eax
mov    %ax,-0x16(%rbp)
movzwl -0x16(%rbp),%eax
add    $0xc000,%eax
mov    %ax,-0x16(%rbp)
movzwl -0x14(%rbp),%ecx
shl    $0x10,%ecx
movzwl -0x18(%rbp),%eax
or     %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<errUnsignedToBase2Exponent>:
push   %rbx
mov    %rsi,%rbx
test   %edi,%edi
je     1126 <errUnsignedToBase2Exponent+0x16>
js     1132 <errUnsignedToBase2Exponent+0x22>
call   1030 <findLeadingBitPosition@plt>
shl    $0x10,%eax
xor    %ecx,%ecx
jmp    113c <errUnsignedToBase2Exponent+0x2c>
mov    $0xfffffffe,%ecx
mov    $0xfffffffd,%eax
jmp    113c <errUnsignedToBase2Exponent+0x2c>
mov    $0xffffffff,%ecx
mov    $0xfffffffe,%eax
mov    %eax,(%rbx)
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @errUnsignedToBase2Exponent(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i16
%7 = alloca i16
%8 = alloca i16
%9 = alloca i16
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i16 0, i16* %9
%13 = load i32, i32* %4
%14 = icmp ule i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32*, i32** %5
store i32 -3, i32* %16
store i32 -2, i32* %3
br label %94
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp uge i32 %18, -2147483648
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i32*, i32** %5
store i32 -2, i32* %21
store i32 -1, i32* %3
br label %94
22: ; preds = %17
%23 = load i32, i32* %4
%24 = call zeroext i16 @findLeadingBitPosition(i32 %23)
store i16 %24, i16* %6
%25 = load i16, i16* %6
store i16 %25, i16* %7
%26 = load i16, i16* %6
%27 = zext i16 %26 to i32
%28 = shl i32 1, %27
store i32 %28, i32* %10
%29 = load i32, i32* %4
%30 = load i32, i32* %10
%31 = sub i32 %29, %30
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp ule i32 %32, 15
br i1 %33, label %34, label %38
34: ; preds = %22
%35 = load i32, i32* %11
%36 = shl i32 %35, 8
%37 = trunc i32 %36 to i16
store i16 %37, i16* %8
br label %78
38: ; preds = %22
%39 = load i32, i32* %11
%40 = icmp ule i32 %39, 255
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i32, i32* %11
%43 = shl i32 %42, 4
%44 = trunc i32 %43 to i16
store i16 %44, i16* %8
br label %77
45: ; preds = %38
%46 = load i32, i32* %11
%47 = icmp ule i32 %46, 4095
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %11
%50 = trunc i32 %49 to i16
store i16 %50, i16* %8
br label %76
51: ; preds = %45
store i32 11, i32* %12
br label %52
52: ; preds = %72, %51
%53 = load i32, i32* %12
%54 = icmp sge i32 %53, 0
br i1 %54, label %55, label %75
55: ; preds = %52
%56 = load i16, i16* %6
%57 = add i16 %56, -1
store i16 %57, i16* %6
%58 = load i32, i32* %11
%59 = load i16, i16* %6
%60 = zext i16 %59 to i32
%61 = shl i32 1, %60
%62 = and i32 %58, %61
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %71
64: ; preds = %55
%65 = load i32, i32* %12
%66 = shl i32 1, %65
%67 = load i16, i16* %8
%68 = zext i16 %67 to i32
%69 = or i32 %68, %66
%70 = trunc i32 %69 to i16
store i16 %70, i16* %8
br label %71
71: ; preds = %64, %55
br label %72
72: ; preds = %71
%73 = load i32, i32* %12
%74 = add nsw i32 %73, -1
store i32 %74, i32* %12
br label %52
75: ; preds = %52
br label %76
76: ; preds = %75, %48
br label %77
77: ; preds = %76, %41
br label %78
78: ; preds = %77, %34
%79 = load i16, i16* %8
%80 = zext i16 %79 to i32
%81 = and i32 %80, 4095
%82 = trunc i32 %81 to i16
store i16 %82, i16* %8
%83 = load i16, i16* %8
%84 = zext i16 %83 to i32
%85 = add nsw i32 %84, 49152
%86 = trunc i32 %85 to i16
store i16 %86, i16* %8
%87 = load i16, i16* %7
%88 = zext i16 %87 to i32
%89 = shl i32 %88, 16
%90 = load i16, i16* %9
%91 = zext i16 %90 to i32
%92 = or i32 %89, %91
%93 = load i32*, i32** %5
store i32 %92, i32* %93
store i32 0, i32* %3
br label %94
94: ; preds = %78, %20, %15
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
define i32 @errUnsignedToBase2Exponent(i32 %0, i32* nocapture writeonly %1) {
%3 = icmp eq i32 %0, 0
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = icmp slt i32 %0, 0
br i1 %5, label %10, label %6
6: ; preds = %4
%7 = tail call zeroext i16 @findLeadingBitPosition(i32 %0)
%8 = zext i16 %7 to i32
%9 = shl nuw i32 %8, 16
br label %10
10: ; preds = %4, %2, %6
%11 = phi i32 [ %9, %6 ], [ -3, %2 ], [ -2, %4 ]
%12 = phi i32 [ 0, %6 ], [ -2, %2 ], [ -1, %4 ]
store i32 %11, i32* %1
ret i32 %12
}