#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*/ makeHeap(double *, int);
int /*<<< orphan*/ siftDown(double *, int /*<<< orphan*/, int);
void heapsort(double heaps[], int count) {
  makeHeap(heaps, count);
  for (int end = count - 1; end > 0; end--) {
    do {
      double t = heaps[0];
      heaps[0] = heaps[end];
      heaps[end] = t;
    } while (0);
    siftDown(heaps, 0, end - 1);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <makeHeap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11a6 <heapsort+0x86>
jmp    1153 <heapsort+0x33>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x18(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1040 <siftDown@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1144 <heapsort+0x24>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
call   1030 <makeHeap@plt>
cmp    $0x2,%ebp
jl     1172 <heapsort+0x52>
mov    %ebp,%ebx
add    $0xfffffffe,%ebp
add    $0x1,%rbx
nopl   0x0(%rax)
lea    -0x2(%rbx),%eax
movsd  (%r14),%xmm0
movsd  (%r14,%rax,8),%xmm1
movsd  %xmm1,(%r14)
movsd  %xmm0,(%r14,%rax,8)
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <siftDown@plt>
add    $0xffffffff,%ebp
add    $0xffffffffffffffff,%rbx
cmp    $0x2,%rbx
ja     1140 <heapsort+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @heapsort(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
%7 = load double*, double** %3
%8 = load i32, i32* %4
%9 = call i32 @makeHeap(double* %7, i32 %8)
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %5
br label %12
12: ; preds = %37, %2
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %40
15: ; preds = %12
br label %16
16: ; preds = %15
%17 = load double*, double** %3
%18 = getelementptr inbounds double, double* %17, i64 0
%19 = load double, double* %18
store double %19, double* %6
%20 = load double*, double** %3
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
%24 = load double, double* %23
%25 = load double*, double** %3
%26 = getelementptr inbounds double, double* %25, i64 0
store double %24, double* %26
%27 = load double, double* %6
%28 = load double*, double** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
store double %27, double* %31
br label %32
32: ; preds = %16
%33 = load double*, double** %3
%34 = load i32, i32* %5
%35 = sub nsw i32 %34, 1
%36 = call i32 @siftDown(double* %33, i32 0, i32 %35)
br label %37
37: ; preds = %32
%38 = load i32, i32* %5
%39 = add nsw i32 %38, -1
store i32 %39, i32* %5
br label %12
40: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @heapsort(double* %0, i32 %1) {
%3 = tail call i32 @makeHeap(double* %0, i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = zext i32 %1 to i64
br label %8
7: ; preds = %8, %2
ret void
8: ; preds = %5, %8
%9 = phi i64 [ %6, %5 ], [ %11, %8 ]
%10 = trunc i64 %9 to i32
%11 = add nsw i64 %9, -1
%12 = load double, double* %0
%13 = and i64 %11, 4294967295
%14 = getelementptr inbounds double, double* %0, i64 %13
%15 = load double, double* %14
store double %15, double* %0
store double %12, double* %14
%16 = add nsw i32 %10, -2
%17 = tail call i32 @siftDown(double* %0, i32 0, i32 %16)
%18 = icmp ugt i64 %9, 2
br i1 %18, label %8, label %7
}