#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 char *halfp) {
  int half = (halfp[0] << 8) + halfp[1];
  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    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x8(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sar    $0xa,%eax
and    $0x1f,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
and    $0x3ff,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    116b <decode_half+0x5b>
mov    -0x14(%rbp),%edi
mov    $0xffffffe8,%esi
call   1030 <ldexp@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    11d1 <decode_half+0xc1>
cmpl   $0x1f,-0x10(%rbp)
je     1193 <decode_half+0x83>
mov    -0x14(%rbp),%edi
add    $0x400,%edi
mov    -0x10(%rbp),%esi
sub    $0x19,%esi
call   1030 <ldexp@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    11cc <decode_half+0xbc>
cmpl   $0x0,-0x14(%rbp)
jne    11b2 <decode_half+0xa2>
mov    0x2e3c(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11c2 <decode_half+0xb2>
mov    0x2e3f(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11d1 <decode_half+0xc1>
mov    -0xc(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     1208 <decode_half+0xf8>
movsd  -0x20(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1212 <decode_half+0x102>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_half>:
push   %rbx
movzbl (%rdi),%ebx
movzbl 0x1(%rdi),%eax
mov    %ebx,%esi
shr    $0x2,%esi
and    $0x1f,%esi
mov    %ebx,%edi
and    $0x3,%edi
shl    $0x8,%edi
or     %eax,%edi
cmp    $0x1f,%esi
je     113a <decode_half+0x2a>
test   %esi,%esi
jne    1147 <decode_half+0x37>
mov    $0xffffffe8,%esi
jmp    1150 <decode_half+0x40>
test   %edi,%edi
jne    1166 <decode_half+0x56>
mov    0x2e9b(%rip),%rax
jmp    116d <decode_half+0x5d>
or     $0x400,%edi
add    $0xffffffe7,%esi
call   1030 <ldexp@plt>
test   $0x80,%ebx
je     1164 <decode_half+0x54>
xorps  0xe9c(%rip),%xmm0
pop    %rbx
ret
mov    0x2e8b(%rip),%rax
movsd  (%rax),%xmm0
test   $0x80,%ebx
je     1164 <decode_half+0x54>
jmp    115d <decode_half+0x4d>

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

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