unsigned char ulaw_encode(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);
  return ulawbyte;
}


x86 assembly (clang, -O0):
<ulaw_encode>:
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 <ulaw_encode+0x27>
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x7f7b,-0x4(%rbp)
jle    113b <ulaw_encode+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
pop    %rbp
ret

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

LLVM IR (-O0):
@ulaw_encode.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 @ulaw_encode(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]* @ulaw_encode.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
ret i8 %41
}

LLVM IR (-O2):
@ulaw_encode.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 @ulaw_encode(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]* @ulaw_encode.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
ret i8 %22
}