#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 uint64_t;
typedef int uint16_t;

/* Variables and functions */
int frexp(double, int *);
scalar_t__ isinf(double);
scalar_t__ isnan(double);
void IEE754_binary64_encode(double x, char out[8]) {
  bool sign = x < 0;
  uint16_t exponent;
  uint64_t fraction;
  if (isinf(x)) {
    exponent = 0x7FF;
    fraction = 0;
  } else if (isnan(x)) {
    exponent = 0x7FF;
    fraction = 0xFFFFFFFFFFFFF;
  } else {
    if (sign)
      x = -x;
    int e = 0;
    fraction = frexp(x, &e) * ((uint64_t)2 << 52);
    if (e <= 1022) {
      exponent = 0;
      fraction = 0;
    } else {
      exponent = e + 1022;
      if (exponent > 0x7FF) {
        exponent = 0x7FF;
        fraction = 0;
      }
    }
  }
  out[0] = ((sign << 7) & 0x80) | ((exponent >> 4) & 0x7F);
  out[1] = ((exponent << 4) & 0xF0) | ((fraction >> 48) & 0x0F);
  out[2] = (fraction >> 40) & 0xFF;
  out[3] = (fraction >> 32) & 0xFF;
  out[4] = (fraction >> 24) & 0xFF;
  out[5] = (fraction >> 16) & 0xFF;
  out[6] = (fraction >> 8) & 0xFF;
  out[7] = fraction & 0xFF;
}


