#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*/ const __m128i;

/* Variables and functions */
int /*<<< orphan*/ const _mm_add_epi16(int /*<<< orphan*/ const,
                                       int /*<<< orphan*/ const);
int /*<<< orphan*/ const _mm_set1_epi16(int);
int /*<<< orphan*/ const _mm_srai_epi16(int /*<<< orphan*/ const, int);
__m128i xx_roundn_epi16(__m128i v_val_d, int bits) {
  const __m128i v_bias_d = _mm_set1_epi16((1 << bits) >> 1);
  const __m128i v_sign_d = _mm_srai_epi16(v_val_d, 15);
  const __m128i v_tmp_d =
      _mm_add_epi16(_mm_add_epi16(v_val_d, v_bias_d), v_sign_d);
  return _mm_srai_epi16(v_tmp_d, bits);
}


x86 assembly (clang, -O0):
<xx_roundn_epi16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%ecx
mov    $0x1,%edi
shl    %cl,%edi
sar    $0x1,%edi
call   1040 <_mm_set1_epi16@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    $0xf,%esi
call   1050 <_mm_srai_epi16@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <_mm_add_epi16@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%esi
call   1030 <_mm_add_epi16@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <_mm_srai_epi16@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xx_roundn_epi16>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x1,%edi
mov    %r14d,%ecx
shl    %cl,%edi
sar    %edi
call   1040 <_mm_set1_epi16@plt>
mov    %eax,%r15d
mov    %ebp,%edi
mov    $0xf,%esi
call   1050 <_mm_srai_epi16@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r15d,%esi
call   1030 <_mm_add_epi16@plt>
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <_mm_add_epi16@plt>
mov    %eax,%edi
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <_mm_srai_epi16@plt>

LLVM IR (-O0):
define i32 @xx_roundn_epi16(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = shl i32 1, %8
%10 = ashr i32 %9, 1
%11 = call i32 @_mm_set1_epi16(i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %3
%13 = call i32 @_mm_srai_epi16(i32 %12, i32 15)
store i32 %13, i32* %6
%14 = load i32, i32* %3
%15 = load i32, i32* %5
%16 = call i32 @_mm_add_epi16(i32 %14, i32 %15)
%17 = load i32, i32* %6
%18 = call i32 @_mm_add_epi16(i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = load i32, i32* %4
%21 = call i32 @_mm_srai_epi16(i32 %19, i32 %20)
ret i32 %21
}

LLVM IR (-O2):
define i32 @xx_roundn_epi16(i32 %0, i32 %1) {
%3 = shl nuw i32 1, %1
%4 = ashr i32 %3, 1
%5 = tail call i32 @_mm_set1_epi16(i32 %4)
%6 = tail call i32 @_mm_srai_epi16(i32 %0, i32 15)
%7 = tail call i32 @_mm_add_epi16(i32 %0, i32 %5)
%8 = tail call i32 @_mm_add_epi16(i32 %7, i32 %6)
%9 = tail call i32 @_mm_srai_epi16(i32 %8, i32 %1)
ret i32 %9
}