#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 /*<<< 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 (in == 0.0)
    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
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    118e <double64_be_write+0x3e>
jp     118e <double64_be_write+0x3e>
jmp    12f3 <double64_be_write+0x1a3>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11bd <double64_be_write+0x6d>
movsd  0xe5c(%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  0xe08(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1070 <floor@plt>
mov    %eax,%edi
call   1060 <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  0xd86(%rip),%xmm1
call   1030 <fmod@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  0xd6c(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1070 <floor@plt>
mov    %eax,%edi
call   1060 <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
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    117f <double64_be_write+0x2f>
jnp    121b <double64_be_write+0xcb>
ucomisd %xmm0,%xmm1
jbe    1190 <double64_be_write+0x40>
xorpd  0xe73(%rip),%xmm0
orb    $0x80,(%rbx)
lea    0xc(%rsp),%rdi
call   1050 <frexp@plt>
mov    $0x3fe,%eax
add    0xc(%rsp),%eax
mov    %eax,%ecx
shr    $0x4,%ecx
and    $0x7f,%cl
or     %cl,(%rbx)
shl    $0x4,%al
or     %al,0x1(%rbx)
mulsd  0xe55(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
call   1070 <floor@plt>
mov    %eax,%edi
call   1060 <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  0xe2a(%rip),%xmm1
movsd  0x10(%rsp),%xmm0
call   1030 <fmod@plt>
mulsd  0xe1f(%rip),%xmm0
call   1070 <floor@plt>
mov    %eax,%edi
call   1060 <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 = fcmp oeq double %9, 0.000000e+00
br i1 %10, label %11, label %12
11: ; preds = %2
br label %102
12: ; preds = %2
%13 = load double, double* %3
%14 = fcmp olt double %13, 0.000000e+00
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load double, double* %3
%17 = fmul double %16, -1.000000e+00
store double %17, double* %3
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = or i32 %21, 128
%23 = trunc i32 %22 to i8
store i8 %23, i8* %19
br label %24
24: ; preds = %15, %12
%25 = load double, double* %3
%26 = call double @frexp(double %25, i32* %5)
store double %26, double* %3
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1022
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = ashr i32 %29, 4
%31 = and i32 %30, 127
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = or i32 %35, %31
%37 = trunc i32 %36 to i8
store i8 %37, i8* %33
%38 = load i32, i32* %5
%39 = shl i32 %38, 4
%40 = and i32 %39, 240
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = or i32 %44, %40
%46 = trunc i32 %45 to i8
store i8 %46, i8* %42
%47 = load double, double* %3
%48 = fmul double %47, 0x41C0000000000000
store double %48, double* %3
%49 = load double, double* %3
%50 = call i32 @floor(double %49)
%51 = call i32 @lrint(i32 %50)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = ashr i32 %52, 24
%54 = and i32 %53, 15
%55 = load i8*, i8** %4
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = or i32 %58, %54
%60 = trunc i32 %59 to i8
store i8 %60, i8* %56
%61 = load i32, i32* %6
%62 = ashr i32 %61, 16
%63 = and i32 %62, 255
%64 = trunc i32 %63 to i8
%65 = load i8*, i8** %4
%66 = getelementptr inbounds i8, i8* %65, i64 2
store i8 %64, i8* %66
%67 = load i32, i32* %6
%68 = ashr i32 %67, 8
%69 = and i32 %68, 255
%70 = trunc i32 %69 to i8
%71 = load i8*, i8** %4
%72 = getelementptr inbounds i8, i8* %71, i64 3
store i8 %70, i8* %72
%73 = load i32, i32* %6
%74 = and i32 %73, 255
%75 = trunc i32 %74 to i8
%76 = load i8*, i8** %4
%77 = getelementptr inbounds i8, i8* %76, i64 4
store i8 %75, i8* %77
%78 = load double, double* %3
%79 = call double @fmod(double %78, double 1.000000e+00)
store double %79, double* %3
%80 = load double, double* %3
%81 = fmul double %80, 0x4170000000000000
store double %81, double* %3
%82 = load double, double* %3
%83 = call i32 @floor(double %82)
%84 = call i32 @lrint(i32 %83)
store i32 %84, i32* %6
%85 = load i32, i32* %6
%86 = ashr i32 %85, 16
%87 = and i32 %86, 255
%88 = trunc i32 %87 to i8
%89 = load i8*, i8** %4
%90 = getelementptr inbounds i8, i8* %89, i64 5
store i8 %88, i8* %90
%91 = load i32, i32* %6
%92 = ashr i32 %91, 8
%93 = and i32 %92, 255
%94 = trunc i32 %93 to i8
%95 = load i8*, i8** %4
%96 = getelementptr inbounds i8, i8* %95, i64 6
store i8 %94, i8* %96
%97 = load i32, i32* %6
%98 = and i32 %97, 255
%99 = trunc i32 %98 to i8
%100 = load i8*, i8** %4
%101 = getelementptr inbounds i8, i8* %100, i64 7
store i8 %99, i8* %101
br label %102
102: ; preds = %24, %11
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 = fcmp oeq double %0, 0.000000e+00
br i1 %6, label %56, label %7
7: ; preds = %2
%8 = fcmp olt double %0, 0.000000e+00
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = fneg double %0
%11 = load i8, i8* %1
%12 = or i8 %11, -128
store i8 %12, i8* %1
br label %13
13: ; preds = %9, %7
%14 = phi double [ %10, %9 ], [ %0, %7 ]
%15 = call double @frexp(double %14, i32* %3)
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1022
%18 = lshr i32 %17, 4
%19 = load i8, i8* %1
%20 = trunc i32 %18 to i8
%21 = and i8 %20, 127
%22 = or i8 %21, %19
store i8 %22, i8* %1
%23 = getelementptr inbounds i8, i8* %1, i64 1
%24 = load i8, i8* %23
%25 = trunc i32 %17 to i8
%26 = shl i8 %25, 4
%27 = or i8 %24, %26
store i8 %27, i8* %23
%28 = fmul double %15, 0x41C0000000000000
%29 = tail call i32 @floor(double %28)
%30 = tail call i32 @lrint(i32 %29)
%31 = lshr i32 %30, 24
%32 = load i8, i8* %23
%33 = trunc i32 %31 to i8
%34 = and i8 %33, 15
%35 = or i8 %34, %32
store i8 %35, i8* %23
%36 = lshr i32 %30, 16
%37 = trunc i32 %36 to i8
%38 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %37, i8* %38
%39 = lshr i32 %30, 8
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %40, i8* %41
%42 = trunc i32 %30 to i8
%43 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %42, i8* %43
%44 = tail call double @fmod(double %28, double 1.000000e+00)
%45 = fmul double %44, 0x4170000000000000
%46 = tail call i32 @floor(double %45)
%47 = tail call i32 @lrint(i32 %46)
%48 = lshr i32 %47, 16
%49 = trunc i32 %48 to i8
%50 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %49, i8* %50
%51 = lshr i32 %47, 8
%52 = trunc i32 %51 to i8
%53 = getelementptr inbounds i8, i8* %1, i64 6
store i8 %52, i8* %53
%54 = trunc i32 %47 to i8
%55 = getelementptr inbounds i8, i8* %1, i64 7
store i8 %54, i8* %55
br label %56
56: ; preds = %2, %13
ret void
}