#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 uint_least64_t;

/* Variables and functions */
double frexp(double, int *);
scalar_t__ isinf(double);
scalar_t__ isnan(double);
int ldexp(double, int);
scalar_t__ signbit(double);
int wire_from_double(uint_least64_t *ponwire, double val) {
  uint_least64_t onwire;
  int e = 0;
  int s = 0;
  if (signbit(val)) {
    s = 1;
    val = -val;
  }
  if (isnan(val)) {
    onwire = ((uint_least64_t)1 << 63) - 1;
  } else if (isinf(val)) {
    onwire = (((uint_least64_t)1 << 11) - 1) << 52;
  } else if (val != 0.0) {
    val = frexp(val, &e);
    e += (1 << 10) - 2;
    if (e > 0) {
      onwire = ldexp(val, 53);
      onwire &= ~((uint_least64_t)1 << 52);
    } else {
      if (e >= -52 + 1)
        onwire = ldexp(val, 53 - 1 + e);
      else
        onwire = 0;
      e = 0;
    }
    onwire |= (uint_least64_t)e << 52;
  } else {
    onwire = 0;
  }
  onwire |= (uint_least64_t)s << 63;
  *ponwire = onwire;
  return 0;
}


x86 assembly (clang, -O0):
<wire_from_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1060 <signbit@plt>
cmp    $0x0,%rax
je     11ab <wire_from_double+0x5b>
movl   $0x1,-0x1c(%rbp)
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <isnan@plt>
cmp    $0x0,%rax
je     11c7 <wire_from_double+0x77>
mov    %eax,-0x14(%rbp)
jmp    129b <wire_from_double+0x14b>
movsd  -0x10(%rbp),%xmm0
call   1070 <isinf@plt>
cmp    $0x0,%rax
je     11e3 <wire_from_double+0x93>
mov    %eax,-0x14(%rbp)
jmp    1296 <wire_from_double+0x146>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1200 <wire_from_double+0xb0>
jp     1200 <wire_from_double+0xb0>
jmp    128a <wire_from_double+0x13a>
movsd  -0x10(%rbp),%xmm0
lea    -0x18(%rbp),%rdi
call   1040 <frexp@plt>
movsd  %xmm0,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x3fe,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1245 <wire_from_double+0xf5>
movsd  -0x10(%rbp),%xmm0
mov    $0x35,%edi
call   1030 <ldexp@plt>
mov    %eax,-0x14(%rbp)
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1275 <wire_from_double+0x125>
cmpl   $0xffffffcd,-0x18(%rbp)
jl     1267 <wire_from_double+0x117>
movsd  -0x10(%rbp),%xmm0
mov    -0x18(%rbp),%edi
add    $0x34,%edi
call   1030 <ldexp@plt>
mov    %eax,-0x14(%rbp)
jmp    126e <wire_from_double+0x11e>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x34,%ecx
shl    %cl,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1291 <wire_from_double+0x141>
movl   $0x0,-0x14(%rbp)
jmp    1296 <wire_from_double+0x146>
jmp    129b <wire_from_double+0x14b>
mov    -0x1c(%rbp),%eax
mov    $0x3f,%ecx
shl    %cl,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wire_from_double>:
sub    $0x28,%rsp
movl   $0x0,0xc(%rsp)
movaps %xmm0,0x10(%rsp)
call   1060 <signbit@plt>
test   %rax,%rax
je     117b <wire_from_double+0x2b>
movapd 0x10(%rsp),%xmm0
xorpd  0xe87(%rip),%xmm0
jmp    1181 <wire_from_double+0x31>
movapd 0x10(%rsp),%xmm0
movapd %xmm0,0x10(%rsp)
call   1050 <isnan@plt>
test   %rax,%rax
je     1198 <wire_from_double+0x48>
xor    %eax,%eax
add    $0x28,%rsp
ret
movapd 0x10(%rsp),%xmm0
call   1070 <isinf@plt>
xorpd  %xmm0,%xmm0
movapd 0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    11b5 <wire_from_double+0x65>
jnp    1191 <wire_from_double+0x41>
test   %rax,%rax
jne    1191 <wire_from_double+0x41>
lea    0xc(%rsp),%rdi
movapd 0x10(%rsp),%xmm0
call   1040 <frexp@plt>
mov    0xc(%rsp),%eax
mov    $0x35,%edi
cmp    $0xfffffc02,%eax
jg     11e8 <wire_from_double+0x98>
cmp    $0xfffffbcf,%eax
jb     1191 <wire_from_double+0x41>
add    $0x432,%eax
mov    %eax,%edi
call   1030 <ldexp@plt>
jmp    1191 <wire_from_double+0x41>

