#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*/ buildmaxheap(double *, int);
int /*<<< orphan*/ maxheapify(double *, int /*<<< orphan*/, int);
void heapsort(double A[], int n) {
  int i, heapsize = n;
  double temp;
  buildmaxheap(A, n);
  for (i = n; i >= 1; i--) {
    temp = A[0];
    A[0] = A[i];
    A[i] = temp;
    heapsize--;
    maxheapify(A, 0, heapsize);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <buildmaxheap@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jl     11aa <heapsort+0x8a>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
movsd  -0x20(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <maxheapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1147 <heapsort+0x27>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <buildmaxheap@plt>
test   %ebp,%ebp
jle    116c <heapsort+0x4c>
mov    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  (%rbx),%xmm0
movsd  (%rbx,%rbp,8),%xmm1
movsd  %xmm1,(%rbx)
movsd  %xmm0,(%rbx,%rbp,8)
lea    -0x1(%rbp),%r14
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <maxheapify@plt>
cmp    $0x1,%rbp
mov    %r14,%rbp
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 i32
%7 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
store i32 %8, i32* %6
%9 = load double*, double** %3
%10 = load i32, i32* %4
%11 = call i32 @buildmaxheap(double* %9, i32 %10)
%12 = load i32, i32* %4
store i32 %12, i32* %5
br label %13
13: ; preds = %37, %2
%14 = load i32, i32* %5
%15 = icmp sge i32 %14, 1
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = load double*, double** %3
%18 = getelementptr inbounds double, double* %17, i64 0
%19 = load double, double* %18
store double %19, double* %7
%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* %7
%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
%32 = load i32, i32* %6
%33 = add nsw i32 %32, -1
store i32 %33, i32* %6
%34 = load double*, double** %3
%35 = load i32, i32* %6
%36 = call i32 @maxheapify(double* %34, i32 0, i32 %35)
br label %37
37: ; preds = %16
%38 = load i32, i32* %5
%39 = add nsw i32 %38, -1
store i32 %39, i32* %5
br label %13
40: ; preds = %13
ret void
}

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