#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*/ bubble_down(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ heapify(int *, int);
int /*<<< orphan*/ swap(int *, int *);
void heapSort(int *A, int size) {
  int i;
  heapify(A, size);
  for (i = 0; i < size; i++) {
    swap(&A[0], &A[size - 1 - i]);
    bubble_down(A, size - i - 1, 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   1040 <heapify@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    119f <heapSort+0x6f>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
sub    -0x10(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    -0x10(%rbp),%esi
sub    $0x1,%esi
xor    %edx,%edx
call   1030 <bubble_down@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1152 <heapSort+0x22>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
call   1040 <heapify@plt>
test   %ebp,%ebp
jle    1174 <heapSort+0x44>
mov    %ebp,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xffffffffffffffff,%rbx
movslq %ebx,%rbp
lea    (%r14,%rbp,4),%rsi
mov    %r14,%rdi
call   1050 <swap@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1030 <bubble_down@plt>
test   %rbx,%rbx
jne    1150 <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 @heapify(i32* %6, i32 %7)
store i32 0, i32* %5
br label %9
9: ; preds = %30, %2
%10 = load i32, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %33
13: ; preds = %9
%14 = load i32*, i32** %3
%15 = getelementptr inbounds i32, i32* %14, i64 0
%16 = load i32*, i32** %3
%17 = load i32, i32* %4
%18 = sub nsw i32 %17, 1
%19 = load i32, i32* %5
%20 = sub nsw i32 %18, %19
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %16, i64 %21
%23 = call i32 @swap(i32* %15, i32* %22)
%24 = load i32*, i32** %3
%25 = load i32, i32* %4
%26 = load i32, i32* %5
%27 = sub nsw i32 %25, %26
%28 = sub nsw i32 %27, 1
%29 = call i32 @bubble_down(i32* %24, i32 %28, i32 0)
br label %30
30: ; preds = %13
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
store i32 %32, i32* %5
br label %9
33: ; preds = %9
ret void
}

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