#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*/ error2(char *);
int fabs(double);
double stub1(double);
double stub2(double);
double getRoot(double (*f)(double), double (*df)(double), double initVal) {
  double x, newx = initVal;
  int nIter = 0;
  do {
    x = newx;
    newx = x - (*f)(x) / (*df)(x);
    nIter++;
  } while ((fabs(x - newx) > 1e-10) && nIter < 100);
  if (fabs(x - newx) > 1e-10) {
    error2("root finder didn't converge");
  }
  return (newx);
}


x86 assembly (clang, -O0):
<getRoot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x20(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x20(%rbp),%xmm0
call   *%rax
movsd  -0x40(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x38(%rbp),%xmm0
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
call   1040 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xe4b(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x41(%rbp)
jbe    11cc <getRoot+0xac>
cmpl   $0x64,-0x2c(%rbp)
setl   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1146 <getRoot+0x26>
movsd  -0x20(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
call   1040 <fabs@plt>
cvtsi2sd %eax,%xmm0
movsd  0xe0e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1208 <getRoot+0xe8>
lea    0xe05(%rip),%rdi
call   1030 <error2@plt>
movsd  -0x28(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRoot>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movsd  %xmm0,0x8(%rsp)
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
call   *%r15
movsd  %xmm0,0x18(%rsp)
movsd  0x10(%rsp),%xmm0
call   *%r14
movsd  0x18(%rsp),%xmm2
divsd  %xmm0,%xmm2
movsd  0x8(%rsp),%xmm1
subsd  %xmm2,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x10(%rsp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,0x10(%rsp)
call   1040 <fabs@plt>
test   %eax,%eax
jle    119b <getRoot+0x7b>
lea    0x1(%rbx),%eax
cmp    $0x63,%ebx
mov    %eax,%ebx
jb     1140 <getRoot+0x20>
movsd  0x10(%rsp),%xmm0
call   1040 <fabs@plt>
test   %eax,%eax
jle    11b6 <getRoot+0x96>
lea    0xe4f(%rip),%rdi
call   1030 <error2@plt>
movsd  0x8(%rsp),%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [28 x i8] c"root finder didn't converge\00"
define double @getRoot(double (double)* %0, double (double)* %1, double %2) {
%4 = alloca double (double)*
%5 = alloca double (double)*
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store double (double)* %0, double (double)** %4
store double (double)* %1, double (double)** %5
store double %2, double* %6
%10 = load double, double* %6
store double %10, double* %8
store i32 0, i32* %9
br label %11
11: ; preds = %34, %3
%12 = load double, double* %8
store double %12, double* %7
%13 = load double, double* %7
%14 = load double (double)*, double (double)** %4
%15 = load double, double* %7
%16 = call double %14(double %15)
%17 = load double (double)*, double (double)** %5
%18 = load double, double* %7
%19 = call double %17(double %18)
%20 = fdiv double %16, %19
%21 = fsub double %13, %20
store double %21, double* %8
%22 = load i32, i32* %9
%23 = add nsw i32 %22, 1
store i32 %23, i32* %9
br label %24
24: ; preds = %11
%25 = load double, double* %7
%26 = load double, double* %8
%27 = fsub double %25, %26
%28 = call i32 @fabs(double %27)
%29 = sitofp i32 %28 to double
%30 = fcmp ogt double %29, 1.000000e-10
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* %9
%33 = icmp slt i32 %32, 100
br label %34
34: ; preds = %31, %24
%35 = phi i1 [ false, %24 ], [ %33, %31 ]
br i1 %35, label %11, label %36
36: ; preds = %34
%37 = load double, double* %7
%38 = load double, double* %8
%39 = fsub double %37, %38
%40 = call i32 @fabs(double %39)
%41 = sitofp i32 %40 to double
%42 = fcmp ogt double %41, 1.000000e-10
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 @error2(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
%46 = load double, double* %8
ret double %46
}

LLVM IR (-O2):
@.str = [28 x i8] c"root finder didn't converge\00"
define double @getRoot(double (double)* nocapture %0, double (double)* nocapture %1, double %2) {
br label %4
4: ; preds = %4, %3
%5 = phi double [ %2, %3 ], [ %10, %4 ]
%6 = phi i32 [ 0, %3 ], [ %11, %4 ]
%7 = tail call double %0(double %5)
%8 = tail call double %1(double %5)
%9 = fdiv double %7, %8
%10 = fsub double %5, %9
%11 = add nuw nsw i32 %6, 1
%12 = fsub double %5, %10
%13 = tail call i32 @fabs(double %12)
%14 = icmp sgt i32 %13, 0
%15 = icmp ult i32 %6, 99
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %4, label %17
17: ; preds = %4
%18 = tail call i32 @fabs(double %12)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @error2(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
ret double %10
}