#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 fun(double);
int /*<<< orphan*/ printf(char *, double);
double bisect(double a, double b, double err, int iter) {
  int i;
  double c, ea, ca;
  ea = 0;
  for (i = 0; i < iter; i++) {
    c = (a + b) / 2;
    printf("c:%lf\n", c);
    if (fun(a) * fun(c) < 0.0)
      b = c;
    else if (fun(a) * fun(c) > 0.0)
      a = c;
    ea = (c - ca) / 2;
    ea *= (ea > 0) ? 1 : -1;
    printf("err:%lf\n", ea);
    if (ea <= err)
      return c;
    ca = c;
  }
  return c;
}


x86 assembly (clang, -O0):
<bisect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
mov    %edi,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    128e <bisect+0x16e>
movsd  -0x10(%rbp),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  0xe99(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
lea    0xe8c(%rip),%rdi
call   1040 <printf@plt>
movsd  -0x10(%rbp),%xmm0
call   1030 <fun@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <fun@plt>
movsd  -0x48(%rbp),%xmm1
mulsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    11bf <bisect+0x9f>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1200 <bisect+0xe0>
movsd  -0x10(%rbp),%xmm0
call   1030 <fun@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <fun@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    11fb <bisect+0xdb>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1200 <bisect+0xe0>
movsd  -0x30(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
movsd  0xdee(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
xorps  %xmm1,%xmm1
mov    $0xffffffff,%eax
mov    $0x1,%ecx
ucomisd %xmm1,%xmm0
cmova  %ecx,%eax
cvtsi2sd %eax,%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
movsd  -0x38(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1276 <bisect+0x156>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1298 <bisect+0x178>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1149 <bisect+0x29>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bisect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
movsd  %xmm2,0x40(%rsp)
test   %edi,%edi
jle    1280 <bisect+0x160>
mov    %edi,%ebp
mov    $0x1,%ebx
lea    0xeda(%rip),%r14
lea    0xeda(%rip),%r15
nopl   (%rax)
movsd  %xmm0,0x10(%rsp)
movsd  %xmm1,0x38(%rsp)
addsd  %xmm0,%xmm1
mulsd  0xe98(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %r14,%rdi
movapd %xmm1,%xmm0
call   1040 <printf@plt>
movsd  0x10(%rsp),%xmm0
call   1030 <fun@plt>
movsd  %xmm0,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
call   1030 <fun@plt>
mulsd  0x20(%rsp),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    11c0 <bisect+0xa0>
movsd  0x8(%rsp),%xmm2
movsd  %xmm2,0x38(%rsp)
jmp    11f8 <bisect+0xd8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  0x10(%rsp),%xmm0
call   1030 <fun@plt>
movsd  %xmm0,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
call   1030 <fun@plt>
mulsd  0x20(%rsp),%xmm0
ucomisd 0xe1e(%rip),%xmm0
jbe    1200 <bisect+0xe0>
movsd  0x8(%rsp),%xmm2
movsd  %xmm2,0x10(%rsp)
movapd %xmm2,%xmm0
jmp    1206 <bisect+0xe6>
xchg   %ax,%ax
movsd  0x8(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
movapd %xmm0,%xmm2
subsd  %xmm1,%xmm2
mulsd  0xde4(%rip),%xmm2
movapd %xmm2,%xmm1
xorpd  0xde8(%rip),%xmm1
xorpd  %xmm0,%xmm0
cmpltsd %xmm2,%xmm0
andpd  %xmm0,%xmm2
andnpd %xmm1,%xmm0
orpd   %xmm2,%xmm0
movapd %xmm0,0x20(%rsp)
mov    %r15,%rdi
call   1040 <printf@plt>
movsd  0x40(%rsp),%xmm0
ucomisd 0x20(%rsp),%xmm0
jae    1282 <bisect+0x162>
lea    0x1(%rbx),%eax
movsd  0x8(%rsp),%xmm2
movsd  %xmm2,0x18(%rsp)
cmp    %ebp,%ebx
mov    %eax,%ebx
movsd  0x38(%rsp),%xmm1
movsd  0x10(%rsp),%xmm0
jl     1150 <bisect+0x30>
jmp    1288 <bisect+0x168>
jmp    1288 <bisect+0x168>
movsd  0x8(%rsp),%xmm2
movaps %xmm2,%xmm0
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"c:%lf\0A\00"
@.str.1 = [9 x i8] c"err:%lf\0A\00"
define double @bisect(double %0, double %1, double %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %37
6: ; preds = %4, %24
%7 = phi double [ %12, %24 ], [ undef, %4 ]
%8 = phi i32 [ %34, %24 ], [ 0, %4 ]
%9 = phi double [ %26, %24 ], [ %0, %4 ]
%10 = phi double [ %25, %24 ], [ %1, %4 ]
%11 = fadd double %10, %9
%12 = fmul double %11, 5.000000e-01
%13 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), double %12)
%14 = tail call double @fun(double %9)
%15 = tail call double @fun(double %12)
%16 = fmul double %14, %15
%17 = fcmp olt double %16, 0.000000e+00
br i1 %17, label %24, label %18
18: ; preds = %6
%19 = tail call double @fun(double %9)
%20 = tail call double @fun(double %12)
%21 = fmul double %19, %20
%22 = fcmp ogt double %21, 0.000000e+00
br i1 %22, label %23, label %24
23: ; preds = %18
br label %24
24: ; preds = %6, %18, %23
%25 = phi double [ %10, %23 ], [ %10, %18 ], [ %12, %6 ]
%26 = phi double [ %12, %23 ], [ %9, %18 ], [ %9, %6 ]
%27 = fsub double %12, %7
%28 = fmul double %27, 5.000000e-01
%29 = fcmp ogt double %28, 0.000000e+00
%30 = fneg double %28
%31 = select i1 %29, double %28, double %30
%32 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), double %31)
%33 = fcmp ugt double %31, %2
%34 = add nuw nsw i32 %8, 1
%35 = icmp slt i32 %34, %3
%36 = select i1 %33, i1 %35, i1 false
br i1 %36, label %6, label %37
37: ; preds = %24, %4
%38 = phi double [ undef, %4 ], [ %12, %24 ]
ret double %38
}