#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
uint16_t single2half(float number) {
  uint32_t num = *((uint32_t *)&number);
  uint32_t s = num >> 31;
  uint32_t e = (num >> 23) & 0x0FF;
  if ((e == 255) && (num & 0x007fffff))
    return 0x7E00;
  if (e > (127 + 15))
    return s ? 0xFC00 : 0x7C00;
  if (e < (127 - 15))
    return 0;
  return (s << 15) | ((e - 127 + 15) << 10) |
         (((num >> 13) & 0x3FF) + ((num >> 12) & 0x01));
}


x86 assembly (clang, -O0):
<single2half>:
push   %rbp
mov    %rsp,%rbp
movss  %xmm0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x1f,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x17,%eax
and    $0xff,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xff,-0x14(%rbp)
jne    1150 <single2half+0x50>
mov    -0xc(%rbp),%eax
and    $0x7fffff,%eax
cmp    $0x0,%eax
je     1150 <single2half+0x50>
movl   $0x7e00,-0x4(%rbp)
jmp    11be <single2half+0xbe>
cmpl   $0x8e,-0x14(%rbp)
jle    1178 <single2half+0x78>
mov    -0x10(%rbp),%edx
mov    $0x7c00,%eax
mov    $0xfc00,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11be <single2half+0xbe>
cmpl   $0x70,-0x14(%rbp)
jge    118e <single2half+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    11be <single2half+0xbe>
mov    -0x10(%rbp),%eax
shl    $0xf,%eax
mov    -0x14(%rbp),%ecx
sub    $0x7f,%ecx
add    $0xf,%ecx
shl    $0xa,%ecx
or     %ecx,%eax
mov    -0xc(%rbp),%ecx
sar    $0xd,%ecx
and    $0x3ff,%ecx
mov    -0xc(%rbp),%edx
sar    $0xc,%edx
and    $0x1,%edx
add    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<single2half>:
movd   %xmm0,%ecx
mov    %ecx,%eax
shr    $0x17,%eax
movzbl %al,%edx
test   $0x7fffff,%ecx
je     1122 <single2half+0x22>
mov    $0x7e00,%eax
cmp    $0xff,%edx
jne    1122 <single2half+0x22>
ret
cmp    $0x8f,%edx
jb     113c <single2half+0x3c>
shr    $0x10,%ecx
and    $0x8000,%ecx
or     $0x7c00,%ecx
mov    %ecx,%eax
ret
xor    %eax,%eax
cmp    $0x70,%edx
jb     1121 <single2half+0x21>
mov    %ecx,%eax
sar    $0x10,%eax
and    $0xffff8000,%eax
shl    $0xa,%edx
add    $0xfffe4000,%edx
mov    %ecx,%esi
shr    $0xd,%esi
and    $0x3ff,%esi
shr    $0xc,%ecx
and    $0x1,%ecx
add    %esi,%ecx
or     %eax,%ecx
or     %edx,%ecx
mov    %ecx,%eax
ret

LLVM IR (-O0):
define i32 @single2half(float %0) {
%2 = alloca i32
%3 = alloca float
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store float %0, float* %3
%7 = bitcast float* %3 to i32*
%8 = load i32, i32* %7
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = ashr i32 %9, 31
store i32 %10, i32* %5
%11 = load i32, i32* %4
%12 = ashr i32 %11, 23
%13 = and i32 %12, 255
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 255
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load i32, i32* %4
%18 = and i32 %17, 8388607
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 32256, i32* %2
br label %49
21: ; preds = %16, %1
%22 = load i32, i32* %6
%23 = icmp sgt i32 %22, 142
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
%27 = zext i1 %26 to i64
%28 = select i1 %26, i32 64512, i32 31744
store i32 %28, i32* %2
br label %49
29: ; preds = %21
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 112
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 0, i32* %2
br label %49
33: ; preds = %29
%34 = load i32, i32* %5
%35 = shl i32 %34, 15
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, 127
%38 = add nsw i32 %37, 15
%39 = shl i32 %38, 10
%40 = or i32 %35, %39
%41 = load i32, i32* %4
%42 = ashr i32 %41, 13
%43 = and i32 %42, 1023
%44 = load i32, i32* %4
%45 = ashr i32 %44, 12
%46 = and i32 %45, 1
%47 = add nsw i32 %43, %46
%48 = or i32 %40, %47
store i32 %48, i32* %2
br label %49
49: ; preds = %33, %32, %24, %20
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
define i32 @single2half(float %0) {
%2 = bitcast float %0 to i32
%3 = lshr i32 %2, 23
%4 = and i32 %3, 255
%5 = icmp ne i32 %4, 255
%6 = and i32 %2, 8388607
%7 = icmp eq i32 %6, 0
%8 = or i1 %7, %5
br i1 %8, label %9, label %28
9: ; preds = %1
%10 = icmp ugt i32 %4, 142
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = icmp sgt i32 %2, -1
%13 = select i1 %12, i32 31744, i32 64512
br label %28
14: ; preds = %9
%15 = icmp ult i32 %4, 112
br i1 %15, label %28, label %16
16: ; preds = %14
%17 = ashr i32 %2, 16
%18 = and i32 %17, -32768
%19 = shl nuw nsw i32 %4, 10
%20 = add nsw i32 %19, -114688
%21 = lshr i32 %2, 13
%22 = and i32 %21, 1023
%23 = lshr i32 %2, 12
%24 = and i32 %23, 1
%25 = add nuw nsw i32 %22, %24
%26 = or i32 %25, %18
%27 = or i32 %26, %20
br label %28
28: ; preds = %14, %1, %16, %11
%29 = phi i32 [ %13, %11 ], [ %27, %16 ], [ 32256, %1 ], [ 0, %14 ]
ret i32 %29
}