#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_ulaw(int);
unsigned char s16_to_ulaw(int pcm_val) {
  int mask;
  int seg;
  unsigned char uval;
  if (pcm_val < 0) {
    pcm_val = 0x84 - pcm_val;
    mask = 0x7f;
  } else {
    pcm_val += 0x84;
    mask = 0xff;
  }
  if (pcm_val > 0x7fff)
    pcm_val = 0x7fff;
  seg = val_seg_ulaw(pcm_val);
  uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
  return uval ^ mask;
}


x86 assembly (clang, -O0):
<s16_to_ulaw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    113c <s16_to_ulaw+0x2c>
mov    $0x84,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x7f,-0x8(%rbp)
jmp    114e <s16_to_ulaw+0x3e>
mov    -0x4(%rbp),%eax
add    $0x84,%eax
mov    %eax,-0x4(%rbp)
movl   $0xff,-0x8(%rbp)
cmpl   $0x7fff,-0x4(%rbp)
jle    1162 <s16_to_ulaw+0x52>
movl   $0x7fff,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <val_seg_ulaw@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_ulaw>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
not    %ebp
shr    $0x18,%ebp
or     $0x7f,%ebp
mov    %edi,%eax
neg    %eax
cmovs  %edi,%eax
add    $0x84,%eax
cmp    $0x7fff,%eax
mov    $0x7fff,%ebx
cmovl  %eax,%ebx
mov    %ebx,%edi
call   1030 <val_seg_ulaw@plt>
lea    0x3(%rax),%ecx
sar    %cl,%ebx
shl    $0x4,%eax
and    $0xf,%ebx
or     %eax,%ebx
xor    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define zeroext i8 @s16_to_ulaw(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 slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* %2
%10 = sub nsw i32 132, %9
store i32 %10, i32* %2
store i32 127, i32* %3
br label %14
11: ; preds = %1
%12 = load i32, i32* %2
%13 = add nsw i32 %12, 132
store i32 %13, i32* %2
store i32 255, i32* %3
br label %14
14: ; preds = %11, %8
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 32767
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 32767, i32* %2
br label %18
18: ; preds = %17, %14
%19 = load i32, i32* %2
%20 = call i32 @val_seg_ulaw(i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = shl i32 %21, 4
%23 = load i32, i32* %2
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 3
%26 = ashr i32 %23, %25
%27 = and i32 %26, 15
%28 = or i32 %22, %27
%29 = trunc i32 %28 to i8
store i8 %29, i8* %5
%30 = load i8, i8* %5
%31 = zext i8 %30 to i32
%32 = load i32, i32* %3
%33 = xor i32 %31, %32
%34 = trunc i32 %33 to i8
ret i8 %34
}

LLVM IR (-O2):
define zeroext i8 @s16_to_ulaw(i32 %0) {
%2 = icmp slt i32 %0, 0
%3 = select i1 %2, i32 127, i32 255
%4 = sub i32 0, %0
%5 = select i1 %2, i32 %4, i32 %0
%6 = add nuw i32 %5, 132
%7 = icmp slt i32 %6, 32767
%8 = select i1 %7, i32 %6, i32 32767
%9 = tail call i32 @val_seg_ulaw(i32 %8)
%10 = shl i32 %9, 4
%11 = add nsw i32 %9, 3
%12 = ashr i32 %8, %11
%13 = and i32 %12, 15
%14 = or i32 %13, %10
%15 = xor i32 %14, %3
%16 = trunc i32 %15 to i8
ret i8 %16
}