unsigned char linear2alaw(short int linear) {
  int mask;
  int seg;
  int pcm_val;
  static int seg_end[8] = {0xFF,  0x1FF,  0x3FF,  0x7FF,
                           0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
  pcm_val = linear;
  if (pcm_val >= 0) {
    mask = 0x55 | 0x80;
  } else {
    mask = 0x55;
    pcm_val = -pcm_val;
  }
  for (seg = 0; seg < 8; seg++) {
    if (pcm_val <= seg_end[seg])
      break;
  }
  return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
}


x86 assembly (clang, -O0):
<linear2alaw>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
movswl -0x2(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1128 <linear2alaw+0x28>
movl   $0xd5,-0x8(%rbp)
jmp    1137 <linear2alaw+0x37>
movl   $0x55,-0x8(%rbp)
xor    %eax,%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x8,-0xc(%rbp)
jge    1177 <linear2alaw+0x77>
mov    -0x10(%rbp),%eax
movslq -0xc(%rbp),%rdx
lea    0x2eda(%rip),%rcx
cmp    (%rcx,%rdx,4),%eax
jg     1164 <linear2alaw+0x64>
jmp    1177 <linear2alaw+0x77>
jmp    1169 <linear2alaw+0x69>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113e <linear2alaw+0x3e>
mov    -0xc(%rbp),%eax
shl    $0x4,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     119e <linear2alaw+0x9e>
mov    -0xc(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ab <linear2alaw+0xab>
mov    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ab <linear2alaw+0xab>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
sar    %cl,%edx
mov    %edx,%ecx
and    $0xf,%ecx
or     %ecx,%eax
xor    -0x8(%rbp),%eax
movzbl %al,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<linear2alaw>:
mov    %edi,%eax
neg    %eax
cmovs  %edi,%eax
cmp    $0x100,%eax
jb     117b <linear2alaw+0x7b>
mov    $0x1,%ecx
cmp    $0x200,%eax
jb     1163 <linear2alaw+0x63>
mov    $0x2,%ecx
cmp    $0x400,%eax
jb     1163 <linear2alaw+0x63>
mov    $0x3,%ecx
cmp    $0x800,%eax
jb     1163 <linear2alaw+0x63>
mov    $0x4,%ecx
cmp    $0x1000,%eax
jb     1163 <linear2alaw+0x63>
mov    $0x5,%ecx
cmp    $0x2000,%eax
jb     1163 <linear2alaw+0x63>
mov    $0x6,%ecx
cmp    $0x4000,%eax
jb     1163 <linear2alaw+0x63>
cmp    $0x8000,%eax
mov    $0x7,%ecx
sbb    $0xffffffff,%ecx
xor    %edx,%edx
test   %di,%di
setns  %dl
shl    $0x7,%edx
or     $0x55,%edx
mov    %ecx,%esi
shl    $0x4,%esi
add    $0x3,%ecx
jmp    1190 <linear2alaw+0x90>
xor    %edx,%edx
test   %di,%di
setns  %dl
shl    $0x7,%edx
or     $0x55,%edx
mov    $0x4,%ecx
xor    %esi,%esi
shr    %cl,%eax
and    $0xf,%eax
or     %esi,%eax
xor    %edx,%eax
ret

LLVM IR (-O0):
@linear2alaw.seg_end = internal global [8 x i32] [i32 255, i32 511, i32 1023, i32 2047, i32 4095, i32 8191, i32 16383, i32 32767]
define zeroext i8 @linear2alaw(i16 signext %0) {
%2 = alloca i16
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i16 %0, i16* %2
%6 = load i16, i16* %2
%7 = sext i16 %6 to i32
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 213, i32* %3
br label %14
11: ; preds = %1
store i32 85, i32* %3
%12 = load i32, i32* %5
%13 = sub nsw i32 0, %12
store i32 %13, i32* %5
br label %14
14: ; preds = %11, %10
store i32 0, i32* %4
br label %15
15: ; preds = %27, %14
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 8
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [8 x i32], [8 x i32]* @linear2alaw.seg_end, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = icmp sle i32 %19, %23
br i1 %24, label %25, label %26
25: ; preds = %18
br label %30
26: ; preds = %18
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %15
30: ; preds = %25, %15
%31 = load i32, i32* %4
%32 = shl i32 %31, 4
%33 = load i32, i32* %5
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 3
br label %40
39: ; preds = %30
br label %40
40: ; preds = %39, %36
%41 = phi i32 [ %38, %36 ], [ 4, %39 ]
%42 = ashr i32 %33, %41
%43 = and i32 %42, 15
%44 = or i32 %32, %43
%45 = load i32, i32* %3
%46 = xor i32 %44, %45
%47 = trunc i32 %46 to i8
ret i8 %47
}

LLVM IR (-O2):
define zeroext i8 @linear2alaw(i16 signext %0) {
%2 = sext i16 %0 to i32
%3 = icmp sgt i16 %0, -1
%4 = sub nsw i32 0, %2
%5 = select i1 %3, i32 %2, i32 %4
%6 = icmp ugt i32 %5, 255
br i1 %6, label %7, label %27
7: ; preds = %1
%8 = icmp ugt i32 %5, 511
br i1 %8, label %9, label %22
9: ; preds = %7
%10 = icmp ugt i32 %5, 1023
br i1 %10, label %11, label %22
11: ; preds = %9
%12 = icmp ugt i32 %5, 2047
br i1 %12, label %13, label %22
13: ; preds = %11
%14 = icmp ugt i32 %5, 4095
br i1 %14, label %15, label %22
15: ; preds = %13
%16 = icmp ugt i32 %5, 8191
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = icmp ugt i32 %5, 16383
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = icmp ugt i32 %5, 32767
%21 = select i1 %20, i32 8, i32 7
br label %22
22: ; preds = %19, %17, %15, %13, %11, %9, %7
%23 = phi i32 [ 1, %7 ], [ 2, %9 ], [ 3, %11 ], [ 4, %13 ], [ 5, %15 ], [ 6, %17 ], [ %21, %19 ]
%24 = select i1 %3, i32 213, i32 85
%25 = shl nuw nsw i32 %23, 4
%26 = add nuw nsw i32 %23, 3
br label %29
27: ; preds = %1
%28 = select i1 %3, i32 213, i32 85
br label %29
29: ; preds = %22, %27
%30 = phi i32 [ 0, %27 ], [ %25, %22 ]
%31 = phi i32 [ %28, %27 ], [ %24, %22 ]
%32 = phi i32 [ 4, %27 ], [ %26, %22 ]
%33 = lshr i32 %5, %32
%34 = and i32 %33, 15
%35 = or i32 %34, %30
%36 = xor i32 %35, %31
%37 = trunc i32 %36 to i8
ret i8 %37
}