#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 */
int float2int(double);
void double_to_i32(double *vv, int *i32) {
  double v = *vv;
  if (v < 0.0) {
    if (v < -1.0)
      *i32 = 0x80000000;
    else
      *i32 = float2int(v * 2147483648.0 - 0.5);
  } else {
    if (v >= (2147483646.5 / 2147483648.0))
      *i32 = 0x7FFFFFFF;
    else
      *i32 = float2int(v * 2147483648.0 + 0.5);
  }
}


x86 assembly (clang, -O0):
<double_to_i32>:
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    118c <double_to_i32+0x7c>
movsd  0xed5(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    115d <double_to_i32+0x4d>
mov    -0x10(%rbp),%rax
movl   $0x80000000,(%rax)
jmp    1187 <double_to_i32+0x77>
movsd  -0x18(%rbp),%xmm0
movsd  0xe9e(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xeaa(%rip),%xmm1
addsd  %xmm1,%xmm0
call   1030 <float2int@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11e1 <double_to_i32+0xd1>
movsd  -0x18(%rbp),%xmm0
movsd  0xe67(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11b2 <double_to_i32+0xa2>
mov    -0x10(%rbp),%rax
movl   $0x7fffffff,(%rax)
jmp    11dc <double_to_i32+0xcc>
movsd  -0x18(%rbp),%xmm0
movsd  0xe49(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe45(%rip),%xmm1
addsd  %xmm1,%xmm0
call   1030 <float2int@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    11e1 <double_to_i32+0xd1>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_to_i32>:
push   %rbx
mov    %rsi,%rbx
movsd  (%rdi),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1147 <double_to_i32+0x37>
mov    $0x80000000,%eax
movsd  0xee9(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     116b <double_to_i32+0x5b>
mulsd  0xecb(%rip),%xmm0
addsd  0xedb(%rip),%xmm0
jmp    1166 <double_to_i32+0x56>
mov    $0x7fffffff,%eax
ucomisd 0xeac(%rip),%xmm0
jae    116b <double_to_i32+0x5b>
mulsd  0xeaa(%rip),%xmm0
addsd  0xeaa(%rip),%xmm0
call   1030 <float2int@plt>
mov    %eax,(%rbx)
pop    %rbx
ret

LLVM IR (-O0):
define void @double_to_i32(double* %0, i32* %1) {
%3 = alloca double*
%4 = alloca i32*
%5 = alloca double
store double* %0, double** %3
store i32* %1, i32** %4
%6 = load double*, double** %3
%7 = load double, double* %6
store double %7, double* %5
%8 = load double, double* %5
%9 = fcmp olt double %8, 0.000000e+00
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = load double, double* %5
%12 = fcmp olt double %11, -1.000000e+00
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32*, i32** %4
store i32 -2147483648, i32* %14
br label %20
15: ; preds = %10
%16 = load double, double* %5
%17 = call double @llvm.fmuladd.f64(double %16, double 0x41E0000000000000, double -5.000000e-01)
%18 = call i32 @float2int(double %17)
%19 = load i32*, i32** %4
store i32 %18, i32* %19
br label %20
20: ; preds = %15, %13
br label %32
21: ; preds = %2
%22 = load double, double* %5
%23 = fcmp oge double %22, 0x3FEFFFFFFFA00000
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = load i32*, i32** %4
store i32 2147483647, i32* %25
br label %31
26: ; preds = %21
%27 = load double, double* %5
%28 = call double @llvm.fmuladd.f64(double %27, double 0x41E0000000000000, double 5.000000e-01)
%29 = call i32 @float2int(double %28)
%30 = load i32*, i32** %4
store i32 %29, i32* %30
br label %31
31: ; preds = %26, %24
br label %32
32: ; preds = %31, %20
ret void
}

LLVM IR (-O2):
define void @double_to_i32(double* nocapture %0, i32* nocapture writeonly %1) {
%3 = load double, double* %0
%4 = fcmp olt double %3, 0.000000e+00
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = fcmp olt double %3, -1.000000e+00
br i1 %6, label %16, label %7
7: ; preds = %5
%8 = tail call double @llvm.fmuladd.f64(double %3, double 0x41E0000000000000, double -5.000000e-01)
br label %13
9: ; preds = %2
%10 = fcmp ult double %3, 0x3FEFFFFFFFA00000
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = tail call double @llvm.fmuladd.f64(double %3, double 0x41E0000000000000, double 5.000000e-01)
br label %13
13: ; preds = %7, %11
%14 = phi double [ %12, %11 ], [ %8, %7 ]
%15 = tail call i32 @float2int(double %14)
br label %16
16: ; preds = %13, %9, %5
%17 = phi i32 [ -2147483648, %5 ], [ 2147483647, %9 ], [ %15, %13 ]
store i32 %17, i32* %1
ret void
}