#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(double);
void double_to_i24(double *vv, unsigned char *i24) {
  double 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.5 / 8388608.0)) {
      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):
<double_to_i24>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11cc <double_to_i24+0xbc>
movsd  0xed5(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    116a <double_to_i24+0x5a>
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x80,0x2(%rax)
jmp    11c7 <double_to_i24+0xb7>
movsd  -0x18(%rbp),%xmm0
movsd  0xe91(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe9d(%rip),%xmm1
addsd  %xmm1,%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    1261 <double_to_i24+0x151>
movsd  -0x18(%rbp),%xmm0
movsd  0xe27(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11ff <double_to_i24+0xef>
mov    -0x10(%rbp),%rax
movb   $0xff,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0xff,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x7f,0x2(%rax)
jmp    125c <double_to_i24+0x14c>
movsd  -0x18(%rbp),%xmm0
movsd  0xdfc(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xdf8(%rip),%xmm1
addsd  %xmm1,%xmm0
call   1030 <float2int@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x20(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    1261 <double_to_i24+0x151>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_to_i24>:
push   %rbx
mov    %rsi,%rbx
movsd  (%rdi),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1139 <double_to_i24+0x29>
movsd  0xeee(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1155 <double_to_i24+0x45>
movw   $0x0,(%rbx)
mov    $0x80,%al
jmp    1179 <double_to_i24+0x69>
ucomisd 0xebf(%rip),%xmm0
jae    1172 <double_to_i24+0x62>
mulsd  0xebd(%rip),%xmm0
addsd  0xebd(%rip),%xmm0
jmp    1165 <double_to_i24+0x55>
mulsd  0xeab(%rip),%xmm0
addsd  0xebb(%rip),%xmm0
call   1030 <float2int@plt>
mov    %ax,(%rbx)
shr    $0x10,%eax
jmp    1179 <double_to_i24+0x69>
movw   $0xffff,(%rbx)
mov    $0x7f,%al
mov    %al,0x2(%rbx)
pop    %rbx
ret

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

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