#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 */
double bits_to_double(unsigned long);
unsigned long double_to_bits(double);
double _truncate(double x) {
  unsigned long bits, exponent;
  if (x < 1.0 && x > -1.0)
    return 0.0;
  bits = double_to_bits(x);
  exponent = (bits >> 52) & 0x7ff;
  exponent -= 1023;
  if (exponent < 52)
    bits &= ~(0xffffffffffffful >> exponent);
  return bits_to_double(bits);
}


x86 assembly (clang, -O0):
<_truncate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  0xecb(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1164 <_truncate+0x44>
movsd  -0x10(%rbp),%xmm0
movsd  0xebb(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1164 <_truncate+0x44>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c8 <_truncate+0xa8>
movsd  -0x10(%rbp),%xmm0
call   1030 <double_to_bits@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x34,%rax
and    $0x7ff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x3ff,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x34,-0x20(%rbp)
jae    11ba <_truncate+0x9a>
mov    -0x20(%rbp),%rcx
movabs $0xfffffffffffff,%rax
shr    %cl,%rax
xor    $0xffffffffffffffff,%rax
and    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <bits_to_double@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_truncate>:
push   %rax
movsd  0xed7(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    113f <_truncate+0x1f>
ucomisd 0xed1(%rip),%xmm0
jbe    113f <_truncate+0x1f>
xorpd  %xmm0,%xmm0
pop    %rax
ret
call   1030 <double_to_bits@plt>
mov    %rax,%rcx
shr    $0x34,%rcx
and    $0x7ff,%ecx
add    $0xfffffffffffffc01,%rcx
movabs $0xfff0000000000000,%rdx
sar    %cl,%rdx
cmp    $0x34,%rcx
mov    $0xffffffffffffffff,%rdi
cmovb  %rdx,%rdi
and    %rax,%rdi
pop    %rax
jmp    1040 <bits_to_double@plt>

LLVM IR (-O0):
define double @_truncate(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i64
%5 = alloca i64
store double %0, double* %3
%6 = load double, double* %3
%7 = fcmp olt double %6, 1.000000e+00
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load double, double* %3
%10 = fcmp ogt double %9, -1.000000e+00
br i1 %10, label %11, label %12
11: ; preds = %8
store double 0.000000e+00, double* %2
br label %31
12: ; preds = %8, %1
%13 = load double, double* %3
%14 = call i64 @double_to_bits(double %13)
store i64 %14, i64* %4
%15 = load i64, i64* %4
%16 = lshr i64 %15, 52
%17 = and i64 %16, 2047
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = sub i64 %18, 1023
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = icmp ult i64 %20, 52
br i1 %21, label %22, label %28
22: ; preds = %12
%23 = load i64, i64* %5
%24 = lshr i64 4503599627370495, %23
%25 = xor i64 %24, -1
%26 = load i64, i64* %4
%27 = and i64 %26, %25
store i64 %27, i64* %4
br label %28
28: ; preds = %22, %12
%29 = load i64, i64* %4
%30 = call double @bits_to_double(i64 %29)
store double %30, double* %2
br label %31
31: ; preds = %28, %11
%32 = load double, double* %2
ret double %32
}

LLVM IR (-O2):
define double @_truncate(double %0) {
%2 = fcmp olt double %0, 1.000000e+00
%3 = fcmp ogt double %0, -1.000000e+00
%4 = and i1 %2, %3
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = tail call i64 @double_to_bits(double %0)
%7 = lshr i64 %6, 52
%8 = and i64 %7, 2047
%9 = add nsw i64 %8, -1023
%10 = icmp ult i64 %9, 52
%11 = ashr i64 -4503599627370496, %9
%12 = select i1 %10, i64 %11, i64 -1
%13 = and i64 %12, %6
%14 = tail call double @bits_to_double(i64 %13)
br label %15
15: ; preds = %1, %5
%16 = phi double [ %14, %5 ], [ 0.000000e+00, %1 ]
ret double %16
}