#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 p_to_b(double, double, double, int, int);
double b_to_p(double b, double rtt, double tzero, int psize, int bval) {
  double p, pi, bres;
  int ctr = 0;
  p = 0.5;
  pi = 0.25;
  while (1) {
    bres = p_to_b(p, rtt, tzero, psize, bval);
    if ((bres > 0.95 * b) && (bres < 1.05 * b))
      return p;
    if (bres > b) {
      p += pi;
    } else {
      p -= pi;
    }
    pi /= 2.0;
    ctr++;
    if (ctr > 30) {
      return p;
    }
  }
}


x86 assembly (clang, -O0):
<b_to_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
mov    %edi,-0x24(%rbp)
mov    %esi,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
movsd  0xecc(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xeb7(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <p_to_b@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xe96(%rip),%xmm1
mulsd  -0x10(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jbe    11b4 <b_to_p+0xa4>
movsd  -0x40(%rbp),%xmm1
movsd  0xe82(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11b4 <b_to_p+0xa4>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1228 <b_to_p+0x118>
movsd  -0x40(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11d8 <b_to_p+0xc8>
movsd  -0x38(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11eb <b_to_p+0xdb>
movsd  -0x38(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  0xe28(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x1e,-0x44(%rbp)
jle    1223 <b_to_p+0x113>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1228 <b_to_p+0x118>
jmp    114e <b_to_p+0x3e>
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b_to_p>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
movsd  %xmm2,0x28(%rsp)
movsd  %xmm1,0x20(%rsp)
movsd  0xecf(%rip),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x18(%rsp)
movsd  0xec5(%rip),%xmm1
movsd  %xmm0,0x10(%rsp)
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0xeb5(%rip),%xmm3
movsd  0xeb5(%rip),%xmm0
mov    $0x1f,%ebx
jmp    11b3 <b_to_p+0xa3>
nopw   0x0(%rax,%rax,1)
movapd 0x30(%rsp),%xmm3
movapd %xmm3,%xmm1
xorpd  0xe9e(%rip),%xmm1
movsd  0x10(%rsp),%xmm2
cmpltsd %xmm0,%xmm2
movapd %xmm2,%xmm0
andpd  %xmm3,%xmm0
andnpd %xmm1,%xmm2
orpd   %xmm0,%xmm2
movsd  (%rsp),%xmm0
addsd  %xmm2,%xmm0
mulsd  0xe6a(%rip),%xmm3
add    $0xffffffff,%ebx
je     11ed <b_to_p+0xdd>
movapd %xmm3,0x30(%rsp)
movsd  %xmm0,(%rsp)
movsd  0x20(%rsp),%xmm1
movsd  0x28(%rsp),%xmm2
mov    %ebp,%edi
mov    %r14d,%esi
call   1030 <p_to_b@plt>
ucomisd 0x18(%rsp),%xmm0
jbe    1170 <b_to_p+0x60>
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    1170 <b_to_p+0x60>
movsd  (%rsp),%xmm0
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @b_to_p(double %0, double %1, double %2, i32 %3, i32 %4) {
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca i32
store double %0, double* %7
store double %1, double* %8
store double %2, double* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 0, i32* %15
store double 5.000000e-01, double* %12
store double 2.500000e-01, double* %13
br label %16
16: ; preds = %5, %55
%17 = load double, double* %12
%18 = load double, double* %8
%19 = load double, double* %9
%20 = load i32, i32* %10
%21 = load i32, i32* %11
%22 = call double @p_to_b(double %17, double %18, double %19, i32 %20, i32 %21)
store double %22, double* %14
%23 = load double, double* %14
%24 = load double, double* %7
%25 = fmul double 0x3FEE666666666666, %24
%26 = fcmp ogt double %23, %25
br i1 %26, label %27, label %34
27: ; preds = %16
%28 = load double, double* %14
%29 = load double, double* %7
%30 = fmul double 1.050000e+00, %29
%31 = fcmp olt double %28, %30
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = load double, double* %12
store double %33, double* %6
br label %56
34: ; preds = %27, %16
%35 = load double, double* %14
%36 = load double, double* %7
%37 = fcmp ogt double %35, %36
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load double, double* %13
%40 = load double, double* %12
%41 = fadd double %40, %39
store double %41, double* %12
br label %46
42: ; preds = %34
%43 = load double, double* %13
%44 = load double, double* %12
%45 = fsub double %44, %43
store double %45, double* %12
br label %46
46: ; preds = %42, %38
%47 = load double, double* %13
%48 = fdiv double %47, 2.000000e+00
store double %48, double* %13
%49 = load i32, i32* %15
%50 = add nsw i32 %49, 1
store i32 %50, i32* %15
%51 = load i32, i32* %15
%52 = icmp sgt i32 %51, 30
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = load double, double* %12
store double %54, double* %6
br label %56
55: ; preds = %46
br label %16
56: ; preds = %53, %32
%57 = load double, double* %6
ret double %57
}

LLVM IR (-O2):
define double @b_to_p(double %0, double %1, double %2, i32 %3, i32 %4) {
%6 = fmul double %0, 0x3FEE666666666666
%7 = fmul double %0, 1.050000e+00
br label %8
8: ; preds = %16, %5
%9 = phi double [ 5.000000e-01, %5 ], [ %20, %16 ]
%10 = phi double [ 2.500000e-01, %5 ], [ %21, %16 ]
%11 = phi i32 [ 0, %5 ], [ %22, %16 ]
%12 = tail call double @p_to_b(double %9, double %1, double %2, i32 %3, i32 %4)
%13 = fcmp ogt double %12, %6
%14 = fcmp olt double %12, %7
%15 = and i1 %13, %14
br i1 %15, label %24, label %16
16: ; preds = %8
%17 = fcmp ogt double %12, %0
%18 = fneg double %10
%19 = select i1 %17, double %10, double %18
%20 = fadd double %9, %19
%21 = fmul double %10, 5.000000e-01
%22 = add nuw nsw i32 %11, 1
%23 = icmp eq i32 %22, 31
br i1 %23, label %24, label %8
24: ; preds = %16, %8
%25 = phi double [ %9, %8 ], [ %20, %16 ]
ret double %25
}