#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 const fabs(double const);
double midpoint(double, double);
scalar_t__ samesign(double const, double);
bool findroot(double (*const f)(double), double const a, double const b,
              double const epsilon, unsigned int const n, double *const p) {
  if (samesign(f(a), f(b)))
    return false;
  double x1 = a, x2 = b;
  double x = midpoint(x1, x2);
  double fx1 = f(x1);
  for (unsigned int i = 0; i < n; ++i) {
    double const fx = f(x);
    if (fabs(fx) < epsilon) {
      if (p != NULL)
        *p = x;
      return true;
    }
    if (samesign(fx, fx1)) {
      x1 = x;
      fx1 = fx;
    } else
      x2 = x;
    x = midpoint(x1, x2);
  }
  return false;
}


x86 assembly (clang, -O0):
<findroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
movsd  %xmm2,-0x28(%rbp)
mov    %esi,-0x2c(%rbp)
mov    %rdx,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x18(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x70(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x20(%rbp),%xmm0
call   *%rax
movaps %xmm0,%xmm1
movsd  -0x70(%rbp),%xmm0
call   1050 <samesign@plt>
cmp    $0x0,%rax
je     1190 <findroot+0x60>
movl   $0x0,-0x4(%rbp)
jmp    1290 <findroot+0x160>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
call   1040 <midpoint@plt>
movsd  %xmm0,-0x50(%rbp)
mov    -0x10(%rbp),%rax
movsd  -0x40(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jae    1289 <findroot+0x159>
mov    -0x10(%rbp),%rax
movsd  -0x50(%rbp),%xmm0
call   *%rax
movsd  %xmm0,-0x68(%rbp)
movsd  -0x68(%rbp),%xmm0
call   1030 <fabs@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    122b <findroot+0xfb>
cmpq   $0x0,-0x38(%rbp)
je     121f <findroot+0xef>
movsd  -0x50(%rbp),%xmm0
mov    -0x38(%rbp),%rax
movsd  %xmm0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1290 <findroot+0x160>
movsd  -0x68(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm1
call   1050 <samesign@plt>
cmp    $0x0,%rax
je     125d <findroot+0x12d>
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
jmp    1267 <findroot+0x137>
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
call   1040 <midpoint@plt>
movsd  %xmm0,-0x50(%rbp)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    11cf <findroot+0x9f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findroot>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
movsd  %xmm2,0x20(%rsp)
movsd  %xmm1,0x18(%rsp)
mov    %rdi,%r12
movsd  %xmm0,0x10(%rsp)
call   *%rdi
movsd  %xmm0,0x8(%rsp)
movsd  0x18(%rsp),%xmm0
call   *%r12
movapd %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
call   1040 <samesign@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    128c <findroot+0x16c>
movsd  0x10(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
call   1030 <midpoint@plt>
movsd  %xmm0,0x8(%rsp)
movsd  0x10(%rsp),%xmm0
call   *%r12
movsd  %xmm0,0x28(%rsp)
test   %r15d,%r15d
je     1267 <findroot+0x147>
movsd  0x8(%rsp),%xmm0
call   *%r12
movapd 0xe4b(%rip),%xmm2
andpd  %xmm0,%xmm2
mov    $0x1,%al
movsd  0x20(%rsp),%xmm1
ucomisd %xmm2,%xmm1
ja     1274 <findroot+0x154>
mov    $0x1,%ebx
jmp    1204 <findroot+0xe4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  %xmm0,0x8(%rsp)
call   *%r12
movapd %xmm0,%xmm2
andpd  0xe0b(%rip),%xmm2
add    $0x1,%ebx
movsd  0x20(%rsp),%xmm1
ucomisd %xmm2,%xmm1
ja     126b <findroot+0x14b>
movapd %xmm0,0x30(%rsp)
movsd  0x28(%rsp),%xmm1
call   1040 <samesign@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1230 <findroot+0x110>
movsd  0x8(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
jmp    123c <findroot+0x11c>
nopl   0x0(%rax,%rax,1)
movsd  0x8(%rsp),%xmm1
movsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  %xmm1,0x18(%rsp)
call   1030 <midpoint@plt>
cmp    %ebx,%r15d
je     1286 <findroot+0x166>
test   %rbp,%rbp
je     11e0 <findroot+0xc0>
movaps 0x30(%rsp),%xmm1
movsd  %xmm1,0x28(%rsp)
jmp    11e0 <findroot+0xc0>
xor    %eax,%eax
jmp    1289 <findroot+0x169>
add    $0xffffffff,%ebx
cmp    %r15d,%ebx
setb   %al
test   %r14,%r14
movsd  0x8(%rsp),%xmm0
je     1289 <findroot+0x169>
movsd  %xmm0,(%r14)
jmp    1289 <findroot+0x169>
seta   %al
movzbl %al,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @findroot(double (double)* %0, double %1, double %2, double %3, i32 %4, double* %5) {
%7 = alloca i32
%8 = alloca double (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
%18 = alloca i32
%19 = alloca double
store double (double)* %0, double (double)** %8
store double %1, double* %9
store double %2, double* %10
store double %3, double* %11
store i32 %4, i32* %12
store double* %5, double** %13
%20 = load double (double)*, double (double)** %8
%21 = load double, double* %9
%22 = call double %20(double %21)
%23 = load double (double)*, double (double)** %8
%24 = load double, double* %10
%25 = call double %23(double %24)
%26 = call i64 @samesign(double %22, double %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %6
store i32 0, i32* %7
br label %75
29: ; preds = %6
%30 = load double, double* %9
store double %30, double* %14
%31 = load double, double* %10
store double %31, double* %15
%32 = load double, double* %14
%33 = load double, double* %15
%34 = call double @midpoint(double %32, double %33)
store double %34, double* %16
%35 = load double (double)*, double (double)** %8
%36 = load double, double* %14
%37 = call double %35(double %36)
store double %37, double* %17
store i32 0, i32* %18
br label %38
38: ; preds = %71, %29
%39 = load i32, i32* %18
%40 = load i32, i32* %12
%41 = icmp ult i32 %39, %40
br i1 %41, label %42, label %74
42: ; preds = %38
%43 = load double (double)*, double (double)** %8
%44 = load double, double* %16
%45 = call double %43(double %44)
store double %45, double* %19
%46 = load double, double* %19
%47 = call double @fabs(double %46)
%48 = load double, double* %11
%49 = fcmp olt double %47, %48
br i1 %49, label %50, label %57
50: ; preds = %42
%51 = load double*, double** %13
%52 = icmp ne double* %51, null
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load double, double* %16
%55 = load double*, double** %13
store double %54, double* %55
br label %56
56: ; preds = %53, %50
store i32 1, i32* %7
br label %75
57: ; preds = %42
%58 = load double, double* %19
%59 = load double, double* %17
%60 = call i64 @samesign(double %58, double %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load double, double* %16
store double %63, double* %14
%64 = load double, double* %19
store double %64, double* %17
br label %67
65: ; preds = %57
%66 = load double, double* %16
store double %66, double* %15
br label %67
67: ; preds = %65, %62
%68 = load double, double* %14
%69 = load double, double* %15
%70 = call double @midpoint(double %68, double %69)
store double %70, double* %16
br label %71
71: ; preds = %67
%72 = load i32, i32* %18
%73 = add i32 %72, 1
store i32 %73, i32* %18
br label %38
74: ; preds = %38
store i32 0, i32* %7
br label %75
75: ; preds = %74, %56, %28
%76 = load i32, i32* %7
ret i32 %76
}

LLVM IR (-O2):
define i32 @findroot(double (double)* nocapture %0, double %1, double %2, double %3, i32 %4, double* writeonly %5) {
%7 = tail call double %0(double %1)
%8 = tail call double %0(double %2)
%9 = tail call i64 @samesign(double %7, double %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %50
11: ; preds = %6
%12 = tail call double @midpoint(double %1, double %2)
%13 = tail call double %0(double %1)
%14 = icmp eq i32 %4, 0
br i1 %14, label %47, label %15
15: ; preds = %11
%16 = tail call double %0(double %12)
%17 = tail call double @llvm.fabs.f64(double %16)
%18 = fcmp olt double %17, %3
br i1 %18, label %26, label %31
19: ; preds = %31
%20 = select i1 %39, double %36, double %32
%21 = tail call double %0(double %42)
%22 = tail call double @llvm.fabs.f64(double %21)
%23 = fcmp olt double %22, %3
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = icmp ult i32 %43, %4
br label %26
26: ; preds = %24, %15
%27 = phi i1 [ true, %15 ], [ %25, %24 ]
%28 = phi double [ %12, %15 ], [ %42, %24 ]
%29 = icmp eq double* %5, null
br i1 %29, label %47, label %30
30: ; preds = %26
store double %28, double* %5
br label %47
31: ; preds = %15, %19
%32 = phi double [ %21, %19 ], [ %16, %15 ]
%33 = phi double [ %40, %19 ], [ %1, %15 ]
%34 = phi double [ %41, %19 ], [ %2, %15 ]
%35 = phi double [ %42, %19 ], [ %12, %15 ]
%36 = phi double [ %20, %19 ], [ %13, %15 ]
%37 = phi i32 [ %43, %19 ], [ 0, %15 ]
%38 = tail call i64 @samesign(double %32, double %36)
%39 = icmp eq i64 %38, 0
%40 = select i1 %39, double %33, double %35
%41 = select i1 %39, double %35, double %34
%42 = tail call double @midpoint(double %40, double %41)
%43 = add nuw i32 %37, 1
%44 = icmp eq i32 %43, %4
br i1 %44, label %45, label %19
45: ; preds = %31
%46 = icmp ult i32 %43, %4
br label %47
47: ; preds = %45, %11, %30, %26
%48 = phi i1 [ %27, %30 ], [ %27, %26 ], [ false, %11 ], [ %46, %45 ]
%49 = zext i1 %48 to i32
br label %50
50: ; preds = %6, %47
%51 = phi i32 [ %49, %47 ], [ 0, %6 ]
ret i32 %51
}