#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 uint8_t;
typedef int int16_t;

/* Variables and functions */
uint8_t comp_encode(int16_t sample) {
  int exp;
  int sign = sample & 0x8000;
  if (sign)
    sample = -sample;
  if (sample > 8158)
    sample = 8158;
  sample += 33;
  if (sample & (1 << 12))
    exp = 7;
  else if (sample & (1 << 11))
    exp = 6;
  else if (sample & (1 << 10))
    exp = 5;
  else if (sample & (1 << 9))
    exp = 4;
  else if (sample & (1 << 8))
    exp = 3;
  else if (sample & (1 << 7))
    exp = 2;
  else if (sample & (1 << 6))
    exp = 1;
  else
    exp = 0;
  return ~(((sample >> (exp + 1)) & 0xF) | (exp << 4) | (sign >> 8));
}


x86 assembly (clang, -O0):
<comp_encode>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0x8000,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1124 <comp_encode+0x24>
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x1fde,-0x4(%rbp)
jle    1138 <comp_encode+0x38>
movl   $0x1fde,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x21,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0x1000,%eax
cmp    $0x0,%eax
je     115e <comp_encode+0x5e>
movl   $0x7,-0x8(%rbp)
jmp    122f <comp_encode+0x12f>
mov    -0x4(%rbp),%eax
and    $0x800,%eax
cmp    $0x0,%eax
je     117b <comp_encode+0x7b>
movl   $0x6,-0x8(%rbp)
jmp    122a <comp_encode+0x12a>
mov    -0x4(%rbp),%eax
and    $0x400,%eax
cmp    $0x0,%eax
je     1198 <comp_encode+0x98>
movl   $0x5,-0x8(%rbp)
jmp    1225 <comp_encode+0x125>
mov    -0x4(%rbp),%eax
and    $0x200,%eax
cmp    $0x0,%eax
je     11b5 <comp_encode+0xb5>
movl   $0x4,-0x8(%rbp)
jmp    1220 <comp_encode+0x120>
mov    -0x4(%rbp),%eax
and    $0x100,%eax
cmp    $0x0,%eax
je     11d2 <comp_encode+0xd2>
movl   $0x3,-0x8(%rbp)
jmp    121b <comp_encode+0x11b>
mov    -0x4(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11ef <comp_encode+0xef>
movl   $0x2,-0x8(%rbp)
jmp    1216 <comp_encode+0x116>
mov    -0x4(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
je     120a <comp_encode+0x10a>
movl   $0x1,-0x8(%rbp)
jmp    1211 <comp_encode+0x111>
movl   $0x0,-0x8(%rbp)
jmp    1216 <comp_encode+0x116>
jmp    121b <comp_encode+0x11b>
jmp    1220 <comp_encode+0x120>
jmp    1225 <comp_encode+0x125>
jmp    122a <comp_encode+0x12a>
jmp    122f <comp_encode+0x12f>
mov    -0x4(%rbp),%eax
mov    -0x8(%rbp),%ecx
add    $0x1,%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    -0x8(%rbp),%ecx
shl    $0x4,%ecx
or     %ecx,%eax
mov    -0xc(%rbp),%ecx
sar    $0x8,%ecx
or     %ecx,%eax
xor    $0xffffffff,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<comp_encode>:
mov    %edi,%ecx
neg    %ecx
mov    %edi,%eax
and    $0x8000,%eax
cmove  %edi,%ecx
cmp    $0x1fde,%ecx
mov    $0x1fde,%edx
cmovl  %ecx,%edx
add    $0x21,%edx
mov    $0x7,%esi
test   $0x1000,%edx
jne    1172 <comp_encode+0x72>
mov    $0x6,%esi
test   $0x800,%edx
jne    1172 <comp_encode+0x72>
mov    $0x5,%esi
test   $0x400,%edx
jne    1172 <comp_encode+0x72>
mov    $0x4,%esi
test   $0x200,%edx
jne    1172 <comp_encode+0x72>
mov    $0x3,%esi
test   $0x100,%edx
jne    1172 <comp_encode+0x72>
mov    $0x2,%esi
test   $0x80,%dl
jne    1172 <comp_encode+0x72>
mov    %edx,%esi
shr    $0x6,%esi
and    $0x1,%esi
lea    0x1(%rsi),%ecx
sar    %cl,%edx
and    $0xf,%edx
shl    $0x4,%esi
or     %edx,%esi
shr    $0x8,%eax
or     %esi,%eax
not    %eax
ret

LLVM IR (-O0):
define i32 @comp_encode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = and i32 %5, 32768
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* %2
%11 = sub nsw i32 0, %10
store i32 %11, i32* %2
br label %12
12: ; preds = %9, %1
%13 = load i32, i32* %2
%14 = icmp sgt i32 %13, 8158
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 8158, i32* %2
br label %16
16: ; preds = %15, %12
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 33
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = and i32 %19, 4096
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 7, i32* %3
br label %60
23: ; preds = %16
%24 = load i32, i32* %2
%25 = and i32 %24, 2048
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 6, i32* %3
br label %59
28: ; preds = %23
%29 = load i32, i32* %2
%30 = and i32 %29, 1024
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 5, i32* %3
br label %58
33: ; preds = %28
%34 = load i32, i32* %2
%35 = and i32 %34, 512
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 4, i32* %3
br label %57
38: ; preds = %33
%39 = load i32, i32* %2
%40 = and i32 %39, 256
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
store i32 3, i32* %3
br label %56
43: ; preds = %38
%44 = load i32, i32* %2
%45 = and i32 %44, 128
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 2, i32* %3
br label %55
48: ; preds = %43
%49 = load i32, i32* %2
%50 = and i32 %49, 64
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 1, i32* %3
br label %54
53: ; preds = %48
store i32 0, i32* %3
br label %54
54: ; preds = %53, %52
br label %55
55: ; preds = %54, %47
br label %56
56: ; preds = %55, %42
br label %57
57: ; preds = %56, %37
br label %58
58: ; preds = %57, %32
br label %59
59: ; preds = %58, %27
br label %60
60: ; preds = %59, %22
%61 = load i32, i32* %2
%62 = load i32, i32* %3
%63 = add nsw i32 %62, 1
%64 = ashr i32 %61, %63
%65 = and i32 %64, 15
%66 = load i32, i32* %3
%67 = shl i32 %66, 4
%68 = or i32 %65, %67
%69 = load i32, i32* %4
%70 = ashr i32 %69, 8
%71 = or i32 %68, %70
%72 = xor i32 %71, -1
ret i32 %72
}

LLVM IR (-O2):
define i32 @comp_encode(i32 %0) {
%2 = and i32 %0, 32768
%3 = icmp eq i32 %2, 0
%4 = sub nsw i32 0, %0
%5 = select i1 %3, i32 %0, i32 %4
%6 = icmp slt i32 %5, 8158
%7 = select i1 %6, i32 %5, i32 8158
%8 = add nsw i32 %7, 33
%9 = and i32 %8, 4096
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %29
11: ; preds = %1
%12 = and i32 %8, 2048
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %11
%15 = and i32 %8, 1024
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = and i32 %8, 512
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = and i32 %8, 256
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = and i32 %8, 128
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = lshr i32 %8, 6
%28 = and i32 %27, 1
br label %29
29: ; preds = %26, %23, %20, %17, %14, %11, %1
%30 = phi i32 [ 7, %1 ], [ 6, %11 ], [ 5, %14 ], [ 4, %17 ], [ 3, %20 ], [ 2, %23 ], [ %28, %26 ]
%31 = add nuw nsw i32 %30, 1
%32 = ashr i32 %8, %31
%33 = and i32 %32, 15
%34 = shl nuw nsw i32 %30, 4
%35 = or i32 %33, %34
%36 = lshr exact i32 %2, 8
%37 = or i32 %35, %36
%38 = xor i32 %37, -1
ret i32 %38
}