#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(double *);
int /*<<< orphan*/ dbl_reverse(double *, size_t);
int /*<<< orphan*/ dbl_sift(double *, size_t, size_t);
void dbl_heapsort(double *array, size_t n, int dir) {
  size_t l, r;
  double t;
  assert(array);
  if (n < 2)
    return;
  l = n / 2;
  r = n - 1;
  while (l > 0)
    dbl_sift(array, --l, r);
  while (1) {
    t = array[0];
    array[0] = array[r];
    array[r] = t;
    if (--r <= 0)
      break;
    dbl_sift(array, 0, r);
  }
  if (dir < 0)
    dbl_reverse(array, n);
}


x86 assembly (clang, -O0):
<dbl_heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <assert@plt>
cmpq   $0x2,-0x10(%rbp)
jae    115c <dbl_heapsort+0x2c>
jmp    121e <dbl_heapsort+0xee>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    119d <dbl_heapsort+0x6d>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0xffffffffffffffff,%rsi
mov    %rsi,-0x20(%rbp)
mov    -0x28(%rbp),%rdx
call   1030 <dbl_sift@plt>
jmp    1174 <dbl_heapsort+0x44>
jmp    11a2 <dbl_heapsort+0x72>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x30(%rbp),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
ja     11f1 <dbl_heapsort+0xc1>
jmp    1207 <dbl_heapsort+0xd7>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <dbl_sift@plt>
jmp    11a2 <dbl_heapsort+0x72>
cmpl   $0x0,-0x14(%rbp)
jge    121e <dbl_heapsort+0xee>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <dbl_reverse@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dbl_heapsort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <assert@plt>
cmp    $0x2,%r14
jb     11ca <dbl_heapsort+0x9a>
lea    -0x1(%r14),%r12
mov    %r14,%rbp
shr    %rbp
add    $0xffffffffffffffff,%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1030 <dbl_sift@plt>
add    $0xffffffffffffffff,%rbp
jb     1160 <dbl_heapsort+0x30>
movsd  (%rbx),%xmm0
movsd  -0x8(%rbx,%r14,8),%xmm1
movsd  %xmm1,(%rbx)
movsd  %xmm0,-0x8(%rbx,%r14,8)
mov    %r14,%rbp
add    $0xfffffffffffffffe,%rbp
je     11c5 <dbl_heapsort+0x95>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1030 <dbl_sift@plt>
movsd  (%rbx),%xmm0
movsd  (%rbx,%rbp,8),%xmm1
movsd  %xmm1,(%rbx)
movsd  %xmm0,(%rbx,%rbp,8)
add    $0xffffffffffffffff,%rbp
jne    11a0 <dbl_heapsort+0x70>
test   %r15d,%r15d
js     11d3 <dbl_heapsort+0xa3>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <dbl_reverse@plt>

LLVM IR (-O0):
define void @dbl_heapsort(double* %0, i64 %1, i32 %2) {
%4 = alloca double*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca double
store double* %0, double** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%10 = load double*, double** %4
%11 = call i32 @assert(double* %10)
%12 = load i64, i64* %5
%13 = icmp ult i64 %12, 2
br i1 %13, label %14, label %15
14: ; preds = %3
br label %59
15: ; preds = %3
%16 = load i64, i64* %5
%17 = udiv i64 %16, 2
store i64 %17, i64* %7
%18 = load i64, i64* %5
%19 = sub i64 %18, 1
store i64 %19, i64* %8
br label %20
20: ; preds = %23, %15
%21 = load i64, i64* %7
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load double*, double** %4
%25 = load i64, i64* %7
%26 = add i64 %25, -1
store i64 %26, i64* %7
%27 = load i64, i64* %8
%28 = call i32 @dbl_sift(double* %24, i64 %26, i64 %27)
br label %20
29: ; preds = %20
br label %30
30: ; preds = %29, %48
%31 = load double*, double** %4
%32 = getelementptr inbounds double, double* %31, i64 0
%33 = load double, double* %32
store double %33, double* %9
%34 = load double*, double** %4
%35 = load i64, i64* %8
%36 = getelementptr inbounds double, double* %34, i64 %35
%37 = load double, double* %36
%38 = load double*, double** %4
%39 = getelementptr inbounds double, double* %38, i64 0
store double %37, double* %39
%40 = load double, double* %9
%41 = load double*, double** %4
%42 = load i64, i64* %8
%43 = getelementptr inbounds double, double* %41, i64 %42
store double %40, double* %43
%44 = load i64, i64* %8
%45 = add i64 %44, -1
store i64 %45, i64* %8
%46 = icmp ule i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %30
br label %52
48: ; preds = %30
%49 = load double*, double** %4
%50 = load i64, i64* %8
%51 = call i32 @dbl_sift(double* %49, i64 0, i64 %50)
br label %30
52: ; preds = %47
%53 = load i32, i32* %6
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load double*, double** %4
%57 = load i64, i64* %5
%58 = call i32 @dbl_reverse(double* %56, i64 %57)
br label %59
59: ; preds = %14, %55, %52
ret void
}

LLVM IR (-O2):
define void @dbl_heapsort(double* %0, i64 %1, i32 %2) {
%4 = tail call i32 @assert(double* %0)
%5 = icmp ult i64 %1, 2
br i1 %5, label %32, label %6
6: ; preds = %3
%7 = add i64 %1, -1
%8 = lshr i64 %1, 1
br label %15
9: ; preds = %15
%10 = load double, double* %0
%11 = getelementptr inbounds double, double* %0, i64 %7
%12 = load double, double* %11
store double %12, double* %0
store double %10, double* %11
%13 = add i64 %1, -2
%14 = icmp eq i64 %13, 0
br i1 %14, label %28, label %20
15: ; preds = %6, %15
%16 = phi i64 [ %17, %15 ], [ %8, %6 ]
%17 = add nsw i64 %16, -1
%18 = tail call i32 @dbl_sift(double* %0, i64 %17, i64 %7)
%19 = icmp eq i64 %17, 0
br i1 %19, label %9, label %15
20: ; preds = %9, %20
%21 = phi i64 [ %26, %20 ], [ %13, %9 ]
%22 = tail call i32 @dbl_sift(double* %0, i64 0, i64 %21)
%23 = load double, double* %0
%24 = getelementptr inbounds double, double* %0, i64 %21
%25 = load double, double* %24
store double %25, double* %0
store double %23, double* %24
%26 = add i64 %21, -1
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %20
28: ; preds = %20, %9
%29 = icmp slt i32 %2, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = tail call i32 @dbl_reverse(double* %0, i64 %1)
br label %32
32: ; preds = %28, %30, %3
ret void
}