#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 fabs(double);
double linear_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 + 2 * scale) {
    return 0.0;
  } else {
    return 1.0 - (d_norm - offset) / (2 * scale);
  }
}


x86 assembly (clang, -O0):
<linear_sim>:
push   %rbp
mov    %rsp,%rbp
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 0xed7(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1158 <linear_sim+0x58>
movsd  0xec2(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11b2 <linear_sim+0xb2>
movsd  -0x30(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm2
movsd  -0x18(%rbp),%xmm1
addsd  %xmm1,%xmm1
addsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jb     1186 <linear_sim+0x86>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11b2 <linear_sim+0xb2>
movsd  -0x30(%rbp),%xmm1
subsd  -0x20(%rbp),%xmm1
movsd  0xe80(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
divsd  %xmm0,%xmm1
movsd  0xe67(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<linear_sim>:
subsd  %xmm3,%xmm0
andpd  0xef4(%rip),%xmm0
ucomisd %xmm0,%xmm2
jae    1141 <linear_sim+0x41>
addsd  %xmm1,%xmm1
movapd %xmm1,%xmm4
addsd  %xmm2,%xmm4
xorpd  %xmm3,%xmm3
ucomisd %xmm4,%xmm0
jae    113c <linear_sim+0x3c>
subsd  %xmm2,%xmm0
divsd  %xmm1,%xmm0
movsd  0xed8(%rip),%xmm3
subsd  %xmm0,%xmm3
movapd %xmm3,%xmm0
ret
movsd  0xec7(%rip),%xmm0
ret

LLVM IR (-O0):
define double @linear_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 %34
19: ; preds = %4
%20 = load double, double* %10
%21 = load double, double* %8
%22 = load double, double* %7
%23 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %22, double %21)
%24 = fcmp oge double %20, %23
br i1 %24, label %25, label %26
25: ; preds = %19
store double 0.000000e+00, double* %5
br label %34
26: ; preds = %19
%27 = load double, double* %10
%28 = load double, double* %8
%29 = fsub double %27, %28
%30 = load double, double* %7
%31 = fmul double 2.000000e+00, %30
%32 = fdiv double %29, %31
%33 = fsub double 1.000000e+00, %32
store double %33, double* %5
br label %34
34: ; preds = %26, %25, %18
%35 = load double, double* %5
ret double %35
}

LLVM IR (-O2):
define double @linear_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 %16
8: ; preds = %4
%9 = tail call double @llvm.fmuladd.f64(double %1, double 2.000000e+00, double %2)
%10 = fcmp ult double %6, %9
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = fsub double %6, %2
%13 = fmul double %1, 2.000000e+00
%14 = fdiv double %12, %13
%15 = fsub double 1.000000e+00, %14
br label %16
16: ; preds = %8, %4, %11
%17 = phi double [ %15, %11 ], [ 1.000000e+00, %4 ], [ 0.000000e+00, %8 ]
ret double %17
}