#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 */
double INFINITY;
double NAN;
double ldexp(int, int);
double decode_half(unsigned short half) {
  int exp = (half >> 10) & 0x1f;
  int mant = half & 0x3ff;
  double val;
  if (exp == 0)
    val = ldexp(mant, -24);
  else if (exp != 31)
    val = ldexp(mant + 1024, exp - 25);
  else
    val = mant == 0 ? INFINITY : NAN;
  return half & 0x8000 ? -val : val;
}


x86 assembly (clang, -O0):
<decode_half>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
sar    $0xa,%eax
and    $0x1f,%eax
mov    %eax,-0x8(%rbp)
movzwl -0x2(%rbp),%eax
and    $0x3ff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1159 <decode_half+0x49>
mov    -0xc(%rbp),%edi
mov    $0xffffffe8,%esi
call   1030 <ldexp@plt>
movsd  %xmm0,-0x18(%rbp)
jmp    11bf <decode_half+0xaf>
cmpl   $0x1f,-0x8(%rbp)
je     1181 <decode_half+0x71>
mov    -0xc(%rbp),%edi
add    $0x400,%edi
mov    -0x8(%rbp),%esi
sub    $0x19,%esi
call   1030 <ldexp@plt>
movsd  %xmm0,-0x18(%rbp)
jmp    11ba <decode_half+0xaa>
cmpl   $0x0,-0xc(%rbp)
jne    11a0 <decode_half+0x90>
mov    0x2e4e(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11b0 <decode_half+0xa0>
mov    0x2e51(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11bf <decode_half+0xaf>
movzwl -0x2(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     11f7 <decode_half+0xe7>
movsd  -0x18(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1201 <decode_half+0xf1>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_half>:
push   %rbx
mov    %edi,%ebx
mov    %edi,%esi
shr    $0xa,%esi
and    $0x1f,%esi
and    $0x3ff,%edi
cmp    $0x1f,%esi
je     1131 <decode_half+0x21>
test   %esi,%esi
jne    113e <decode_half+0x2e>
mov    $0xffffffe8,%esi
jmp    1147 <decode_half+0x37>
test   %edi,%edi
jne    115d <decode_half+0x4d>
mov    0x2ea4(%rip),%rax
jmp    1164 <decode_half+0x54>
or     $0x400,%edi
add    $0xffffffe7,%esi
call   1030 <ldexp@plt>
test   $0x8000,%ebx
je     115b <decode_half+0x4b>
xorps  0xea5(%rip),%xmm0
pop    %rbx
ret
mov    0x2e94(%rip),%rax
movsd  (%rax),%xmm0
test   $0x8000,%ebx
je     115b <decode_half+0x4b>
jmp    1154 <decode_half+0x44>

LLVM IR (-O0):
@INFINITY = global double 0.000000e+00
@NAN = global double 0.000000e+00
define double @decode_half(i16 zeroext %0) {
%2 = alloca i16
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
store i16 %0, i16* %2
%6 = load i16, i16* %2
%7 = zext i16 %6 to i32
%8 = ashr i32 %7, 10
%9 = and i32 %8, 31
store i32 %9, i32* %3
%10 = load i16, i16* %2
%11 = zext i16 %10 to i32
%12 = and i32 %11, 1023
store i32 %12, i32* %4
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call double @ldexp(i32 %16, i32 -24)
store double %17, double* %5
br label %37
18: ; preds = %1
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 31
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1024
%24 = load i32, i32* %3
%25 = sub nsw i32 %24, 25
%26 = call double @ldexp(i32 %23, i32 %25)
store double %26, double* %5
br label %36
27: ; preds = %18
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = load double, double* @INFINITY
br label %34
32: ; preds = %27
%33 = load double, double* @NAN
br label %34
34: ; preds = %32, %30
%35 = phi double [ %31, %30 ], [ %33, %32 ]
store double %35, double* %5
br label %36
36: ; preds = %34, %21
br label %37
37: ; preds = %36, %15
%38 = load i16, i16* %2
%39 = zext i16 %38 to i32
%40 = and i32 %39, 32768
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load double, double* %5
%44 = fneg double %43
br label %47
45: ; preds = %37
%46 = load double, double* %5
br label %47
47: ; preds = %45, %42
%48 = phi double [ %44, %42 ], [ %46, %45 ]
ret double %48
}

LLVM IR (-O2):
@INFINITY = global double 0.000000e+00
@NAN = global double 0.000000e+00
define double @decode_half(i16 zeroext %0) {
%2 = zext i16 %0 to i32
%3 = lshr i32 %2, 10
%4 = and i32 %3, 31
%5 = and i32 %2, 1023
switch i32 %4, label %8 [
i32 0, label %6
i32 31, label %12
]
6: ; preds = %1
%7 = tail call double @ldexp(i32 %5, i32 -24)
br label %17
8: ; preds = %1
%9 = or i32 %5, 1024
%10 = add nsw i32 %4, -25
%11 = tail call double @ldexp(i32 %9, i32 %10)
br label %17
12: ; preds = %1
%13 = icmp eq i32 %5, 0
%14 = load double, double* @INFINITY
%15 = load double, double* @NAN
%16 = select i1 %13, double %14, double %15
br label %17
17: ; preds = %8, %12, %6
%18 = phi double [ %7, %6 ], [ %11, %8 ], [ %16, %12 ]
%19 = and i32 %2, 32768
%20 = icmp eq i32 %19, 0
%21 = fneg double %18
%22 = select i1 %20, double %18, double %21
ret double %22
}