#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 scalar_t__ int16_t;

/* Variables and functions */
scalar_t__ INT16_MAX;
scalar_t__ INT16_MIN;
scalar_t__ isnan(double);
int16_t __hsail_cvt_zeroi_sat_s16_f64(double a) {
  if (isnan(a))
    return 0;
  if (a >= (double)INT16_MAX)
    return INT16_MAX;
  if (a <= (double)INT16_MIN)
    return INT16_MIN;
  return (int16_t)a;
}


x86 assembly (clang, -O0):
<__hsail_cvt_zeroi_sat_s16_f64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <isnan@plt>
cmp    $0x0,%rax
je     113e <__hsail_cvt_zeroi_sat_s16_f64+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    11a4 <__hsail_cvt_zeroi_sat_s16_f64+0x94>
movsd  -0x10(%rbp),%xmm0
mov    0x2e96(%rip),%rax
cvtsi2sdq (%rax),%xmm1
ucomisd %xmm1,%xmm0
jb     116c <__hsail_cvt_zeroi_sat_s16_f64+0x5c>
mov    0x2e80(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <__hsail_cvt_zeroi_sat_s16_f64+0x94>
movsd  -0x10(%rbp),%xmm1
mov    0x2e60(%rip),%rax
cvtsi2sdq (%rax),%xmm0
ucomisd %xmm1,%xmm0
jb     119a <__hsail_cvt_zeroi_sat_s16_f64+0x8a>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <__hsail_cvt_zeroi_sat_s16_f64+0x94>
cvttsd2si -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__hsail_cvt_zeroi_sat_s16_f64>:
push   %rax
movsd  %xmm0,(%rsp)
call   1030 <isnan@plt>
test   %rax,%rax
je     1124 <__hsail_cvt_zeroi_sat_s16_f64+0x14>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2eb5(%rip),%rax
mov    (%rax),%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jae    115e <__hsail_cvt_zeroi_sat_s16_f64+0x4e>
mov    0x2e90(%rip),%rax
mov    (%rax),%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
ucomisd %xmm1,%xmm0
jae    115e <__hsail_cvt_zeroi_sat_s16_f64+0x4e>
cvttsd2si %xmm1,%rax
pop    %rcx
ret

LLVM IR (-O0):
@INT16_MAX = global i64 0
@INT16_MIN = global i64 0
define i64 @__hsail_cvt_zeroi_sat_s16_f64(double %0) {
%2 = alloca i64
%3 = alloca double
store double %0, double* %3
%4 = load double, double* %3
%5 = call i64 @isnan(double %4)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i64 0, i64* %2
br label %25
8: ; preds = %1
%9 = load double, double* %3
%10 = load i64, i64* @INT16_MAX
%11 = sitofp i64 %10 to double
%12 = fcmp oge double %9, %11
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = load i64, i64* @INT16_MAX
store i64 %14, i64* %2
br label %25
15: ; preds = %8
%16 = load double, double* %3
%17 = load i64, i64* @INT16_MIN
%18 = sitofp i64 %17 to double
%19 = fcmp ole double %16, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = load i64, i64* @INT16_MIN
store i64 %21, i64* %2
br label %25
22: ; preds = %15
%23 = load double, double* %3
%24 = fptosi double %23 to i64
store i64 %24, i64* %2
br label %25
25: ; preds = %22, %20, %13, %7
%26 = load i64, i64* %2
ret i64 %26
}

LLVM IR (-O2):
@INT16_MAX = global i64 0
@INT16_MIN = global i64 0
define i64 @__hsail_cvt_zeroi_sat_s16_f64(double %0) {
%2 = tail call i64 @isnan(double %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %14
4: ; preds = %1
%5 = load i64, i64* @INT16_MAX
%6 = sitofp i64 %5 to double
%7 = fcmp ugt double %6, %0
br i1 %7, label %8, label %14
8: ; preds = %4
%9 = load i64, i64* @INT16_MIN
%10 = sitofp i64 %9 to double
%11 = fcmp ult double %10, %0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = fptosi double %0 to i64
br label %14
14: ; preds = %8, %4, %1, %12
%15 = phi i64 [ %13, %12 ], [ 0, %1 ], [ %5, %4 ], [ %9, %8 ]
ret i64 %15
}