#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 */
void sort_by_idxs(double *restrict o_coeffs, size_t *restrict o_coeffs_idx,
                  size_t count) {
  size_t median_idx;
  double tmp_median;
  size_t tmp_median_idx;
  size_t ii;
  while (count > 1) {
    median_idx = 0;
    for (ii = 1; ii < count; ii++) {
      if (o_coeffs_idx[ii] < o_coeffs_idx[median_idx]) {
        tmp_median = o_coeffs[median_idx];
        o_coeffs[median_idx] = o_coeffs[ii];
        o_coeffs[ii] = o_coeffs[median_idx + 1];
        o_coeffs[median_idx + 1] = tmp_median;
        tmp_median_idx = o_coeffs_idx[median_idx];
        o_coeffs_idx[median_idx] = o_coeffs_idx[ii];
        o_coeffs_idx[ii] = o_coeffs_idx[median_idx + 1];
        o_coeffs_idx[median_idx + 1] = tmp_median_idx;
        median_idx = median_idx + 1;
      }
    }
    sort_by_idxs(o_coeffs, o_coeffs_idx, median_idx);
    o_coeffs = o_coeffs + median_idx + 1;
    o_coeffs_idx = o_coeffs_idx + median_idx + 1;
    count = count - median_idx - 1;
  }
}


