#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 /*<<< orphan*/ assert(int);
int fvec_arg_min(float const *, int);
int /*<<< orphan*/ fvec_k_min_hoare(float const *, int, int *, int);
int /*<<< orphan*/ fvec_k_min_maxheap(float const *, int, int *, int);
void fvec_k_min(const float *val, int n, int *idx, int k) {
  assert(k <= n);
  if (n == 0 || k == 0)
    return;
  if (k == 1) {
    *idx = fvec_arg_min(val, n);
    return;
  }
  if (n > 3 * k)
    fvec_k_min_maxheap(val, n, idx, k);
  else
    fvec_k_min_hoare(val, n, idx, k);
}


x86 assembly (clang, -O0):
<fvec_k_min>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
je     117d <fvec_k_min+0x3d>
cmpl   $0x0,-0x1c(%rbp)
jne    1182 <fvec_k_min+0x42>
jmp    11df <fvec_k_min+0x9f>
cmpl   $0x1,-0x1c(%rbp)
jne    11a5 <fvec_k_min+0x65>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <fvec_arg_min@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    11df <fvec_k_min+0x9f>
mov    -0xc(%rbp),%eax
imul   $0x3,-0x1c(%rbp),%ecx
cmp    %ecx,%eax
jle    11cc <fvec_k_min+0x8c>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1050 <fvec_k_min_maxheap@plt>
jmp    11df <fvec_k_min+0x9f>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1060 <fvec_k_min_hoare@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fvec_k_min>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
xor    %edi,%edi
cmp    %esi,%ecx
setle  %dil
call   1040 <assert@plt>
test   %ebp,%ebp
je     1178 <fvec_k_min+0x38>
test   %ebx,%ebx
je     1178 <fvec_k_min+0x38>
cmp    $0x1,%ebx
jne    1183 <fvec_k_min+0x43>
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <fvec_arg_min@plt>
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    (%rbx,%rbx,2),%eax
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
add    $0x8,%rsp
cmp    %ebp,%eax
jge    11a3 <fvec_k_min+0x63>
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fvec_k_min_maxheap@plt>
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fvec_k_min_hoare@plt>

LLVM IR (-O0):
define void @fvec_k_min(float* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca float*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store float* %0, float** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%9 = load i32, i32* %8
%10 = load i32, i32* %6
%11 = icmp sle i32 %9, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16, %4
br label %45
20: ; preds = %16
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load float*, float** %5
%25 = load i32, i32* %6
%26 = call i32 @fvec_arg_min(float* %24, i32 %25)
%27 = load i32*, i32** %7
store i32 %26, i32* %27
br label %45
28: ; preds = %20
%29 = load i32, i32* %6
%30 = load i32, i32* %8
%31 = mul nsw i32 3, %30
%32 = icmp sgt i32 %29, %31
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load float*, float** %5
%35 = load i32, i32* %6
%36 = load i32*, i32** %7
%37 = load i32, i32* %8
%38 = call i32 @fvec_k_min_maxheap(float* %34, i32 %35, i32* %36, i32 %37)
br label %45
39: ; preds = %28
%40 = load float*, float** %5
%41 = load i32, i32* %6
%42 = load i32*, i32** %7
%43 = load i32, i32* %8
%44 = call i32 @fvec_k_min_hoare(float* %40, i32 %41, i32* %42, i32 %43)
br label %45
45: ; preds = %19, %23, %39, %33
ret void
}

LLVM IR (-O2):
define void @fvec_k_min(float* %0, i32 %1, i32* %2, i32 %3) {
%5 = icmp sle i32 %3, %1
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp eq i32 %1, 0
%9 = icmp eq i32 %3, 0
%10 = or i1 %8, %9
br i1 %10, label %22, label %11
11: ; preds = %4
%12 = icmp eq i32 %3, 1
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @fvec_arg_min(float* %0, i32 %1)
store i32 %14, i32* %2
br label %22
15: ; preds = %11
%16 = mul nsw i32 %3, 3
%17 = icmp slt i32 %16, %1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @fvec_k_min_maxheap(float* %0, i32 %1, i32* %2, i32 %3)
br label %22
20: ; preds = %15
%21 = tail call i32 @fvec_k_min_hoare(float* %0, i32 %1, i32* %2, i32 %3)
br label %22
22: ; preds = %4, %13, %20, %18
ret void
}