#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);
double fabs(double);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
double stub1(double, double);
double stub2(double, double);
double stub3(double, double);
double bisection2(double (*func)(double, double), double x1, double x2,
                  double p1, double acc) {
  int j;
  double val, val2;
  double diff, mid;
  double root;
  val = (*func)(x1, p1);
  val2 = (*func)(x2, p1);
  if (val == 0.0)
    return (x1);
  if (val2 == 0.0)
    return (x2);
  if (val * val2 > 0.0) {
    fprintf(stderr, "Function not bracketed in bisection2");
    exit(1);
  }
  if (val < 0.0) {
    root = x1;
    diff = x2 - x1;
  } else {
    root = x2;
    diff = x1 - x2;
  }
  for (j = 1; j <= 10000; j++) {
    diff /= 2.0;
    mid = root + diff;
    val2 = (*func)(mid, p1);
    if (val2 <= 0.0)
      root = mid;
    if (fabs(diff) < acc || val2 == 0.0)
      return (root);
  }
  fprintf(stderr, "Too many steps in bisection2");
  exit(1);
}


x86 assembly (clang, -O0):
<bisection2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
movsd  %xmm2,-0x28(%rbp)
movsd  %xmm3,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
call   *%rax
movsd  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
call   *%rax
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1191 <bisection2+0x71>
jp     1191 <bisection2+0x71>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1311 <bisection2+0x1f1>
movsd  -0x48(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11b8 <bisection2+0x98>
jp     11b8 <bisection2+0x98>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1311 <bisection2+0x1f1>
movsd  -0x40(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11ee <bisection2+0xce>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
xorps  %xmm0,%xmm0
ucomisd -0x40(%rbp),%xmm0
jbe    121a <bisection2+0xfa>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
jmp    1233 <bisection2+0x113>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0x2710,-0x34(%rbp)
jg     12f2 <bisection2+0x1d2>
movsd  -0x50(%rbp),%xmm0
movsd  0xdac(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x60(%rbp),%xmm0
addsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x58(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
call   *%rax
movsd  %xmm0,-0x48(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x48(%rbp),%xmm0
jb     1299 <bisection2+0x179>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x50(%rbp),%xmm1
movaps 0xd6b(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
ja     12d0 <bisection2+0x1b0>
movsd  -0x48(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    12df <bisection2+0x1bf>
jp     12df <bisection2+0x1bf>
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1311 <bisection2+0x1f1>
jmp    12e4 <bisection2+0x1c4>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    123a <bisection2+0x11a>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd43(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
movsd  -0x8(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bisection2>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
movsd  %xmm3,0x40(%rsp)
movsd  %xmm2,0x8(%rsp)
movaps %xmm1,0x10(%rsp)
mov    %rdi,%rbx
movaps %xmm0,0x20(%rsp)
movapd %xmm2,%xmm1
call   *%rdi
movaps %xmm0,0x30(%rsp)
movapd 0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
call   *%rbx
movapd 0x30(%rsp),%xmm5
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm5
jne    1172 <bisection2+0x52>
jp     1172 <bisection2+0x52>
movapd 0x20(%rsp),%xmm2
jmp    1184 <bisection2+0x64>
movapd 0x10(%rsp),%xmm4
ucomisd %xmm1,%xmm0
jne    118f <bisection2+0x6f>
jp     118f <bisection2+0x6f>
movapd %xmm4,%xmm2
movapd %xmm2,%xmm0
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
mulsd  %xmm5,%xmm0
xorpd  %xmm3,%xmm3
ucomisd %xmm3,%xmm0
ja     1285 <bisection2+0x165>
ucomisd %xmm5,%xmm3
movapd 0x20(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
movapd %xmm4,%xmm0
ja     11c1 <bisection2+0xa1>
movapd %xmm2,%xmm4
subsd  %xmm0,%xmm4
jmp    11c9 <bisection2+0xa9>
movapd %xmm0,%xmm4
subsd  %xmm2,%xmm4
cmpltsd %xmm3,%xmm5
andpd  %xmm5,%xmm2
andnpd %xmm0,%xmm5
orpd   %xmm5,%xmm2
mov    $0x2710,%ebp
nop
movapd %xmm2,0x20(%rsp)
mulsd  0xe12(%rip),%xmm4
movapd %xmm4,0x10(%rsp)
addsd  %xmm4,%xmm2
movapd %xmm2,0x30(%rsp)
movapd %xmm2,%xmm0
call   *%rbx
movapd 0x10(%rsp),%xmm4
movapd %xmm0,%xmm1
xorpd  %xmm5,%xmm5
cmpnlesd %xmm5,%xmm1
movapd %xmm1,%xmm2
andnpd 0x30(%rsp),%xmm2
andpd  0x20(%rsp),%xmm1
orpd   %xmm2,%xmm1
movapd %xmm1,%xmm2
movapd %xmm4,%xmm1
andpd  0xdd5(%rip),%xmm1
movsd  0x40(%rsp),%xmm3
ucomisd %xmm1,%xmm3
ja     1184 <bisection2+0x64>
ucomisd %xmm5,%xmm0
jne    1257 <bisection2+0x137>
jnp    1184 <bisection2+0x64>
add    $0xffffffff,%ebp
movsd  0x8(%rsp),%xmm1
jne    11e0 <bisection2+0xc0>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1040 <exit@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
jmp    1276 <bisection2+0x156>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Function not bracketed in bisection2\00"
@.str.1 = [29 x i8] c"Too many steps in bisection2\00"
define double @bisection2(double (double, double)* %0, double %1, double %2, double %3, double %4) {
%6 = alloca double
%7 = alloca double (double, double)*
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca i32
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
store double (double, double)* %0, double (double, double)** %7
store double %1, double* %8
store double %2, double* %9
store double %3, double* %10
store double %4, double* %11
%18 = load double (double, double)*, double (double, double)** %7
%19 = load double, double* %8
%20 = load double, double* %10
%21 = call double %18(double %19, double %20)
store double %21, double* %13
%22 = load double (double, double)*, double (double, double)** %7
%23 = load double, double* %9
%24 = load double, double* %10
%25 = call double %22(double %23, double %24)
store double %25, double* %14
%26 = load double, double* %13
%27 = fcmp oeq double %26, 0.000000e+00
br i1 %27, label %28, label %30
28: ; preds = %5
%29 = load double, double* %8
store double %29, double* %6
br label %93
30: ; preds = %5
%31 = load double, double* %14
%32 = fcmp oeq double %31, 0.000000e+00
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = load double, double* %9
store double %34, double* %6
br label %93
35: ; preds = %30
%36 = load double, double* %13
%37 = load double, double* %14
%38 = fmul double %36, %37
%39 = fcmp ogt double %38, 0.000000e+00
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %35
%45 = load double, double* %13
%46 = fcmp olt double %45, 0.000000e+00
br i1 %46, label %47, label %52
47: ; preds = %44
%48 = load double, double* %8
store double %48, double* %17
%49 = load double, double* %9
%50 = load double, double* %8
%51 = fsub double %49, %50
store double %51, double* %15
br label %57
52: ; preds = %44
%53 = load double, double* %9
store double %53, double* %17
%54 = load double, double* %8
%55 = load double, double* %9
%56 = fsub double %54, %55
store double %56, double* %15
br label %57
57: ; preds = %52, %47
store i32 1, i32* %12
br label %58
58: ; preds = %86, %57
%59 = load i32, i32* %12
%60 = icmp sle i32 %59, 10000
br i1 %60, label %61, label %89
61: ; preds = %58
%62 = load double, double* %15
%63 = fdiv double %62, 2.000000e+00
store double %63, double* %15
%64 = load double, double* %17
%65 = load double, double* %15
%66 = fadd double %64, %65
store double %66, double* %16
%67 = load double (double, double)*, double (double, double)** %7
%68 = load double, double* %16
%69 = load double, double* %10
%70 = call double %67(double %68, double %69)
store double %70, double* %14
%71 = load double, double* %14
%72 = fcmp ole double %71, 0.000000e+00
br i1 %72, label %73, label %75
73: ; preds = %61
%74 = load double, double* %16
store double %74, double* %17
br label %75
75: ; preds = %73, %61
%76 = load double, double* %15
%77 = call double @llvm.fabs.f64(double %76)
%78 = load double, double* %11
%79 = fcmp olt double %77, %78
br i1 %79, label %83, label %80
80: ; preds = %75
%81 = load double, double* %14
%82 = fcmp oeq double %81, 0.000000e+00
br i1 %82, label %83, label %85
83: ; preds = %80, %75
%84 = load double, double* %17
store double %84, double* %6
br label %93
85: ; preds = %80
br label %86
86: ; preds = %85
%87 = load i32, i32* %12
%88 = add nsw i32 %87, 1
store i32 %88, i32* %12
br label %58
89: ; preds = %58
%90 = load i32, i32* @stderr
%91 = call i32 @fprintf(i32 %90, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %83, %33, %28
%94 = load double, double* %6
ret double %94
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Function not bracketed in bisection2\00"
@.str.1 = [29 x i8] c"Too many steps in bisection2\00"
define double @bisection2(double (double, double)* nocapture %0, double %1, double %2, double %3, double %4) {
%6 = tail call double %0(double %1, double %3)
%7 = tail call double %0(double %2, double %3)
%8 = fcmp oeq double %6, 0.000000e+00
br i1 %8, label %44, label %9
9: ; preds = %5
%10 = fcmp oeq double %7, 0.000000e+00
br i1 %10, label %44, label %11
11: ; preds = %9
%12 = fmul double %6, %7
%13 = fcmp ogt double %12, 0.000000e+00
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = fcmp olt double %6, 0.000000e+00
%20 = fsub double %2, %1
%21 = fsub double %1, %2
%22 = select i1 %19, double %20, double %21
%23 = select i1 %19, double %1, double %2
br label %27
24: ; preds = %27
%25 = add nuw nsw i32 %30, 1
%26 = icmp eq i32 %25, 10001
br i1 %26, label %40, label %27
27: ; preds = %18, %24
%28 = phi double [ %23, %18 ], [ %35, %24 ]
%29 = phi double [ %22, %18 ], [ %31, %24 ]
%30 = phi i32 [ 1, %18 ], [ %25, %24 ]
%31 = fmul double %29, 5.000000e-01
%32 = fadd double %28, %31
%33 = tail call double %0(double %32, double %3)
%34 = fcmp ugt double %33, 0.000000e+00
%35 = select i1 %34, double %28, double %32
%36 = tail call double @llvm.fabs.f64(double %31)
%37 = fcmp olt double %36, %4
%38 = fcmp oeq double %33, 0.000000e+00
%39 = select i1 %37, i1 true, i1 %38
br i1 %39, label %44, label %24
40: ; preds = %24
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %27, %9, %5
%45 = phi double [ %1, %5 ], [ %2, %9 ], [ %35, %27 ]
ret double %45
}