x86 assembly (clang, -O0):
<sort_by_idxs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jbe    128d <sort_by_idxs+0x17d>
movq   $0x0,-0x20(%rbp)
movq   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1239 <sort_by_idxs+0x129>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,8),%rax
jae    1223 <sort_by_idxs+0x113>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  0x8(%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x28(%rbp),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  %xmm0,0x8(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    0x8(%rax,%rcx,8),%rdx
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rdx,0x8(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1228 <sort_by_idxs+0x118>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    113f <sort_by_idxs+0x2f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <sort_by_idxs@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1124 <sort_by_idxs+0x14>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_by_idxs>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%rdx
jb     11c4 <sort_by_idxs+0xb4>
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
jmp    115a <sort_by_idxs+0x4a>
nopl   (%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <sort_by_idxs@plt>
lea    (%r15,%rbx,8),%r15
add    $0x8,%r15
lea    (%r12,%rbx,8),%r12
add    $0x8,%r12
not    %rbx
add    %rbx,%r14
cmp    $0x1,%r14
jbe    11c4 <sort_by_idxs+0xb4>
mov    $0x1,%eax
xor    %ebx,%ebx
jmp    1179 <sort_by_idxs+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rax
cmp    %rax,%r14
je     1130 <sort_by_idxs+0x20>
mov    (%r12,%rax,8),%rdx
mov    (%r12,%rbx,8),%rcx
cmp    %rcx,%rdx
jae    1170 <sort_by_idxs+0x60>
movsd  (%r15,%rbx,8),%xmm0
movsd  (%r15,%rax,8),%xmm1
movsd  %xmm1,(%r15,%rbx,8)
movsd  0x8(%r15,%rbx,8),%xmm1
movsd  %xmm1,(%r15,%rax,8)
movsd  %xmm0,0x8(%r15,%rbx,8)
mov    %rdx,(%r12,%rbx,8)
mov    0x8(%r12,%rbx,8),%rdx
mov    %rdx,(%r12,%rax,8)
mov    %rcx,0x8(%r12,%rbx,8)
add    $0x1,%rbx
jmp    1170 <sort_by_idxs+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @sort_by_idxs(double* noalias %0, i64* noalias %1, i64 %2) {
%4 = alloca double*
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca i64
%10 = alloca i64
store double* %0, double** %4
store i64* %1, i64** %5
store i64 %2, i64* %6
br label %11
11: ; preds = %84, %3
%12 = load i64, i64* %6
%13 = icmp ugt i64 %12, 1
br i1 %13, label %14, label %100
14: ; preds = %11
store i64 0, i64* %7
store i64 1, i64* %10
br label %15
15: ; preds = %81, %14
%16 = load i64, i64* %10
%17 = load i64, i64* %6
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %84
19: ; preds = %15
%20 = load i64*, i64** %5
%21 = load i64, i64* %10
%22 = getelementptr inbounds i64, i64* %20, i64 %21
%23 = load i64, i64* %22
%24 = load i64*, i64** %5
%25 = load i64, i64* %7
%26 = getelementptr inbounds i64, i64* %24, i64 %25
%27 = load i64, i64* %26
%28 = icmp ult i64 %23, %27
br i1 %28, label %29, label %80
29: ; preds = %19
%30 = load double*, double** %4
%31 = load i64, i64* %7
%32 = getelementptr inbounds double, double* %30, i64 %31
%33 = load double, double* %32
store double %33, double* %8
%34 = load double*, double** %4
%35 = load i64, i64* %10
%36 = getelementptr inbounds double, double* %34, i64 %35
%37 = load double, double* %36
%38 = load double*, double** %4
%39 = load i64, i64* %7
%40 = getelementptr inbounds double, double* %38, i64 %39
store double %37, double* %40
%41 = load double*, double** %4
%42 = load i64, i64* %7
%43 = add i64 %42, 1
%44 = getelementptr inbounds double, double* %41, i64 %43
%45 = load double, double* %44
%46 = load double*, double** %4
%47 = load i64, i64* %10
%48 = getelementptr inbounds double, double* %46, i64 %47
store double %45, double* %48
%49 = load double, double* %8
%50 = load double*, double** %4
%51 = load i64, i64* %7
%52 = add i64 %51, 1
%53 = getelementptr inbounds double, double* %50, i64 %52
store double %49, double* %53
%54 = load i64*, i64** %5
%55 = load i64, i64* %7
%56 = getelementptr inbounds i64, i64* %54, i64 %55
%57 = load i64, i64* %56
store i64 %57, i64* %9
%58 = load i64*, i64** %5
%59 = load i64, i64* %10
%60 = getelementptr inbounds i64, i64* %58, i64 %59
%61 = load i64, i64* %60
%62 = load i64*, i64** %5
%63 = load i64, i64* %7
%64 = getelementptr inbounds i64, i64* %62, i64 %63
store i64 %61, i64* %64
%65 = load i64*, i64** %5
%66 = load i64, i64* %7
%67 = add i64 %66, 1
%68 = getelementptr inbounds i64, i64* %65, i64 %67
%69 = load i64, i64* %68
%70 = load i64*, i64** %5
%71 = load i64, i64* %10
%72 = getelementptr inbounds i64, i64* %70, i64 %71
store i64 %69, i64* %72
%73 = load i64, i64* %9
%74 = load i64*, i64** %5
%75 = load i64, i64* %7
%76 = add i64 %75, 1
%77 = getelementptr inbounds i64, i64* %74, i64 %76
store i64 %73, i64* %77
%78 = load i64, i64* %7
%79 = add i64 %78, 1
store i64 %79, i64* %7
br label %80
80: ; preds = %29, %19
br label %81
81: ; preds = %80
%82 = load i64, i64* %10
%83 = add i64 %82, 1
store i64 %83, i64* %10
br label %15
84: ; preds = %15
%85 = load double*, double** %4
%86 = load i64*, i64** %5
%87 = load i64, i64* %7
%88 = load double*, double** %4
%89 = load i64, i64* %7
%90 = getelementptr inbounds double, double* %88, i64 %89
%91 = getelementptr inbounds double, double* %90, i64 1
store double* %91, double** %4
%92 = load i64*, i64** %5
%93 = load i64, i64* %7
%94 = getelementptr inbounds i64, i64* %92, i64 %93
%95 = getelementptr inbounds i64, i64* %94, i64 1
store i64* %95, i64** %5
%96 = load i64, i64* %6
%97 = load i64, i64* %7
%98 = sub i64 %96, %97
%99 = sub i64 %98, 1
store i64 %99, i64* %6
br label %11
100: ; preds = %11
ret void
}

LLVM IR (-O2):
define void @sort_by_idxs(double* noalias nocapture %0, i64* noalias nocapture %1, i64 %2) {
%4 = icmp ugt i64 %2, 1
br i1 %4, label %5, label %39
5: ; preds = %3, %31
%6 = phi double* [ %33, %31 ], [ %0, %3 ]
%7 = phi i64* [ %35, %31 ], [ %1, %3 ]
%8 = phi i64 [ %37, %31 ], [ %2, %3 ]
br label %9
9: ; preds = %5, %27
%10 = phi i64 [ %29, %27 ], [ 1, %5 ]
%11 = phi i64 [ %28, %27 ], [ 0, %5 ]
%12 = getelementptr inbounds i64, i64* %7, i64 %10
%13 = load i64, i64* %12
%14 = getelementptr inbounds i64, i64* %7, i64 %11
%15 = load i64, i64* %14
%16 = icmp ult i64 %13, %15
br i1 %16, label %17, label %27
17: ; preds = %9
%18 = getelementptr inbounds double, double* %6, i64 %11
%19 = load double, double* %18
%20 = getelementptr inbounds double, double* %6, i64 %10
%21 = load double, double* %20
store double %21, double* %18
%22 = add i64 %11, 1
%23 = getelementptr inbounds double, double* %6, i64 %22
%24 = load double, double* %23
store double %24, double* %20
store double %19, double* %23
store i64 %13, i64* %14
%25 = getelementptr inbounds i64, i64* %7, i64 %22
%26 = load i64, i64* %25
store i64 %26, i64* %12
store i64 %15, i64* %25
br label %27
27: ; preds = %9, %17
%28 = phi i64 [ %22, %17 ], [ %11, %9 ]
%29 = add nuw i64 %10, 1
%30 = icmp eq i64 %29, %8
br i1 %30, label %31, label %9
31: ; preds = %27
tail call void @sort_by_idxs(double* %6, i64* %7, i64 %28)
%32 = getelementptr inbounds double, double* %6, i64 1
%33 = getelementptr inbounds double, double* %32, i64 %28
%34 = getelementptr inbounds i64, i64* %7, i64 1
%35 = getelementptr inbounds i64, i64* %34, i64 %28
%36 = xor i64 %28, -1
%37 = add i64 %8, %36
%38 = icmp ugt i64 %37, 1
br i1 %38, label %5, label %39
39: ; preds = %31, %3
ret void
}