#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(int *, int);
int /*<<< orphan*/ maxHeapify(int *, int /*<<< orphan*/, int);
void heapSort(int *A, int n) {
  int length = n;
  int temp;
  buildMaxHeap(A, n);
  for (int i = length; i > 0; i--) {
    temp = *(A);
    *(A) = *(A + i);
    *(A + i) = temp;
    n--;
    maxHeapify(A, 0, n);
  }
}


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,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <buildMaxHeap@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    119e <heapSort+0x7e>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <maxHeapify@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%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,%r14
call   1030 <buildMaxHeap@plt>
test   %ebp,%ebp
jle    1167 <heapSort+0x47>
mov    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%eax
mov    (%r14,%rbp,4),%ecx
mov    %ecx,(%r14)
mov    %eax,(%r14,%rbp,4)
lea    -0x1(%rbp),%rbx
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <maxHeapify@plt>
cmp    $0x1,%rbp
mov    %rbx,%rbp
ja     1140 <heapSort+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define void @heapSort(i32* %0, i32 %1) {
%3 = tail call i32 @buildMaxHeap(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 0
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 ], [ %13, %8 ]
%10 = load i32, i32* %0
%11 = getelementptr inbounds i32, i32* %0, i64 %9
%12 = load i32, i32* %11
store i32 %12, i32* %0
store i32 %10, i32* %11
%13 = add nsw i64 %9, -1
%14 = trunc i64 %13 to i32
%15 = tail call i32 @maxHeapify(i32* %0, i32 0, i32 %14)
%16 = icmp ugt i64 %9, 1
br i1 %16, label %8, label %7
}