LLVM IR (-O0):
define i32 @wire_from_double(i32* %0, double %1) {
%3 = alloca i32*
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store double %1, double* %4
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load double, double* %4
%9 = call i64 @signbit(double %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
store i32 1, i32* %7
%12 = load double, double* %4
%13 = fneg double %12
store double %13, double* %4
br label %14
14: ; preds = %11, %2
%15 = load double, double* %4
%16 = call i64 @isnan(double %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 poison, i32* %5
br label %57
19: ; preds = %14
%20 = load double, double* %4
%21 = call i64 @isinf(double %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 poison, i32* %5
br label %56
24: ; preds = %19
%25 = load double, double* %4
%26 = fcmp une double %25, 0.000000e+00
br i1 %26, label %27, label %54
27: ; preds = %24
%28 = load double, double* %4
%29 = call double @frexp(double %28, i32* %6)
store double %29, double* %4
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1022
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %27
%35 = load double, double* %4
%36 = call i32 @ldexp(double %35, i32 53)
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = and i32 %37, poison
store i32 %38, i32* %5
br label %49
39: ; preds = %27
%40 = load i32, i32* %6
%41 = icmp sge i32 %40, -51
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load double, double* %4
%44 = load i32, i32* %6
%45 = add nsw i32 52, %44
%46 = call i32 @ldexp(double %43, i32 %45)
store i32 %46, i32* %5
br label %48
47: ; preds = %39
store i32 0, i32* %5
br label %48
48: ; preds = %47, %42
store i32 0, i32* %6
br label %49
49: ; preds = %48, %34
%50 = load i32, i32* %6
%51 = shl i32 %50, 52
%52 = load i32, i32* %5
%53 = or i32 %52, %51
store i32 %53, i32* %5
br label %55
54: ; preds = %24
store i32 0, i32* %5
br label %55
55: ; preds = %54, %49
br label %56
56: ; preds = %55, %23
br label %57
57: ; preds = %56, %18
%58 = load i32, i32* %7
%59 = shl i32 %58, 63
%60 = load i32, i32* %5
%61 = or i32 %60, %59
store i32 %61, i32* %5
%62 = load i32, i32* %5
%63 = load i32*, i32** %3
store i32 %62, i32* %63
ret i32 0
}

LLVM IR (-O2):
define i32 @wire_from_double(i32* nocapture readnone %0, double %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i64 @signbit(double %1)
%6 = icmp eq i64 %5, 0
%7 = fneg double %1
%8 = select i1 %6, double %1, double %7
%9 = tail call i64 @isnan(double %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %2
%12 = tail call i64 @isinf(double %8)
%13 = icmp eq i64 %12, 0
%14 = fcmp une double %8, 0.000000e+00
%15 = and i1 %14, %13
br i1 %15, label %16, label %27
16: ; preds = %11
%17 = call double @frexp(double %8, i32* %3)
%18 = load i32, i32* %3
%19 = icmp sgt i32 %18, -1022
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = icmp ugt i32 %18, -1074
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = add nsw i32 %18, 1074
br label %24
24: ; preds = %16, %22
%25 = phi i32 [ %23, %22 ], [ 53, %16 ]
%26 = tail call i32 @ldexp(double %17, i32 %25)
br label %27
27: ; preds = %24, %20, %11, %2
ret i32 0
}