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


x86 assembly (clang, -O0):
<secantMethod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %edi,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <f@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <f@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1222 <secantMethod+0x102>
movsd  -0x18(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm2
subsd  -0x10(%rbp),%xmm2
mulsd  %xmm2,%xmm1
movsd  -0x30(%rbp),%xmm2
subsd  -0x38(%rbp),%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%esi
movsd  -0x28(%rbp),%xmm0
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
movsd  -0x28(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
movaps 0xe4e(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xe3a(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11e7 <secantMethod+0xc7>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    122f <secantMethod+0x10f>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1030 <f@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    1162 <secantMethod+0x42>
movsd  0xdd6(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secantMethod>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
movsd  %xmm0,(%rsp)
movsd  %xmm1,0x10(%rsp)
movaps %xmm1,%xmm0
call   1030 <f@plt>
movsd  %xmm0,0x8(%rsp)
movsd  (%rsp),%xmm0
call   1030 <f@plt>
movsd  0xea8(%rip),%xmm1
test   %r15d,%r15d
jle    1225 <secantMethod+0x105>
neg    %r15d
mov    $0x1,%ebx
lea    0xeb0(%rip),%r14
movsd  0x8(%rsp),%xmm3
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm3,0x8(%rsp)
movsd  (%rsp),%xmm1
movsd  0x10(%rsp),%xmm4
movapd %xmm4,%xmm2
movsd  %xmm4,(%rsp)
subsd  %xmm1,%xmm2
mulsd  %xmm3,%xmm2
movapd %xmm3,%xmm1
subsd  %xmm0,%xmm1
divsd  %xmm1,%xmm2
movapd %xmm4,%xmm0
subsd  %xmm2,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
movsd  0x10(%rsp),%xmm2
movapd %xmm2,%xmm0
subsd  (%rsp),%xmm0
andpd  0xe33(%rip),%xmm0
movsd  0xe23(%rip),%xmm1
ucomisd %xmm0,%xmm1
movapd %xmm2,%xmm0
ja     1221 <secantMethod+0x101>
lea    0x1(%rbx),%ebp
call   1030 <f@plt>
movapd %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
lea    (%r15,%rbx,1),%eax
add    $0x1,%eax
movapd %xmm1,%xmm3
mov    %ebp,%ebx
cmp    $0x1,%eax
jne    1180 <secantMethod+0x60>
movsd  0xde1(%rip),%xmm1
jmp    1225 <secantMethod+0x105>
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 @secantMethod(double %0, double %1, i32 %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
store double %0, double* %5
store double %1, double* %6
store i32 %2, i32* %7
store i32 1, i32* %8
store double 0.000000e+00, double* %9
%12 = load double, double* %6
%13 = call double @f(double %12)
store double %13, double* %10
%14 = load double, double* %5
%15 = call double @f(double %14)
store double %15, double* %11
br label %16
16: ; preds = %42, %3
%17 = load i32, i32* %8
%18 = load i32, i32* %7
%19 = icmp sle i32 %17, %18
br i1 %19, label %20, label %50
20: ; preds = %16
%21 = load double, double* %6
%22 = load double, double* %10
%23 = load double, double* %6
%24 = load double, double* %5
%25 = fsub double %23, %24
%26 = fmul double %22, %25
%27 = load double, double* %10
%28 = load double, double* %11
%29 = fsub double %27, %28
%30 = fdiv double %26, %29
%31 = fsub double %21, %30
store double %31, double* %9
%32 = load i32, i32* %8
%33 = load double, double* %9
%34 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %32, double %33)
%35 = load double, double* %9
%36 = load double, double* %6
%37 = fsub double %35, %36
%38 = call double @llvm.fabs.f64(double %37)
%39 = fcmp olt double %38, 1.000000e-04
br i1 %39, label %40, label %42
40: ; preds = %20
%41 = load double, double* %9
store double %41, double* %4
br label %51
42: ; preds = %20
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
%45 = load double, double* %6
store double %45, double* %5
%46 = load double, double* %9
store double %46, double* %6
%47 = load double, double* %10
store double %47, double* %11
%48 = load double, double* %9
%49 = call double @f(double %48)
store double %49, double* %10
br label %16
50: ; preds = %16
store double -1.000000e+00, double* %4
br label %51
51: ; preds = %50, %40
%52 = load double, double* %4
ret double %52
}

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