#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 derivative(double, int, double (*)(double, void *), void *);
double fabs(double);
int /*<<< orphan*/ printf(char *, double, double);
double stub1(double, void *);
double newton_raphson(double x0, double tolerance,
                      double (*my_func)(double, void *), void *params) {
  double difference;
  double x1;
  double de;
  do {
    de = derivative(x0, 1e-4, &(*my_func), params);
    x1 = x0 - (*(my_func))(x0, params) / de;
    printf("NR: d= %e, x1 = %e\n", de, x1);
    difference = fabs(x1 - x0);
    x0 = x1;
  } while (difference > tolerance);
  return x1;
}


x86 assembly (clang, -O0):
<newton_raphson>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %edi,%edi
call   1040 <derivative@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
mov    -0x20(%rbp),%rdi
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
divsd  -0x38(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
lea    0xe7d(%rip),%rdi
call   1030 <printf@plt>
movsd  -0x30(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movaps 0xe57(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x28(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
ja     113a <newton_raphson+0x1a>
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newton_raphson>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movsd  %xmm1,0x10(%rsp)
lea    0xed4(%rip),%r15
nopl   0x0(%rax)
movsd  %xmm0,(%rsp)
movsd  %xmm0,0x8(%rsp)
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <derivative@plt>
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm0
mov    %r14,%rdi
call   *%rbx
movsd  0x18(%rsp),%xmm2
divsd  %xmm2,%xmm0
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
mov    %r15,%rdi
movapd %xmm2,%xmm0
movsd  (%rsp),%xmm1
call   1030 <printf@plt>
movsd  (%rsp),%xmm0
movapd %xmm0,%xmm1
subsd  0x8(%rsp),%xmm1
andpd  0xe57(%rip),%xmm1
ucomisd 0x10(%rsp),%xmm1
ja     1140 <newton_raphson+0x20>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [20 x i8] c"NR: d= %e, x1 = %e\0A\00"
define double @newton_raphson(double %0, double %1, double (double, i8*)* %2, i8* %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double (double, i8*)*
%8 = alloca i8*
%9 = alloca double
%10 = alloca double
%11 = alloca double
store double %0, double* %5
store double %1, double* %6
store double (double, i8*)* %2, double (double, i8*)** %7
store i8* %3, i8** %8
br label %12
12: ; preds = %33, %4
%13 = load double, double* %5
%14 = load double (double, i8*)*, double (double, i8*)** %7
%15 = load i8*, i8** %8
%16 = call double @derivative(double %13, i32 0, double (double, i8*)* %14, i8* %15)
store double %16, double* %11
%17 = load double, double* %5
%18 = load double (double, i8*)*, double (double, i8*)** %7
%19 = load double, double* %5
%20 = load i8*, i8** %8
%21 = call double %18(double %19, i8* %20)
%22 = load double, double* %11
%23 = fdiv double %21, %22
%24 = fsub double %17, %23
store double %24, double* %10
%25 = load double, double* %11
%26 = load double, double* %10
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), double %25, double %26)
%28 = load double, double* %10
%29 = load double, double* %5
%30 = fsub double %28, %29
%31 = call double @llvm.fabs.f64(double %30)
store double %31, double* %9
%32 = load double, double* %10
store double %32, double* %5
br label %33
33: ; preds = %12
%34 = load double, double* %9
%35 = load double, double* %6
%36 = fcmp ogt double %34, %35
br i1 %36, label %12, label %37
37: ; preds = %33
%38 = load double, double* %10
ret double %38
}

LLVM IR (-O2):
@.str = [20 x i8] c"NR: d= %e, x1 = %e\0A\00"
define double @newton_raphson(double %0, double %1, double (double, i8*)* %2, i8* %3) {
br label %5
5: ; preds = %5, %4
%6 = phi double [ %0, %4 ], [ %10, %5 ]
%7 = tail call double @derivative(double %6, i32 0, double (double, i8*)* %2, i8* %3)
%8 = tail call double %2(double %6, i8* %3)
%9 = fdiv double %8, %7
%10 = fsub double %6, %9
%11 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), double %7, double %10)
%12 = fsub double %10, %6
%13 = tail call double @llvm.fabs.f64(double %12)
%14 = fcmp ogt double %13, %1
br i1 %14, label %5, label %15
15: ; preds = %5
ret double %10
}