#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*/ buidMaxHeap(int *, int);
int /*<<< orphan*/ maxHeapify(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void heapSort(int A[], int n) {
  buidMaxHeap(A, n);
  int i;
  for (i = n; i >= 2; i--) {
    swap(&A[1], &A[i]);
    n--;
    maxHeapify(A, n, 1);
  }
}


x86 assembly (clang, -O0):
<heapSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <buidMaxHeap@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jl     119f <heapSort+0x6f>
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <swap@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x1,%edx
call   1040 <maxHeapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1151 <heapSort+0x21>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <buidMaxHeap@plt>
cmp    $0x2,%ebx
jl     118c <heapSort+0x5c>
lea    0x4(%r14),%r15
mov    %ebx,%r13d
lea    (%r14,%r13,4),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <swap@plt>
lea    -0x1(%r13),%r12
mov    %r14,%rdi
mov    %r12d,%esi
mov    $0x1,%edx
call   1040 <maxHeapify@plt>
add    $0xfffffffffffffffc,%rbx
cmp    $0x2,%r13
mov    %r12,%r13
ja     1160 <heapSort+0x30>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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