#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 */
float float_max(float, float);
scalar_t__ frexp(float, int *);
void floats_to_rgbe(float floats[3], unsigned char rgbe[4]) {
  float rv = floats[0];
  float gv = floats[1];
  float bv = floats[2];
  float max_v = float_max(float_max(rv, gv), bv);
  if (max_v < 1e-32f) {
    rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
  } else {
    int ev;
    float max_m = (float)frexp(max_v, &ev);
    float factor = (1 / max_v) * max_m * 256;
    rgbe[0] = (unsigned char)(rv * factor);
    rgbe[1] = (unsigned char)(gv * factor);
    rgbe[2] = (unsigned char)(bv * factor);
    rgbe[3] = (unsigned char)(ev + 128);
  }
}


x86 assembly (clang, -O0):
<floats_to_rgbe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movss  0x4(%rax),%xmm0
movss  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movss  0x8(%rax),%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x14(%rbp),%xmm0
movss  -0x18(%rbp),%xmm1
call   1040 <float_max@plt>
movss  -0x1c(%rbp),%xmm1
call   1040 <float_max@plt>
movss  %xmm0,-0x20(%rbp)
movss  0xe81(%rip),%xmm0
ucomiss -0x20(%rbp),%xmm0
jbe    11ad <floats_to_rgbe+0x8d>
mov    -0x10(%rbp),%rax
movb   $0x0,0x3(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x2(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1251 <floats_to_rgbe+0x131>
movss  -0x20(%rbp),%xmm0
lea    -0x24(%rbp),%rdi
call   1030 <frexp@plt>
cvtsi2ss %rax,%xmm0
movss  %xmm0,-0x28(%rbp)
movss  -0x20(%rbp),%xmm1
movss  0xe32(%rip),%xmm0
divss  %xmm1,%xmm0
movss  -0x28(%rbp),%xmm1
mulss  %xmm1,%xmm0
movss  0xe21(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  %xmm0,-0x2c(%rbp)
movss  -0x14(%rbp),%xmm0
movss  -0x2c(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movss  -0x18(%rbp),%xmm0
movss  -0x2c(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
movss  -0x1c(%rbp),%xmm0
movss  -0x2c(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x24(%rbp),%eax
add    $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x3(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<floats_to_rgbe>:
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
movss  (%rdi),%xmm0
movss  0x4(%rdi),%xmm1
movss  0x8(%rdi),%xmm2
movss  %xmm2,0xc(%rsp)
movss  %xmm0,0x14(%rsp)
movss  %xmm1,0x18(%rsp)
call   1040 <float_max@plt>
movss  0xc(%rsp),%xmm1
call   1040 <float_max@plt>
movss  0xea0(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    116d <floats_to_rgbe+0x4d>
movl   $0x0,(%rbx)
jmp    11da <floats_to_rgbe+0xba>
lea    0x1c(%rsp),%rdi
movss  %xmm0,0x10(%rsp)
call   1030 <frexp@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  0xe77(%rip),%xmm1
divss  0x10(%rsp),%xmm1
mulss  %xmm0,%xmm1
mulss  0xe69(%rip),%xmm1
movss  0x14(%rsp),%xmm0
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %al,(%rbx)
movss  0x18(%rsp),%xmm0
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %al,0x1(%rbx)
movss  0xc(%rsp),%xmm0
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %al,0x2(%rbx)
mov    0x1c(%rsp),%al
xor    $0x80,%al
mov    %al,0x3(%rbx)
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define void @floats_to_rgbe(float* %0, i8* %1) {
%3 = alloca float*
%4 = alloca i8*
%5 = alloca float
%6 = alloca float
%7 = alloca float
%8 = alloca float
%9 = alloca i32
%10 = alloca float
%11 = alloca float
store float* %0, float** %3
store i8* %1, i8** %4
%12 = load float*, float** %3
%13 = getelementptr inbounds float, float* %12, i64 0
%14 = load float, float* %13
store float %14, float* %5
%15 = load float*, float** %3
%16 = getelementptr inbounds float, float* %15, i64 1
%17 = load float, float* %16
store float %17, float* %6
%18 = load float*, float** %3
%19 = getelementptr inbounds float, float* %18, i64 2
%20 = load float, float* %19
store float %20, float* %7
%21 = load float, float* %5
%22 = load float, float* %6
%23 = call float @float_max(float %21, float %22)
%24 = load float, float* %7
%25 = call float @float_max(float %23, float %24)
store float %25, float* %8
%26 = load float, float* %8
%27 = fcmp olt float %26, 0x3949F623E0000000
br i1 %27, label %28, label %37
28: ; preds = %2
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i64 3
store i8 0, i8* %30
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 2
store i8 0, i8* %32
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 0, i8* %34
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i64 0
store i8 0, i8* %36
br label %69
37: ; preds = %2
%38 = load float, float* %8
%39 = call i64 @frexp(float %38, i32* %9)
%40 = sitofp i64 %39 to float
store float %40, float* %10
%41 = load float, float* %8
%42 = fdiv float 1.000000e+00, %41
%43 = load float, float* %10
%44 = fmul float %42, %43
%45 = fmul float %44, 2.560000e+02
store float %45, float* %11
%46 = load float, float* %5
%47 = load float, float* %11
%48 = fmul float %46, %47
%49 = fptoui float %48 to i8
%50 = load i8*, i8** %4
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 %49, i8* %51
%52 = load float, float* %6
%53 = load float, float* %11
%54 = fmul float %52, %53
%55 = fptoui float %54 to i8
%56 = load i8*, i8** %4
%57 = getelementptr inbounds i8, i8* %56, i64 1
store i8 %55, i8* %57
%58 = load float, float* %7
%59 = load float, float* %11
%60 = fmul float %58, %59
%61 = fptoui float %60 to i8
%62 = load i8*, i8** %4
%63 = getelementptr inbounds i8, i8* %62, i64 2
store i8 %61, i8* %63
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 128
%66 = trunc i32 %65 to i8
%67 = load i8*, i8** %4
%68 = getelementptr inbounds i8, i8* %67, i64 3
store i8 %66, i8* %68
br label %69
69: ; preds = %37, %28
ret void
}

LLVM IR (-O2):
define void @floats_to_rgbe(float* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = load float, float* %0
%5 = getelementptr inbounds float, float* %0, i64 1
%6 = load float, float* %5
%7 = getelementptr inbounds float, float* %0, i64 2
%8 = load float, float* %7
%9 = tail call float @float_max(float %4, float %6)
%10 = tail call float @float_max(float %9, float %8)
%11 = fcmp olt float %10, 0x3949F623E0000000
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = bitcast i8* %1 to i32*
store i32 0, i32* %13
br label %33
14: ; preds = %2
%15 = bitcast i32* %3 to i8*
%16 = call i64 @frexp(float %10, i32* %3)
%17 = sitofp i64 %16 to float
%18 = fdiv float 1.000000e+00, %10
%19 = fmul float %18, %17
%20 = fmul float %19, 2.560000e+02
%21 = fmul float %4, %20
%22 = fptoui float %21 to i8
store i8 %22, i8* %1
%23 = fmul float %6, %20
%24 = fptoui float %23 to i8
%25 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %24, i8* %25
%26 = fmul float %8, %20
%27 = fptoui float %26 to i8
%28 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %27, i8* %28
%29 = load i32, i32* %3
%30 = trunc i32 %29 to i8
%31 = xor i8 %30, -128
%32 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %31, i8* %32
br label %33
33: ; preds = %14, %12
ret void
}