#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 get_time_ms();
int /*<<< orphan*/ mink(double *, int *, int, int);
double normrand(int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sort_indices(double *, int *, int);
void test_mink() {
  double x[10] = {0, 90, 70, 40, 20, 10, 30, 80, 50, 60};
  int idx[5];
  mink(x, idx, 10, 5);
  printf("idx = [ ");
  int i;
  for (i = 0; i < 5; i++)
    printf("%d ", idx[i]);
  printf("]\n");
  int n = 100000;
  double y[n];
  for (i = 0; i < n; i++)
    y[i] = normrand(0, 1);
  int yi[n];
  double t = get_time_ms();
  sort_indices(y, yi, n);
  printf("sorted %d numbers in %f ms\n", n, get_time_ms() - t);
  t = get_time_ms();
  mink(y, yi, n, 100);
  printf("got the min %d numbers in %f ms\n", 100, get_time_ms() - t);
}


x86 assembly (clang, -O0):
<test_mink>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    -0x50(%rbp),%rdi
lea    0xe8a(%rip),%rsi
mov    $0x50,%edx
call   1060 <memcpy@plt>
lea    -0x50(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    $0xa,%edx
mov    $0x5,%ecx
call   1040 <mink@plt>
lea    0xeb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x74(%rbp)
cmpl   $0x5,-0x74(%rbp)
jge    11da <test_mink+0x7a>
movslq -0x74(%rbp),%rax
mov    -0x70(%rbp,%rax,4),%esi
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11ac <test_mink+0x4c>
lea    0xe7c(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movl   $0x186a0,-0x78(%rbp)
mov    -0x78(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x80(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x88(%rbp)
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x78(%rbp),%eax
jge    1259 <test_mink+0xf9>
xor    %edi,%edi
mov    $0x1,%esi
call   1070 <normrand@plt>
mov    -0xa0(%rbp),%rax
movslq -0x74(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1223 <test_mink+0xc3>
mov    -0x78(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x90(%rbp)
mov    $0x0,%al
call   1050 <get_time_ms@plt>
mov    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
movsd  %xmm0,-0x98(%rbp)
ff
mov    -0x78(%rbp),%edx
call   1080 <sort_indices@plt>
mov    -0x78(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    $0x0,%al
call   1050 <get_time_ms@plt>
mov    -0xac(%rbp),%esi
subsd  -0x98(%rbp),%xmm0
ff
lea    0xd97(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <get_time_ms@plt>
mov    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
movsd  %xmm0,-0x98(%rbp)
ff
mov    -0x78(%rbp),%edx
mov    $0x64,%ecx
call   1040 <mink@plt>
mov    $0x0,%al
call   1050 <get_time_ms@plt>
subsd  -0x98(%rbp),%xmm0
ff
lea    0xd6c(%rip),%rdi
mov    $0x64,%esi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x80(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_mink>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0xc3578,%rsp
movaps 0xec9(%rip),%xmm0
movaps %xmm0,-0x50(%rbp)
movaps 0xeae(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
movaps 0xe93(%rip),%xmm0
movaps %xmm0,-0x70(%rbp)
movaps 0xe78(%rip),%xmm0
movaps %xmm0,-0x80(%rbp)
movapd 0xe5c(%rip),%xmm0
movapd %xmm0,-0x90(%rbp)
ff
lea    -0x90(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0xa,%edx
mov    $0x5,%ecx
call   1050 <mink@plt>
lea    0xe83(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x40(%rbp),%esi
lea    0xe79(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x3c(%rbp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x38(%rbp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x34(%rbp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x30(%rbp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r15
nopl   (%rax)
xor    %edi,%edi
mov    $0x1,%esi
call   1070 <normrand@plt>
movsd  %xmm0,-0xc3590(%rbp,%rbx,8)
f3 ff
add    $0x1,%rbx
cmp    $0x186a0,%rbx
jne    1230 <test_mink+0xd0>
lea    -0x61a80(%rsp),%rbx
ff
mov    %rbx,%rsp
xor    %eax,%eax
call   1060 <get_time_ms@plt>
movsd  %xmm0,-0x20(%rbp)
lea    -0xc3590(%rbp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x186a0,%edx
call   1080 <sort_indices@plt>
xor    %eax,%eax
call   1060 <get_time_ms@plt>
subsd  -0x20(%rbp),%xmm0
lea    0xdca(%rip),%rdi
mov    $0x186a0,%esi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
call   1060 <get_time_ms@plt>
movsd  %xmm0,-0x20(%rbp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x186a0,%edx
mov    $0x64,%ecx
call   1050 <mink@plt>
xor    %eax,%eax
call   1060 <get_time_ms@plt>
subsd  -0x20(%rbp),%xmm0
lea    0xda6(%rip),%rdi
mov    $0x64,%esi
mov    $0x1,%al
call   1040 <printf@plt>
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.test_mink.x = [10 x double] [double 0.000000e+00, double 9.000000e+01, double 7.000000e+01, double 4.000000e+01, double 2.000000e+01, double 1.000000e+01, double 3.000000e+01, double 8.000000e+01, double 5.000000e+01, double 6.000000e+01]
@.str = [9 x i8] c"idx = [ \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [3 x i8] c"]\0A\00"
@.str.3 = [28 x i8] c"sorted %d numbers in %f ms\0A\00"
@.str.4 = [33 x i8] c"got the min %d numbers in %f ms\0A\00"
define void @test_mink() {
%1 = alloca [10 x double]
%2 = alloca [5 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = bitcast [10 x double]* %1 to i8*
%10 = getelementptr inbounds [10 x double], [10 x double]* %1, i64 0, i64 0
%11 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 0
%12 = call i32 @mink(double* %10, i32* %11, i32 10, i32 5)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %14
14: ; preds = %23, %0
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 5
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 %19
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %17
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %14
26: ; preds = %14
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 100000, i32* %4
%28 = load i32, i32* %4
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %5
%31 = alloca double, i64 %29
store i64 %29, i64* %6
store i32 0, i32* %3
br label %32
32: ; preds = %41, %26
%33 = load i32, i32* %3
%34 = load i32, i32* %4
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = call double @normrand(i32 0, i32 1)
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds double, double* %31, i64 %39
store double %37, double* %40
br label %41
41: ; preds = %36
%42 = load i32, i32* %3
%43 = add nsw i32 %42, 1
store i32 %43, i32* %3
br label %32
44: ; preds = %32
%45 = load i32, i32* %4
%46 = zext i32 %45 to i64
%47 = alloca i32, i64 %46
store i64 %46, i64* %7
%48 = call double (...) @get_time_ms()
store double %48, double* %8
%49 = load i32, i32* %4
%50 = call i32 @sort_indices(double* %31, i32* %47, i32 %49)
%51 = load i32, i32* %4
%52 = call double (...) @get_time_ms()
%53 = load double, double* %8
%54 = fsub double %52, %53
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %51, double %54)
%56 = call double (...) @get_time_ms()
store double %56, double* %8
%57 = load i32, i32* %4
%58 = call i32 @mink(double* %31, i32* %47, i32 %57, i32 100)
%59 = call double (...) @get_time_ms()
%60 = load double, double* %8
%61 = fsub double %59, %60
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 100, double %61)
%63 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@__const.test_mink.x = [10 x double] [double 0.000000e+00, double 9.000000e+01, double 7.000000e+01, double 4.000000e+01, double 2.000000e+01, double 1.000000e+01, double 3.000000e+01, double 8.000000e+01, double 5.000000e+01, double 6.000000e+01]
@.str = [9 x i8] c"idx = [ \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.3 = [28 x i8] c"sorted %d numbers in %f ms\0A\00"
@.str.4 = [33 x i8] c"got the min %d numbers in %f ms\0A\00"
@str = [2 x i8] c"]\00"
define void @test_mink() {
%1 = alloca [10 x double]
%2 = alloca [5 x i32]
%3 = bitcast [10 x double]* %1 to i8*
%4 = bitcast [5 x i32]* %2 to i8*
%5 = getelementptr inbounds [10 x double], [10 x double]* %1, i64 0, i64 0
%6 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 0
%7 = call i32 @mink(double* %5, i32* %6, i32 10, i32 5)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %6
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 2
%15 = load i32, i32* %14
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 3
%18 = load i32, i32* %17
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 4
%21 = load i32, i32* %20
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%24 = call i8* @llvm.stacksave()
%25 = alloca [100000 x double]
br label %26
26: ; preds = %0, %26
%27 = phi i64 [ 0, %0 ], [ %30, %26 ]
%28 = call double @normrand(i32 0, i32 1)
%29 = getelementptr inbounds [100000 x double], [100000 x double]* %25, i64 0, i64 %27
store double %28, double* %29
%30 = add nuw nsw i64 %27, 1
%31 = icmp eq i64 %30, 100000
br i1 %31, label %32, label %26
32: ; preds = %26
%33 = getelementptr inbounds [100000 x double], [100000 x double]* %25, i64 0, i64 0
%34 = alloca [100000 x i32]
%35 = getelementptr inbounds [100000 x i32], [100000 x i32]* %34, i64 0, i64 0
%36 = call double (...) @get_time_ms()
%37 = call i32 @sort_indices(double* %33, i32* %35, i32 100000)
%38 = call double (...) @get_time_ms()
%39 = fsub double %38, %36
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 100000, double %39)
%41 = call double (...) @get_time_ms()
%42 = call i32 @mink(double* %33, i32* %35, i32 100000, i32 100)
%43 = call double (...) @get_time_ms()
%44 = fsub double %43, %41
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 100, double %44)
ret void
}