unsigned char calc_linear_to_ulaw(int sample) {
  static int exp_lut[256] = {
      0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
      4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
      5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6,
      6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
      6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
      6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
      7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
      7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
      7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
      7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
      7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
  int sign, exponent, mantissa;
  unsigned char ulawbyte;
  sign = (sample >> 8) & 0x80;
  if (sign != 0)
    sample = -sample;
  if (sample > 32635)
    sample = 32635;
  sample = sample + 0x84;
  exponent = exp_lut[(sample >> 7) & 0xFF];
  mantissa = (sample >> (exponent + 3)) & 0x0F;
  ulawbyte = ~(sign | (exponent << 4) | mantissa);
  if (ulawbyte == 0)
    ulawbyte = 0x02;
  return (ulawbyte);
}


x86 assembly (clang, -O0):
<calc_linear_to_ulaw>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x8,%eax
and    $0x80,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1127 <calc_linear_to_ulaw+0x27>
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x7f7b,-0x4(%rbp)
jle    113b <calc_linear_to_ulaw+0x3b>
movl   $0x7f7b,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x84,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x7,%eax
and    $0xff,%eax
movslq %eax,%rcx
lea    0x2ed5(%rip),%rax
mov    (%rax,%rcx,4),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x3,%ecx
sar    %cl,%eax
and    $0xf,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x4,%ecx
or     %ecx,%eax
or     -0x10(%rbp),%eax
xor    $0xffffffff,%eax
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    1197 <calc_linear_to_ulaw+0x97>
movb   $0x2,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_linear_to_ulaw>:
mov    %edi,%eax
shr    $0x8,%eax
and    $0x80,%eax
mov    %edi,%ecx
neg    %ecx
test   %eax,%eax
cmove  %edi,%ecx
cmp    $0x7f7b,%ecx
mov    $0x7f7b,%edx
cmovl  %ecx,%edx
add    $0x84,%edx
mov    %edx,%ecx
shr    $0x7,%ecx
movzbl %cl,%ecx
lea    0xeca(%rip),%rsi
mov    (%rsi,%rcx,4),%esi
lea    0x3(%rsi),%ecx
sar    %cl,%edx
and    $0xf,%edx
shl    $0x4,%esi
or     %eax,%esi
or     %edx,%esi
cmp    $0xff,%sil
not    %sil
movzbl %sil,%ecx
mov    $0x2,%eax
cmovne %ecx,%eax
ret

LLVM IR (-O0):
@calc_linear_to_ulaw.exp_lut = internal global [256 x i32] [i32 0, i32 0, i32 1, i32 1, i32 2, i32 2, i32 2, i32 2, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7]
define zeroext i8 @calc_linear_to_ulaw(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = ashr i32 %7, 8
%9 = and i32 %8, 128
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* %2
%14 = sub nsw i32 0, %13
store i32 %14, i32* %2
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* %2
%17 = icmp sgt i32 %16, 32635
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 32635, i32* %2
br label %19
19: ; preds = %18, %15
%20 = load i32, i32* %2
%21 = add nsw i32 %20, 132
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = ashr i32 %22, 7
%24 = and i32 %23, 255
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [256 x i32], [256 x i32]* @calc_linear_to_ulaw.exp_lut, i64 0, i64 %25
%27 = load i32, i32* %26
store i32 %27, i32* %4
%28 = load i32, i32* %2
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 3
%31 = ashr i32 %28, %30
%32 = and i32 %31, 15
store i32 %32, i32* %5
%33 = load i32, i32* %3
%34 = load i32, i32* %4
%35 = shl i32 %34, 4
%36 = or i32 %33, %35
%37 = load i32, i32* %5
%38 = or i32 %36, %37
%39 = xor i32 %38, -1
%40 = trunc i32 %39 to i8
store i8 %40, i8* %6
%41 = load i8, i8* %6
%42 = zext i8 %41 to i32
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %19
store i8 2, i8* %6
br label %45
45: ; preds = %44, %19
%46 = load i8, i8* %6
ret i8 %46
}

LLVM IR (-O2):
@calc_linear_to_ulaw.exp_lut = internal unnamed_addr constant [256 x i32] [i32 0, i32 0, i32 1, i32 1, i32 2, i32 2, i32 2, i32 2, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 6, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7]
define zeroext i8 @calc_linear_to_ulaw(i32 %0) {
%2 = lshr i32 %0, 8
%3 = and i32 %2, 128
%4 = icmp eq i32 %3, 0
%5 = sub nsw i32 0, %0
%6 = select i1 %4, i32 %0, i32 %5
%7 = icmp slt i32 %6, 32635
%8 = select i1 %7, i32 %6, i32 32635
%9 = add nsw i32 %8, 132
%10 = lshr i32 %9, 7
%11 = and i32 %10, 255
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [256 x i32], [256 x i32]* @calc_linear_to_ulaw.exp_lut, i64 0, i64 %12
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 3
%16 = ashr i32 %9, %15
%17 = and i32 %16, 15
%18 = shl i32 %14, 4
%19 = or i32 %18, %3
%20 = or i32 %19, %17
%21 = trunc i32 %20 to i8
%22 = xor i8 %21, -1
%23 = icmp eq i8 %21, -1
%24 = select i1 %23, i8 2, i8 %22
ret i8 %24
}