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


x86 assembly (clang, -O0):
<falsePosition>:
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   1040 <g@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <g@plt>
movsd  %xmm0,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     124b <falsePosition+0x12b>
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    0xe6c(%rip),%rdi
call   1030 <printf@plt>
movsd  -0x28(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
movaps 0xe46(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xe32(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11ef <falsePosition+0xcf>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1258 <falsePosition+0x138>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1040 <g@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
mulsd  -0x30(%rbp),%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    1232 <falsePosition+0x112>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    116a <falsePosition+0x4a>
movsd  0xdad(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<falsePosition>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %edi,%r15d
movaps %xmm0,0x10(%rsp)
movapd %xmm1,0x20(%rsp)
movapd %xmm1,%xmm0
call   1040 <g@plt>
movsd  %xmm0,0x8(%rsp)
movapd 0x10(%rsp),%xmm0
call   1040 <g@plt>
movapd %xmm0,%xmm4
movsd  0xea3(%rip),%xmm0
test   %r15d,%r15d
jle    1268 <falsePosition+0x148>
neg    %r15d
mov    $0x1,%ebx
lea    0xeab(%rip),%r14
movapd 0x20(%rsp),%xmm3
movapd %xmm3,%xmm0
movsd  0x8(%rsp),%xmm2
movapd 0x10(%rsp),%xmm1
jmp    11ce <falsePosition+0xae>
nopl   (%rax)
cmpltsd %xmm3,%xmm1
movapd 0x20(%rsp),%xmm3
andpd  %xmm1,%xmm3
andnpd 0x10(%rsp),%xmm1
orpd   %xmm3,%xmm1
lea    (%r15,%rbx,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
movapd %xmm0,%xmm2
mov    %ecx,%ebx
movapd 0x40(%rsp),%xmm0
movapd %xmm0,%xmm3
cmp    $0x1,%eax
je     1260 <falsePosition+0x140>
movsd  %xmm2,0x8(%rsp)
movapd %xmm3,0x20(%rsp)
movapd %xmm1,0x10(%rsp)
subsd  %xmm1,%xmm3
mulsd  %xmm2,%xmm3
movapd %xmm2,%xmm1
movsd  %xmm4,0x38(%rsp)
subsd  %xmm4,%xmm1
divsd  %xmm1,%xmm3
subsd  %xmm3,%xmm0
movapd %xmm0,0x40(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
movapd 0x40(%rsp),%xmm0
movapd %xmm0,%xmm2
subsd  0x20(%rsp),%xmm2
andpd  0xdea(%rip),%xmm2
movsd  0xdda(%rip),%xmm1
ucomisd %xmm2,%xmm1
ja     1268 <falsePosition+0x148>
call   1040 <g@plt>
movsd  0x8(%rsp),%xmm4
movapd %xmm4,%xmm1
mulsd  %xmm0,%xmm1
xorpd  %xmm3,%xmm3
ucomisd %xmm1,%xmm3
ja     1190 <falsePosition+0x70>
movsd  0x38(%rsp),%xmm4
jmp    1190 <falsePosition+0x70>
movsd  0xd98(%rip),%xmm0
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [16 x i8] c"Iter %d: %.9lf\0A\00"
define double @falsePosition(double %0, double %1, i32 %2) {
%4 = tail call double @g(double %1)
%5 = tail call double @g(double %0)
%6 = icmp slt i32 %2, 1
br i1 %6, label %30, label %7
7: ; preds = %3, %22
%8 = phi double [ %28, %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 [ %27, %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-05
br i1 %21, label %30, label %22
22: ; preds = %7
%23 = add nuw i32 %10, 1
%24 = tail call double @g(double %17)
%25 = fmul double %9, %24
%26 = fcmp olt double %25, 0.000000e+00
%27 = select i1 %26, double %11, double %12
%28 = select i1 %26, double %9, double %8
%29 = icmp eq i32 %10, %2
br i1 %29, label %30, label %7
30: ; preds = %7, %22, %3
%31 = phi double [ -1.000000e+00, %3 ], [ -1.000000e+00, %22 ], [ %17, %7 ]
ret double %31
}