#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 */
void _set_ushort(char *data, size_t pos, double value) {
  unsigned short *buf = (unsigned short *)data;
  buf[pos] = (unsigned short)((((value + 0.5) > 0.0
                                    ? (int)(value + 0.5)
                                    : (((int)(value + 0.5) - value + 0.5) != 0.0
                                           ? (int)(value + 0.5) - 1
                                           : (int)(value + 0.5)))));
  return;
}


x86 assembly (clang, -O0):
<_set_ushort>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movsd  0xedf(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    114c <_set_ushort+0x4c>
movsd  0xec5(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
jmp    11c0 <_set_ushort+0xc0>
movsd  0xeac(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%eax
cvtsi2sd %eax,%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  0xe92(%rip),%xmm1
addsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    118a <_set_ushort+0x8a>
jp     118a <_set_ushort+0x8a>
jmp    11a6 <_set_ushort+0xa6>
movsd  0xe6e(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ba <_set_ushort+0xba>
movsd  0xe52(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %ax,%dx
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
pop    %rbp
ret

x86 assembly (clang, -O2):
<_set_ushort>:
movsd  0xef8(%rip),%xmm2
addsd  %xmm0,%xmm2
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm2
cvttsd2si %xmm2,%eax
ja     1138 <_set_ushort+0x38>
xorps  %xmm2,%xmm2
cvtsi2sd %eax,%xmm2
subsd  %xmm0,%xmm2
addsd  0xed3(%rip),%xmm2
ucomisd %xmm1,%xmm2
jne    1135 <_set_ushort+0x35>
jnp    1138 <_set_ushort+0x38>
add    $0xffffffff,%eax
mov    %ax,(%rdi,%rsi,2)
ret

LLVM IR (-O0):
define void @_set_ushort(i8* %0, i64 %1, double %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca double
%7 = alloca i16*
store i8* %0, i8** %4
store i64 %1, i64* %5
store double %2, double* %6
%8 = load i8*, i8** %4
%9 = bitcast i8* %8 to i16*
store i16* %9, i16** %7
%10 = load double, double* %6
%11 = fadd double %10, 5.000000e-01
%12 = fcmp ogt double %11, 0.000000e+00
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load double, double* %6
%15 = fadd double %14, 5.000000e-01
%16 = fptosi double %15 to i32
br label %37
17: ; preds = %3
%18 = load double, double* %6
%19 = fadd double %18, 5.000000e-01
%20 = fptosi double %19 to i32
%21 = sitofp i32 %20 to double
%22 = load double, double* %6
%23 = fsub double %21, %22
%24 = fadd double %23, 5.000000e-01
%25 = fcmp une double %24, 0.000000e+00
br i1 %25, label %26, label %31
26: ; preds = %17
%27 = load double, double* %6
%28 = fadd double %27, 5.000000e-01
%29 = fptosi double %28 to i32
%30 = sub nsw i32 %29, 1
br label %35
31: ; preds = %17
%32 = load double, double* %6
%33 = fadd double %32, 5.000000e-01
%34 = fptosi double %33 to i32
br label %35
35: ; preds = %31, %26
%36 = phi i32 [ %30, %26 ], [ %34, %31 ]
br label %37
37: ; preds = %35, %13
%38 = phi i32 [ %16, %13 ], [ %36, %35 ]
%39 = trunc i32 %38 to i16
%40 = load i16*, i16** %7
%41 = load i64, i64* %5
%42 = getelementptr inbounds i16, i16* %40, i64 %41
store i16 %39, i16* %42
ret void
}

LLVM IR (-O2):
define void @_set_ushort(i8* nocapture writeonly %0, i64 %1, double %2) {
%4 = fadd double %2, 5.000000e-01
%5 = fcmp ogt double %4, 0.000000e+00
%6 = fptosi double %4 to i32
br i1 %5, label %14, label %7
7: ; preds = %3
%8 = sitofp i32 %6 to double
%9 = fsub double %8, %2
%10 = fadd double %9, 5.000000e-01
%11 = fcmp une double %10, 0.000000e+00
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = add nsw i32 %6, -1
br label %14
14: ; preds = %3, %7, %12
%15 = phi i32 [ %13, %12 ], [ %6, %7 ], [ %6, %3 ]
%16 = bitcast i8* %0 to i16*
%17 = trunc i32 %15 to i16
%18 = getelementptr inbounds i16, i16* %16, i64 %1
store i16 %17, i16* %18
ret void
}