#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 */
typedef int /*<<< orphan*/ __m128i;

/* Variables and functions */
int /*<<< orphan*/ _mm_adds_epi16(int /*<<< orphan*/ const,
                                  int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_max_epi16(int /*<<< orphan*/, int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_min_epi16(int /*<<< orphan*/, int /*<<< orphan*/ const);
int /*<<< orphan*/ _mm_set1_epi16(int);
int /*<<< orphan*/ _mm_slli_epi16(int /*<<< orphan*/ const, int const);
int /*<<< orphan*/ _mm_sub_epi16(int /*<<< orphan*/, int /*<<< orphan*/ const);
__m128i add_clamp(const __m128i in0, const __m128i in1, const int bd) {
  const __m128i zero = _mm_set1_epi16(0);
  const __m128i one = _mm_set1_epi16(1);
  const __m128i max = _mm_sub_epi16(_mm_slli_epi16(one, bd), one);
  __m128i d;
  d = _mm_adds_epi16(in0, in1);
  d = _mm_max_epi16(d, zero);
  d = _mm_min_epi16(d, max);
  return d;
}


x86 assembly (clang, -O0):
<add_clamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
xor    %edi,%edi
call   1040 <_mm_set1_epi16@plt>
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   1040 <_mm_set1_epi16@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1080 <_mm_slli_epi16@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%esi
call   1030 <_mm_sub_epi16@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1060 <_mm_adds_epi16@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1050 <_mm_max_epi16@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <_mm_min_epi16@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_clamp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%r12d
xor    %edi,%edi
call   1040 <_mm_set1_epi16@plt>
mov    %eax,%r15d
mov    $0x1,%edi
call   1040 <_mm_set1_epi16@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %ebx,%esi
call   1080 <_mm_slli_epi16@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1030 <_mm_sub_epi16@plt>
mov    %eax,%ebx
mov    %r12d,%edi
mov    %r14d,%esi
call   1060 <_mm_adds_epi16@plt>
mov    %eax,%edi
mov    %r15d,%esi
call   1050 <_mm_max_epi16@plt>
mov    %eax,%edi
mov    %ebx,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <_mm_min_epi16@plt>

LLVM IR (-O0):
define i32 @add_clamp(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = call i32 @_mm_set1_epi16(i32 0)
store i32 %11, i32* %7
%12 = call i32 @_mm_set1_epi16(i32 1)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* %6
%15 = call i32 @_mm_slli_epi16(i32 %13, i32 %14)
%16 = load i32, i32* %8
%17 = call i32 @_mm_sub_epi16(i32 %15, i32 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = call i32 @_mm_adds_epi16(i32 %18, i32 %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = load i32, i32* %7
%23 = call i32 @_mm_max_epi16(i32 %21, i32 %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = load i32, i32* %9
%26 = call i32 @_mm_min_epi16(i32 %24, i32 %25)
store i32 %26, i32* %10
%27 = load i32, i32* %10
ret i32 %27
}

LLVM IR (-O2):
define i32 @add_clamp(i32 %0, i32 %1, i32 %2) {
%4 = tail call i32 @_mm_set1_epi16(i32 0)
%5 = tail call i32 @_mm_set1_epi16(i32 1)
%6 = tail call i32 @_mm_slli_epi16(i32 %5, i32 %2)
%7 = tail call i32 @_mm_sub_epi16(i32 %6, i32 %5)
%8 = tail call i32 @_mm_adds_epi16(i32 %0, i32 %1)
%9 = tail call i32 @_mm_max_epi16(i32 %8, i32 %4)
%10 = tail call i32 @_mm_min_epi16(i32 %9, i32 %7)
ret i32 %10
}