#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 */

/* Variables and functions */
int val_seg(int);
unsigned char s16_to_alaw(int pcm_val) {
  int mask;
  int seg;
  unsigned char aval;
  if (pcm_val >= 0) {
    mask = 0xD5;
  } else {
    mask = 0x55;
    pcm_val = -pcm_val;
    if (pcm_val > 0x7fff)
      pcm_val = 0x7fff;
  }
  if (pcm_val < 256)
    aval = pcm_val >> 4;
  else {
    seg = val_seg(pcm_val);
    aval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
  }
  return aval ^ mask;
}


x86 assembly (clang, -O0):
<s16_to_alaw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1131 <s16_to_alaw+0x21>
movl   $0xd5,-0x8(%rbp)
jmp    1159 <s16_to_alaw+0x49>
movl   $0x55,-0x8(%rbp)
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
cmpl   $0x7fff,-0x4(%rbp)
jle    1154 <s16_to_alaw+0x44>
movl   $0x7fff,-0x4(%rbp)
jmp    1159 <s16_to_alaw+0x49>
cmpl   $0x100,-0x4(%rbp)
jge    1174 <s16_to_alaw+0x64>
mov    -0x4(%rbp),%eax
sar    $0x4,%eax
mov    %al,-0xd(%rbp)
jmp    119a <s16_to_alaw+0x8a>
mov    -0x4(%rbp),%edi
call   1030 <val_seg@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x4,%eax
mov    -0x4(%rbp),%edx
mov    -0xc(%rbp),%ecx
add    $0x3,%ecx
sar    %cl,%edx
mov    %edx,%ecx
and    $0xf,%ecx
or     %ecx,%eax
mov    %al,-0xd(%rbp)
movzbl -0xd(%rbp),%eax
xor    -0x8(%rbp),%eax
movzbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s16_to_alaw>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %edi,%eax
neg    %eax
cmp    $0x7fff,%eax
mov    $0x7fff,%ebp
cmovl  %eax,%ebp
test   %edi,%edi
cmovns %edi,%ebp
cmp    $0xff,%ebp
jg     1138 <s16_to_alaw+0x28>
sar    $0x4,%ebp
jmp    114c <s16_to_alaw+0x3c>
mov    %ebp,%edi
call   1030 <val_seg@plt>
lea    0x3(%rax),%ecx
shr    %cl,%ebp
shl    $0x4,%eax
and    $0xf,%ebp
or     %eax,%ebp
not    %ebx
shr    $0x18,%ebx
and    $0xffffff80,%ebx
or     $0x55,%ebx
xor    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @s16_to_alaw(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 213, i32* %3
br label %16
9: ; preds = %1
store i32 85, i32* %3
%10 = load i32, i32* %2
%11 = sub nsw i32 0, %10
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp sgt i32 %12, 32767
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 32767, i32* %2
br label %15
15: ; preds = %14, %9
br label %16
16: ; preds = %15, %8
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 256
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* %2
%21 = ashr i32 %20, 4
%22 = trunc i32 %21 to i8
store i8 %22, i8* %5
br label %35
23: ; preds = %16
%24 = load i32, i32* %2
%25 = call i32 @val_seg(i32 %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = shl i32 %26, 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
%33 = or i32 %27, %32
%34 = trunc i32 %33 to i8
store i8 %34, i8* %5
br label %35
35: ; preds = %23, %19
%36 = load i8, i8* %5
%37 = zext i8 %36 to i32
%38 = load i32, i32* %3
%39 = xor i32 %37, %38
%40 = trunc i32 %39 to i8
ret i8 %40
}

LLVM IR (-O2):
define zeroext i8 @s16_to_alaw(i32 %0) {
%2 = icmp sgt i32 %0, -1
%3 = sub nsw i32 0, %0
%4 = icmp slt i32 %3, 32767
%5 = select i1 %4, i32 %3, i32 32767
%6 = select i1 %2, i32 %0, i32 %5
%7 = icmp slt i32 %6, 256
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = ashr i32 %6, 4
br label %17
10: ; preds = %1
%11 = tail call i32 @val_seg(i32 %6)
%12 = shl i32 %11, 4
%13 = add nsw i32 %11, 3
%14 = lshr i32 %6, %13
%15 = and i32 %14, 15
%16 = or i32 %15, %12
br label %17
17: ; preds = %10, %8
%18 = phi i32 [ %9, %8 ], [ %16, %10 ]
%19 = select i1 %2, i32 213, i32 85
%20 = xor i32 %18, %19
%21 = trunc i32 %20 to i8
ret i8 %21
}