#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 lroundf(float);
float mClampF(float, float, float);
int array_index_by_custom_perc(float value, float min, float max,
                               int total_indices) {
  value = mClampF(value, min, max);
  float span = max - min;
  float span_val = value - min;
  float span_perc = span_val / span * 100.0;
  float index_perc = total_indices / 100.0f;
  int result = lroundf(span_perc * index_perc);
  if (result >= total_indices)
    result = total_indices - 1;
  else if (result < 0)
    result = 0;
  return result;
}


x86 assembly (clang, -O0):
<array_index_by_custom_perc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movss  %xmm0,-0x4(%rbp)
movss  %xmm1,-0x8(%rbp)
movss  %xmm2,-0xc(%rbp)
mov    %edi,-0x10(%rbp)
movss  -0x4(%rbp),%xmm0
movss  -0x8(%rbp),%xmm1
movss  -0xc(%rbp),%xmm2
call   1030 <mClampF@plt>
movss  %xmm0,-0x4(%rbp)
movss  -0xc(%rbp),%xmm0
subss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x4(%rbp),%xmm0
subss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x18(%rbp),%xmm0
divss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe81(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
cvtsi2ssl -0x10(%rbp),%xmm0
movss  0xe5f(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x1c(%rbp),%xmm0
mulss  -0x20(%rbp),%xmm0
call   1040 <lroundf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     11d6 <array_index_by_custom_perc+0xb6>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ec <array_index_by_custom_perc+0xcc>
cmpl   $0x0,-0x24(%rbp)
jge    11e7 <array_index_by_custom_perc+0xc7>
movl   $0x0,-0x24(%rbp)
jmp    11ec <array_index_by_custom_perc+0xcc>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_index_by_custom_perc>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
movaps %xmm2,0x10(%rsp)
movss  %xmm1,0xc(%rsp)
call   1030 <mClampF@plt>
movaps 0x10(%rsp),%xmm2
movss  0xc(%rsp),%xmm1
subss  %xmm1,%xmm2
subss  %xmm1,%xmm0
xorps  %xmm1,%xmm1
cvtsi2ss %ebx,%xmm1
unpcklps %xmm0,%xmm1
movaps 0xea5(%rip),%xmm0
unpcklps %xmm2,%xmm0
divps  %xmm0,%xmm1
movaps %xmm1,%xmm0
shufps $0x55,%xmm1,%xmm0
mulss  0xea0(%rip),%xmm0
mulss  %xmm1,%xmm0
call   1040 <lroundf@plt>
xor    %ecx,%ecx
test   %eax,%eax
cmovns %eax,%ecx
lea    -0x1(%rbx),%edx
cmp    %ebx,%eax
cmovge %edx,%ecx
mov    %ecx,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @array_index_by_custom_perc(float %0, float %1, float %2, i32 %3) {
%5 = tail call float @mClampF(float %0, float %1, float %2)
%6 = fsub float %2, %1
%7 = fsub float %5, %1
%8 = sitofp i32 %3 to float
%9 = insertelement <2 x float> poison, float %8, i64 0
%10 = insertelement <2 x float> %9, float %7, i64 1
%11 = insertelement <2 x float> <float 1.000000e+02, float poison>, float %6, i64 1
%12 = fdiv <2 x float> %10, %11
%13 = extractelement <2 x float> %12, i64 1
%14 = fmul float %13, 1.000000e+02
%15 = extractelement <2 x float> %12, i64 0
%16 = fmul float %15, %14
%17 = tail call i32 @lroundf(float %16)
%18 = icmp slt i32 %17, %3
%19 = add nsw i32 %3, -1
%20 = icmp sgt i32 %17, 0
%21 = select i1 %20, i32 %17, i32 0
%22 = select i1 %18, i32 %21, i32 %19
ret i32 %22
}