#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*/ percolateDown(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printData(int *, int);
int /*<<< orphan*/ printf(char *, int);
void heapSort(int data[], int size) {
  int i;
  buildHeap(data, size);
  for (i = size - 1; i > 0; i--) {
    {
      int _temp = *(&data[i]);
      *(&data[i]) = *(&data[0]);
      *(&data[0]) = _temp;
    };
    percolateDown(data, i, 0);
    printf("%2d: ", i);
    printData(data, 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   1040 <buildHeap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11cd <heapSort+0x8d>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1060 <percolateDown@plt>
mov    -0x10(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <printData@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1164 <heapSort+0x24>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%rbx
call   1040 <buildHeap@plt>
cmp    $0x2,%r15d
jl     11ab <heapSort+0x6b>
mov    %r15d,%r12d
lea    0xe9d(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0x1(%r12),%rbp
mov    %ebp,%eax
mov    (%rbx,%rax,4),%ecx
mov    (%rbx),%edx
mov    %edx,(%rbx,%rax,4)
mov    %ecx,(%rbx)
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1060 <percolateDown@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <printData@plt>
cmp    $0x2,%r12
mov    %rbp,%r12
ja     1170 <heapSort+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%2d: \00"
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* %5
br label %12
12: ; preds = %39, %2
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %42
15: ; preds = %12
%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 = load i32, i32* %19
store i32 %20, i32* %6
%21 = load i32*, i32** %3
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32, i32* %22
%24 = load i32*, i32** %3
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
store i32 %23, i32* %27
%28 = load i32, i32* %6
%29 = load i32*, i32** %3
%30 = getelementptr inbounds i32, i32* %29, i64 0
store i32 %28, i32* %30
%31 = load i32*, i32** %3
%32 = load i32, i32* %5
%33 = call i32 @percolateDown(i32* %31, i32 %32, i32 0)
%34 = load i32, i32* %5
%35 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i32*, i32** %3
%37 = load i32, i32* %4
%38 = call i32 @printData(i32* %36, i32 %37)
br label %39
39: ; preds = %15
%40 = load i32, i32* %5
%41 = add nsw i32 %40, -1
store i32 %41, i32* %5
br label %12
42: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%2d: \00"
define void @heapSort(i32* %0, i32 %1) {
%3 = tail call i32 @buildHeap(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %19
5: ; preds = %2
%6 = zext i32 %1 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ %6, %5 ], [ %9, %7 ]
%9 = add nsw i64 %8, -1
%10 = trunc i64 %9 to i32
%11 = and i64 %9, 4294967295
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = load i32, i32* %12
%14 = load i32, i32* %0
store i32 %14, i32* %12
store i32 %13, i32* %0
%15 = tail call i32 @percolateDown(i32* %0, i32 %10, i32 0)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %10)
%17 = tail call i32 @printData(i32* %0, i32 %1)
%18 = icmp ugt i64 %8, 2
br i1 %18, label %7, label %19
19: ; preds = %7, %2
ret void
}