double percentile_index(float percent, int n) {
  double p;
  if (n < 1)
    return -1;
  p = percent * 0.01;
  if (p < 0.0 || p > 1.0)
    return -1;
  p = p * (n + 1);
  if (p > n)
    return (double)n - 1;
  if (p < 1)
    return 0.0;
  return (p - 1.0);
}


x86 assembly (clang, -O0):
<percentile_index>:
push   %rbp
mov    %rsp,%rbp
movss  %xmm0,-0xc(%rbp)
mov    %edi,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jge    1128 <percentile_index+0x28>
movsd  0xef2(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f6 <percentile_index+0xf6>
movss  -0xc(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xec7(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
ja     1167 <percentile_index+0x67>
movsd  -0x18(%rbp),%xmm0
movsd  0xeab(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1179 <percentile_index+0x79>
movsd  0xea1(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f6 <percentile_index+0xf6>
movsd  -0x18(%rbp),%xmm0
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm1
ucomisd %xmm1,%xmm0
jbe    11c0 <percentile_index+0xc0>
cvtsi2sdl -0x10(%rbp),%xmm0
movsd  0xe56(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f6 <percentile_index+0xf6>
movsd  0xe40(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11e0 <percentile_index+0xe0>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f6 <percentile_index+0xf6>
movsd  -0x18(%rbp),%xmm0
movsd  0xe1b(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<percentile_index>:
movaps %xmm0,%xmm1
movsd  0xef5(%rip),%xmm0
test   %edi,%edi
jle    1171 <percentile_index+0x71>
cvtss2sd %xmm1,%xmm1
mulsd  0xeed(%rip),%xmm1
xorpd  %xmm2,%xmm2
ucomisd %xmm1,%xmm2
ja     1171 <percentile_index+0x71>
ucomisd 0xee3(%rip),%xmm1
ja     1171 <percentile_index+0x71>
lea    0x1(%rdi),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %edi,%xmm0
ucomisd %xmm0,%xmm1
jbe    1153 <percentile_index+0x53>
addsd  0xeae(%rip),%xmm0
ret
xorpd  %xmm0,%xmm0
movsd  0xeb1(%rip),%xmm2
ucomisd %xmm1,%xmm2
ja     1171 <percentile_index+0x71>
addsd  0xe93(%rip),%xmm1
movapd %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @percentile_index(float %0, i32 %1) {
%3 = alloca double
%4 = alloca float
%5 = alloca i32
%6 = alloca double
store float %0, float* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %10
9: ; preds = %2
store double -1.000000e+00, double* %3
br label %41
10: ; preds = %2
%11 = load float, float* %4
%12 = fpext float %11 to double
%13 = fmul double %12, 1.000000e-02
store double %13, double* %6
%14 = load double, double* %6
%15 = fcmp olt double %14, 0.000000e+00
br i1 %15, label %19, label %16
16: ; preds = %10
%17 = load double, double* %6
%18 = fcmp ogt double %17, 1.000000e+00
br i1 %18, label %19, label %20
19: ; preds = %16, %10
store double -1.000000e+00, double* %3
br label %41
20: ; preds = %16
%21 = load double, double* %6
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
%24 = sitofp i32 %23 to double
%25 = fmul double %21, %24
store double %25, double* %6
%26 = load double, double* %6
%27 = load i32, i32* %5
%28 = sitofp i32 %27 to double
%29 = fcmp ogt double %26, %28
br i1 %29, label %30, label %34
30: ; preds = %20
%31 = load i32, i32* %5
%32 = sitofp i32 %31 to double
%33 = fsub double %32, 1.000000e+00
store double %33, double* %3
br label %41
34: ; preds = %20
%35 = load double, double* %6
%36 = fcmp olt double %35, 1.000000e+00
br i1 %36, label %37, label %38
37: ; preds = %34
store double 0.000000e+00, double* %3
br label %41
38: ; preds = %34
%39 = load double, double* %6
%40 = fsub double %39, 1.000000e+00
store double %40, double* %3
br label %41
41: ; preds = %38, %37, %30, %19, %9
%42 = load double, double* %3
ret double %42
}

LLVM IR (-O2):
define double @percentile_index(float %0, i32 %1) {
%3 = icmp slt i32 %1, 1
br i1 %3, label %22, label %4
4: ; preds = %2
%5 = fpext float %0 to double
%6 = fmul double %5, 1.000000e-02
%7 = fcmp olt double %6, 0.000000e+00
%8 = fcmp ogt double %6, 1.000000e+00
%9 = or i1 %7, %8
br i1 %9, label %22, label %10
10: ; preds = %4
%11 = add nuw nsw i32 %1, 1
%12 = sitofp i32 %11 to double
%13 = fmul double %6, %12
%14 = sitofp i32 %1 to double
%15 = fcmp ogt double %13, %14
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = fadd double %14, -1.000000e+00
br label %22
18: ; preds = %10
%19 = fcmp olt double %13, 1.000000e+00
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = fadd double %13, -1.000000e+00
br label %22
22: ; preds = %18, %4, %2, %20, %16
%23 = phi double [ %17, %16 ], [ %21, %20 ], [ -1.000000e+00, %2 ], [ -1.000000e+00, %4 ], [ 0.000000e+00, %18 ]
ret double %23
}