#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 /*<<< orphan*/ v;
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int *, double *, int);
unsigned short encode_half(double val) {
  uint64_t v;
  memcpy(&v, &val, sizeof(v));
  int sign = v >> 63 << 15;
  int exp = (v >> 52) & 0x7ff;
  int mant = v << 12 >> 12 >> (53 - 11);
  exp -= 1023;
  if (exp == 1024) {
    exp = 16;
    mant >>= 1;
  } else if (exp >= 16) {
    exp = 15;
    mant = 1023;
  } else if (exp >= -14) {
  } else if (exp >= -24) {
    mant |= 1024;
    mant >>= -(exp + 14);
    exp = -15;
  } else {
    return 0;
  }
  return sign | ((exp + 15) << 10) | mant;
}


x86 assembly (clang, -O0):
<encode_half>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    $0x3f,%ecx
sar    %cl,%eax
shl    $0xf,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x34,%ecx
sar    %cl,%eax
and    $0x7ff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
shl    $0xc,%eax
sar    $0xc,%eax
mov    $0x2a,%ecx
sar    %cl,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x3ff,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x400,-0x1c(%rbp)
jne    1191 <encode_half+0x81>
movl   $0x10,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    120a <encode_half+0xfa>
cmpl   $0x10,-0x1c(%rbp)
jl     11ae <encode_half+0x9e>
movl   $0xf,-0x1c(%rbp)
movl   $0x3ff,-0x20(%rbp)
jmp    1205 <encode_half+0xf5>
cmpl   $0xfffffff2,-0x1c(%rbp)
jl     11bd <encode_half+0xad>
jmp    1200 <encode_half+0xf0>
cmpl   $0xffffffe8,-0x1c(%rbp)
jl     11f0 <encode_half+0xe0>
mov    -0x20(%rbp),%eax
or     $0x400,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0xe,%eax
xor    %ecx,%ecx
sub    %eax,%ecx
mov    -0x20(%rbp),%eax
sar    %cl,%eax
mov    %eax,-0x20(%rbp)
movl   $0xfffffff1,-0x1c(%rbp)
jmp    11fb <encode_half+0xeb>
movw   $0x0,-0x2(%rbp)
jmp    121f <encode_half+0x10f>
jmp    1200 <encode_half+0xf0>
jmp    1205 <encode_half+0xf5>
jmp    120a <encode_half+0xfa>
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    $0xf,%ecx
shl    $0xa,%ecx
or     %ecx,%eax
or     -0x20(%rbp),%eax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_half>:
sub    $0x18,%rsp
movsd  %xmm0,0x8(%rsp)
lea    0x14(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1030 <memcpy@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
define zeroext i16 @encode_half(double %0) {
%2 = alloca i16
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store double %0, double* %3
%8 = call i32 @memcpy(i32* %4, double* %3, i32 4)
%9 = load i32, i32* %4
%10 = ashr i32 %9, 63
%11 = shl i32 %10, 15
store i32 %11, i32* %5
%12 = load i32, i32* %4
%13 = ashr i32 %12, 52
%14 = and i32 %13, 2047
store i32 %14, i32* %6
%15 = load i32, i32* %4
%16 = shl i32 %15, 12
%17 = ashr i32 %16, 12
%18 = ashr i32 %17, 42
store i32 %18, i32* %7
%19 = load i32, i32* %6
%20 = sub nsw i32 %19, 1023
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 1024
br i1 %22, label %23, label %26
23: ; preds = %1
store i32 16, i32* %6
%24 = load i32, i32* %7
%25 = ashr i32 %24, 1
store i32 %25, i32* %7
br label %49
26: ; preds = %1
%27 = load i32, i32* %6
%28 = icmp sge i32 %27, 16
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 15, i32* %6
store i32 1023, i32* %7
br label %48
30: ; preds = %26
%31 = load i32, i32* %6
%32 = icmp sge i32 %31, -14
br i1 %32, label %33, label %34
33: ; preds = %30
br label %47
34: ; preds = %30
%35 = load i32, i32* %6
%36 = icmp sge i32 %35, -24
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = load i32, i32* %7
%39 = or i32 %38, 1024
store i32 %39, i32* %7
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 14
%42 = sub nsw i32 0, %41
%43 = load i32, i32* %7
%44 = ashr i32 %43, %42
store i32 %44, i32* %7
store i32 -15, i32* %6
br label %46
45: ; preds = %34
store i16 0, i16* %2
br label %58
46: ; preds = %37
br label %47
47: ; preds = %46, %33
br label %48
48: ; preds = %47, %29
br label %49
49: ; preds = %48, %23
%50 = load i32, i32* %5
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 15
%53 = shl i32 %52, 10
%54 = or i32 %50, %53
%55 = load i32, i32* %7
%56 = or i32 %54, %55
%57 = trunc i32 %56 to i16
store i16 %57, i16* %2
br label %58
58: ; preds = %49, %45
%59 = load i16, i16* %2
ret i16 %59
}

LLVM IR (-O2):
define zeroext i16 @encode_half(double %0) {
%2 = alloca double
%3 = alloca i32
store double %0, double* %2
%4 = bitcast i32* %3 to i8*
%5 = call i32 @memcpy(i32* %3, double* %2, i32 4)
ret i16 0
}