#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 _scale_tolerance_up(double, double, double);
double fabs(double);
scalar_t__ isnan(double);
int _double_approx_eq(double d1, double d2, double tolerance) {
  double diff, scaled_tolerance;
  if ((isnan(d1) && isnan(d2)) || (d1 == d2)) {
    return 1;
  }
  scaled_tolerance = _scale_tolerance_up(d1, d2, tolerance);
  diff = fabs(d1 - d2);
  return (diff <= scaled_tolerance) ? 1 : 0;
}


x86 assembly (clang, -O0):
<_double_approx_eq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
je     115f <_double_approx_eq+0x3f>
movsd  -0x18(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
jne    1175 <_double_approx_eq+0x55>
movsd  -0x10(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jne    1181 <_double_approx_eq+0x61>
jp     1181 <_double_approx_eq+0x61>
movl   $0x1,-0x4(%rbp)
jmp    11cf <_double_approx_eq+0xaf>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
call   1030 <_scale_tolerance_up@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movaps 0xe55(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
xor    %eax,%eax
mov    $0x1,%ecx
ucomisd %xmm1,%xmm0
cmovae %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_double_approx_eq>:
sub    $0x28,%rsp
movsd  %xmm2,0x20(%rsp)
movsd  %xmm1,0x8(%rsp)
movaps %xmm0,0x10(%rsp)
call   1040 <isnan@plt>
test   %rax,%rax
je     1198 <_double_approx_eq+0x78>
movsd  0x8(%rsp),%xmm0
call   1040 <isnan@plt>
mov    %rax,%rcx
movapd 0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
mov    $0x1,%eax
jne    1166 <_double_approx_eq+0x46>
jnp    1193 <_double_approx_eq+0x73>
test   %rcx,%rcx
jne    1193 <_double_approx_eq+0x73>
movsd  0x20(%rsp),%xmm2
call   1030 <_scale_tolerance_up@plt>
movapd 0x10(%rsp),%xmm1
subsd  0x8(%rsp),%xmm1
andpd  0xe76(%rip),%xmm1
xor    %eax,%eax
ucomisd %xmm1,%xmm0
setae  %al
add    $0x28,%rsp
ret
mov    $0x1,%eax
movsd  0x8(%rsp),%xmm1
movapd 0x10(%rsp),%xmm0
ucomisd %xmm1,%xmm0
jne    116b <_double_approx_eq+0x4b>
jnp    1193 <_double_approx_eq+0x73>
jmp    116b <_double_approx_eq+0x4b>

LLVM IR (-O0):
define i32 @_double_approx_eq(double %0, double %1, double %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
%10 = load double, double* %5
%11 = call i64 @isnan(double %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load double, double* %6
%15 = call i64 @isnan(double %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13, %3
%18 = load double, double* %5
%19 = load double, double* %6
%20 = fcmp oeq double %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17, %13
store i32 1, i32* %4
br label %36
22: ; preds = %17
%23 = load double, double* %5
%24 = load double, double* %6
%25 = load double, double* %7
%26 = call double @_scale_tolerance_up(double %23, double %24, double %25)
store double %26, double* %9
%27 = load double, double* %5
%28 = load double, double* %6
%29 = fsub double %27, %28
%30 = call double @llvm.fabs.f64(double %29)
store double %30, double* %8
%31 = load double, double* %8
%32 = load double, double* %9
%33 = fcmp ole double %31, %32
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 1, i32 0
store i32 %35, i32* %4
br label %36
36: ; preds = %22, %21
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @_double_approx_eq(double %0, double %1, double %2) {
%4 = tail call i64 @isnan(double %0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i64 @isnan(double %1)
%8 = icmp ne i64 %7, 0
%9 = fcmp oeq double %0, %1
%10 = or i1 %9, %8
br i1 %10, label %19, label %13
11: ; preds = %3
%12 = fcmp oeq double %0, %1
br i1 %12, label %19, label %13
13: ; preds = %6, %11
%14 = tail call double @_scale_tolerance_up(double %0, double %1, double %2)
%15 = fsub double %0, %1
%16 = tail call double @llvm.fabs.f64(double %15)
%17 = fcmp ole double %16, %14
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %6, %11, %13
%20 = phi i32 [ %18, %13 ], [ 1, %11 ], [ 1, %6 ]
ret i32 %20
}