#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*/ HeapAdjust(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ print(int *, int);
void HeapSort(int A[], int heap_size) {
  BuildHeap(A, heap_size);
  int temp;
  for (int i = heap_size - 1; i >= 0; i--) {
    temp = A[0];
    A[0] = A[i];
    A[i] = temp;
    HeapAdjust(A, 0, i);
  }
  print(A, heap_size);
}


x86 assembly (clang, -O0):
<HeapSort>:
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   1030 <BuildHeap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11a2 <HeapSort+0x72>
mov    -0x8(%rbp),%rax
mov    (%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,(%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
xor    %esi,%esi
call   1050 <HeapAdjust@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1154 <HeapSort+0x24>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <print@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HeapSort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1030 <BuildHeap@plt>
test   %r14d,%r14d
jle    1179 <HeapSort+0x49>
mov    %r14d,%ebp
nopw   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    (%r15),%eax
mov    %ebx,%ecx
mov    (%r15,%rcx,4),%edx
mov    %edx,(%r15)
mov    %eax,(%r15,%rcx,4)
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1050 <HeapAdjust@plt>
cmp    $0x1,%rbp
mov    %rbx,%rbp
ja     1150 <HeapSort+0x20>
mov    %r15,%rdi
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <print@plt>

LLVM IR (-O0):
define void @HeapSort(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 @BuildHeap(i32* %7, i32 %8)
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %6
br label %12
12: ; preds = %34, %2
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %37
15: ; preds = %12
%16 = load i32*, i32** %3
%17 = getelementptr inbounds i32, i32* %16, i64 0
%18 = load i32, i32* %17
store i32 %18, i32* %5
%19 = load i32*, i32** %3
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = load i32*, i32** %3
%25 = getelementptr inbounds i32, i32* %24, i64 0
store i32 %23, i32* %25
%26 = load i32, i32* %5
%27 = load i32*, i32** %3
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
store i32 %26, i32* %30
%31 = load i32*, i32** %3
%32 = load i32, i32* %6
%33 = call i32 @HeapAdjust(i32* %31, i32 0, i32 %32)
br label %34
34: ; preds = %15
%35 = load i32, i32* %6
%36 = add nsw i32 %35, -1
store i32 %36, i32* %6
br label %12
37: ; preds = %12
%38 = load i32*, i32** %3
%39 = load i32, i32* %4
%40 = call i32 @print(i32* %38, i32 %39)
ret void
}

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