#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*/ buildHeap(int *, int);
int /*<<< orphan*/ heapSanity(int *, int, int);
int /*<<< orphan*/ heapify(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ swap(int *, int *);
void heapSort(int *a, int n) {
  int i;
  buildHeap(a, n);
  heapSanity(a, i, n);
  for (i = n - 1; i > 0; i--) {
    swap(&a[0], &a[i]);
    heapify(a, i, 0);
  }
}


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 <buildHeap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <heapSanity@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11b1 <heapSort+0x71>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1060 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1050 <heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1173 <heapSort+0x33>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
call   1030 <buildHeap@plt>
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <heapSanity@plt>
cmp    $0x2,%ebp
jl     1187 <heapSort+0x47>
mov    %ebp,%ebp
nop
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
call   1060 <swap@plt>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1050 <heapify@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1160 <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
store i32* %0, i32** %3
store i32 %1, i32* %4
%6 = load i32*, i32** %3
%7 = load i32, i32* %4
%8 = call i32 @buildHeap(i32* %6, i32 %7)
%9 = load i32*, i32** %3
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = call i32 @heapSanity(i32* %9, i32 %10, i32 %11)
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %5
br label %15
15: ; preds = %29, %2
%16 = load i32, i32* %5
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %15
%19 = load i32*, i32** %3
%20 = getelementptr inbounds i32, i32* %19, i64 0
%21 = load i32*, i32** %3
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = call i32 @swap(i32* %20, i32* %24)
%26 = load i32*, i32** %3
%27 = load i32, i32* %5
%28 = call i32 @heapify(i32* %26, i32 %27, i32 0)
br label %29
29: ; preds = %18
%30 = load i32, i32* %5
%31 = add nsw i32 %30, -1
store i32 %31, i32* %5
br label %15
32: ; preds = %15
ret void
}

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