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

/* Variables and functions */
float fp16_ieee_to_fp32_value(int /*<<< orphan*/ const);
int /*<<< orphan*/ isfinite(float);
int roundf(float);
uint8_t u16_f16_u8(const uint16_t val) {
  float f = fp16_ieee_to_fp32_value(val);
  if (!isfinite(f) || f < 0)
    return 0;
  else if (f > 1)
    return 255;
  else
    return roundf(f * 255);
}


x86 assembly (clang, -O0):
<u16_f16_u8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <fp16_ieee_to_fp32_value@plt>
movss  %xmm0,-0xc(%rbp)
movss  -0xc(%rbp),%xmm0
call   1040 <isfinite@plt>
cmp    $0x0,%eax
je     1168 <u16_f16_u8+0x38>
xorps  %xmm0,%xmm0
ucomiss -0xc(%rbp),%xmm0
jbe    1174 <u16_f16_u8+0x44>
movl   $0x0,-0x4(%rbp)
jmp    11ab <u16_f16_u8+0x7b>
movss  -0xc(%rbp),%xmm0
movss  0xe7f(%rip),%xmm1
ucomiss %xmm1,%xmm0
jbe    1196 <u16_f16_u8+0x66>
movl   $0xff,-0x4(%rbp)
jmp    11ab <u16_f16_u8+0x7b>
movss  0xe66(%rip),%xmm0
mulss  -0xc(%rbp),%xmm0
call   1050 <roundf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u16_f16_u8>:
push   %rax
call   1030 <fp16_ieee_to_fp32_value@plt>
movss  %xmm0,0x4(%rsp)
call   1040 <isfinite@plt>
mov    %eax,%ecx
xor    %eax,%eax
xorps  %xmm0,%xmm0
movss  0x4(%rsp),%xmm1
ucomiss %xmm1,%xmm0
ja     1168 <u16_f16_u8+0x38>
test   %ecx,%ecx
je     1168 <u16_f16_u8+0x38>
movaps %xmm1,%xmm0
mov    $0xff,%eax
ucomiss 0xe9a(%rip),%xmm1
jbe    116a <u16_f16_u8+0x3a>
pop    %rcx
ret
mulss  0xe92(%rip),%xmm0
pop    %rax
jmp    1050 <roundf@plt>

LLVM IR (-O0):
define i32 @u16_f16_u8(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca float
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call float @fp16_ieee_to_fp32_value(i32 %5)
store float %6, float* %4
%7 = load float, float* %4
%8 = call i32 @isfinite(float %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load float, float* %4
%12 = fcmp olt float %11, 0.000000e+00
br i1 %12, label %13, label %14
13: ; preds = %10, %1
store i32 0, i32* %2
br label %22
14: ; preds = %10
%15 = load float, float* %4
%16 = fcmp ogt float %15, 1.000000e+00
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 255, i32* %2
br label %22
18: ; preds = %14
%19 = load float, float* %4
%20 = fmul float %19, 2.550000e+02
%21 = call i32 @roundf(float %20)
store i32 %21, i32* %2
br label %22
22: ; preds = %18, %17, %13
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
define i32 @u16_f16_u8(i32 %0) {
%2 = tail call float @fp16_ieee_to_fp32_value(i32 %0)
%3 = tail call i32 @isfinite(float %2)
%4 = icmp eq i32 %3, 0
%5 = fcmp olt float %2, 0.000000e+00
%6 = or i1 %5, %4
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = fcmp ogt float %2, 1.000000e+00
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = fmul float %2, 2.550000e+02
%11 = tail call i32 @roundf(float %10)
br label %12
12: ; preds = %7, %1, %9
%13 = phi i32 [ %11, %9 ], [ 0, %1 ], [ 255, %7 ]
ret i32 %13
}