#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 fabs(double);
void sort_by_abs_coeffs(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;
  double fabs_o_coeffs_ii;
  double fabs_o_coeffs_median_idx;
  size_t ii;
  while (count > 1) {
    median_idx = 0;
    for (ii = 1; ii < count; ii++) {
      fabs_o_coeffs_ii = fabs(o_coeffs[ii]);
      fabs_o_coeffs_median_idx = fabs(o_coeffs[median_idx]);
      if ((fabs_o_coeffs_ii > fabs_o_coeffs_median_idx) ||
          ((fabs_o_coeffs_ii == fabs_o_coeffs_median_idx) &&
           (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_abs_coeffs(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_abs_coeffs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jbe    12ed <sort_by_abs_coeffs+0x1dd>
movq   $0x0,-0x20(%rbp)
movq   $0x1,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1299 <sort_by_abs_coeffs+0x189>
mov    -0x8(%rbp),%rax
mov    -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movaps 0xe9f(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movaps 0xe82(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x38(%rbp),%xmm0
ucomisd -0x40(%rbp),%xmm0
ja     11cb <sort_by_abs_coeffs+0xbb>
movsd  -0x38(%rbp),%xmm0
ucomisd -0x40(%rbp),%xmm0
jne    1283 <sort_by_abs_coeffs+0x173>
jp     1283 <sort_by_abs_coeffs+0x173>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,8),%rax
jae    1283 <sort_by_abs_coeffs+0x173>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x48(%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    -0x48(%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    -0x48(%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    -0x48(%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    1288 <sort_by_abs_coeffs+0x178>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    113f <sort_by_abs_coeffs+0x2f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <sort_by_abs_coeffs@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_abs_coeffs+0x14>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_by_abs_coeffs>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%rdx
jb     11fe <sort_by_abs_coeffs+0xee>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
movapd 0xecd(%rip),%xmm4
jmp    1176 <sort_by_abs_coeffs+0x66>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <sort_by_abs_coeffs@plt>
movapd 0xeaa(%rip),%xmm4
lea    (%r12,%rbx,8),%r12
add    $0x8,%r12
lea    (%r15,%rbx,8),%r15
add    $0x8,%r15
not    %rbx
add    %rbx,%r14
cmp    $0x1,%r14
jbe    11fe <sort_by_abs_coeffs+0xee>
mov    $0x1,%eax
xor    %ebx,%ebx
jmp    11c5 <sort_by_abs_coeffs+0xb5>
nop
mov    (%r15,%rbx,8),%rcx
mov    (%r15,%rax,8),%rdx
movsd  %xmm1,(%r12,%rbx,8)
movsd  0x8(%r12,%rbx,8),%xmm1
movsd  %xmm1,(%r12,%rax,8)
movsd  %xmm0,0x8(%r12,%rbx,8)
mov    %rdx,(%r15,%rbx,8)
mov    0x8(%r15,%rbx,8),%rdx
mov    %rdx,(%r15,%rax,8)
mov    %rcx,0x8(%r15,%rbx,8)
add    $0x1,%rbx
add    $0x1,%rax
cmp    %rax,%r14
je     1140 <sort_by_abs_coeffs+0x30>
movsd  (%r12,%rax,8),%xmm1
movapd %xmm1,%xmm2
andpd  %xmm4,%xmm2
movsd  (%r12,%rbx,8),%xmm0
movapd %xmm0,%xmm3
andpd  %xmm4,%xmm3
ucomisd %xmm3,%xmm2
ja     1180 <sort_by_abs_coeffs+0x70>
ucomisd %xmm3,%xmm2
jne    11b8 <sort_by_abs_coeffs+0xa8>
jp     11b8 <sort_by_abs_coeffs+0xa8>
mov    (%r15,%rax,8),%rdx
mov    (%r15,%rbx,8),%rcx
cmp    %rcx,%rdx
jb     1188 <sort_by_abs_coeffs+0x78>
jmp    11b8 <sort_by_abs_coeffs+0xa8>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @sort_by_abs_coeffs(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 double
%11 = alloca double
%12 = alloca i64
store double* %0, double** %4
store i64* %1, i64** %5
store i64 %2, i64* %6
br label %13
13: ; preds = %104, %3
%14 = load i64, i64* %6
%15 = icmp ugt i64 %14, 1
br i1 %15, label %16, label %120
16: ; preds = %13
store i64 0, i64* %7
store i64 1, i64* %12
br label %17
17: ; preds = %101, %16
%18 = load i64, i64* %12
%19 = load i64, i64* %6
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %104
21: ; preds = %17
%22 = load double*, double** %4
%23 = load i64, i64* %12
%24 = getelementptr inbounds double, double* %22, i64 %23
%25 = load double, double* %24
%26 = call double @llvm.fabs.f64(double %25)
store double %26, double* %10
%27 = load double*, double** %4
%28 = load i64, i64* %7
%29 = getelementptr inbounds double, double* %27, i64 %28
%30 = load double, double* %29
%31 = call double @llvm.fabs.f64(double %30)
store double %31, double* %11
%32 = load double, double* %10
%33 = load double, double* %11
%34 = fcmp ogt double %32, %33
br i1 %34, label %49, label %35
35: ; preds = %21
%36 = load double, double* %10
%37 = load double, double* %11
%38 = fcmp oeq double %36, %37
br i1 %38, label %39, label %100
39: ; preds = %35
%40 = load i64*, i64** %5
%41 = load i64, i64* %12
%42 = getelementptr inbounds i64, i64* %40, i64 %41
%43 = load i64, i64* %42
%44 = load i64*, i64** %5
%45 = load i64, i64* %7
%46 = getelementptr inbounds i64, i64* %44, i64 %45
%47 = load i64, i64* %46
%48 = icmp ult i64 %43, %47
br i1 %48, label %49, label %100
49: ; preds = %39, %21
%50 = load double*, double** %4
%51 = load i64, i64* %7
%52 = getelementptr inbounds double, double* %50, i64 %51
%53 = load double, double* %52
store double %53, double* %8
%54 = load double*, double** %4
%55 = load i64, i64* %12
%56 = getelementptr inbounds double, double* %54, i64 %55
%57 = load double, double* %56
%58 = load double*, double** %4
%59 = load i64, i64* %7
%60 = getelementptr inbounds double, double* %58, i64 %59
store double %57, double* %60
%61 = load double*, double** %4
%62 = load i64, i64* %7
%63 = add i64 %62, 1
%64 = getelementptr inbounds double, double* %61, i64 %63
%65 = load double, double* %64
%66 = load double*, double** %4
%67 = load i64, i64* %12
%68 = getelementptr inbounds double, double* %66, i64 %67
store double %65, double* %68
%69 = load double, double* %8
%70 = load double*, double** %4
%71 = load i64, i64* %7
%72 = add i64 %71, 1
%73 = getelementptr inbounds double, double* %70, i64 %72
store double %69, double* %73
%74 = load i64*, i64** %5
%75 = load i64, i64* %7
%76 = getelementptr inbounds i64, i64* %74, i64 %75
%77 = load i64, i64* %76
store i64 %77, i64* %9
%78 = load i64*, i64** %5
%79 = load i64, i64* %12
%80 = getelementptr inbounds i64, i64* %78, i64 %79
%81 = load i64, i64* %80
%82 = load i64*, i64** %5
%83 = load i64, i64* %7
%84 = getelementptr inbounds i64, i64* %82, i64 %83
store i64 %81, i64* %84
%85 = load i64*, i64** %5
%86 = load i64, i64* %7
%87 = add i64 %86, 1
%88 = getelementptr inbounds i64, i64* %85, i64 %87
%89 = load i64, i64* %88
%90 = load i64*, i64** %5
%91 = load i64, i64* %12
%92 = getelementptr inbounds i64, i64* %90, i64 %91
store i64 %89, i64* %92
%93 = load i64, i64* %9
%94 = load i64*, i64** %5
%95 = load i64, i64* %7
%96 = add i64 %95, 1
%97 = getelementptr inbounds i64, i64* %94, i64 %96
store i64 %93, i64* %97
%98 = load i64, i64* %7
%99 = add i64 %98, 1
store i64 %99, i64* %7
br label %100
100: ; preds = %49, %39, %35
br label %101
101: ; preds = %100
%102 = load i64, i64* %12
%103 = add i64 %102, 1
store i64 %103, i64* %12
br label %17
104: ; preds = %17
%105 = load double*, double** %4
%106 = load i64*, i64** %5
%107 = load i64, i64* %7
%108 = load double*, double** %4
%109 = load i64, i64* %7
%110 = getelementptr inbounds double, double* %108, i64 %109
%111 = getelementptr inbounds double, double* %110, i64 1
store double* %111, double** %4
%112 = load i64*, i64** %5
%113 = load i64, i64* %7
%114 = getelementptr inbounds i64, i64* %112, i64 %113
%115 = getelementptr inbounds i64, i64* %114, i64 1
store i64* %115, i64** %5
%116 = load i64, i64* %6
%117 = load i64, i64* %7
%118 = sub i64 %116, %117
%119 = sub i64 %118, 1
store i64 %119, i64* %6
br label %13
120: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @sort_by_abs_coeffs(double* noalias nocapture %0, i64* noalias nocapture %1, i64 %2) {
%4 = icmp ugt i64 %2, 1
br i1 %4, label %5, label %54
5: ; preds = %3, %46
%6 = phi double* [ %48, %46 ], [ %0, %3 ]
%7 = phi i64* [ %50, %46 ], [ %1, %3 ]
%8 = phi i64 [ %52, %46 ], [ %2, %3 ]
br label %9
9: ; preds = %5, %42
%10 = phi i64 [ %44, %42 ], [ 1, %5 ]
%11 = phi i64 [ %43, %42 ], [ 0, %5 ]
%12 = getelementptr inbounds double, double* %6, i64 %10
%13 = load double, double* %12
%14 = tail call double @llvm.fabs.f64(double %13)
%15 = getelementptr inbounds double, double* %6, i64 %11
%16 = load double, double* %15
%17 = tail call double @llvm.fabs.f64(double %16)
%18 = fcmp ogt double %14, %17
br i1 %18, label %19, label %24
19: ; preds = %9
%20 = getelementptr inbounds i64, i64* %7, i64 %11
%21 = load i64, i64* %20
%22 = getelementptr inbounds i64, i64* %7, i64 %10
%23 = load i64, i64* %22
br label %32
24: ; preds = %9
%25 = fcmp oeq double %14, %17
br i1 %25, label %26, label %42
26: ; preds = %24
%27 = getelementptr inbounds i64, i64* %7, i64 %10
%28 = load i64, i64* %27
%29 = getelementptr inbounds i64, i64* %7, i64 %11
%30 = load i64, i64* %29
%31 = icmp ult i64 %28, %30
br i1 %31, label %32, label %42
32: ; preds = %19, %26
%33 = phi i64 [ %23, %19 ], [ %28, %26 ]
%34 = phi i64 [ %21, %19 ], [ %30, %26 ]
store double %13, double* %15
%35 = add i64 %11, 1
%36 = getelementptr inbounds double, double* %6, i64 %35
%37 = load double, double* %36
store double %37, double* %12
store double %16, double* %36
%38 = getelementptr inbounds i64, i64* %7, i64 %11
%39 = getelementptr inbounds i64, i64* %7, i64 %10
store i64 %33, i64* %38
%40 = getelementptr inbounds i64, i64* %7, i64 %35
%41 = load i64, i64* %40
store i64 %41, i64* %39
store i64 %34, i64* %40
br label %42
42: ; preds = %24, %26, %32
%43 = phi i64 [ %35, %32 ], [ %11, %26 ], [ %11, %24 ]
%44 = add nuw i64 %10, 1
%45 = icmp eq i64 %44, %8
br i1 %45, label %46, label %9
46: ; preds = %42
tail call void @sort_by_abs_coeffs(double* %6, i64* %7, i64 %43)
%47 = getelementptr inbounds double, double* %6, i64 1
%48 = getelementptr inbounds double, double* %47, i64 %43
%49 = getelementptr inbounds i64, i64* %7, i64 1
%50 = getelementptr inbounds i64, i64* %49, i64 %43
%51 = xor i64 %43, -1
%52 = add i64 %8, %51
%53 = icmp ugt i64 %52, 1
br i1 %53, label %5, label %54
54: ; preds = %46, %3
ret void
}