#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 */
scalar_t__ floor(int);
int log(double);
double pow(double, double);
unsigned int doubletoIEEE754(double d) {
  unsigned int sn = 0, ep = 0, ma;
  unsigned int id;
  if (d < 0.0) {
    sn = 1;
    d = -d;
  }
  if (d != 0.0) {
    int ee;
    ee = (int)floor(log(d) / log(2.0));
    if (ee < -126)
      ee = -126;
    d *= pow(0.5, (double)ee);
    ee += 127;
    if (ee < 1)
      ee = 0;
    else if (ee > 254) {
      ee = 255;
      d = 0.0;
    }
    ep = ee;
  } else {
    ep = 0;
  }
  ma = ((unsigned int)(d * (1 << 23) + 0.5)) & 0x7fffff;
  id = (sn << 31) | (ep << 23) | ma;
  return id;
}


x86 assembly (clang, -O0):
<doubletoIEEE754>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    1181 <doubletoIEEE754+0x51>
movl   $0x1,-0xc(%rbp)
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    119e <doubletoIEEE754+0x6e>
jp     119e <doubletoIEEE754+0x6e>
jmp    1242 <doubletoIEEE754+0x112>
movsd  -0x8(%rbp),%xmm0
call   1030 <log@plt>
mov    %eax,-0x20(%rbp)
movsd  0xe4d(%rip),%xmm0
call   1030 <log@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <floor@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffff82,-0x1c(%rbp)
jge    11db <doubletoIEEE754+0xab>
movl   $0xffffff82,-0x1c(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  0xe20(%rip),%xmm0
call   1040 <pow@plt>
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x7f,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    1216 <doubletoIEEE754+0xe6>
movl   $0x0,-0x1c(%rbp)
jmp    1237 <doubletoIEEE754+0x107>
cmpl   $0xfe,-0x1c(%rbp)
jle    1232 <doubletoIEEE754+0x102>
movl   $0xff,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1237 <doubletoIEEE754+0x107>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1249 <doubletoIEEE754+0x119>
movl   $0x0,-0x10(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xdba(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xda6(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
and    $0x7fffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1f,%eax
mov    -0x10(%rbp),%ecx
shl    $0x17,%ecx
or     %ecx,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doubletoIEEE754>:
push   %rbx
sub    $0x20,%rsp
movapd 0xec3(%rip),%xmm1
xorpd  %xmm0,%xmm1
xorpd  %xmm3,%xmm3
movapd %xmm0,%xmm2
cmpltsd %xmm3,%xmm2
andpd  %xmm2,%xmm1
andnpd %xmm0,%xmm2
orpd   %xmm1,%xmm2
ucomisd %xmm3,%xmm2
jne    1166 <doubletoIEEE754+0x36>
jnp    11e6 <doubletoIEEE754+0xb6>
movapd %xmm0,0x10(%rsp)
movapd %xmm2,%xmm0
movapd %xmm2,(%rsp)
call   1040 <log@plt>
mov    %eax,%ebx
movsd  0xe8c(%rip),%xmm0
call   1040 <log@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <floor@plt>
cmp    $0xffffff83,%eax
mov    $0xffffff82,%ebx
cmovge %eax,%ebx
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
xorpd  0xe4f(%rip),%xmm0
call   1030 <exp2@plt>
mov    %ebx,%eax
shl    $0x17,%eax
add    $0x3f800000,%eax
xorpd  %xmm2,%xmm2
cmp    $0x7f,%ebx
jg     11d2 <doubletoIEEE754+0xa2>
mulsd  (%rsp),%xmm0
movapd %xmm0,%xmm2
mov    $0x7f800000,%ecx
cmovg  %ecx,%eax
movapd 0x10(%rsp),%xmm0
xorpd  %xmm3,%xmm3
jmp    11e8 <doubletoIEEE754+0xb8>
xor    %eax,%eax
xor    %ecx,%ecx
ucomisd %xmm0,%xmm3
seta   %cl
shl    $0x1f,%ecx
or     %eax,%ecx
mulsd  0xe1a(%rip),%xmm2
addsd  0xe1a(%rip),%xmm2
cvttsd2si %xmm2,%rax
and    $0x7fffff,%eax
or     %ecx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @doubletoIEEE754(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store double %0, double* %2
store i32 0, i32* %3
store i32 0, i32* %4
%8 = load double, double* %2
%9 = fcmp olt double %8, 0.000000e+00
br i1 %9, label %10, label %13
10: ; preds = %1
store i32 1, i32* %3
%11 = load double, double* %2
%12 = fneg double %11
store double %12, double* %2
br label %13
13: ; preds = %10, %1
%14 = load double, double* %2
%15 = fcmp une double %14, 0.000000e+00
br i1 %15, label %16, label %44
16: ; preds = %13
%17 = load double, double* %2
%18 = call i32 @log(double %17)
%19 = call i32 @log(double 2.000000e+00)
%20 = sdiv i32 %18, %19
%21 = call i64 @floor(i32 %20)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, -126
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 -126, i32* %7
br label %26
26: ; preds = %25, %16
%27 = load i32, i32* %7
%28 = sitofp i32 %27 to double
%29 = call double @pow(double 5.000000e-01, double %28)
%30 = load double, double* %2
%31 = fmul double %30, %29
store double %31, double* %2
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 127
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %37
36: ; preds = %26
store i32 0, i32* %7
br label %42
37: ; preds = %26
%38 = load i32, i32* %7
%39 = icmp sgt i32 %38, 254
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 255, i32* %7
store double 0.000000e+00, double* %2
br label %41
41: ; preds = %40, %37
br label %42
42: ; preds = %41, %36
%43 = load i32, i32* %7
store i32 %43, i32* %4
br label %45
44: ; preds = %13
store i32 0, i32* %4
br label %45
45: ; preds = %44, %42
%46 = load double, double* %2
%47 = call double @llvm.fmuladd.f64(double %46, double 0x4160000000000000, double 5.000000e-01)
%48 = fptoui double %47 to i32
%49 = and i32 %48, 8388607
store i32 %49, i32* %5
%50 = load i32, i32* %3
%51 = shl i32 %50, 31
%52 = load i32, i32* %4
%53 = shl i32 %52, 23
%54 = or i32 %51, %53
%55 = load i32, i32* %5
%56 = or i32 %54, %55
store i32 %56, i32* %6
%57 = load i32, i32* %6
ret i32 %57
}

LLVM IR (-O2):
define i32 @doubletoIEEE754(double %0) {
%2 = fcmp olt double %0, 0.000000e+00
%3 = fneg double %0
%4 = select i1 %2, double %3, double %0
%5 = fcmp une double %4, 0.000000e+00
br i1 %5, label %6, label %23
6: ; preds = %1
%7 = tail call i32 @log(double %4)
%8 = tail call i32 @log(double 2.000000e+00)
%9 = sdiv i32 %7, %8
%10 = tail call i64 @floor(i32 %9)
%11 = trunc i64 %10 to i32
%12 = icmp sgt i32 %11, -126
%13 = select i1 %12, i32 %11, i32 -126
%14 = sitofp i32 %13 to double
%15 = fneg double %14
%16 = tail call double @exp2(double %15)
%17 = fmul double %4, %16
%18 = icmp sgt i32 %13, 127
%19 = select i1 %18, double 0.000000e+00, double %17
%20 = shl i32 %13, 23
%21 = add i32 %20, 1065353216
%22 = select i1 %18, i32 2139095040, i32 %21
br label %23
23: ; preds = %1, %6
%24 = phi i32 [ %22, %6 ], [ 0, %1 ]
%25 = phi double [ %19, %6 ], [ %4, %1 ]
%26 = select i1 %2, i32 -2147483648, i32 0
%27 = tail call double @llvm.fmuladd.f64(double %25, double 0x4160000000000000, double 5.000000e-01)
%28 = fptoui double %27 to i32
%29 = and i32 %28, 8388607
%30 = or i32 %24, %26
%31 = or i32 %30, %29
ret i32 %31
}