#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 f(double);
double fabs(double);
double fprime(double);
double fprime2(double);
int /*<<< orphan*/ printf(char *, int, double);
double getRoot_f(double p0, int maxiter) {
  int i = 1;
  double fp0 = f(p0);
  double f1p0 = fprime(p0);
  double f2p0 = fprime2(p0);
  double p1 = 0;
  while (i <= maxiter) {
    p1 = p0 - (fp0 * f1p0) / (f1p0 * f1p0 - fp0 * f2p0);
    printf("Iter %d: %.9lf\n", i, p1);
    if (fabs(p1 - p0) < 0.00001)
      return p1;
    i++;
    p0 = p1;
    fp0 = f(p1);
    f1p0 = fprime(p1);
    f2p0 = fprime2(p1);
  }
  return -1;
}


x86 assembly (clang, -O0):
<getRoot_f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <f@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <fprime@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1060 <fprime2@plt>
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1276 <getRoot_f+0x136>
movsd  -0x10(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
mulsd  -0x28(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x28(%rbp),%xmm4
movsd  -0x20(%rbp),%xmm3
mulsd  -0x30(%rbp),%xmm3
movq   %xmm3,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm3
mulsd  %xmm4,%xmm2
addsd  %xmm3,%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x18(%rbp),%esi
movsd  -0x38(%rbp),%xmm0
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
movsd  -0x38(%rbp),%xmm1
subsd  -0x10(%rbp),%xmm1
movaps 0xe04(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xdf0(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1231 <getRoot_f+0xf1>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1283 <getRoot_f+0x143>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1030 <f@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1050 <fprime@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1060 <fprime2@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    118c <getRoot_f+0x4c>
movsd  0xd82(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRoot_f>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
movsd  %xmm0,(%rsp)
call   1030 <f@plt>
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
call   1050 <fprime@plt>
movsd  %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
call   1060 <fprime2@plt>
movsd  0xe81(%rip),%xmm1
test   %r15d,%r15d
jle    1263 <getRoot_f+0x123>
neg    %r15d
mov    $0x1,%ebx
lea    0xe89(%rip),%r14
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rsp),%xmm3
movsd  %xmm3,0x8(%rsp)
mulsd  %xmm1,%xmm0
mulsd  %xmm2,%xmm1
mulsd  %xmm2,%xmm2
subsd  %xmm0,%xmm2
divsd  %xmm2,%xmm1
movapd %xmm3,%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
movsd  (%rsp),%xmm2
movapd %xmm2,%xmm0
subsd  0x8(%rsp),%xmm0
andpd  0xe13(%rip),%xmm0
movsd  0xe03(%rip),%xmm1
ucomisd %xmm0,%xmm1
movapd %xmm2,%xmm0
ja     125f <getRoot_f+0x11f>
lea    0x1(%rbx),%ebp
call   1030 <f@plt>
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
call   1050 <fprime@plt>
movsd  %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
call   1060 <fprime2@plt>
movsd  0x10(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
lea    (%r15,%rbx,1),%eax
add    $0x1,%eax
mov    %ebp,%ebx
cmp    $0x1,%eax
jne    11b0 <getRoot_f+0x70>
movsd  0xda3(%rip),%xmm1
jmp    1263 <getRoot_f+0x123>
movapd %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Iter %d: %.9lf\0A\00"
define double @getRoot_f(double %0, i32 %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
store i32 1, i32* %6
%11 = load double, double* %4
%12 = call double @f(double %11)
store double %12, double* %7
%13 = load double, double* %4
%14 = call double @fprime(double %13)
store double %14, double* %8
%15 = load double, double* %4
%16 = call double @fprime2(double %15)
store double %16, double* %9
store double 0.000000e+00, double* %10
br label %17
17: ; preds = %45, %2
%18 = load i32, i32* %6
%19 = load i32, i32* %5
%20 = icmp sle i32 %18, %19
br i1 %20, label %21, label %55
21: ; preds = %17
%22 = load double, double* %4
%23 = load double, double* %7
%24 = load double, double* %8
%25 = fmul double %23, %24
%26 = load double, double* %8
%27 = load double, double* %8
%28 = load double, double* %7
%29 = load double, double* %9
%30 = fmul double %28, %29
%31 = fneg double %30
%32 = call double @llvm.fmuladd.f64(double %26, double %27, double %31)
%33 = fdiv double %25, %32
%34 = fsub double %22, %33
store double %34, double* %10
%35 = load i32, i32* %6
%36 = load double, double* %10
%37 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %35, double %36)
%38 = load double, double* %10
%39 = load double, double* %4
%40 = fsub double %38, %39
%41 = call double @llvm.fabs.f64(double %40)
%42 = fcmp olt double %41, 1.000000e-05
br i1 %42, label %43, label %45
43: ; preds = %21
%44 = load double, double* %10
store double %44, double* %3
br label %56
45: ; preds = %21
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
%48 = load double, double* %10
store double %48, double* %4
%49 = load double, double* %10
%50 = call double @f(double %49)
store double %50, double* %7
%51 = load double, double* %10
%52 = call double @fprime(double %51)
store double %52, double* %8
%53 = load double, double* %10
%54 = call double @fprime2(double %53)
store double %54, double* %9
br label %17
55: ; preds = %17
store double -1.000000e+00, double* %3
br label %56
56: ; preds = %55, %43
%57 = load double, double* %3
ret double %57
}

LLVM IR (-O2):
@.str = [16 x i8] c"Iter %d: %.9lf\0A\00"
define double @getRoot_f(double %0, i32 %1) {
%3 = tail call double @f(double %0)
%4 = tail call double @fprime(double %0)
%5 = tail call double @fprime2(double %0)
%6 = icmp slt i32 %1, 1
br i1 %6, label %29, label %7
7: ; preds = %2, %23
%8 = phi double [ %27, %23 ], [ %5, %2 ]
%9 = phi double [ %26, %23 ], [ %4, %2 ]
%10 = phi double [ %25, %23 ], [ %3, %2 ]
%11 = phi i32 [ %24, %23 ], [ 1, %2 ]
%12 = phi double [ %18, %23 ], [ %0, %2 ]
%13 = fmul double %10, %9
%14 = fneg double %10
%15 = fmul double %8, %14
%16 = tail call double @llvm.fmuladd.f64(double %9, double %9, double %15)
%17 = fdiv double %13, %16
%18 = fsub double %12, %17
%19 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11, double %18)
%20 = fsub double %18, %12
%21 = tail call double @llvm.fabs.f64(double %20)
%22 = fcmp olt double %21, 1.000000e-05
br i1 %22, label %29, label %23
23: ; preds = %7
%24 = add nuw i32 %11, 1
%25 = tail call double @f(double %18)
%26 = tail call double @fprime(double %18)
%27 = tail call double @fprime2(double %18)
%28 = icmp eq i32 %11, %1
br i1 %28, label %29, label %7
29: ; preds = %7, %23, %2
%30 = phi double [ -1.000000e+00, %2 ], [ -1.000000e+00, %23 ], [ %18, %7 ]
ret double %30
}