#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 exp(double);
double fabs(double);
double log(double);
double sqrt(double);
double squared_sim(double d, double scale, double offset, double origin) {
  double d_norm;
  d_norm = fabs(d - origin);
  if (d_norm <= offset) {
    return 1.0;
  } else if (d_norm >= offset + sqrt(2.0) * scale) {
    return 0.0;
  } else {
    return 1.0 - 0.5 * exp(2.0 * log((d_norm - offset) / scale));
  }
}


x86 assembly (clang, -O0):
<squared_sim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  %xmm3,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movaps 0xea3(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     118c <squared_sim+0x5c>
movsd  0xe9e(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1223 <squared_sim+0xf3>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xe68(%rip),%xmm0
call   1050 <sqrt@plt>
movsd  -0x40(%rbp),%xmm2
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm3
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jb     11de <squared_sim+0xae>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1223 <squared_sim+0xf3>
movsd  -0x30(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
call   1040 <log@plt>
movaps %xmm0,%xmm1
movsd  0xe13(%rip),%xmm0
mulsd  %xmm1,%xmm0
call   1030 <exp@plt>
movsd  0xe0a(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe06(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<squared_sim>:
push   %rax
subsd  %xmm3,%xmm0
andpd  0xed3(%rip),%xmm0
ucomisd %xmm0,%xmm2
jae    1175 <squared_sim+0x55>
movsd  0xedd(%rip),%xmm4
mulsd  %xmm1,%xmm4
addsd  %xmm2,%xmm4
xorpd  %xmm3,%xmm3
ucomisd %xmm4,%xmm0
jae    117d <squared_sim+0x5d>
subsd  %xmm2,%xmm0
divsd  %xmm1,%xmm0
call   1040 <log@plt>
addsd  %xmm0,%xmm0
call   1030 <exp@plt>
mulsd  0xeb5(%rip),%xmm0
addsd  0xe9d(%rip),%xmm0
pop    %rax
ret
movsd  0xe93(%rip),%xmm3
movapd %xmm3,%xmm0
pop    %rax
ret

LLVM IR (-O0):
define double @squared_sim(double %0, double %1, double %2, double %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double %0, double* %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
%11 = load double, double* %6
%12 = load double, double* %9
%13 = fsub double %11, %12
%14 = call double @llvm.fabs.f64(double %13)
store double %14, double* %10
%15 = load double, double* %10
%16 = load double, double* %8
%17 = fcmp ole double %15, %16
br i1 %17, label %18, label %19
18: ; preds = %4
store double 1.000000e+00, double* %5
br label %37
19: ; preds = %4
%20 = load double, double* %10
%21 = load double, double* %8
%22 = call double @sqrt(double 2.000000e+00)
%23 = load double, double* %7
%24 = call double @llvm.fmuladd.f64(double %22, double %23, double %21)
%25 = fcmp oge double %20, %24
br i1 %25, label %26, label %27
26: ; preds = %19
store double 0.000000e+00, double* %5
br label %37
27: ; preds = %19
%28 = load double, double* %10
%29 = load double, double* %8
%30 = fsub double %28, %29
%31 = load double, double* %7
%32 = fdiv double %30, %31
%33 = call double @log(double %32)
%34 = fmul double 2.000000e+00, %33
%35 = call double @exp(double %34)
%36 = call double @llvm.fmuladd.f64(double -5.000000e-01, double %35, double 1.000000e+00)
store double %36, double* %5
br label %37
37: ; preds = %27, %26, %18
%38 = load double, double* %5
ret double %38
}

LLVM IR (-O2):
define double @squared_sim(double %0, double %1, double %2, double %3) {
%5 = fsub double %0, %3
%6 = tail call double @llvm.fabs.f64(double %5)
%7 = fcmp ugt double %6, %2
br i1 %7, label %8, label %18
8: ; preds = %4
%9 = tail call double @llvm.fmuladd.f64(double %1, double 0x3FF6A09E667F3BCD, double %2)
%10 = fcmp ult double %6, %9
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = fsub double %6, %2
%13 = fdiv double %12, %1
%14 = tail call double @log(double %13)
%15 = fmul double %14, 2.000000e+00
%16 = tail call double @exp(double %15)
%17 = tail call double @llvm.fmuladd.f64(double %16, double -5.000000e-01, double 1.000000e+00)
br label %18
18: ; preds = %8, %4, %11
%19 = phi double [ %17, %11 ], [ 1.000000e+00, %4 ], [ 0.000000e+00, %8 ]
ret double %19
}