#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 expm1(int);
double tanh(double x) {
  int sign = 0;
  if (x < 0) {
    sign = 1;
    x = -x;
  }
  x = expm1(-2 * x);
  x = x / (x + 2);
  return sign ? x : -x;
}


x86 assembly (clang, -O0):
<tanh>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    115a <tanh+0x4a>
movl   $0x1,-0xc(%rbp)
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  0xea6(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <expm1@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xe7e(%rip),%xmm1
addsd  -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11a9 <tanh+0x99>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11ca <tanh+0xba>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tanh>:
sub    $0x18,%rsp
movapd %xmm0,(%rsp)
movapd %xmm0,%xmm2
xorpd  0xedb(%rip),%xmm2
xorpd  %xmm3,%xmm3
movapd %xmm0,%xmm1
cmpltsd %xmm3,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
mulsd  0xeca(%rip),%xmm1
cvttsd2si %xmm1,%edi
call   1030 <expm1@plt>
xorpd  %xmm1,%xmm1
ucomisd (%rsp),%xmm1
movsd  0xeb8(%rip),%xmm1
addsd  %xmm0,%xmm1
divsd  %xmm1,%xmm0
ja     1172 <tanh+0x62>
xorpd  0xe8e(%rip),%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @tanh(double %0) {
%2 = alloca double
%3 = alloca i32
store double %0, double* %2
store i32 0, i32* %3
%4 = load double, double* %2
%5 = fcmp olt double %4, 0.000000e+00
br i1 %5, label %6, label %9
6: ; preds = %1
store i32 1, i32* %3
%7 = load double, double* %2
%8 = fneg double %7
store double %8, double* %2
br label %9
9: ; preds = %6, %1
%10 = load double, double* %2
%11 = fmul double -2.000000e+00, %10
%12 = fptosi double %11 to i32
%13 = call double @expm1(i32 %12)
store double %13, double* %2
%14 = load double, double* %2
%15 = load double, double* %2
%16 = fadd double %15, 2.000000e+00
%17 = fdiv double %14, %16
store double %17, double* %2
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %9
%21 = load double, double* %2
br label %25
22: ; preds = %9
%23 = load double, double* %2
%24 = fneg double %23
br label %25
25: ; preds = %22, %20
%26 = phi double [ %21, %20 ], [ %24, %22 ]
ret double %26
}

LLVM IR (-O2):
define double @tanh(double %0) {
%2 = fcmp olt double %0, 0.000000e+00
%3 = fneg double %0
%4 = select i1 %2, double %3, double %0
%5 = fmul double %4, -2.000000e+00
%6 = fptosi double %5 to i32
%7 = tail call double @expm1(i32 %6)
%8 = fadd double %7, 2.000000e+00
%9 = fdiv double %7, %8
%10 = fneg double %9
%11 = select i1 %2, double %9, double %10
ret double %11
}