#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 fabs(double);
double pow(int, int);
int /*<<< orphan*/ printf(char *, ...);
int binsearch_root(double a, double b, double (*f)(double), int *steps,
                   double *root) {
  double c;
  printf("Step\t|f(x)|\n");
  *steps = 0;
  while (++*steps < 1000) {
    c = (a + b) / 2;
    *root = f(c);
    printf("%d\t%.17g\n", *steps, *root);
    if ((fabs(*root - 0) < pow(10, -7)) || (b - a) / 2 < pow(10, -7)) {
      return 1;
    }
    if (((c > 0) - (c < 0)) == ((a > 0) - (a < 0))) {
      a = c;
    } else {
      b = c;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<binsearch_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x28(%rbp)
mov    %rdx,-0x30(%rbp)
lea    0xed3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rcx
mov    (%rcx),%eax
add    $0x1,%eax
mov    %eax,(%rcx)
cmp    $0x3e8,%eax
jge    12ad <binsearch_root+0x18d>
movsd  -0x10(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  0xe92(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movsd  -0x38(%rbp),%xmm0
call   *%rax
mov    -0x30(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
movsd  (%rax),%xmm0
lea    0xe76(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x30(%rbp),%rax
movsd  (%rax),%xmm0
xorps  %xmm1,%xmm1
subsd  %xmm1,%xmm0
movaps 0xe34(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    $0xa,%edi
mov    $0xfffffff9,%esi
call   1040 <pow@plt>
movsd  -0x40(%rbp),%xmm1
ucomisd %xmm1,%xmm0
ja     122c <binsearch_root+0x10c>
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  0xe0b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0xa,%edi
mov    $0xfffffff9,%esi
call   1040 <pow@plt>
movsd  -0x48(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jbe    1238 <binsearch_root+0x118>
movl   $0x1,-0x4(%rbp)
jmp    12b4 <binsearch_root+0x194>
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%eax
xorps  %xmm0,%xmm0
ucomisd -0x38(%rbp),%xmm0
seta   %cl
and    $0x1,%cl
movzbl %cl,%ecx
sub    %ecx,%eax
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %cl
and    $0x1,%cl
movzbl %cl,%ecx
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
seta   %dl
and    $0x1,%dl
movzbl %dl,%edx
sub    %edx,%ecx
cmp    %ecx,%eax
jne    129e <binsearch_root+0x17e>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    12a8 <binsearch_root+0x188>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1156 <binsearch_root+0x36>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binsearch_root>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
movsd  %xmm1,(%rsp)
movsd  %xmm0,0x8(%rsp)
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
movsd  0x8(%rsp),%xmm3
movsd  (%rsp),%xmm0
movl   $0x1,(%rbx)
movsd  0xe8b(%rip),%xmm2
lea    0xea4(%rip),%r12
mov    $0x1,%ebp
jmp    11b2 <binsearch_root+0x82>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movapd %xmm2,%xmm3
movapd %xmm1,%xmm0
movsd  0xe60(%rip),%xmm2
mov    (%rbx),%eax
lea    0x1(%rax),%ecx
mov    %ecx,(%rbx)
cmp    $0x3e7,%eax
jge    128b <binsearch_root+0x15b>
movsd  %xmm0,(%rsp)
movsd  %xmm3,0x8(%rsp)
addsd  %xmm3,%xmm0
mulsd  %xmm2,%xmm0
movsd  %xmm0,0x28(%rsp)
call   *%r15
movsd  %xmm0,(%r14)
mov    (%rbx),%esi
mov    %r12,%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movsd  (%r14),%xmm0
andpd  0xe24(%rip),%xmm0
movapd %xmm0,0x10(%rsp)
mov    $0xa,%edi
mov    $0xfffffff9,%esi
call   1050 <pow@plt>
ucomisd 0x10(%rsp),%xmm0
ja     128d <binsearch_root+0x15d>
movsd  (%rsp),%xmm0
subsd  0x8(%rsp),%xmm0
mulsd  0xde0(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    $0xa,%edi
mov    $0xfffffff9,%esi
call   1050 <pow@plt>
ucomisd 0x10(%rsp),%xmm0
ja     128d <binsearch_root+0x15d>
xor    %eax,%eax
xorpd  %xmm0,%xmm0
movsd  0x28(%rsp),%xmm1
ucomisd %xmm0,%xmm1
seta   %al
xor    %ecx,%ecx
ucomisd %xmm1,%xmm0
seta   %cl
sub    %ecx,%eax
xor    %ecx,%ecx
movsd  0x8(%rsp),%xmm2
ucomisd %xmm0,%xmm2
seta   %cl
xor    %edx,%edx
ucomisd %xmm2,%xmm0
seta   %dl
sub    %edx,%ecx
cmp    %ecx,%eax
jne    1190 <binsearch_root+0x60>
movapd %xmm1,%xmm3
movsd  (%rsp),%xmm0
jmp    1198 <binsearch_root+0x68>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Step\09|f(x)|\0A\00"
@.str.1 = [10 x i8] c"%d\09%.17g\0A\00"
define i32 @binsearch_root(double %0, double %1, double (double)* %2, i32* %3, double* %4) {
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double (double)*
%10 = alloca i32*
%11 = alloca double*
%12 = alloca double
store double %0, double* %7
store double %1, double* %8
store double (double)* %2, double (double)** %9
store i32* %3, i32** %10
store double* %4, double** %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = load i32*, i32** %10
store i32 0, i32* %14
br label %15
15: ; preds = %68, %5
%16 = load i32*, i32** %10
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
store i32 %18, i32* %16
%19 = icmp slt i32 %18, 1000
br i1 %19, label %20, label %69
20: ; preds = %15
%21 = load double, double* %7
%22 = load double, double* %8
%23 = fadd double %21, %22
%24 = fdiv double %23, 2.000000e+00
store double %24, double* %12
%25 = load double (double)*, double (double)** %9
%26 = load double, double* %12
%27 = call double %25(double %26)
%28 = load double*, double** %11
store double %27, double* %28
%29 = load i32*, i32** %10
%30 = load i32, i32* %29
%31 = load double*, double** %11
%32 = load double, double* %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %30, double %32)
%34 = load double*, double** %11
%35 = load double, double* %34
%36 = fsub double %35, 0.000000e+00
%37 = call double @llvm.fabs.f64(double %36)
%38 = call double @pow(i32 10, i32 -7)
%39 = fcmp olt double %37, %38
br i1 %39, label %47, label %40
40: ; preds = %20
%41 = load double, double* %8
%42 = load double, double* %7
%43 = fsub double %41, %42
%44 = fdiv double %43, 2.000000e+00
%45 = call double @pow(i32 10, i32 -7)
%46 = fcmp olt double %44, %45
br i1 %46, label %47, label %48
47: ; preds = %40, %20
store i32 1, i32* %6
br label %70
48: ; preds = %40
%49 = load double, double* %12
%50 = fcmp ogt double %49, 0.000000e+00
%51 = zext i1 %50 to i32
%52 = load double, double* %12
%53 = fcmp olt double %52, 0.000000e+00
%54 = zext i1 %53 to i32
%55 = sub nsw i32 %51, %54
%56 = load double, double* %7
%57 = fcmp ogt double %56, 0.000000e+00
%58 = zext i1 %57 to i32
%59 = load double, double* %7
%60 = fcmp olt double %59, 0.000000e+00
%61 = zext i1 %60 to i32
%62 = sub nsw i32 %58, %61
%63 = icmp eq i32 %55, %62
br i1 %63, label %64, label %66
64: ; preds = %48
%65 = load double, double* %12
store double %65, double* %7
br label %68
66: ; preds = %48
%67 = load double, double* %12
store double %67, double* %8
br label %68
68: ; preds = %66, %64
br label %15
69: ; preds = %15
store i32 0, i32* %6
br label %70
70: ; preds = %69, %47
%71 = load i32, i32* %6
ret i32 %71
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"%d\09%.17g\0A\00"
@str = [12 x i8] c"Step\09|f(x)|\00"
define i32 @binsearch_root(double %0, double %1, double (double)* nocapture %2, i32* nocapture %3, double* nocapture %4) {
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
store i32 1, i32* %3
br label %7
7: ; preds = %5, %24
%8 = phi double [ %0, %5 ], [ %37, %24 ]
%9 = phi double [ %1, %5 ], [ %36, %24 ]
%10 = fadd double %9, %8
%11 = fmul double %10, 5.000000e-01
%12 = tail call double %2(double %11)
store double %12, double* %4
%13 = load i32, i32* %3
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %13, double %12)
%15 = load double, double* %4
%16 = tail call double @llvm.fabs.f64(double %15)
%17 = tail call double @pow(i32 10, i32 -7)
%18 = fcmp olt double %16, %17
br i1 %18, label %41, label %19
19: ; preds = %7
%20 = fsub double %9, %8
%21 = fmul double %20, 5.000000e-01
%22 = tail call double @pow(i32 10, i32 -7)
%23 = fcmp olt double %21, %22
br i1 %23, label %41, label %24
24: ; preds = %19
%25 = fcmp ogt double %11, 0.000000e+00
%26 = zext i1 %25 to i32
%27 = fcmp olt double %11, 0.000000e+00
%28 = sext i1 %27 to i32
%29 = add nsw i32 %28, %26
%30 = fcmp ogt double %8, 0.000000e+00
%31 = zext i1 %30 to i32
%32 = fcmp olt double %8, 0.000000e+00
%33 = sext i1 %32 to i32
%34 = add nsw i32 %33, %31
%35 = icmp eq i32 %29, %34
%36 = select i1 %35, double %9, double %11
%37 = select i1 %35, double %11, double %8
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
%40 = icmp slt i32 %38, 999
br i1 %40, label %7, label %41
41: ; preds = %24, %7, %19
%42 = phi i32 [ 1, %19 ], [ 1, %7 ], [ 0, %24 ]
ret i32 %42
}