#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);
double find_rayleigh_peirce_limit(double const, double const, double const,
                                  int const);
double log(double const);
double peirce_func(int const, double const, double const, double const,
                   double const, int const);
double find_peirce_limit(const int rayleigh, const int N, const int n,
                         const int m) {
  const double a = (double)(N - n - m) / (double)n;
  const double b = 2. / (double)(N - n);
  const double part_1 = (double)n / (double)(N - n);
  const double part_2 = (double)(N - n) / (double)N;
  const double log_P = (double)n * log(part_1) + (double)N * log(part_2);
  if (rayleigh == 2)
    return (find_rayleigh_peirce_limit(a, b, log_P, n));
  else {
    double x1 = 0., y1 = peirce_func(rayleigh, x1, a, b, log_P, n);
    double x2 = 2., y2 = peirce_func(rayleigh, x2, a, b, log_P, n);
    const double thresh = 1e-8;
    while (y2 > 0.) {
      x1 = x2;
      y1 = y2;
      x2 += x2;
      y2 = peirce_func(rayleigh, x2, a, b, log_P, n);
    }
    while (fabs(x2 - x1) > thresh) {
      const double xnew = x2 + (x1 - x2) * y2 / (y2 - y1);
      x1 = x2;
      y1 = y2;
      x2 = xnew;
      y2 = peirce_func(rayleigh, xnew, a, b, log_P, n);
    }
    return (x2);
  }
}


