#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 ceil(double);
double floor(double);
double fmod(double, double);
int /*<<< orphan*/ isfinite(double);
int toint32(double d) {
  double two32 = 4294967296.0;
  double two31 = 2147483648.0;
  if (!isfinite(d) || d == 0)
    return 0;
  d = fmod(d, two32);
  d = d >= 0 ? floor(d) : ceil(d) + two32;
  if (d >= two31)
    return d - two32;
  else
    return d;
}


x86 assembly (clang, -O0):
<toint32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  0xeb3(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe9e(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <isfinite@plt>
cmp    $0x0,%eax
je     1192 <toint32+0x52>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    119e <toint32+0x5e>
jp     119e <toint32+0x5e>
movl   $0x0,-0x4(%rbp)
jmp    1224 <toint32+0xe4>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
call   1040 <fmod@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11d8 <toint32+0x98>
movsd  -0x10(%rbp),%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x28(%rbp)
jmp    11ec <toint32+0xac>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     121c <toint32+0xdc>
movsd  -0x10(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x4(%rbp)
jmp    1224 <toint32+0xe4>
cvttsd2si -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toint32>:
push   %rax
movsd  %xmm0,(%rsp)
call   1050 <isfinite@plt>
mov    %eax,%ecx
xor    %eax,%eax
xorpd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    1160 <toint32+0x20>
jnp    11b3 <toint32+0x73>
test   %ecx,%ecx
je     11b3 <toint32+0x73>
movapd %xmm1,%xmm0
movsd  0xe90(%rip),%xmm1
call   1040 <fmod@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jae    1198 <toint32+0x58>
call   1030 <ceil@plt>
addsd  0xe74(%rip),%xmm0
ucomisd 0xe74(%rip),%xmm0
jae    11a7 <toint32+0x67>
jmp    11af <toint32+0x6f>
call   1060 <floor@plt>
ucomisd 0xe63(%rip),%xmm0
jb     11af <toint32+0x6f>
addsd  0xe61(%rip),%xmm0
cvttsd2si %xmm0,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @toint32(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca double
%5 = alloca double
store double %0, double* %3
store double 0x41F0000000000000, double* %4
store double 0x41E0000000000000, double* %5
%6 = load double, double* %3
%7 = call i32 @isfinite(double %6)
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load double, double* %3
%11 = fcmp oeq double %10, 0.000000e+00
br i1 %11, label %12, label %13
12: ; preds = %9, %1
store i32 0, i32* %2
br label %40
13: ; preds = %9
%14 = load double, double* %3
%15 = load double, double* %4
%16 = call double @fmod(double %14, double %15)
store double %16, double* %3
%17 = load double, double* %3
%18 = fcmp oge double %17, 0.000000e+00
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load double, double* %3
%21 = call double @llvm.floor.f64(double %20)
br label %27
22: ; preds = %13
%23 = load double, double* %3
%24 = call double @llvm.ceil.f64(double %23)
%25 = load double, double* %4
%26 = fadd double %24, %25
br label %27
27: ; preds = %22, %19
%28 = phi double [ %21, %19 ], [ %26, %22 ]
store double %28, double* %3
%29 = load double, double* %3
%30 = load double, double* %5
%31 = fcmp oge double %29, %30
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load double, double* %3
%34 = load double, double* %4
%35 = fsub double %33, %34
%36 = fptosi double %35 to i32
store i32 %36, i32* %2
br label %40
37: ; preds = %27
%38 = load double, double* %3
%39 = fptosi double %38 to i32
store i32 %39, i32* %2
br label %40
40: ; preds = %37, %32, %12
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
define i32 @toint32(double %0) {
%2 = tail call i32 @isfinite(double %0)
%3 = icmp eq i32 %2, 0
%4 = fcmp oeq double %0, 0.000000e+00
%5 = or i1 %4, %3
br i1 %5, label %22, label %6
6: ; preds = %1
%7 = tail call double @fmod(double %0, double 0x41F0000000000000)
%8 = fcmp ult double %7, 0.000000e+00
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call double @llvm.floor.f64(double %7)
br label %14
11: ; preds = %6
%12 = tail call double @llvm.ceil.f64(double %7)
%13 = fadd double %12, 0x41F0000000000000
br label %14
14: ; preds = %11, %9
%15 = phi double [ %10, %9 ], [ %13, %11 ]
%16 = fcmp ult double %15, 0x41E0000000000000
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = fadd double %15, 0xC1F0000000000000
%19 = fptosi double %18 to i32
br label %22
20: ; preds = %14
%21 = fptosi double %15 to i32
br label %22
22: ; preds = %1, %20, %17
%23 = phi i32 [ %19, %17 ], [ %21, %20 ], [ 0, %1 ]
ret i32 %23
}