#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 /*<<< orphan*/ printf(char *, double, unsigned int, unsigned int);
signed is_fp2fd_overflow(double fp, unsigned width, unsigned frac_bits) {
  unsigned long long fd_max;
  fd_max = ~0ull >> 1;
  fp = fp > 0 ? fp : -fp;
  if (16 == width || 32 == width || 64 == width) {
    fd_max >>= (64 - width);
    fd_max >>= frac_bits;
  } else
    return 0;
  if (fp > (double)fd_max) {
    printf(
        "[34mWARNING[0m:Fp 2 Fd Overflow Occurs, Raw %f, Width %u, Frac %u\n",
        fp, width, frac_bits);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_fp2fd_overflow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %esi,-0x18(%rbp)
movabs $0x7fffffffffffffff,%rax
ff ff 7f
mov    %rax,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1152 <is_fp2fd_overflow+0x42>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1173 <is_fp2fd_overflow+0x63>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    $0x10,%eax
cmp    -0x14(%rbp),%eax
je     11a7 <is_fp2fd_overflow+0x97>
mov    $0x20,%eax
cmp    -0x14(%rbp),%eax
je     11a7 <is_fp2fd_overflow+0x97>
mov    $0x40,%eax
cmp    -0x14(%rbp),%eax
jne    11d1 <is_fp2fd_overflow+0xc1>
mov    $0x40,%ecx
sub    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,%ecx
shr    %cl,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,%ecx
shr    %cl,%rax
mov    %rax,-0x20(%rbp)
jmp    11dd <is_fp2fd_overflow+0xcd>
movl   $0x0,-0x4(%rbp)
jmp    123d <is_fp2fd_overflow+0x12d>
movsd  -0x10(%rbp),%xmm0
movaps 0xe17(%rip),%xmm2
movq   -0x20(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xe16(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jbe    1236 <is_fp2fd_overflow+0x126>
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdfb(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123d <is_fp2fd_overflow+0x12d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_fp2fd_overflow>:
push   %rax
mov    %esi,%edx
mov    %edi,%esi
xor    %eax,%eax
lea    -0x10(%rsi),%ecx
cmp    $0x30,%ecx
ja     1189 <is_fp2fd_overflow+0x79>
movabs $0x1000000010001,%rdi
bt     %rcx,%rdi
jae    1189 <is_fp2fd_overflow+0x79>
movapd 0xec9(%rip),%xmm2
xorpd  %xmm0,%xmm2
xorpd  %xmm1,%xmm1
cmpltsd %xmm0,%xmm1
andpd  %xmm1,%xmm0
andnpd %xmm2,%xmm1
mov    %esi,%ecx
neg    %cl
movabs $0x7fffffffffffffff,%rdi
ff ff 7f
shr    %cl,%rdi
orpd   %xmm0,%xmm1
mov    %edx,%ecx
shr    %cl,%rdi
xorps  %xmm0,%xmm0
cvtsi2sd %rdi,%xmm0
ucomisd %xmm0,%xmm1
jbe    1189 <is_fp2fd_overflow+0x79>
lea    0xe95(%rip),%rdi
movapd %xmm1,%xmm0
call   1030 <printf@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [69 x i8] c"\1B[34mWARNING\1B[0m:Fp 2 Fd Overflow Occurs, Raw %f, Width %u, Frac %u\0A\00"
define i32 @is_fp2fd_overflow(double %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store double %0, double* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64 9223372036854775807, i64* %8
%9 = load double, double* %5
%10 = fcmp ogt double %9, 0.000000e+00
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load double, double* %5
br label %16
13: ; preds = %3
%14 = load double, double* %5
%15 = fneg double %14
br label %16
16: ; preds = %13, %11
%17 = phi double [ %12, %11 ], [ %15, %13 ]
store double %17, double* %5
%18 = load i32, i32* %6
%19 = icmp eq i32 16, %18
br i1 %19, label %26, label %20
20: ; preds = %16
%21 = load i32, i32* %6
%22 = icmp eq i32 32, %21
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* %6
%25 = icmp eq i32 64, %24
br i1 %25, label %26, label %36
26: ; preds = %23, %20, %16
%27 = load i32, i32* %6
%28 = sub i32 64, %27
%29 = load i64, i64* %8
%30 = zext i32 %28 to i64
%31 = lshr i64 %29, %30
store i64 %31, i64* %8
%32 = load i32, i32* %7
%33 = load i64, i64* %8
%34 = zext i32 %32 to i64
%35 = lshr i64 %33, %34
store i64 %35, i64* %8
br label %37
36: ; preds = %23
store i32 0, i32* %4
br label %48
37: ; preds = %26
%38 = load double, double* %5
%39 = load i64, i64* %8
%40 = uitofp i64 %39 to double
%41 = fcmp ogt double %38, %40
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load double, double* %5
%44 = load i32, i32* %6
%45 = load i32, i32* %7
%46 = call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), double %43, i32 %44, i32 %45)
store i32 1, i32* %4
br label %48
47: ; preds = %37
store i32 0, i32* %4
br label %48
48: ; preds = %47, %42, %36
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [69 x i8] c"\1B[34mWARNING\1B[0m:Fp 2 Fd Overflow Occurs, Raw %f, Width %u, Frac %u\0A\00"
define i32 @is_fp2fd_overflow(double %0, i32 %1, i32 %2) {
%4 = fcmp ogt double %0, 0.000000e+00
%5 = fneg double %0
%6 = select i1 %4, double %0, double %5
switch i32 %1, label %17 [
i32 64, label %7
i32 32, label %7
i32 16, label %7
]
7: ; preds = %3, %3, %3
%8 = sub nuw nsw i32 64, %1
%9 = zext i32 %8 to i64
%10 = lshr i64 9223372036854775807, %9
%11 = zext i32 %2 to i64
%12 = lshr i64 %10, %11
%13 = uitofp i64 %12 to double
%14 = fcmp ogt double %6, %13
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), double %6, i32 %1, i32 %2)
br label %17
17: ; preds = %7, %3, %15
%18 = phi i32 [ 1, %15 ], [ 0, %3 ], [ 0, %7 ]
ret i32 %18
}