#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 */
int fabs(double);
int /*<<< orphan*/ floor(double);
double fmod(double, double);
double frexp(double, int *);
int lrint(int /*<<< orphan*/);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void double64_be_write(double in, unsigned char *out) {
  int exponent, mantissa;
  memset(out, 0, sizeof(double));
  if (fabs(in) < 1e-30)
    return;
  if (in < 0.0) {
    in *= -1.0;
    out[0] |= 0x80;
  };
  in = frexp(in, &exponent);
  exponent += 1022;
  out[0] |= (exponent >> 4) & 0x7F;
  out[1] |= (exponent << 4) & 0xF0;
  in *= 0x20000000;
  mantissa = lrint(floor(in));
  out[1] |= (mantissa >> 24) & 0xF;
  out[2] = (mantissa >> 16) & 0xFF;
  out[3] = (mantissa >> 8) & 0xFF;
  out[4] = mantissa & 0xFF;
  in = fmod(in, 1.0);
  in *= 0x1000000;
  mantissa = lrint(floor(in));
  out[5] = (mantissa >> 16) & 0xFF;
  out[6] = (mantissa >> 8) & 0xFF;
  out[7] = mantissa & 0xFF;
  return;
}


x86 assembly (clang, -O0):
<double64_be_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movsd  -0x8(%rbp),%xmm0
call   1060 <fabs@plt>
cvtsi2sd %eax,%xmm1
movsd  0xe69(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11a6 <double64_be_write+0x46>
jmp    130b <double64_be_write+0x1ab>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11d5 <double64_be_write+0x75>
movsd  0xe4c(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%ecx
or     $0x80,%ecx
mov    %cl,(%rax)
movsd  -0x8(%rbp),%xmm0
lea    -0x14(%rbp),%rdi
call   1050 <frexp@plt>
movsd  %xmm0,-0x8(%rbp)
mov    -0x14(%rbp),%eax
add    $0x3fe,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
sar    $0x4,%edx
and    $0x7f,%edx
mov    -0x10(%rbp),%rax
movzbl (%rax),%ecx
or     %edx,%ecx
mov    %cl,(%rax)
mov    -0x14(%rbp),%edx
shl    $0x4,%edx
and    $0xf0,%edx
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
or     %edx,%ecx
mov    %cl,0x1(%rax)
movsd  0xdf8(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1080 <floor@plt>
mov    %eax,%edi
call   1070 <lrint@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edx
sar    $0x18,%edx
and    $0xf,%edx
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
or     %edx,%ecx
mov    %cl,0x1(%rax)
mov    -0x18(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x4(%rax)
movsd  -0x8(%rbp),%xmm0
movsd  0xd76(%rip),%xmm1
call   1030 <fmod@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  0xd5c(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1080 <floor@plt>
mov    %eax,%edi
call   1070 <lrint@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x5(%rax)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x6(%rax)
mov    -0x18(%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):
<double64_be_write>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movaps %xmm0,0x10(%rsp)
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movapd 0x10(%rsp),%xmm0
call   1060 <fabs@plt>
test   %eax,%eax
jle    123a <double64_be_write+0xda>
xorpd  %xmm0,%xmm0
movapd 0x10(%rsp),%xmm1
ucomisd %xmm1,%xmm0
movapd %xmm1,%xmm0
jbe    11ab <double64_be_write+0x4b>
xorpd  0xe58(%rip),%xmm0
orb    $0x80,(%rbx)
lea    0xc(%rsp),%rdi
call   1050 <frexp@plt>
mov    $0x3fe,%eax
add    0xc(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %eax,%ecx
shr    $0x4,%ecx
and    $0x7f,%cl
or     %cl,(%rbx)
shl    $0x4,%al
or     %al,0x1(%rbx)
mulsd  0xe36(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
call   1080 <floor@plt>
mov    %eax,%edi
call   1070 <lrint@plt>
mov    %eax,%ecx
shr    $0x18,%ecx
and    $0xf,%cl
or     %cl,0x1(%rbx)
mov    %eax,%ecx
shr    $0x10,%ecx
mov    %cl,0x2(%rbx)
mov    %ah,0x3(%rbx)
mov    %al,0x4(%rbx)
movsd  0xe0b(%rip),%xmm1
movsd  0x10(%rsp),%xmm0
call   1030 <fmod@plt>
mulsd  0xe00(%rip),%xmm0
call   1080 <floor@plt>
mov    %eax,%edi
call   1070 <lrint@plt>
mov    %eax,%ecx
shr    $0x10,%ecx
mov    %cl,0x5(%rbx)
mov    %ah,0x6(%rbx)
mov    %al,0x7(%rbx)
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define void @double64_be_write(double %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @memset(i8* %1, i32 0, i32 8)
%6 = tail call i32 @fabs(double %0)
%7 = icmp slt i32 %6, 1
br i1 %7, label %57, label %8
8: ; preds = %2
%9 = fcmp olt double %0, 0.000000e+00
br i1 %9, label %10, label %14
10: ; preds = %8
%11 = fneg double %0
%12 = load i8, i8* %1
%13 = or i8 %12, -128
store i8 %13, i8* %1
br label %14
14: ; preds = %10, %8
%15 = phi double [ %11, %10 ], [ %0, %8 ]
%16 = call double @frexp(double %15, i32* %3)
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1022
store i32 %18, i32* %3
%19 = lshr i32 %18, 4
%20 = load i8, i8* %1
%21 = trunc i32 %19 to i8
%22 = and i8 %21, 127
%23 = or i8 %22, %20
store i8 %23, i8* %1
%24 = getelementptr inbounds i8, i8* %1, i64 1
%25 = load i8, i8* %24
%26 = trunc i32 %18 to i8
%27 = shl i8 %26, 4
%28 = or i8 %25, %27
store i8 %28, i8* %24
%29 = fmul double %16, 0x41C0000000000000
%30 = tail call i32 @floor(double %29)
%31 = tail call i32 @lrint(i32 %30)
%32 = lshr i32 %31, 24
%33 = load i8, i8* %24
%34 = trunc i32 %32 to i8
%35 = and i8 %34, 15
%36 = or i8 %35, %33
store i8 %36, i8* %24
%37 = lshr i32 %31, 16
%38 = trunc i32 %37 to i8
%39 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %38, i8* %39
%40 = lshr i32 %31, 8
%41 = trunc i32 %40 to i8
%42 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %41, i8* %42
%43 = trunc i32 %31 to i8
%44 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %43, i8* %44
%45 = tail call double @fmod(double %29, double 1.000000e+00)
%46 = fmul double %45, 0x4170000000000000
%47 = tail call i32 @floor(double %46)
%48 = tail call i32 @lrint(i32 %47)
%49 = lshr i32 %48, 16
%50 = trunc i32 %49 to i8
%51 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %50, i8* %51
%52 = lshr i32 %48, 8
%53 = trunc i32 %52 to i8
%54 = getelementptr inbounds i8, i8* %1, i64 6
store i8 %53, i8* %54
%55 = trunc i32 %48 to i8
%56 = getelementptr inbounds i8, i8* %1, i64 7
store i8 %55, i8* %56
br label %57
57: ; preds = %2, %14
ret void
}