#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*/ fnBuildMaxHeap(int *, int);
int /*<<< orphan*/ fnMaxHeapify(int *, int, int);
void fnHeapSort(int a[], int n) {
  int temp, i;
  fnBuildMaxHeap(a, n);
  for (i = n; i >= 2; i--) {
    temp = a[1];
    a[1] = a[i];
    a[i] = temp;
    fnMaxHeapify(a, 1, i - 1);
  }
}


x86 assembly (clang, -O0):
<fnHeapSort>:
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   1040 <fnBuildMaxHeap@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jl     1197 <fnHeapSort+0x77>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
mov    $0x1,%esi
call   1030 <fnMaxHeapify@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1141 <fnHeapSort+0x21>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnHeapSort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <fnBuildMaxHeap@plt>
cmp    $0x2,%ebp
jl     1169 <fnHeapSort+0x49>
mov    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x4(%rbx),%eax
mov    (%rbx,%rbp,4),%ecx
mov    %ecx,0x4(%rbx)
mov    %eax,(%rbx,%rbp,4)
lea    -0x1(%rbp),%r14
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1030 <fnMaxHeapify@plt>
cmp    $0x2,%rbp
mov    %r14,%rbp
ja     1140 <fnHeapSort+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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