x86 assembly (clang, -O0):
<IEE754_binary64_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     117b <IEE754_binary64_encode+0x4b>
movl   $0x7ff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    123d <IEE754_binary64_encode+0x10d>
movsd  -0x8(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
je     11a2 <IEE754_binary64_encode+0x72>
movl   $0x7ff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
jmp    1238 <IEE754_binary64_encode+0x108>
cmpl   $0x0,-0x14(%rbp)
je     11cd <IEE754_binary64_encode+0x9d>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movl   $0x0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
lea    -0x20(%rbp),%rdi
call   1030 <frexp@plt>
imul   %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x3fe,-0x20(%rbp)
jg     1208 <IEE754_binary64_encode+0xd8>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    1233 <IEE754_binary64_encode+0x103>
mov    -0x20(%rbp),%eax
add    $0x3fe,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x7ff,-0x18(%rbp)
jle    122e <IEE754_binary64_encode+0xfe>
movl   $0x7ff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    1233 <IEE754_binary64_encode+0x103>
jmp    1238 <IEE754_binary64_encode+0x108>
jmp    123d <IEE754_binary64_encode+0x10d>
mov    -0x14(%rbp),%eax
shl    $0x7,%eax
and    $0x80,%eax
mov    -0x18(%rbp),%ecx
sar    $0x4,%ecx
and    $0x7f,%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
shl    $0x4,%eax
and    $0xf0,%eax
mov    -0x1c(%rbp),%edx
mov    $0x30,%ecx
sar    %cl,%edx
mov    %edx,%ecx
and    $0xf,%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x1c(%rbp),%eax
mov    $0x28,%ecx
sar    %cl,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x1c(%rbp),%eax
mov    $0x20,%ecx
sar    %cl,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x1c(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x4(%rax)
mov    -0x1c(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x5(%rax)
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x6(%rax)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x7(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IEE754_binary64_encode>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movapd %xmm0,0x10(%rsp)
call   1050 <isinf@plt>
mov    $0x7f,%ebp
test   %rax,%rax
je     1152 <IEE754_binary64_encode+0x22>
xor    %ecx,%ecx
jmp    11c8 <IEE754_binary64_encode+0x98>
movapd 0x10(%rsp),%xmm0
call   1040 <isnan@plt>
mov    $0xff,%cl
test   %rax,%rax
jne    11c8 <IEE754_binary64_encode+0x98>
movapd 0xe94(%rip),%xmm1
movapd 0x10(%rsp),%xmm3
xorpd  %xmm3,%xmm1
xorpd  %xmm2,%xmm2
movapd %xmm3,%xmm0
cmpltsd %xmm2,%xmm0
andpd  %xmm0,%xmm1
andnpd %xmm3,%xmm0
orpd   %xmm1,%xmm0
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <frexp@plt>
mov    0xc(%rsp),%eax
lea    0x3fe(%rax),%ecx
shr    $0x4,%ecx
and    $0x7f,%ecx
cmp    $0x402,%eax
mov    $0x7f,%ebp
cmovl  %ecx,%ebp
xor    %ecx,%ecx
cmp    $0x3ff,%eax
cmovl  %ecx,%ebp
xorpd  %xmm0,%xmm0
xor    %eax,%eax
ucomisd 0x10(%rsp),%xmm0
seta   %al
shl    $0x7,%eax
or     %ebp,%eax
mov    %al,(%rbx)
movzbl %cl,%eax
imul   $0x1010101,%eax,%eax
mov    %eax,0x4(%rbx)
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @IEE754_binary64_encode(double %0, i8* %1) {
%3 = alloca double
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store double %0, double* %3
store i8* %1, i8** %4
%9 = load double, double* %3
%10 = fcmp olt double %9, 0.000000e+00
%11 = zext i1 %10 to i32
store i32 %11, i32* %5
%12 = load double, double* %3
%13 = call i64 @isinf(double %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 2047, i32* %6
store i32 0, i32* %7
br label %43
16: ; preds = %2
%17 = load double, double* %3
%18 = call i64 @isnan(double %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 2047, i32* %6
store i32 -1, i32* %7
br label %42
21: ; preds = %16
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load double, double* %3
%26 = fneg double %25
store double %26, double* %3
br label %27
27: ; preds = %24, %21
store i32 0, i32* %8
%28 = load double, double* %3
%29 = call i32 @frexp(double %28, i32* %8)
%30 = mul nsw i32 %29, poison
store i32 %30, i32* %7
%31 = load i32, i32* %8
%32 = icmp sle i32 %31, 1022
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 0, i32* %6
store i32 0, i32* %7
br label %41
34: ; preds = %27
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1022
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp sgt i32 %37, 2047
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 2047, i32* %6
store i32 0, i32* %7
br label %40
40: ; preds = %39, %34
br label %41
41: ; preds = %40, %33
br label %42
42: ; preds = %41, %20
br label %43
43: ; preds = %42, %15
%44 = load i32, i32* %5
%45 = shl i32 %44, 7
%46 = and i32 %45, 128
%47 = load i32, i32* %6
%48 = ashr i32 %47, 4
%49 = and i32 %48, 127
%50 = or i32 %46, %49
%51 = trunc i32 %50 to i8
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i64 0
store i8 %51, i8* %53
%54 = load i32, i32* %6
%55 = shl i32 %54, 4
%56 = and i32 %55, 240
%57 = load i32, i32* %7
%58 = ashr i32 %57, 48
%59 = and i32 %58, 15
%60 = or i32 %56, %59
%61 = trunc i32 %60 to i8
%62 = load i8*, i8** %4
%63 = getelementptr inbounds i8, i8* %62, i64 1
store i8 %61, i8* %63
%64 = load i32, i32* %7
%65 = ashr i32 %64, 40
%66 = and i32 %65, 255
%67 = trunc i32 %66 to i8
%68 = load i8*, i8** %4
%69 = getelementptr inbounds i8, i8* %68, i64 2
store i8 %67, i8* %69
%70 = load i32, i32* %7
%71 = ashr i32 %70, 32
%72 = and i32 %71, 255
%73 = trunc i32 %72 to i8
%74 = load i8*, i8** %4
%75 = getelementptr inbounds i8, i8* %74, i64 3
store i8 %73, i8* %75
%76 = load i32, i32* %7
%77 = ashr i32 %76, 24
%78 = and i32 %77, 255
%79 = trunc i32 %78 to i8
%80 = load i8*, i8** %4
%81 = getelementptr inbounds i8, i8* %80, i64 4
store i8 %79, i8* %81
%82 = load i32, i32* %7
%83 = ashr i32 %82, 16
%84 = and i32 %83, 255
%85 = trunc i32 %84 to i8
%86 = load i8*, i8** %4
%87 = getelementptr inbounds i8, i8* %86, i64 5
store i8 %85, i8* %87
%88 = load i32, i32* %7
%89 = ashr i32 %88, 8
%90 = and i32 %89, 255
%91 = trunc i32 %90 to i8
%92 = load i8*, i8** %4
%93 = getelementptr inbounds i8, i8* %92, i64 6
store i8 %91, i8* %93
%94 = load i32, i32* %7
%95 = and i32 %94, 255
%96 = trunc i32 %95 to i8
%97 = load i8*, i8** %4
%98 = getelementptr inbounds i8, i8* %97, i64 7
store i8 %96, i8* %98
ret void
}

LLVM IR (-O2):
define void @IEE754_binary64_encode(double %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = fcmp olt double %0, 0.000000e+00
%5 = tail call i64 @isinf(double %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = tail call i64 @isnan(double %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = fneg double %0
%12 = select i1 %4, double %11, double %0
%13 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%14 = call i32 @frexp(double %12, i32* %3)
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 1023
%17 = add nsw i32 %15, 1022
%18 = icmp sgt i32 %15, 1025
%19 = lshr i32 %17, 4
%20 = and i32 %19, 127
%21 = select i1 %18, i32 127, i32 %20
%22 = select i1 %16, i32 0, i32 %21
br label %23
23: ; preds = %7, %2, %10
%24 = phi i32 [ %22, %10 ], [ 127, %2 ], [ 127, %7 ]
%25 = phi i8 [ 0, %10 ], [ 0, %2 ], [ -1, %7 ]
%26 = select i1 %4, i32 128, i32 0
%27 = or i32 %24, %26
%28 = trunc i32 %27 to i8
store i8 %28, i8* %1
%29 = getelementptr inbounds i8, i8* %1, i64 4
ret void
}