#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 >= 1.0) {
      i24[0] = i24[1] = 0xff;
      i24[2] = 0x7f;
    } else {
      int i = float2int(v * 8388607 + 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  0xebc(%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  0xe8d(%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    1281 <float_to_i24+0x171>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe0b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     121b <float_to_i24+0x10b>
mov    -0x10(%rbp),%rax
movb   $0xff,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0xff,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x7f,0x2(%rax)
jmp    127c <float_to_i24+0x16c>
movss  0xe05(%rip),%xmm0
mulss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xdd4(%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    1281 <float_to_i24+0x171>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<float_to_i24>:
push   %rbx
mov    %rsi,%rbx
movss  (%rdi),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm0,%xmm1
jbe    1136 <float_to_i24+0x26>
movss  0xee4(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    1151 <float_to_i24+0x41>
movw   $0x0,(%rbx)
mov    $0x80,%al
jmp    117d <float_to_i24+0x6d>
ucomiss 0xec3(%rip),%xmm0
jae    1176 <float_to_i24+0x66>
mulss  0xebd(%rip),%xmm0
addss  0xeb9(%rip),%xmm0
jmp    1169 <float_to_i24+0x59>
cvtss2sd %xmm0,%xmm0
mulsd  0xeb3(%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 %84
48: ; preds = %2
%49 = load float, float* %5
%50 = fpext float %49 to double
%51 = fcmp oge double %50, 1.000000e+00
br i1 %51, label %52, label %59
52: ; preds = %48
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i64 1
store i8 -1, i8* %54
%55 = load i8*, i8** %4
%56 = getelementptr inbounds i8, i8* %55, i64 0
store i8 -1, i8* %56
%57 = load i8*, i8** %4
%58 = getelementptr inbounds i8, i8* %57, i64 2
store i8 127, i8* %58
br label %83
59: ; preds = %48
%60 = load float, float* %5
%61 = fmul float %60, 0x415FFFFFC0000000
%62 = fpext float %61 to double
%63 = fadd double %62, 5.000000e-01
%64 = fptrunc double %63 to float
%65 = call i32 @float2int(float %64)
store i32 %65, i32* %7
%66 = load i32, i32* %7
%67 = and i32 %66, 255
%68 = trunc i32 %67 to i8
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i64 0
store i8 %68, i8* %70
%71 = load i32, i32* %7
%72 = ashr i32 %71, 8
%73 = and i32 %72, 255
%74 = trunc i32 %73 to i8
%75 = load i8*, i8** %4
%76 = getelementptr inbounds i8, i8* %75, i64 1
store i8 %74, i8* %76
%77 = load i32, i32* %7
%78 = ashr i32 %77, 16
%79 = and i32 %78, 255
%80 = trunc i32 %79 to i8
%81 = load i8*, i8** %4
%82 = getelementptr inbounds i8, i8* %81, i64 2
store i8 %80, i8* %82
br label %83
83: ; preds = %59, %52
br label %84
84: ; preds = %83, %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, 1.000000e+00
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 = fmul float %3, 0x415FFFFFC0000000
%26 = fadd float %25, 5.000000e-01
%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
}