#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 derivative(double (*f)(double), double x0, int order) {
  const double delta = 1.0e-6;
  if (order == 1) {
    double x1 = x0 - delta;
    double x2 = x0 + delta;
    double y1 = f(x1);
    double y2 = f(x2);
    return (y2 - y1) / (x2 - x1);
  } else if (order == 2) {
    double x1 = x0 - delta;
    double x2 = x0 + delta;
    double y1 = derivative(f, x1, 1);
    double y2 = derivative(f, x2, 1);
    return (y2 - y1) / (x2 - x1);
  } else {
    printf("order too high error \n");
    return 0;
  }
}


x86 assembly (clang, -O0):
<derivative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
movsd  0xec4(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    11b5 <derivative+0x95>
movsd  -0x18(%rbp),%xmm0
movsd  0xea8(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe97(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x30(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm1
subsd  -0x30(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    124d <derivative+0x12d>
cmpl   $0x2,-0x1c(%rbp)
jne    1239 <derivative+0x119>
movsd  -0x18(%rbp),%xmm0
movsd  0xe34(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  0xe23(%rip),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
movsd  -0x50(%rbp),%xmm0
mov    $0x1,%esi
call   1040 <derivative@plt>
movsd  %xmm0,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
movsd  -0x58(%rbp),%xmm0
mov    $0x1,%esi
call   1040 <derivative@plt>
movsd  %xmm0,-0x68(%rbp)
movsd  -0x68(%rbp),%xmm0
subsd  -0x60(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm1
subsd  -0x50(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    124d <derivative+0x12d>
lea    0xdc8(%rip),%rdi
call   1030 <printf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<derivative>:
push   %rbx
sub    $0x60,%rsp
mov    %rdi,%rbx
cmp    $0x2,%esi
je     1172 <derivative+0x62>
cmp    $0x1,%esi
jne    1221 <derivative+0x111>
movsd  0xf02(%rip),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x20(%rsp)
addsd  0xef8(%rip),%xmm0
movapd %xmm0,(%rsp)
movapd %xmm1,%xmm0
call   *%rbx
movsd  %xmm0,0x10(%rsp)
movapd (%rsp),%xmm0
call   *%rbx
subsd  0x10(%rsp),%xmm0
movapd (%rsp),%xmm1
subsd  0x20(%rsp),%xmm1
divsd  %xmm1,%xmm0
jmp    1231 <derivative+0x121>
unpcklpd %xmm0,%xmm0
addpd  0xe82(%rip),%xmm0
movapd %xmm0,(%rsp)
movapd 0xe85(%rip),%xmm1
addpd  %xmm0,%xmm1
movapd %xmm1,0x20(%rsp)
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
call   *%rbx
movapd %xmm0,0x50(%rsp)
movapd 0xe73(%rip),%xmm0
addpd  (%rsp),%xmm0
movapd %xmm0,0x10(%rsp)
unpckhpd %xmm0,%xmm0
call   *%rbx
movapd %xmm0,0x40(%rsp)
movaps 0x20(%rsp),%xmm0
call   *%rbx
movaps %xmm0,0x30(%rsp)
movapd 0x10(%rsp),%xmm0
call   *%rbx
unpcklpd 0x40(%rsp),%xmm0
movapd 0x30(%rsp),%xmm1
unpcklpd 0x50(%rsp),%xmm1
subpd  %xmm1,%xmm0
movapd 0x10(%rsp),%xmm1
subpd  0x20(%rsp),%xmm1
divpd  %xmm1,%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
subsd  %xmm1,%xmm0
movapd (%rsp),%xmm2
movapd %xmm2,%xmm1
unpckhpd %xmm2,%xmm1
subsd  %xmm1,%xmm2
divsd  %xmm2,%xmm0
jmp    1231 <derivative+0x121>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
xorpd  %xmm0,%xmm0
add    $0x60,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"order too high error \0A\00"
define double @derivative(double (double)* %0, double %1, i32 %2) {
%4 = alloca double
%5 = alloca double (double)*
%6 = alloca double
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
store double (double)* %0, double (double)** %5
store double %1, double* %6
store i32 %2, i32* %7
store double 0x3EB0C6F7A0B5ED8D, double* %8
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %37
19: ; preds = %3
%20 = load double, double* %6
%21 = fsub double %20, 0x3EB0C6F7A0B5ED8D
store double %21, double* %9
%22 = load double, double* %6
%23 = fadd double %22, 0x3EB0C6F7A0B5ED8D
store double %23, double* %10
%24 = load double (double)*, double (double)** %5
%25 = load double, double* %9
%26 = call double %24(double %25)
store double %26, double* %11
%27 = load double (double)*, double (double)** %5
%28 = load double, double* %10
%29 = call double %27(double %28)
store double %29, double* %12
%30 = load double, double* %12
%31 = load double, double* %11
%32 = fsub double %30, %31
%33 = load double, double* %10
%34 = load double, double* %9
%35 = fsub double %33, %34
%36 = fdiv double %32, %35
store double %36, double* %4
br label %60
37: ; preds = %3
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, 2
br i1 %39, label %40, label %58
40: ; preds = %37
%41 = load double, double* %6
%42 = fsub double %41, 0x3EB0C6F7A0B5ED8D
store double %42, double* %13
%43 = load double, double* %6
%44 = fadd double %43, 0x3EB0C6F7A0B5ED8D
store double %44, double* %14
%45 = load double (double)*, double (double)** %5
%46 = load double, double* %13
%47 = call double @derivative(double (double)* %45, double %46, i32 1)
store double %47, double* %15
%48 = load double (double)*, double (double)** %5
%49 = load double, double* %14
%50 = call double @derivative(double (double)* %48, double %49, i32 1)
store double %50, double* %16
%51 = load double, double* %16
%52 = load double, double* %15
%53 = fsub double %51, %52
%54 = load double, double* %14
%55 = load double, double* %13
%56 = fsub double %54, %55
%57 = fdiv double %53, %56
store double %57, double* %4
br label %60
58: ; preds = %37
%59 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store double 0.000000e+00, double* %4
br label %60
60: ; preds = %58, %40, %19
%61 = load double, double* %4
ret double %61
}

LLVM IR (-O2):
@str = [22 x i8] c"order too high error \00"
define double @derivative(double (double)* nocapture %0, double %1, i32 %2) {
switch i32 %2, label %40 [
i32 1, label %4
i32 2, label %12
]
4: ; preds = %3
%5 = fadd double %1, 0xBEB0C6F7A0B5ED8D
%6 = fadd double %1, 0x3EB0C6F7A0B5ED8D
%7 = tail call double %0(double %5)
%8 = tail call double %0(double %6)
%9 = fsub double %8, %7
%10 = fsub double %6, %5
%11 = fdiv double %9, %10
br label %42
12: ; preds = %3
%13 = insertelement <2 x double> poison, double %1, i64 0
%14 = shufflevector <2 x double> %13, <2 x double> poison, <2 x i32> zeroinitializer
%15 = fadd <2 x double> %14, <double 0x3EB0C6F7A0B5ED8D, double 0xBEB0C6F7A0B5ED8D>
%16 = fadd <2 x double> %15, <double 0xBEB0C6F7A0B5ED8D, double 0xBEB0C6F7A0B5ED8D>
%17 = extractelement <2 x double> %16, i64 1
%18 = tail call double %0(double %17)
%19 = fadd <2 x double> %15, <double 0x3EB0C6F7A0B5ED8D, double 0x3EB0C6F7A0B5ED8D>
%20 = extractelement <2 x double> %19, i64 1
%21 = tail call double %0(double %20)
%22 = extractelement <2 x double> %16, i64 0
%23 = tail call double %0(double %22)
%24 = extractelement <2 x double> %19, i64 0
%25 = tail call double %0(double %24)
%26 = insertelement <2 x double> poison, double %25, i64 0
%27 = insertelement <2 x double> %26, double %21, i64 1
%28 = insertelement <2 x double> poison, double %23, i64 0
%29 = insertelement <2 x double> %28, double %18, i64 1
%30 = fsub <2 x double> %27, %29
%31 = fsub <2 x double> %19, %16
%32 = fdiv <2 x double> %30, %31
%33 = shufflevector <2 x double> %32, <2 x double> poison, <2 x i32> <i32 1, i32 undef>
%34 = fsub <2 x double> %32, %33
%35 = extractelement <2 x double> %34, i64 0
%36 = shufflevector <2 x double> %15, <2 x double> poison, <2 x i32> <i32 1, i32 undef>
%37 = fsub <2 x double> %15, %36
%38 = extractelement <2 x double> %37, i64 0
%39 = fdiv double %35, %38
br label %42
40: ; preds = %3
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %42
42: ; preds = %40, %12, %4
%43 = phi double [ %11, %4 ], [ %39, %12 ], [ 0.000000e+00, %40 ]
ret double %43
}