unsigned char linear2alaw(int sample) {
  int sign, mantissa, exponent;
  static unsigned char table[128] = {
      0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 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, 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};
  sign = (sample >> 8) & 0x80;
  if (sign)
    sample = ~sample;
  sample >>= 4;
  exponent = table[sample >> 4];
  if (exponent > 1)
    sample >>= exponent - 1;
  return ((sample & 0x000f) | (exponent << 4) | (sign ^ 0x80)) ^ 0x0055;
}


x86 assembly (clang, -O0):
<linear2alaw>:
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     1128 <linear2alaw+0x28>
mov    -0x4(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
movslq %eax,%rcx
lea    0x2eef(%rip),%rax
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    1160 <linear2alaw+0x60>
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x4(%rbp),%eax
sar    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
and    $0xf,%eax
mov    -0x10(%rbp),%ecx
shl    $0x4,%ecx
or     %ecx,%eax
mov    -0x8(%rbp),%ecx
xor    $0x80,%ecx
or     %ecx,%eax
xor    $0x55,%eax
movzbl %al,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<linear2alaw>:
mov    %edi,%eax
shr    $0x8,%eax
and    $0x80,%eax
movsbl %al,%edx
sar    $0x7,%edx
xor    %edi,%edx
mov    %edx,%esi
sar    $0x4,%esi
mov    %edx,%ecx
sar    $0x8,%ecx
movslq %ecx,%rcx
lea    0xeda(%rip),%rdi
movzbl (%rcx,%rdi,1),%edi
lea    -0x1(%rdi),%r8d
xor    %r9d,%r9d
cmp    $0x1,%ecx
movzbl %r8b,%ecx
cmove  %r9d,%ecx
cmp    $0x100,%edx
cmovb  %r9d,%ecx
sar    %cl,%esi
and    $0xf,%esi
shl    $0x4,%edi
or     %esi,%edi
xor    $0x80,%eax
or     %edi,%eax
xor    $0x55,%al
ret

LLVM IR (-O0):
@linear2alaw.table = internal global [128 x i8] c"\00\01\02\02\03\03\03\03\04\04\04\04\04\04\04\04\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07"
define zeroext i8 @linear2alaw(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = ashr i32 %6, 8
%8 = and i32 %7, 128
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* %2
%13 = xor i32 %12, -1
store i32 %13, i32* %2
br label %14
14: ; preds = %11, %1
%15 = load i32, i32* %2
%16 = ashr i32 %15, 4
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = ashr i32 %17, 4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [128 x i8], [128 x i8]* @linear2alaw.table, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 1
br i1 %24, label %25, label %30
25: ; preds = %14
%26 = load i32, i32* %5
%27 = sub nsw i32 %26, 1
%28 = load i32, i32* %2
%29 = ashr i32 %28, %27
store i32 %29, i32* %2
br label %30
30: ; preds = %25, %14
%31 = load i32, i32* %2
%32 = and i32 %31, 15
%33 = load i32, i32* %5
%34 = shl i32 %33, 4
%35 = or i32 %32, %34
%36 = load i32, i32* %3
%37 = xor i32 %36, 128
%38 = or i32 %35, %37
%39 = xor i32 %38, 85
%40 = trunc i32 %39 to i8
ret i8 %40
}

LLVM IR (-O2):
@linear2alaw.table = internal unnamed_addr constant [128 x i8] c"\00\01\02\02\03\03\03\03\04\04\04\04\04\04\04\04\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\05\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\06\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07\07"
define zeroext i8 @linear2alaw(i32 %0) {
%2 = lshr i32 %0, 8
%3 = and i32 %2, 128
%4 = shl nuw i32 %3, 24
%5 = ashr exact i32 %4, 31
%6 = xor i32 %5, %0
%7 = ashr i32 %6, 4
%8 = ashr i32 %6, 8
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [128 x i8], [128 x i8]* @linear2alaw.table, i64 0, i64 %9
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = icmp ugt i32 %6, 255
%14 = icmp ne i32 %8, 1
%15 = and i1 %13, %14
%16 = add nsw i32 %12, -1
%17 = select i1 %15, i32 %16, i32 0
%18 = ashr i32 %7, %17
%19 = and i32 %18, 15
%20 = shl nuw nsw i32 %12, 4
%21 = or i32 %19, %20
%22 = xor i32 %3, 128
%23 = or i32 %21, %22
%24 = trunc i32 %23 to i8
%25 = xor i8 %24, 85
ret i8 %25
}