#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 INT_MAX;
unsigned long int cast(double value) {
  unsigned long int result;
  if (value >= INT_MAX)
    return INT_MAX;
  result = value;
  return (result <= value) ? result : result - 1;
}


x86 assembly (clang, -O0):
<cast>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
mov    0x2ed3(%rip),%rax
ucomisd (%rax),%xmm0
jb     1159 <cast+0x59>
mov    0x2ec2(%rip),%rax
movsd  (%rax),%xmm0
movsd  0xece(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11df <cast+0xdf>
movsd  -0x10(%rbp),%xmm0
movsd  0xe9a(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rcx
cvttsd2si %xmm0,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x18(%rbp)
movaps 0xe81(%rip),%xmm0
movq   -0x18(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xe80(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     11cb <cast+0xcb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11d7 <cast+0xd7>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<cast>:
mov    0x2ee1(%rip),%rax
movsd  (%rax),%xmm1
ucomisd %xmm1,%xmm0
jae    115e <cast+0x5e>
cvttsd2si %xmm0,%rcx
mov    %rcx,%rdx
movapd %xmm0,%xmm1
subsd  0xedb(%rip),%xmm1
sar    $0x3f,%rdx
cvttsd2si %xmm1,%rax
and    %rdx,%rax
or     %rcx,%rax
movq   %rax,%xmm1
punpckldq 0xecf(%rip),%xmm1
subpd  0xed7(%rip),%xmm1
movapd %xmm1,%xmm2
unpckhpd %xmm1,%xmm2
addsd  %xmm1,%xmm2
ucomisd %xmm2,%xmm0
sbb    $0x0,%rax
ret
cvttsd2si %xmm1,%rcx
mov    %rcx,%rdx
sar    $0x3f,%rdx
subsd  0xe8e(%rip),%xmm1
cvttsd2si %xmm1,%rax
and    %rdx,%rax
or     %rcx,%rax
ret

LLVM IR (-O0):
@INT_MAX = global double 0.000000e+00
define i64 @cast(double %0) {
%2 = alloca i64
%3 = alloca double
%4 = alloca i64
store double %0, double* %3
%5 = load double, double* %3
%6 = load double, double* @INT_MAX
%7 = fcmp oge double %5, %6
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load double, double* @INT_MAX
%10 = fptoui double %9 to i64
store i64 %10, i64* %2
br label %25
11: ; preds = %1
%12 = load double, double* %3
%13 = fptoui double %12 to i64
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = uitofp i64 %14 to double
%16 = load double, double* %3
%17 = fcmp ole double %15, %16
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = load i64, i64* %4
br label %23
20: ; preds = %11
%21 = load i64, i64* %4
%22 = sub i64 %21, 1
br label %23
23: ; preds = %20, %18
%24 = phi i64 [ %19, %18 ], [ %22, %20 ]
store i64 %24, i64* %2
br label %25
25: ; preds = %23, %8
%26 = load i64, i64* %2
ret i64 %26
}

LLVM IR (-O2):
@INT_MAX = global double 0.000000e+00
define i64 @cast(double %0) {
%2 = load double, double* @INT_MAX
%3 = fcmp ugt double %2, %0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = fptoui double %2 to i64
br label %12
6: ; preds = %1
%7 = fptoui double %0 to i64
%8 = uitofp i64 %7 to double
%9 = fcmp ugt double %8, %0
%10 = sext i1 %9 to i64
%11 = add i64 %10, %7
br label %12
12: ; preds = %6, %4
%13 = phi i64 [ %5, %4 ], [ %11, %6 ]
ret i64 %13
}