#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);
int /*<<< orphan*/ printf(char *, double, double, ...);
double bisect(double x_left, double x_right, double epsilon,
              double f(double farg)) {
  double x_mid, f_left, f_mid, f_right;
  int root_found;
  f_left = f(x_left);
  f_right = f(x_right);
  root_found = 0;
  while (fabs(x_right - x_left) > epsilon && !root_found) {
    x_mid = (x_left + x_right) / 2.0;
    f_mid = f(x_mid);
    if (f_mid == 0.0) {
      root_found = 1;
    } else if (f_left * f_mid < 0.0) {
      x_right = x_mid;
    } else {
      x_left = x_mid;
    }
    if (root_found)
      printf("\nRoot found at x = %.7f, midpoint of [%.7f, %.7f]", x_mid,
             x_left, x_right);
    else
      printf("\nNew interval is [%.7f, %.7f]", x_left, x_right);
  }
  return ((x_left + x_right) / 2.0);
}


x86 assembly (clang, -O0):
<bisect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x8(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movaps 0xe9d(%rip),%xmm1
pand   %xmm1,%xmm0
xor    %eax,%eax
ucomisd -0x18(%rbp),%xmm0
mov    %al,-0x45(%rbp)
jbe    1183 <bisect+0x73>
cmpl   $0x0,-0x44(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    1193 <bisect+0x83>
jmp    1260 <bisect+0x150>
movsd  -0x8(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  0xe6b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11e2 <bisect+0xd2>
jp     11e2 <bisect+0xd2>
movl   $0x1,-0x44(%rbp)
jmp    1217 <bisect+0x107>
movsd  -0x30(%rbp),%xmm1
mulsd  -0x38(%rbp),%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    1208 <bisect+0xf8>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1212 <bisect+0x102>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1217 <bisect+0x107>
cmpl   $0x0,-0x44(%rbp)
je     1243 <bisect+0x133>
movsd  -0x28(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm2
lea    0xde1(%rip),%rdi
mov    $0x3,%al
call   1030 <printf@plt>
jmp    125b <bisect+0x14b>
movsd  -0x8(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
lea    0xdf6(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
jmp    1152 <bisect+0x42>
movsd  -0x8(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  0xd9e(%rip),%xmm1
divsd  %xmm1,%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bisect>:
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdi,%rbx
movsd  %xmm2,0x28(%rsp)
movaps %xmm1,0x10(%rsp)
movsd  %xmm0,(%rsp)
call   *%rdi
movsd  %xmm0,0x40(%rsp)
movaps 0x10(%rsp),%xmm0
call   *%rbx
movsd  (%rsp),%xmm4
movapd 0x10(%rsp),%xmm2
movsd  0x28(%rsp),%xmm1
movapd %xmm2,%xmm0
subsd  %xmm4,%xmm0
andpd  0xea6(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    126f <bisect+0x15f>
lea    0xedf(%rip),%r14
movapd %xmm4,%xmm3
nop
movsd  %xmm3,0x8(%rsp)
movapd %xmm2,0x10(%rsp)
movapd %xmm2,%xmm0
movsd  %xmm4,(%rsp)
addsd  %xmm4,%xmm0
mulsd  0xe7f(%rip),%xmm0
movapd %xmm0,0x30(%rsp)
call   *%rbx
movapd 0x30(%rsp),%xmm5
xorpd  %xmm1,%xmm1
movsd  0x8(%rsp),%xmm2
movsd  0x40(%rsp),%xmm4
mulsd  %xmm0,%xmm4
ucomisd %xmm4,%xmm1
ja     11bd <bisect+0xad>
movapd %xmm5,%xmm2
ucomisd %xmm1,%xmm0
cmpltsd %xmm1,%xmm4
movapd %xmm4,%xmm1
andpd  %xmm5,%xmm1
movapd 0x10(%rsp),%xmm3
andnpd %xmm3,%xmm4
orpd   %xmm1,%xmm4
movapd %xmm4,%xmm6
jne    11ec <bisect+0xdc>
movapd %xmm4,%xmm6
jp     11ec <bisect+0xdc>
movapd %xmm3,%xmm6
jne    11f5 <bisect+0xe5>
jp     11f5 <bisect+0xe5>
movsd  (%rsp),%xmm2
movsd  %xmm2,0x8(%rsp)
jne    11ff <bisect+0xef>
jnp    1279 <bisect+0x169>
movsd  %xmm0,(%rsp)
movapd %xmm4,0x30(%rsp)
mov    %r14,%rdi
movapd %xmm2,%xmm0
movapd %xmm6,%xmm1
mov    $0x2,%al
movapd %xmm6,0x10(%rsp)
call   1030 <printf@plt>
movsd  0x8(%rsp),%xmm3
movapd 0x10(%rsp),%xmm0
subsd  %xmm3,%xmm0
andpd  0xdc6(%rip),%xmm0
movsd  0x28(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jbe    12a1 <bisect+0x191>
xorpd  %xmm2,%xmm2
movsd  (%rsp),%xmm0
ucomisd %xmm2,%xmm0
movapd %xmm3,%xmm4
movapd 0x30(%rsp),%xmm0
movapd %xmm0,%xmm2
jne    1170 <bisect+0x60>
jp     1170 <bisect+0x60>
jmp    12a6 <bisect+0x196>
movapd %xmm2,%xmm0
movapd %xmm4,%xmm3
jmp    12a6 <bisect+0x196>
lea    0xd98(%rip),%rdi
movapd %xmm5,%xmm0
movapd %xmm2,%xmm1
movapd %xmm6,%xmm2
mov    $0x3,%al
call   1030 <printf@plt>
movsd  0x8(%rsp),%xmm3
movapd 0x10(%rsp),%xmm0
jmp    12a6 <bisect+0x196>
movaps 0x30(%rsp),%xmm0
addsd  %xmm3,%xmm0
mulsd  0xd5e(%rip),%xmm0
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [50 x i8] c"\0ARoot found at x = %.7f, midpoint of [%.7f, %.7f]\00"
@.str.1 = [30 x i8] c"\0ANew interval is [%.7f, %.7f]\00"
define double @bisect(double %0, double %1, double %2, double (double)* %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double (double)*
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca i32
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
store double (double)* %3, double (double)** %8
%14 = load double (double)*, double (double)** %8
%15 = load double, double* %5
%16 = call double %14(double %15)
store double %16, double* %10
%17 = load double (double)*, double (double)** %8
%18 = load double, double* %6
%19 = call double %17(double %18)
store double %19, double* %12
store i32 0, i32* %13
br label %20
20: ; preds = %66, %4
%21 = load double, double* %6
%22 = load double, double* %5
%23 = fsub double %21, %22
%24 = call double @llvm.fabs.f64(double %23)
%25 = load double, double* %7
%26 = fcmp ogt double %24, %25
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = load i32, i32* %13
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br label %31
31: ; preds = %27, %20
%32 = phi i1 [ false, %20 ], [ %30, %27 ]
br i1 %32, label %33, label %67
33: ; preds = %31
%34 = load double, double* %5
%35 = load double, double* %6
%36 = fadd double %34, %35
%37 = fdiv double %36, 2.000000e+00
store double %37, double* %9
%38 = load double (double)*, double (double)** %8
%39 = load double, double* %9
%40 = call double %38(double %39)
store double %40, double* %11
%41 = load double, double* %11
%42 = fcmp oeq double %41, 0.000000e+00
br i1 %42, label %43, label %44
43: ; preds = %33
store i32 1, i32* %13
br label %54
44: ; preds = %33
%45 = load double, double* %10
%46 = load double, double* %11
%47 = fmul double %45, %46
%48 = fcmp olt double %47, 0.000000e+00
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = load double, double* %9
store double %50, double* %6
br label %53
51: ; preds = %44
%52 = load double, double* %9
store double %52, double* %5
br label %53
53: ; preds = %51, %49
br label %54
54: ; preds = %53, %43
%55 = load i32, i32* %13
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %54
%58 = load double, double* %9
%59 = load double, double* %5
%60 = load double, double* %6
%61 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), double %58, double %59, double %60)
br label %66
62: ; preds = %54
%63 = load double, double* %5
%64 = load double, double* %6
%65 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), double %63, double %64)
br label %66
66: ; preds = %62, %57
br label %20
67: ; preds = %31
%68 = load double, double* %5
%69 = load double, double* %6
%70 = fadd double %68, %69
%71 = fdiv double %70, 2.000000e+00
ret double %71
}

LLVM IR (-O2):
@.str = [50 x i8] c"\0ARoot found at x = %.7f, midpoint of [%.7f, %.7f]\00"
@.str.1 = [30 x i8] c"\0ANew interval is [%.7f, %.7f]\00"
define double @bisect(double %0, double %1, double %2, double (double)* nocapture %3) {
%5 = tail call double %3(double %0)
%6 = tail call double %3(double %1)
%7 = fsub double %1, %0
%8 = tail call double @llvm.fabs.f64(double %7)
%9 = fcmp ogt double %8, %2
br i1 %9, label %10, label %32
10: ; preds = %4, %25
%11 = phi double [ %22, %25 ], [ %0, %4 ]
%12 = phi double [ %19, %25 ], [ %1, %4 ]
%13 = fadd double %12, %11
%14 = fmul double %13, 5.000000e-01
%15 = tail call double %3(double %14)
%16 = fcmp oeq double %15, 0.000000e+00
%17 = fmul double %5, %15
%18 = fcmp olt double %17, 0.000000e+00
%19 = select i1 %18, double %14, double %12
%20 = select i1 %16, double %12, double %19
%21 = select i1 %16, i1 true, i1 %18
%22 = select i1 %21, double %11, double %14
br i1 %16, label %23, label %25
23: ; preds = %10
%24 = tail call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), double %14, double %22, double %20)
br label %32
25: ; preds = %10
%26 = xor i1 %16, true
%27 = tail call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), double %22, double %20)
%28 = fsub double %20, %22
%29 = tail call double @llvm.fabs.f64(double %28)
%30 = fcmp ogt double %29, %2
%31 = select i1 %30, i1 %26, i1 false
br i1 %31, label %10, label %32
32: ; preds = %25, %23, %4
%33 = phi double [ %1, %4 ], [ %12, %23 ], [ %19, %25 ]
%34 = phi double [ %0, %4 ], [ %22, %23 ], [ %22, %25 ]
%35 = fadd double %33, %34
%36 = fmul double %35, 5.000000e-01
ret double %36
}