x86 assembly (clang, -O0):
<find_peirce_limit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
cvtsi2sd %eax,%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
cvtsi2sd %eax,%xmm1
movsd  0xe7c(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
cvtsi2sd %eax,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
cvtsi2sd %eax,%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1060 <log@plt>
movsd  %xmm0,-0x80(%rbp)
cvtsi2sdl -0x10(%rbp),%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x38(%rbp),%xmm0
call   1060 <log@plt>
movsd  -0x88(%rbp),%xmm1
ff
movsd  -0x80(%rbp),%xmm2
movaps %xmm0,%xmm3
movsd  -0x78(%rbp),%xmm0
mulsd  %xmm3,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
cmpl   $0x2,-0xc(%rbp)
jne    123e <find_peirce_limit+0xfe>
movsd  -0x20(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm2
mov    -0x14(%rbp),%edi
call   1030 <find_rayleigh_peirce_limit@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    13a8 <find_peirce_limit+0x268>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0xc(%rbp),%edi
movsd  -0x48(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm3
mov    -0x14(%rbp),%esi
call   1040 <peirce_func@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  0xd8e(%rip),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0xc(%rbp),%edi
movsd  -0x58(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm3
mov    -0x14(%rbp),%esi
call   1040 <peirce_func@plt>
movsd  %xmm0,-0x60(%rbp)
movsd  0xd65(%rip),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x60(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1306 <find_peirce_limit+0x1c6>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x58(%rbp),%xmm0
addsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0xc(%rbp),%edi
movsd  -0x58(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm3
mov    -0x14(%rbp),%esi
call   1040 <peirce_func@plt>
movsd  %xmm0,-0x60(%rbp)
jmp    12a8 <find_peirce_limit+0x168>
jmp    130b <find_peirce_limit+0x1cb>
movsd  -0x58(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
call   1050 <fabs@plt>
movsd  0xce6(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    139e <find_peirce_limit+0x25e>
movsd  -0x58(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
subsd  -0x58(%rbp),%xmm1
mulsd  -0x60(%rbp),%xmm1
movsd  -0x60(%rbp),%xmm2
subsd  -0x50(%rbp),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0xc(%rbp),%edi
movsd  -0x70(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm3
mov    -0x14(%rbp),%esi
call   1040 <peirce_func@plt>
movsd  %xmm0,-0x60(%rbp)
jmp    130b <find_peirce_limit+0x1cb>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_peirce_limit>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
cvtsi2sd %esi,%xmm1
movsd  %xmm1,0x18(%rsp)
mov    %edx,%ebx
sub    %edx,%esi
cvtsi2sd %esi,%xmm3
sub    %ecx,%esi
cvtsi2sd %esi,%xmm0
mov    %edi,%ebp
cvtsi2sd %edx,%xmm2
movsd  %xmm2,0x8(%rsp)
divsd  %xmm2,%xmm0
movsd  %xmm0,0x28(%rsp)
movsd  0xe94(%rip),%xmm0
divsd  %xmm3,%xmm0
movsd  %xmm0,0x20(%rsp)
movapd %xmm2,%xmm0
divsd  %xmm3,%xmm0
divsd  %xmm1,%xmm3
movsd  %xmm3,0x10(%rsp)
call   1050 <log@plt>
movsd  %xmm0,0x30(%rsp)
movsd  0x10(%rsp),%xmm0
call   1050 <log@plt>
movsd  0x30(%rsp),%xmm3
mulsd  0x18(%rsp),%xmm0
mulsd  0x8(%rsp),%xmm3
addsd  %xmm0,%xmm3
cmp    $0x2,%ebp
jne    11d6 <find_peirce_limit+0xa6>
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
movapd %xmm3,%xmm2
mov    %ebx,%edi
add    $0x38,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <find_rayleigh_peirce_limit@plt>
xorpd  %xmm0,%xmm0
mov    %ebp,%edi
movsd  0x28(%rsp),%xmm1
movsd  0x20(%rsp),%xmm2
movsd  %xmm3,0x30(%rsp)
mov    %ebx,%esi
call   1040 <peirce_func@plt>
movsd  %xmm0,0x8(%rsp)
mov    %ebp,%edi
movsd  0xdfb(%rip),%xmm0
movsd  0x28(%rsp),%xmm1
movsd  0x20(%rsp),%xmm2
movsd  0x30(%rsp),%xmm3
mov    %ebx,%esi
call   1040 <peirce_func@plt>
xorpd  %xmm3,%xmm3
ucomisd %xmm3,%xmm0
jbe    129b <find_peirce_limit+0x16b>
movsd  0xdd0(%rip),%xmm5
movapd %xmm0,%xmm1
movapd %xmm5,%xmm2
nopl   0x0(%rax,%rax,1)
movsd  %xmm5,0x10(%rsp)
movsd  %xmm1,0x18(%rsp)
addsd  %xmm2,%xmm2
movsd  %xmm2,0x8(%rsp)
mov    %ebp,%edi
movapd %xmm2,%xmm0
movsd  0x28(%rsp),%xmm1
movsd  0x20(%rsp),%xmm2
movsd  0x30(%rsp),%xmm3
mov    %ebx,%esi
call   1040 <peirce_func@plt>
movsd  0x8(%rsp),%xmm2
ucomisd 0xd8d(%rip),%xmm0
movsd  0x10(%rsp),%xmm3
movsd  0x18(%rsp),%xmm4
movapd %xmm0,%xmm1
movapd %xmm2,%xmm5
ja     1240 <find_peirce_limit+0x110>
jmp    12a9 <find_peirce_limit+0x179>
movsd  0xd5d(%rip),%xmm2
movsd  0x8(%rsp),%xmm4
movapd %xmm2,%xmm1
subsd  %xmm3,%xmm1
andpd  0xd67(%rip),%xmm1
ucomisd 0xd47(%rip),%xmm1
jbe    1341 <find_peirce_limit+0x211>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm2,0x10(%rsp)
movsd  %xmm0,0x18(%rsp)
subsd  %xmm2,%xmm3
mulsd  %xmm0,%xmm3
subsd  %xmm4,%xmm0
divsd  %xmm0,%xmm3
movapd %xmm3,%xmm0
addsd  %xmm2,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %ebp,%edi
movsd  0x28(%rsp),%xmm1
movsd  0x20(%rsp),%xmm2
movsd  0x30(%rsp),%xmm3
mov    %ebx,%esi
call   1040 <peirce_func@plt>
movsd  0x8(%rsp),%xmm2
movapd %xmm2,%xmm1
movsd  0x10(%rsp),%xmm3
subsd  %xmm3,%xmm1
andpd  0xcef(%rip),%xmm1
ucomisd 0xccf(%rip),%xmm1
movsd  0x18(%rsp),%xmm4
ja     12d0 <find_peirce_limit+0x1a0>
movapd %xmm2,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define double @find_peirce_limit(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca double
%20 = alloca double
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = sub nsw i32 %21, %22
%24 = load i32, i32* %9
%25 = sub nsw i32 %23, %24
%26 = sitofp i32 %25 to double
%27 = load i32, i32* %8
%28 = sitofp i32 %27 to double
%29 = fdiv double %26, %28
store double %29, double* %10
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = sub nsw i32 %30, %31
%33 = sitofp i32 %32 to double
%34 = fdiv double 2.000000e+00, %33
store double %34, double* %11
%35 = load i32, i32* %8
%36 = sitofp i32 %35 to double
%37 = load i32, i32* %7
%38 = load i32, i32* %8
%39 = sub nsw i32 %37, %38
%40 = sitofp i32 %39 to double
%41 = fdiv double %36, %40
store double %41, double* %12
%42 = load i32, i32* %7
%43 = load i32, i32* %8
%44 = sub nsw i32 %42, %43
%45 = sitofp i32 %44 to double
%46 = load i32, i32* %7
%47 = sitofp i32 %46 to double
%48 = fdiv double %45, %47
store double %48, double* %13
%49 = load i32, i32* %8
%50 = sitofp i32 %49 to double
%51 = load double, double* %12
%52 = call double @log(double %51)
%53 = load i32, i32* %7
%54 = sitofp i32 %53 to double
%55 = load double, double* %13
%56 = call double @log(double %55)
%57 = fmul double %54, %56
%58 = call double @llvm.fmuladd.f64(double %50, double %52, double %57)
store double %58, double* %14
%59 = load i32, i32* %6
%60 = icmp eq i32 %59, 2
br i1 %60, label %61, label %67
61: ; preds = %4
%62 = load double, double* %10
%63 = load double, double* %11
%64 = load double, double* %14
%65 = load i32, i32* %8
%66 = call double @find_rayleigh_peirce_limit(double %62, double %63, double %64, i32 %65)
store double %66, double* %5
br label %129
67: ; preds = %4
store double 0.000000e+00, double* %15
%68 = load i32, i32* %6
%69 = load double, double* %15
%70 = load double, double* %10
%71 = load double, double* %11
%72 = load double, double* %14
%73 = load i32, i32* %8
%74 = call double @peirce_func(i32 %68, double %69, double %70, double %71, double %72, i32 %73)
store double %74, double* %16
store double 2.000000e+00, double* %17
%75 = load i32, i32* %6
%76 = load double, double* %17
%77 = load double, double* %10
%78 = load double, double* %11
%79 = load double, double* %14
%80 = load i32, i32* %8
%81 = call double @peirce_func(i32 %75, double %76, double %77, double %78, double %79, i32 %80)
store double %81, double* %18
store double 1.000000e-08, double* %19
br label %82
82: ; preds = %85, %67
%83 = load double, double* %18
%84 = fcmp ogt double %83, 0.000000e+00
br i1 %84, label %85, label %98
85: ; preds = %82
%86 = load double, double* %17
store double %86, double* %15
%87 = load double, double* %18
store double %87, double* %16
%88 = load double, double* %17
%89 = load double, double* %17
%90 = fadd double %89, %88
store double %90, double* %17
%91 = load i32, i32* %6
%92 = load double, double* %17
%93 = load double, double* %10
%94 = load double, double* %11
%95 = load double, double* %14
%96 = load i32, i32* %8
%97 = call double @peirce_func(i32 %91, double %92, double %93, double %94, double %95, i32 %96)
store double %97, double* %18
br label %82
98: ; preds = %82
br label %99
99: ; preds = %105, %98
%100 = load double, double* %17
%101 = load double, double* %15
%102 = fsub double %100, %101
%103 = call double @fabs(double %102)
%104 = fcmp ogt double %103, 1.000000e-08
br i1 %104, label %105, label %127
105: ; preds = %99
%106 = load double, double* %17
%107 = load double, double* %15
%108 = load double, double* %17
%109 = fsub double %107, %108
%110 = load double, double* %18
%111 = fmul double %109, %110
%112 = load double, double* %18
%113 = load double, double* %16
%114 = fsub double %112, %113
%115 = fdiv double %111, %114
%116 = fadd double %106, %115
store double %116, double* %20
%117 = load double, double* %17
store double %117, double* %15
%118 = load double, double* %18
store double %118, double* %16
%119 = load double, double* %20
store double %119, double* %17
%120 = load i32, i32* %6
%121 = load double, double* %20
%122 = load double, double* %10
%123 = load double, double* %11
%124 = load double, double* %14
%125 = load i32, i32* %8
%126 = call double @peirce_func(i32 %120, double %121, double %122, double %123, double %124, i32 %125)
store double %126, double* %18
br label %99
127: ; preds = %99
%128 = load double, double* %17
store double %128, double* %5
br label %129
129: ; preds = %127, %61
%130 = load double, double* %5
ret double %130
}

LLVM IR (-O2):
define double @find_peirce_limit(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = sub nsw i32 %1, %2
%6 = sub nsw i32 %5, %3
%7 = sitofp i32 %6 to double
%8 = sitofp i32 %2 to double
%9 = fdiv double %7, %8
%10 = sitofp i32 %5 to double
%11 = fdiv double 2.000000e+00, %10
%12 = fdiv double %8, %10
%13 = sitofp i32 %1 to double
%14 = fdiv double %10, %13
%15 = tail call double @log(double %12)
%16 = tail call double @log(double %14)
%17 = fmul double %16, %13
%18 = tail call double @llvm.fmuladd.f64(double %8, double %15, double %17)
%19 = icmp eq i32 %0, 2
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = tail call double @find_rayleigh_peirce_limit(double %9, double %11, double %18, i32 %2)
br label %54
22: ; preds = %4
%23 = tail call double @peirce_func(i32 %0, double 0.000000e+00, double %9, double %11, double %18, i32 %2)
%24 = tail call double @peirce_func(i32 %0, double 2.000000e+00, double %9, double %11, double %18, i32 %2)
%25 = fcmp ogt double %24, 0.000000e+00
br i1 %25, label %34, label %26
26: ; preds = %34, %22
%27 = phi double [ 0.000000e+00, %22 ], [ %36, %34 ]
%28 = phi double [ %23, %22 ], [ %35, %34 ]
%29 = phi double [ 2.000000e+00, %22 ], [ %37, %34 ]
%30 = phi double [ %24, %22 ], [ %38, %34 ]
%31 = fsub double %29, %27
%32 = tail call double @llvm.fabs.f64(double %31)
%33 = fcmp ogt double %32, 1.000000e-08
br i1 %33, label %40, label %54
34: ; preds = %22, %34
%35 = phi double [ %38, %34 ], [ %24, %22 ]
%36 = phi double [ %37, %34 ], [ 2.000000e+00, %22 ]
%37 = fadd double %36, %36
%38 = tail call double @peirce_func(i32 %0, double %37, double %9, double %11, double %18, i32 %2)
%39 = fcmp ogt double %38, 0.000000e+00
br i1 %39, label %34, label %26
40: ; preds = %26, %40
%41 = phi double [ %50, %40 ], [ %30, %26 ]
%42 = phi double [ %49, %40 ], [ %29, %26 ]
%43 = phi double [ %41, %40 ], [ %28, %26 ]
%44 = phi double [ %42, %40 ], [ %27, %26 ]
%45 = fsub double %44, %42
%46 = fmul double %45, %41
%47 = fsub double %41, %43
%48 = fdiv double %46, %47
%49 = fadd double %42, %48
%50 = tail call double @peirce_func(i32 %0, double %49, double %9, double %11, double %18, i32 %2)
%51 = fsub double %49, %42
%52 = tail call double @llvm.fabs.f64(double %51)
%53 = fcmp ogt double %52, 1.000000e-08
br i1 %53, label %40, label %54
54: ; preds = %40, %26, %20
%55 = phi double [ %21, %20 ], [ %29, %26 ], [ %49, %40 ]
ret double %55
}