#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 unsigned char i;

/* Variables and functions */
int float2int(float);
void float_to_i24(float *vv, unsigned char *i24) {
  float v = *vv;
  if (v < 0.0) {
    if (v < -1.0) {
      i24[0] = i24[1] = 0x00;
      i24[2] = 0x80;
    } else {
      int i = float2int(v * 8388608.0 - 0.5);
      i24[0] = (i)&0xff;
      i24[1] = (i >> 8) & 0xff;
      i24[2] = (i >> 16) & 0xff;
    }
  } else {
    if (v >= (8388606.5f / 8388608.0f)) {
      i24[0] = i24[1] = 0xff;
      i24[2] = 0x7f;
    } else {
      int i = float2int(v * 8388608.0 + 0.5);
      i24[0] = (i)&0xff;
      i24[1] = (i >> 8) & 0xff;
      i24[2] = (i >> 16) & 0xff;
    }
  }
}


x86 assembly (clang, -O0):
<float_to_i24>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    11e4 <float_to_i24+0xd4>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xec4(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    117a <float_to_i24+0x6a>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x80,0x2(%rax)
jmp    11df <float_to_i24+0xcf>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe7d(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe89(%rip),%xmm1
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1030 <float2int@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
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)
jmp    1280 <float_to_i24+0x170>
movss  -0x14(%rbp),%xmm0
movss  0xe0f(%rip),%xmm1
ucomiss %xmm1,%xmm0
jb     1216 <float_to_i24+0x106>
mov    -0x10(%rbp),%rax
movb   $0xff,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0xff,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x7f,0x2(%rax)
jmp    127b <float_to_i24+0x16b>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xde1(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xddd(%rip),%xmm1
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1030 <float2int@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x1c(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    1280 <float_to_i24+0x170>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<float_to_i24>:
push   %rbx
mov    %rsi,%rbx
movss  (%rdi),%xmm1
cvtss2sd %xmm1,%xmm0
xorps  %xmm2,%xmm2
ucomiss %xmm1,%xmm2
jbe    113a <float_to_i24+0x2a>
movss  0xed8(%rip),%xmm2
ucomiss %xmm1,%xmm2
jbe    1155 <float_to_i24+0x45>
movw   $0x0,(%rbx)
mov    $0x80,%al
jmp    117d <float_to_i24+0x6d>
ucomiss 0xebf(%rip),%xmm1
jae    1176 <float_to_i24+0x66>
mulsd  0xebd(%rip),%xmm0
addsd  0xebd(%rip),%xmm0
jmp    1165 <float_to_i24+0x55>
mulsd  0xeab(%rip),%xmm0
addsd  0xeb3(%rip),%xmm0
cvtsd2ss %xmm0,%xmm0
call   1030 <float2int@plt>
mov    %ax,(%rbx)
shr    $0x10,%eax
jmp    117d <float_to_i24+0x6d>
movw   $0xffff,(%rbx)
mov    $0x7f,%al
mov    %al,0x2(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
define void @float_to_i24(float* %0, i8* %1) {
%3 = alloca float*
%4 = alloca i8*
%5 = alloca float
%6 = alloca i32
%7 = alloca i32
store float* %0, float** %3
store i8* %1, i8** %4
%8 = load float*, float** %3
%9 = load float, float* %8
store float %9, float* %5
%10 = load float, float* %5
%11 = fpext float %10 to double
%12 = fcmp olt double %11, 0.000000e+00
br i1 %12, label %13, label %48
13: ; preds = %2
%14 = load float, float* %5
%15 = fpext float %14 to double
%16 = fcmp olt double %15, -1.000000e+00
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 0, i8* %19
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 0, i8* %21
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 2
store i8 -128, i8* %23
br label %47
24: ; preds = %13
%25 = load float, float* %5
%26 = fpext float %25 to double
%27 = call double @llvm.fmuladd.f64(double %26, double 0x4160000000000000, double -5.000000e-01)
%28 = fptrunc double %27 to float
%29 = call i32 @float2int(float %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = and i32 %30, 255
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 0
store i8 %32, i8* %34
%35 = load i32, i32* %6
%36 = ashr i32 %35, 8
%37 = and i32 %36, 255
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8 %38, i8* %40
%41 = load i32, i32* %6
%42 = ashr i32 %41, 16
%43 = and i32 %42, 255
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %4
%46 = getelementptr inbounds i8, i8* %45, i64 2
store i8 %44, i8* %46
br label %47
47: ; preds = %24, %17
br label %82
48: ; preds = %2
%49 = load float, float* %5
%50 = fcmp oge float %49, 0x3FEFFFFFA0000000
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i64 1
store i8 -1, i8* %53
%54 = load i8*, i8** %4
%55 = getelementptr inbounds i8, i8* %54, i64 0
store i8 -1, i8* %55
%56 = load i8*, i8** %4
%57 = getelementptr inbounds i8, i8* %56, i64 2
store i8 127, i8* %57
br label %81
58: ; preds = %48
%59 = load float, float* %5
%60 = fpext float %59 to double
%61 = call double @llvm.fmuladd.f64(double %60, double 0x4160000000000000, double 5.000000e-01)
%62 = fptrunc double %61 to float
%63 = call i32 @float2int(float %62)
store i32 %63, i32* %7
%64 = load i32, i32* %7
%65 = and i32 %64, 255
%66 = trunc i32 %65 to i8
%67 = load i8*, i8** %4
%68 = getelementptr inbounds i8, i8* %67, i64 0
store i8 %66, i8* %68
%69 = load i32, i32* %7
%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 1
store i8 %72, i8* %74
%75 = load i32, i32* %7
%76 = ashr i32 %75, 16
%77 = and i32 %76, 255
%78 = trunc i32 %77 to i8
%79 = load i8*, i8** %4
%80 = getelementptr inbounds i8, i8* %79, i64 2
store i8 %78, i8* %80
br label %81
81: ; preds = %58, %51
br label %82
82: ; preds = %81, %47
ret void
}

LLVM IR (-O2):
define void @float_to_i24(float* nocapture %0, i8* nocapture writeonly %1) {
%3 = load float, float* %0
%4 = fpext float %3 to double
%5 = fcmp olt float %3, 0.000000e+00
br i1 %5, label %6, label %20
6: ; preds = %2
%7 = fcmp olt float %3, -1.000000e+00
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %9
store i8 0, i8* %1
br label %34
10: ; preds = %6
%11 = tail call double @llvm.fmuladd.f64(double %4, double 0x4160000000000000, double -5.000000e-01)
%12 = fptrunc double %11 to float
%13 = tail call i32 @float2int(float %12)
%14 = trunc i32 %13 to i8
store i8 %14, i8* %1
%15 = lshr i32 %13, 8
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %16, i8* %17
%18 = lshr i32 %13, 16
%19 = trunc i32 %18 to i8
br label %34
20: ; preds = %2
%21 = fcmp ult float %3, 0x3FEFFFFFA0000000
br i1 %21, label %24, label %22
22: ; preds = %20
%23 = getelementptr inbounds i8, i8* %1, i64 1
store i8 -1, i8* %23
store i8 -1, i8* %1
br label %34
24: ; preds = %20
%25 = tail call double @llvm.fmuladd.f64(double %4, double 0x4160000000000000, double 5.000000e-01)
%26 = fptrunc double %25 to float
%27 = tail call i32 @float2int(float %26)
%28 = trunc i32 %27 to i8
store i8 %28, i8* %1
%29 = lshr i32 %27, 8
%30 = trunc i32 %29 to i8
%31 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %30, i8* %31
%32 = lshr i32 %27, 16
%33 = trunc i32 %32 to i8
br label %34
34: ; preds = %22, %24, %8, %10
%35 = phi i8 [ 127, %22 ], [ %33, %24 ], [ -128, %8 ], [ %19, %10 ]
%36 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %35, i8* %36
ret void
}