#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(float);
float frexp(float, int *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void float32_be_write(float in, unsigned char *out) {
  int exponent, mantissa, negative = 0;
  memset(out, 0, sizeof(int));
  if (fabs(in) < 1e-30)
    return;
  if (in < 0.0) {
    in *= -1.0;
    negative = 1;
  };
  in = frexp(in, &exponent);
  exponent += 126;
  in *= (float)0x1000000;
  mantissa = (((int)in) & 0x7FFFFF);
  if (negative)
    out[0] |= 0x80;
  if (exponent & 0x01)
    out[1] |= 0x80;
  out[3] = mantissa & 0xFF;
  out[2] = (mantissa >> 8) & 0xFF;
  out[1] |= (mantissa >> 16) & 0x7F;
  out[0] |= (exponent >> 1) & 0x7F;
  return;
}


x86 assembly (clang, -O0):
<float32_be_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movss  %xmm0,-0x4(%rbp)
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movss  -0x4(%rbp),%xmm0
call   1050 <fabs@plt>
cvtsi2sd %eax,%xmm1
movsd  0xe92(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    117d <float32_be_write+0x4d>
jmp    127b <float32_be_write+0x14b>
movss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    11b8 <float32_be_write+0x88>
movss  -0x4(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe64(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
movss  -0x4(%rbp),%xmm0
lea    -0x14(%rbp),%rdi
call   1040 <frexp@plt>
movss  %xmm0,-0x4(%rbp)
mov    -0x14(%rbp),%eax
add    $0x7e,%eax
mov    %eax,-0x14(%rbp)
movss  0xe34(%rip),%xmm0
mulss  -0x4(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
cvttss2si -0x4(%rbp),%eax
and    $0x7fffff,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     120c <float32_be_write+0xdc>
mov    -0x10(%rbp),%rax
movzbl (%rax),%ecx
or     $0x80,%ecx
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     122c <float32_be_write+0xfc>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
or     $0x80,%ecx
mov    %cl,0x1(%rax)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x18(%rbp),%edx
sar    $0x10,%edx
and    $0x7f,%edx
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
or     %edx,%ecx
mov    %cl,0x1(%rax)
mov    -0x14(%rbp),%edx
sar    $0x1,%edx
and    $0x7f,%edx
mov    -0x10(%rbp),%rax
movzbl (%rax),%ecx
or     %edx,%ecx
mov    %cl,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<float32_be_write>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
movaps %xmm0,0x10(%rsp)
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movaps 0x10(%rsp),%xmm0
call   1050 <fabs@plt>
test   %eax,%eax
jle    11c4 <float32_be_write+0x94>
movaps 0xea2(%rip),%xmm1
movaps 0x10(%rsp),%xmm2
xorps  %xmm2,%xmm1
xorps  %xmm3,%xmm3
movaps %xmm2,%xmm0
cmpltss %xmm3,%xmm0
andps  %xmm0,%xmm1
andnps %xmm2,%xmm0
orps   %xmm1,%xmm0
lea    0xc(%rsp),%rdi
call   1040 <frexp@plt>
xorps  %xmm1,%xmm1
ucomiss 0x10(%rsp),%xmm1
mov    0xc(%rsp),%eax
mulss  0xe78(%rip),%xmm0
cvttss2si %xmm0,%ecx
jbe    11a1 <float32_be_write+0x71>
orb    $0x80,(%rbx)
mov    %eax,%edx
shl    $0x7,%dl
or     0x1(%rbx),%dl
mov    %ecx,%esi
rol    $0x8,%si
mov    %si,0x2(%rbx)
shr    $0x10,%ecx
and    $0x7f,%cl
or     %dl,%cl
mov    %cl,0x1(%rbx)
add    $0x7e,%al
shr    %al
or     %al,(%rbx)
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define void @float32_be_write(float %0, i8* %1) {
%3 = alloca float
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store float %0, float* %3
store i8* %1, i8** %4
store i32 0, i32* %7
%8 = load i8*, i8** %4
%9 = call i32 @memset(i8* %8, i32 0, i32 4)
%10 = load float, float* %3
%11 = call i32 @fabs(float %10)
%12 = sitofp i32 %11 to double
%13 = fcmp olt double %12, 1.000000e-30
br i1 %13, label %14, label %15
14: ; preds = %2
br label %84
15: ; preds = %2
%16 = load float, float* %3
%17 = fpext float %16 to double
%18 = fcmp olt double %17, 0.000000e+00
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load float, float* %3
%21 = fpext float %20 to double
%22 = fmul double %21, -1.000000e+00
%23 = fptrunc double %22 to float
store float %23, float* %3
store i32 1, i32* %7
br label %24
24: ; preds = %19, %15
%25 = load float, float* %3
%26 = call float @frexp(float %25, i32* %5)
store float %26, float* %3
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 126
store i32 %28, i32* %5
%29 = load float, float* %3
%30 = fmul float %29, 0x4170000000000000
store float %30, float* %3
%31 = load float, float* %3
%32 = fptosi float %31 to i32
%33 = and i32 %32, 8388607
store i32 %33, i32* %6
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %24
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = or i32 %40, 128
%42 = trunc i32 %41 to i8
store i8 %42, i8* %38
br label %43
43: ; preds = %36, %24
%44 = load i32, i32* %5
%45 = and i32 %44, 1
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %43
%48 = load i8*, i8** %4
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = or i32 %51, 128
%53 = trunc i32 %52 to i8
store i8 %53, i8* %49
br label %54
54: ; preds = %47, %43
%55 = load i32, i32* %6
%56 = and i32 %55, 255
%57 = trunc i32 %56 to i8
%58 = load i8*, i8** %4
%59 = getelementptr inbounds i8, i8* %58, i64 3
store i8 %57, i8* %59
%60 = load i32, i32* %6
%61 = ashr i32 %60, 8
%62 = and i32 %61, 255
%63 = trunc i32 %62 to i8
%64 = load i8*, i8** %4
%65 = getelementptr inbounds i8, i8* %64, i64 2
store i8 %63, i8* %65
%66 = load i32, i32* %6
%67 = ashr i32 %66, 16
%68 = and i32 %67, 127
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i64 1
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = or i32 %72, %68
%74 = trunc i32 %73 to i8
store i8 %74, i8* %70
%75 = load i32, i32* %5
%76 = ashr i32 %75, 1
%77 = and i32 %76, 127
%78 = load i8*, i8** %4
%79 = getelementptr inbounds i8, i8* %78, i64 0
%80 = load i8, i8* %79
%81 = zext i8 %80 to i32
%82 = or i32 %81, %77
%83 = trunc i32 %82 to i8
store i8 %83, i8* %79
br label %84
84: ; preds = %54, %14
ret void
}

LLVM IR (-O2):
define void @float32_be_write(float %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @memset(i8* %1, i32 0, i32 4)
%6 = tail call i32 @fabs(float %0)
%7 = icmp slt i32 %6, 1
br i1 %7, label %40, label %8
8: ; preds = %2
%9 = fcmp olt float %0, 0.000000e+00
%10 = fneg float %0
%11 = select i1 %9, float %10, float %0
%12 = call float @frexp(float %11, i32* %3)
%13 = load i32, i32* %3
%14 = fmul float %12, 0x4170000000000000
%15 = fptosi float %14 to i32
br i1 %9, label %16, label %19
16: ; preds = %8
%17 = load i8, i8* %1
%18 = or i8 %17, -128
store i8 %18, i8* %1
br label %19
19: ; preds = %16, %8
%20 = getelementptr inbounds i8, i8* %1, i64 1
%21 = load i8, i8* %20
%22 = trunc i32 %13 to i8
%23 = shl i8 %22, 7
%24 = or i8 %23, %21
%25 = trunc i32 %15 to i8
%26 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %25, i8* %26
%27 = lshr i32 %15, 8
%28 = trunc i32 %27 to i8
%29 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %28, i8* %29
%30 = lshr i32 %15, 16
%31 = getelementptr inbounds i8, i8* %1, i64 1
%32 = trunc i32 %30 to i8
%33 = and i8 %32, 127
%34 = or i8 %24, %33
store i8 %34, i8* %31
%35 = load i8, i8* %1
%36 = trunc i32 %13 to i8
%37 = add i8 %36, 126
%38 = lshr i8 %37, 1
%39 = or i8 %35, %38
store i8 %39, i8* %1
br label %40
40: ; preds = %2, %19
ret void
}