#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 check_initial_values(double (*)(double, void *), void *, double, double);
int /*<<< orphan*/ exit(int);
double fabs(double);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, double, double);
int /*<<< orphan*/ stdout;
double bisection_root_finding(double (*f)(double x, void *params), void *params,
                              double x_min_start, double x_max_start) {
  double x_min = x_min_start;
  double x_mid;
  double x_max = x_max_start;
  double y_min;
  double y_mid;
  double y_max;
  int flag = 0;
  int iter = 0;
  int imax = 100000;
  flag = check_initial_values(f, params, x_min, x_max);
  if (flag) {
    if (flag == 1)
      return x_min;
    if (flag == 2)
      return x_max;
  }
  while (!flag) {
    x_mid = 0.5 * (x_min + x_max);
    y_mid = f(x_mid, params);
    if (fabs(y_mid) < 1.e-9 || fabs(x_max - x_min) < 1.0e-14) {
      flag = 1;
    } else {
      y_min = f(x_min, params);
      y_max = f(x_max, params);
      if (y_min * y_mid > 0) {
        x_min = x_mid;
      } else {
        x_max = x_mid;
      }
      iter++;
      if (iter >= imax) {
        printf("Exceeded %d iterations, exiting (x=%f, f=%f)...\n", imax, x_mid,
               y_mid);
        fflush(stdout);
        exit(-1);
      }
    }
  }
  return x_mid;
}


