#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*/ exit(int);
int fabs(double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
double bisect(double (*func)(double), double lower, double upper) {
  if (func(lower) > func(upper)) {
    int tmp = lower;
    lower = upper;
    upper = tmp;
  }
  int iter = 0;
  double midpoint;
  do {
    midpoint = 0.5 * lower + 0.5 * upper;
    if (fabs(func(midpoint)) < 1e-10) {
      return midpoint;
    } else if (func(midpoint) < 0) {
      lower = midpoint;
    } else {
      upper = midpoint;
    }
    iter++;
  } while (iter < 200);
  fprintf(stderr, "No root detected!\n");
  exit(1);
}


x86 assembly (clang, -O0):
<bisect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    118f <bisect+0x5f>
cvttsd2si -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe65(%rip),%xmm1
mulsd  -0x18(%rbp),%xmm1
movsd  0xe58(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
call   *%rax
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm1
movsd  0xe27(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11ee <bisect+0xbe>
movsd  -0x28(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x8(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    1218 <bisect+0xe8>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1222 <bisect+0xf2>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1227 <bisect+0xf7>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xc8,-0x20(%rbp)
jl     1196 <bisect+0x66>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<bisect>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
movaps %xmm1,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rdi,%rbx
call   *%rdi
movsd  %xmm0,0x20(%rsp)
movapd 0x10(%rsp),%xmm0
call   *%rbx
movapd (%rsp),%xmm3
movapd 0x10(%rsp),%xmm1
movapd %xmm0,%xmm2
movsd  0x20(%rsp),%xmm4
ucomisd %xmm0,%xmm4
cvttpd2dq %xmm3,%xmm0
cvtdq2pd %xmm0,%xmm0
cmpltsd %xmm4,%xmm2
andpd  %xmm2,%xmm0
andnpd %xmm1,%xmm2
orpd   %xmm0,%xmm2
ja     118a <bisect+0x5a>
movapd %xmm3,%xmm1
mov    $0xc8,%ebp
jmp    11c0 <bisect+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpltsd 0xe5f(%rip),%xmm0
movapd 0x20(%rsp),%xmm2
andpd  %xmm0,%xmm2
andnpd %xmm3,%xmm0
orpd   %xmm0,%xmm2
add    $0xffffffff,%ebp
je     1225 <bisect+0xf5>
movapd %xmm2,0x20(%rsp)
movapd %xmm2,%xmm0
movsd  0xe2e(%rip),%xmm2
mulsd  %xmm2,%xmm0
movapd %xmm1,0x10(%rsp)
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movapd %xmm1,(%rsp)
movapd %xmm1,%xmm0
call   *%rbx
call   1030 <fabs@plt>
test   %eax,%eax
jle    121a <bisect+0xea>
movapd (%rsp),%xmm0
call   *%rbx
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
movapd (%rsp),%xmm3
movapd %xmm3,%xmm1
ja     11a0 <bisect+0x70>
movapd 0x10(%rsp),%xmm1
jmp    11a0 <bisect+0x70>
movaps (%rsp),%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"No root detected!\0A\00"
define double @bisect(double (double)* %0, double %1, double %2) {
%4 = alloca double (double)*
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
store double (double)* %0, double (double)** %4
store double %1, double* %5
store double %2, double* %6
%10 = load double (double)*, double (double)** %4
%11 = load double, double* %5
%12 = call double %10(double %11)
%13 = load double (double)*, double (double)** %4
%14 = load double, double* %6
%15 = call double %13(double %14)
%16 = fcmp ogt double %12, %15
br i1 %16, label %17, label %23
17: ; preds = %3
%18 = load double, double* %5
%19 = fptosi double %18 to i32
store i32 %19, i32* %7
%20 = load double, double* %6
store double %20, double* %5
%21 = load i32, i32* %7
%22 = sitofp i32 %21 to double
store double %22, double* %6
br label %23
23: ; preds = %17, %3
store i32 0, i32* %8
br label %24
24: ; preds = %50, %23
%25 = load double, double* %5
%26 = load double, double* %6
%27 = fmul double 5.000000e-01, %26
%28 = call double @llvm.fmuladd.f64(double 5.000000e-01, double %25, double %27)
store double %28, double* %9
%29 = load double (double)*, double (double)** %4
%30 = load double, double* %9
%31 = call double %29(double %30)
%32 = call i32 @fabs(double %31)
%33 = sitofp i32 %32 to double
%34 = fcmp olt double %33, 1.000000e-10
br i1 %34, label %35, label %37
35: ; preds = %24
%36 = load double, double* %9
ret double %36
37: ; preds = %24
%38 = load double (double)*, double (double)** %4
%39 = load double, double* %9
%40 = call double %38(double %39)
%41 = fcmp olt double %40, 0.000000e+00
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = load double, double* %9
store double %43, double* %5
br label %46
44: ; preds = %37
%45 = load double, double* %9
store double %45, double* %6
br label %46
46: ; preds = %44, %42
br label %47
47: ; preds = %46
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
store i32 %49, i32* %8
br label %50
50: ; preds = %47
%51 = load i32, i32* %8
%52 = icmp slt i32 %51, 200
br i1 %52, label %24, label %53
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"No root detected!\0A\00"
define double @bisect(double (double)* nocapture %0, double %1, double %2) {
%4 = tail call double %0(double %1)
%5 = tail call double %0(double %2)
%6 = fcmp ogt double %4, %5
%7 = fptosi double %1 to i32
%8 = sitofp i32 %7 to double
%9 = select i1 %6, double %8, double %2
%10 = select i1 %6, double %2, double %1
br label %11
11: ; preds = %21, %3
%12 = phi double [ %9, %3 ], [ %24, %21 ]
%13 = phi i32 [ 0, %3 ], [ %26, %21 ]
%14 = phi double [ %10, %3 ], [ %25, %21 ]
%15 = fmul double %12, 5.000000e-01
%16 = tail call double @llvm.fmuladd.f64(double %14, double 5.000000e-01, double %15)
%17 = tail call double %0(double %16)
%18 = tail call i32 @fabs(double %17)
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %11
ret double %16
21: ; preds = %11
%22 = tail call double %0(double %16)
%23 = fcmp olt double %22, 0.000000e+00
%24 = select i1 %23, double %12, double %16
%25 = select i1 %23, double %16, double %14
%26 = add nuw nsw i32 %13, 1
%27 = icmp eq i32 %26, 200
br i1 %27, label %28, label %11
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
}