#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 int uint32_t;
typedef int uint16_t;
typedef int int32_t;
typedef int int16_t;

/* Variables and functions */
float frexpf(float, int *);
int /*<<< orphan*/ roundf(float);
uint32_t pack_float(float value) {
  int exponent;
  float mantissa = frexpf(value, &exponent);
  int32_t fixed_mantissa = (int32_t)roundf(mantissa * 0x8000);
  int16_t fractional;
  if (fixed_mantissa > 0x7fff) {
    fractional = 0x7fff;
  } else if (fixed_mantissa < -0x8000) {
    fractional = -0x8000;
  } else {
    fractional = fixed_mantissa;
  }
  return (((uint32_t)exponent & 0xffff) << 16) | (uint16_t)fractional;
}


x86 assembly (clang, -O0):
<pack_float>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
lea    -0x8(%rbp),%rdi
call   1030 <frexpf@plt>
movss  %xmm0,-0xc(%rbp)
movss  0xeb8(%rip),%xmm0
mulss  -0xc(%rbp),%xmm0
call   1040 <roundf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x7fff,-0x10(%rbp)
jle    116e <pack_float+0x4e>
movl   $0x7fff,-0x14(%rbp)
jmp    1192 <pack_float+0x72>
cmpl   $0xffff8000,-0x10(%rbp)
jge    1187 <pack_float+0x67>
movl   $0xffff8000,-0x14(%rbp)
jmp    118d <pack_float+0x6d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1192 <pack_float+0x72>
mov    -0x8(%rbp),%eax
and    $0xffff,%eax
shl    $0x10,%eax
or     -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pack_float>:
push   %rax
lea    0x4(%rsp),%rdi
call   1030 <frexpf@plt>
mulss  0xecd(%rip),%xmm0
call   1040 <roundf@plt>
cmp    $0xffff8001,%eax
mov    $0xffff8000,%ecx
cmovl  %ecx,%eax
cmp    $0x7fff,%eax
mov    $0x7fff,%ecx
cmovge %ecx,%eax
mov    0x4(%rsp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @pack_float(float %0) {
%2 = alloca float
%3 = alloca i32
%4 = alloca float
%5 = alloca i32
%6 = alloca i32
store float %0, float* %2
%7 = load float, float* %2
%8 = call float @frexpf(float %7, i32* %3)
store float %8, float* %4
%9 = load float, float* %4
%10 = fmul float %9, 3.276800e+04
%11 = call i32 @roundf(float %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 32767
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 32767, i32* %6
br label %22
15: ; preds = %1
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, -32768
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -32768, i32* %6
br label %21
19: ; preds = %15
%20 = load i32, i32* %5
store i32 %20, i32* %6
br label %21
21: ; preds = %19, %18
br label %22
22: ; preds = %21, %14
%23 = load i32, i32* %3
%24 = and i32 %23, 65535
%25 = shl i32 %24, 16
%26 = load i32, i32* %6
%27 = or i32 %25, %26
ret i32 %27
}

LLVM IR (-O2):
define i32 @pack_float(float %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call float @frexpf(float %0, i32* %2)
%5 = fmul float %4, 3.276800e+04
%6 = tail call i32 @roundf(float %5)
%7 = icmp sgt i32 %6, -32768
%8 = select i1 %7, i32 %6, i32 -32768
%9 = icmp slt i32 %8, 32767
%10 = select i1 %9, i32 %8, i32 32767
%11 = load i32, i32* %2
%12 = shl i32 %11, 16
%13 = or i32 %10, %12
ret i32 %13
}