#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 fabs(double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, double, double, int);
int /*<<< orphan*/ stdout;
double bisect(double (*f)(double), double a, double b) {
  double fa, x, fx;
  int i;
  i = 0;
  fa = f(a);
  fx = 1.0;
  while ((fabs(fx) > 1e-9) && (i < 200)) {
    x = 0.5 * (a + b);
    fx = f(x);
    if ((fa * fx) > 0.0) {
      a = x;
      fa = fx;
    } else {
      b = x;
    }
    i++;
  }
  fprintf(stdout, "Bisection: %f %f %d\n", fx, x, i);
  return x;
}


x86 assembly (clang, -O0):
<bisect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x20(%rbp)
movsd  0xeab(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <fabs@plt>
cvtsi2sd %eax,%xmm0
xor    %eax,%eax
movsd  0xe96(%rip),%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x35(%rbp)
jbe    118c <bisect+0x6c>
cmpl   $0xc8,-0x34(%rbp)
setl   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    119c <bisect+0x7c>
jmp    120f <bisect+0xef>
movsd  -0x10(%rbp),%xmm1
addsd  -0x18(%rbp),%xmm1
movsd  0xe62(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movsd  -0x28(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11f7 <bisect+0xd7>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    1201 <bisect+0xe1>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    115a <bisect+0x3a>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
movsd  -0x30(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mov    -0x34(%rbp),%edx
lea    0xdec(%rip),%rsi
call   1040 <fprintf@plt>
movsd  -0x28(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bisect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
movsd  %xmm1,(%rsp)
movaps %xmm0,0x20(%rsp)
mov    %rdi,%r14
call   *%rdi
movsd  %xmm0,0x8(%rsp)
movsd  0xebb(%rip),%xmm0
call   1030 <fabs@plt>
movapd 0x20(%rsp),%xmm0
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movapd %xmm0,0x20(%rsp)
addsd  (%rsp),%xmm0
mulsd  0xe95(%rip),%xmm0
movapd %xmm0,0x10(%rsp)
call   *%r14
movapd %xmm0,%xmm1
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x38(%rsp)
ucomisd 0xe74(%rip),%xmm0
movapd %xmm1,%xmm0
ja     11b0 <bisect+0x90>
movaps 0x10(%rsp),%xmm0
movsd  %xmm0,(%rsp)
movapd %xmm2,%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0x1(%rbx),%ebp
movsd  %xmm1,0x30(%rsp)
movapd %xmm1,%xmm0
call   1030 <fabs@plt>
test   %eax,%eax
movapd 0x20(%rsp),%xmm2
jle    11fd <bisect+0xdd>
xorpd  %xmm0,%xmm0
cmpltsd 0x38(%rsp),%xmm0
movapd %xmm0,%xmm1
andpd  0x10(%rsp),%xmm1
andnpd %xmm2,%xmm0
orpd   %xmm1,%xmm0
cmp    $0xc7,%ebx
mov    %ebp,%ebx
jb     1160 <bisect+0x40>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
movsd  0x30(%rsp),%xmm0
movaps 0x10(%rsp),%xmm1
mov    %ebp,%edx
call   1040 <fprintf@plt>
movaps 0x10(%rsp),%xmm0
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@stdout = global i32 0
@.str = [21 x i8] c"Bisection: %f %f %d\0A\00"
define double @bisect(double (double)* nocapture %0, double %1, double %2) {
%4 = tail call double %0(double %1)
%5 = tail call i32 @fabs(double 1.000000e+00)
%6 = icmp sgt i32 %5, 0
br label %7
7: ; preds = %3, %7
%8 = phi i32 [ %20, %7 ], [ 0, %3 ]
%9 = phi double [ %19, %7 ], [ %4, %3 ]
%10 = phi double [ %18, %7 ], [ %2, %3 ]
%11 = phi double [ %17, %7 ], [ %1, %3 ]
%12 = fadd double %11, %10
%13 = fmul double %12, 5.000000e-01
%14 = tail call double %0(double %13)
%15 = fmul double %9, %14
%16 = fcmp ogt double %15, 0.000000e+00
%17 = select i1 %16, double %13, double %11
%18 = select i1 %16, double %10, double %13
%19 = select i1 %16, double %14, double %9
%20 = add nuw nsw i32 %8, 1
%21 = tail call i32 @fabs(double %14)
%22 = icmp sgt i32 %21, 0
%23 = icmp ult i32 %8, 199
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %7, label %25
25: ; preds = %7
%26 = load i32, i32* @stdout
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), double %14, double %13, i32 %20)
ret double %13
}