x86 assembly (clang, -O0):
<bisection_root_finding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
movsd  %xmm1,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x5c(%rbp)
movl   $0x0,-0x60(%rbp)
movl   $0x186a0,-0x64(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movsd  -0x30(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm1
call   1050 <check_initial_values@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     11de <bisection_root_finding+0x9e>
cmpl   $0x1,-0x5c(%rbp)
jne    11c0 <bisection_root_finding+0x80>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    132f <bisection_root_finding+0x1ef>
cmpl   $0x2,-0x5c(%rbp)
jne    11d9 <bisection_root_finding+0x99>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    132f <bisection_root_finding+0x1ef>
jmp    11de <bisection_root_finding+0x9e>
jmp    11e3 <bisection_root_finding+0xa3>
cmpl   $0x0,-0x5c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f9 <bisection_root_finding+0xb9>
jmp    1325 <bisection_root_finding+0x1e5>
movsd  -0x30(%rbp),%xmm1
addsd  -0x40(%rbp),%xmm1
movsd  0xdfd(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
mov    -0x18(%rbp),%rdi
call   *%rax
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm1
movaps 0xdec(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xdc0(%rip),%xmm0
ucomisd %xmm1,%xmm0
ja     1271 <bisection_root_finding+0x131>
movsd  -0x40(%rbp),%xmm1
subsd  -0x30(%rbp),%xmm1
movaps 0xdc5(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  0xda9(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    127d <bisection_root_finding+0x13d>
movl   $0x1,-0x5c(%rbp)
jmp    1320 <bisection_root_finding+0x1e0>
mov    -0x10(%rbp),%rax
movsd  -0x30(%rbp),%xmm0
mov    -0x18(%rbp),%rdi
call   *%rax
movsd  %xmm0,-0x48(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
mov    -0x18(%rbp),%rdi
call   *%rax
movsd  %xmm0,-0x58(%rbp)
movsd  -0x48(%rbp),%xmm0
mulsd  -0x50(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12cb <bisection_root_finding+0x18b>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    12d5 <bisection_root_finding+0x195>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x64(%rbp),%eax
jl     131b <bisection_root_finding+0x1db>
mov    -0x64(%rbp),%esi
movsd  -0x38(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm1
lea    0xd32(%rip),%rdi
call   1030 <printf@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    1320 <bisection_root_finding+0x1e0>
jmp    11e3 <bisection_root_finding+0xa3>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bisection_root_finding>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movsd  %xmm0,0x8(%rsp)
movapd %xmm1,0x10(%rsp)
call   1050 <check_initial_values@plt>
cmp    $0x2,%eax
je     1277 <bisection_root_finding+0x137>
mov    %eax,%ebp
test   %eax,%eax
jne    127e <bisection_root_finding+0x13e>
add    $0xffffffff,%ebp
movapd 0x10(%rsp),%xmm2
movsd  0x8(%rsp),%xmm3
jmp    11ba <bisection_root_finding+0x7a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xorpd  %xmm4,%xmm4
cmpltsd %xmm2,%xmm4
movapd 0x10(%rsp),%xmm2
andpd  %xmm4,%xmm2
andnpd %xmm0,%xmm4
orpd   %xmm4,%xmm2
add    $0x1,%ebp
cmp    $0x1869f,%ebp
jae    1294 <bisection_root_finding+0x154>
movsd  %xmm3,0x8(%rsp)
movapd %xmm2,0x10(%rsp)
movapd %xmm2,%xmm0
addsd  %xmm3,%xmm0
mulsd  0xe2a(%rip),%xmm0
movapd %xmm0,0x20(%rsp)
mov    %r14,%rdi
call   *%rbx
movapd %xmm0,%xmm3
movapd 0xe33(%rip),%xmm1
andpd  %xmm1,%xmm0
movsd  0xe0f(%rip),%xmm2
ucomisd %xmm0,%xmm2
ja     1286 <bisection_root_finding+0x146>
movapd 0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm4
subsd  %xmm4,%xmm0
andpd  %xmm1,%xmm0
movsd  0xdf1(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     1286 <bisection_root_finding+0x146>
movapd %xmm4,%xmm0
mov    %r14,%rdi
movapd %xmm3,0x40(%rsp)
call   *%rbx
movsd  %xmm0,0x38(%rsp)
movaps 0x10(%rsp),%xmm0
mov    %r14,%rdi
call   *%rbx
movsd  0x38(%rsp),%xmm2
movapd 0x40(%rsp),%xmm1
mulsd  %xmm1,%xmm2
ucomisd 0xdbc(%rip),%xmm2
movapd 0x20(%rsp),%xmm0
movapd %xmm0,%xmm3
ja     1190 <bisection_root_finding+0x50>
movsd  0x8(%rsp),%xmm3
jmp    1190 <bisection_root_finding+0x50>
movaps 0x10(%rsp),%xmm0
jmp    128b <bisection_root_finding+0x14b>
movsd  0x8(%rsp),%xmm0
jmp    128b <bisection_root_finding+0x14b>
movaps 0x20(%rsp),%xmm0
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd95(%rip),%rdi
mov    $0x186a0,%esi
call   1030 <printf@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"Exceeded %d iterations, exiting (x=%f, f=%f)...\0A\00"
@stdout = global i32 0
define double @bisection_root_finding(double (double, i8*)* %0, i8* %1, double %2, double %3) {
%5 = alloca double
%6 = alloca double (double, i8*)*
%7 = alloca i8*
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store double (double, i8*)* %0, double (double, i8*)** %6
store i8* %1, i8** %7
store double %2, double* %8
store double %3, double* %9
%19 = load double, double* %8
store double %19, double* %10
%20 = load double, double* %9
store double %20, double* %12
store i32 0, i32* %16
store i32 0, i32* %17
store i32 100000, i32* %18
%21 = load double (double, i8*)*, double (double, i8*)** %6
%22 = load i8*, i8** %7
%23 = load double, double* %10
%24 = load double, double* %12
%25 = call i32 @check_initial_values(double (double, i8*)* %21, i8* %22, double %23, double %24)
store i32 %25, i32* %16
%26 = load i32, i32* %16
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %4
%29 = load i32, i32* %16
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = load double, double* %10
store double %32, double* %5
br label %98
33: ; preds = %28
%34 = load i32, i32* %16
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load double, double* %12
store double %37, double* %5
br label %98
38: ; preds = %33
br label %39
39: ; preds = %38, %4
br label %40
40: ; preds = %95, %39
%41 = load i32, i32* %16
%42 = icmp ne i32 %41, 0
%43 = xor i1 %42, true
br i1 %43, label %44, label %96
44: ; preds = %40
%45 = load double, double* %10
%46 = load double, double* %12
%47 = fadd double %45, %46
%48 = fmul double 5.000000e-01, %47
store double %48, double* %11
%49 = load double (double, i8*)*, double (double, i8*)** %6
%50 = load double, double* %11
%51 = load i8*, i8** %7
%52 = call double %49(double %50, i8* %51)
store double %52, double* %14
%53 = load double, double* %14
%54 = call double @llvm.fabs.f64(double %53)
%55 = fcmp olt double %54, 1.000000e-09
br i1 %55, label %62, label %56
56: ; preds = %44
%57 = load double, double* %12
%58 = load double, double* %10
%59 = fsub double %57, %58
%60 = call double @llvm.fabs.f64(double %59)
%61 = fcmp olt double %60, 0x3D06849B86A12B9B
br i1 %61, label %62, label %63
62: ; preds = %56, %44
store i32 1, i32* %16
br label %95
63: ; preds = %56
%64 = load double (double, i8*)*, double (double, i8*)** %6
%65 = load double, double* %10
%66 = load i8*, i8** %7
%67 = call double %64(double %65, i8* %66)
store double %67, double* %13
%68 = load double (double, i8*)*, double (double, i8*)** %6
%69 = load double, double* %12
%70 = load i8*, i8** %7
%71 = call double %68(double %69, i8* %70)
store double %71, double* %15
%72 = load double, double* %13
%73 = load double, double* %14
%74 = fmul double %72, %73
%75 = fcmp ogt double %74, 0.000000e+00
br i1 %75, label %76, label %78
76: ; preds = %63
%77 = load double, double* %11
store double %77, double* %10
br label %80
78: ; preds = %63
%79 = load double, double* %11
store double %79, double* %12
br label %80
80: ; preds = %78, %76
%81 = load i32, i32* %17
%82 = add nsw i32 %81, 1
store i32 %82, i32* %17
%83 = load i32, i32* %17
%84 = load i32, i32* %18
%85 = icmp sge i32 %83, %84
br i1 %85, label %86, label %94
86: ; preds = %80
%87 = load i32, i32* %18
%88 = load double, double* %11
%89 = load double, double* %14
%90 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %87, double %88, double %89)
%91 = load i32, i32* @stdout
%92 = call i32 @fflush(i32 %91)
%93 = call i32 @exit(i32 -1)
unreachable
94: ; preds = %80
br label %95
95: ; preds = %94, %62
br label %40
96: ; preds = %40
%97 = load double, double* %11
store double %97, double* %5
br label %98
98: ; preds = %96, %36, %31
%99 = load double, double* %5
ret double %99
}

LLVM IR (-O2):
@.str = [49 x i8] c"Exceeded %d iterations, exiting (x=%f, f=%f)...\0A\00"
@stdout = global i32 0
define double @bisection_root_finding(double (double, i8*)* %0, i8* %1, double %2, double %3) {
%5 = tail call i32 @check_initial_values(double (double, i8*)* %0, i8* %1, double %2, double %3)
switch i32 %5, label %34 [
i32 2, label %6
i32 0, label %7
]
6: ; preds = %4
br label %34
7: ; preds = %4, %20
%8 = phi i32 [ %27, %20 ], [ %5, %4 ]
%9 = phi double [ %26, %20 ], [ %2, %4 ]
%10 = phi double [ %25, %20 ], [ %3, %4 ]
%11 = fadd double %10, %9
%12 = fmul double %11, 5.000000e-01
%13 = tail call double %0(double %12, i8* %1)
%14 = tail call double @llvm.fabs.f64(double %13)
%15 = fcmp olt double %14, 1.000000e-09
br i1 %15, label %34, label %16
16: ; preds = %7
%17 = fsub double %10, %9
%18 = tail call double @llvm.fabs.f64(double %17)
%19 = fcmp olt double %18, 0x3D06849B86A12B9B
br i1 %19, label %34, label %20
20: ; preds = %16
%21 = tail call double %0(double %9, i8* %1)
%22 = tail call double %0(double %10, i8* %1)
%23 = fmul double %13, %21
%24 = fcmp ogt double %23, 0.000000e+00
%25 = select i1 %24, double %10, double %12
%26 = select i1 %24, double %12, double %9
%27 = add nuw nsw i32 %8, 1
%28 = icmp ugt i32 %8, 99998
br i1 %28, label %29, label %7
29: ; preds = %20
%30 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 100000, double %12, double %13)
%31 = load i32, i32* @stdout
%32 = tail call i32 @fflush(i32 %31)
%33 = tail call i32 @exit(i32 -1)
unreachable
34: ; preds = %7, %16, %4, %6
%35 = phi double [ %3, %6 ], [ %2, %4 ], [ %12, %16 ], [ %12, %7 ]
ret double %35
}