#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 */
int pow(int, unsigned int);
int tc_red_eval_ewma(unsigned qmin, unsigned burst, unsigned avpkt) {
  int wlog = 1;
  double W = 0.5;
  double a = (double)burst + 1 - (double)qmin / avpkt;
  if (a < 1.0)
    return -1;
  for (wlog = 1; wlog < 32; wlog++, W /= 2) {
    if (a <= (1 - pow(1 - W, burst)) / W)
      return wlog;
  }
  return -1;
}


x86 assembly (clang, -O0):
<tc_red_eval_ewma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movabs $0x3fe0000000000000,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  0xeba(%rip),%xmm1
addsd  %xmm1,%xmm0
mov    -0x8(%rbp),%eax
cvtsi2sd %rax,%xmm1
mov    -0x10(%rbp),%eax
cvtsi2sd %rax,%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe91(%rip),%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    1186 <tc_red_eval_ewma+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <tc_red_eval_ewma+0x106>
movl   $0x1,-0x14(%rbp)
cmpl   $0x20,-0x14(%rbp)
jge    120f <tc_red_eval_ewma+0xff>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe57(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
cvttsd2si %xmm0,%edi
mov    -0xc(%rbp),%esi
call   1030 <pow@plt>
movsd  -0x30(%rbp),%xmm1
mov    %eax,%ecx
mov    $0x1,%eax
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
divsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     11e6 <tc_red_eval_ewma+0xd6>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <tc_red_eval_ewma+0x106>
jmp    11eb <tc_red_eval_ewma+0xdb>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe07(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    118d <tc_red_eval_ewma+0x7d>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tc_red_eval_ewma>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%eax
cvtsi2sd %rax,%xmm2
movsd  0xed9(%rip),%xmm3
addsd  %xmm3,%xmm2
mov    %edi,%eax
cvtsi2sd %rax,%xmm0
mov    %edx,%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
subsd  %xmm0,%xmm2
mov    $0xffffffff,%r14d
ucomisd %xmm2,%xmm3
ja     120d <tc_red_eval_ewma+0xfd>
mov    %esi,%ebx
movsd  %xmm2,0x8(%rsp)
movsd  0xea7(%rip),%xmm1
mov    $0x2,%ebp
cs nopw 0x0(%rax,%rax,1)
movsd  %xmm1,(%rsp)
movapd %xmm3,%xmm0
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
mov    %ebx,%esi
call   1030 <pow@plt>
movsd  (%rsp),%xmm1
mov    $0x1,%ecx
sub    %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
divsd  %xmm1,%xmm0
ucomisd 0x8(%rsp),%xmm0
jae    1207 <tc_red_eval_ewma+0xf7>
mulsd  0xe59(%rip),%xmm1
cmp    $0x20,%ebp
je     120d <tc_red_eval_ewma+0xfd>
movsd  0xe44(%rip),%xmm0
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
mov    %ebx,%esi
movsd  %xmm1,(%rsp)
call   1030 <pow@plt>
movsd  (%rsp),%xmm1
mov    $0x1,%ecx
sub    %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
divsd  %xmm1,%xmm0
ucomisd 0x8(%rsp),%xmm0
jae    120a <tc_red_eval_ewma+0xfa>
mulsd  0xe11(%rip),%xmm1
add    $0x2,%ebp
movsd  0xdfe(%rip),%xmm3
jmp    1170 <tc_red_eval_ewma+0x60>
add    $0xffffffff,%ebp
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @tc_red_eval_ewma(i32 %0, i32 %1, i32 %2) {
%4 = uitofp i32 %1 to double
%5 = fadd double %4, 1.000000e+00
%6 = uitofp i32 %0 to double
%7 = uitofp i32 %2 to double
%8 = fdiv double %6, %7
%9 = fsub double %5, %8
%10 = fcmp olt double %9, 1.000000e+00
br i1 %10, label %36, label %11
11: ; preds = %3, %33
%12 = phi double [ %35, %33 ], [ 5.000000e-01, %3 ]
%13 = phi i32 [ %34, %33 ], [ 1, %3 ]
%14 = fsub double 1.000000e+00, %12
%15 = fptosi double %14 to i32
%16 = tail call i32 @pow(i32 %15, i32 %1)
%17 = sub nsw i32 1, %16
%18 = sitofp i32 %17 to double
%19 = fdiv double %18, %12
%20 = fcmp ugt double %9, %19
br i1 %20, label %21, label %36
21: ; preds = %11
%22 = add nuw nsw i32 %13, 1
%23 = fmul double %12, 5.000000e-01
%24 = icmp eq i32 %22, 32
br i1 %24, label %36, label %25
25: ; preds = %21
%26 = fsub double 1.000000e+00, %23
%27 = fptosi double %26 to i32
%28 = tail call i32 @pow(i32 %27, i32 %1)
%29 = sub nsw i32 1, %28
%30 = sitofp i32 %29 to double
%31 = fdiv double %30, %23
%32 = fcmp ugt double %9, %31
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = add nuw nsw i32 %13, 2
%35 = fmul double %23, 5.000000e-01
br label %11
36: ; preds = %21, %11, %25, %3
%37 = phi i32 [ -1, %3 ], [ -1, %21 ], [ %13, %11 ], [ %22, %25 ]
ret i32 %37
}