#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 calcAv(int, double);
double calcTGivenW(int, double);
int calcWinsizeGivenAv(int n, double desiredAv) {
  double N = n;
  if (n <= 0)
    return 0;
  if (desiredAv <= 0)
    return 0;
  int maxW = n + 1;
  int w = 1;
  for (; w < maxW; w++) {
    double T = calcTGivenW(n, (double)w);
    double Av = calcAv(n, T);
    if (Av < desiredAv)
      break;
  }
  if (w == maxW) {
    return 0;
  }
  return w;
}


x86 assembly (clang, -O0):
<calcWinsizeGivenAv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1150 <calcWinsizeGivenAv+0x30>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <calcWinsizeGivenAv+0xd4>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     116a <calcWinsizeGivenAv+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <calcWinsizeGivenAv+0xd4>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d6 <calcWinsizeGivenAv+0xb6>
mov    -0x8(%rbp),%edi
cvtsi2sdl -0x20(%rbp),%xmm0
call   1030 <calcTGivenW@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
movsd  -0x28(%rbp),%xmm0
call   1040 <calcAv@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x10(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11c3 <calcWinsizeGivenAv+0xa3>
jmp    11d6 <calcWinsizeGivenAv+0xb6>
jmp    11c8 <calcWinsizeGivenAv+0xa8>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    117a <calcWinsizeGivenAv+0x5a>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    11ee <calcWinsizeGivenAv+0xce>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <calcWinsizeGivenAv+0xd4>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calcWinsizeGivenAv>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
test   %edi,%edi
jle    119e <calcWinsizeGivenAv+0x7e>
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jae    119e <calcWinsizeGivenAv+0x7e>
mov    %edi,%ebx
lea    0x1(%rbx),%r14d
mov    %edi,%r15d
neg    %r15d
mov    $0x1,%ebp
movsd  %xmm1,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
mov    %ebx,%edi
call   1030 <calcTGivenW@plt>
mov    %ebx,%edi
call   1040 <calcAv@plt>
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
ja     1196 <calcWinsizeGivenAv+0x76>
lea    (%r15,%rbp,1),%eax
add    $0x1,%eax
mov    %ebp,%ecx
add    $0x1,%ecx
mov    %ecx,%ebp
cmp    $0x1,%eax
jne    1160 <calcWinsizeGivenAv+0x40>
mov    %r14d,%ebp
xor    %eax,%eax
cmp    %r14d,%ebp
cmovne %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @calcWinsizeGivenAv(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
store i32 %0, i32* %4
store double %1, double* %5
%11 = load i32, i32* %4
%12 = sitofp i32 %11 to double
store double %12, double* %6
%13 = load i32, i32* %4
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %50
16: ; preds = %2
%17 = load double, double* %5
%18 = fcmp ole double %17, 0.000000e+00
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 0, i32* %3
br label %50
20: ; preds = %16
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
store i32 1, i32* %8
br label %23
23: ; preds = %40, %20
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %43
27: ; preds = %23
%28 = load i32, i32* %4
%29 = load i32, i32* %8
%30 = sitofp i32 %29 to double
%31 = call double @calcTGivenW(i32 %28, double %30)
store double %31, double* %9
%32 = load i32, i32* %4
%33 = load double, double* %9
%34 = call double @calcAv(i32 %32, double %33)
store double %34, double* %10
%35 = load double, double* %10
%36 = load double, double* %5
%37 = fcmp olt double %35, %36
br i1 %37, label %38, label %39
38: ; preds = %27
br label %43
39: ; preds = %27
br label %40
40: ; preds = %39
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %23
43: ; preds = %38, %23
%44 = load i32, i32* %8
%45 = load i32, i32* %7
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 0, i32* %3
br label %50
48: ; preds = %43
%49 = load i32, i32* %8
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %47, %19, %15
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @calcWinsizeGivenAv(i32 %0, double %1) {
%3 = icmp sgt i32 %0, 0
%4 = fcmp ugt double %1, 0.000000e+00
%5 = and i1 %3, %4
br i1 %5, label %6, label %21
6: ; preds = %2
%7 = add nuw i32 %0, 1
br label %8
8: ; preds = %6, %14
%9 = phi i32 [ %15, %14 ], [ 1, %6 ]
%10 = sitofp i32 %9 to double
%11 = tail call double @calcTGivenW(i32 %0, double %10)
%12 = tail call double @calcAv(i32 %0, double %11)
%13 = fcmp olt double %12, %1
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = add nuw i32 %9, 1
%16 = icmp eq i32 %9, %0
br i1 %16, label %17, label %8
17: ; preds = %14, %8
%18 = phi i32 [ %7, %14 ], [ %9, %8 ]
%19 = icmp eq i32 %18, %7
%20 = select i1 %19, i32 0, i32 %18
br label %21
21: ; preds = %2, %17
%22 = phi i32 [ %20, %17 ], [ 0, %2 ]
ret i32 %22
}