#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*/ build_max_heap(int *, int);
int /*<<< orphan*/ heap_max_balance(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ swap(int *, int, int /*<<< orphan*/);
void heapsort(int array[], int num) {
  int i;
  int heapsize = num;
  build_max_heap(array, num);
  for (i = num; i > 0; i--) {
    heapsize--;
    swap(array, i, 0);
    heap_max_balance(array, 0, heapsize);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1194 <heapsort+0x64>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <heap_max_balance@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1157 <heapsort+0x27>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
call   1040 <build_max_heap@plt>
test   %ebx,%ebx
jle    1172 <heapsort+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1050 <swap@plt>
lea    -0x1(%rbx),%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <heap_max_balance@plt>
cmp    $0x1,%ebx
mov    %ebp,%ebx
ja     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
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
store i32 %7, i32* %6
%8 = load i32*, i32** %3
%9 = load i32, i32* %4
%10 = call i32 @build_max_heap(i32* %8, i32 %9)
%11 = load i32, i32* %4
store i32 %11, i32* %5
br label %12
12: ; preds = %24, %2
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = load i32, i32* %6
%17 = add nsw i32 %16, -1
store i32 %17, i32* %6
%18 = load i32*, i32** %3
%19 = load i32, i32* %5
%20 = call i32 @swap(i32* %18, i32 %19, i32 0)
%21 = load i32*, i32** %3
%22 = load i32, i32* %6
%23 = call i32 @heap_max_balance(i32* %21, i32 0, i32 %22)
br label %24
24: ; preds = %15
%25 = load i32, i32* %5
%26 = add nsw i32 %25, -1
store i32 %26, i32* %5
br label %12
27: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @heapsort(i32* %0, i32 %1) {
%3 = tail call i32 @build_max_heap(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %11
5: ; preds = %2, %5
%6 = phi i32 [ %7, %5 ], [ %1, %2 ]
%7 = add nsw i32 %6, -1
%8 = tail call i32 @swap(i32* %0, i32 %6, i32 0)
%9 = tail call i32 @heap_max_balance(i32* %0, i32 0, i32 %7)
%10 = icmp ugt i32 %6, 1
br i1 %10, label %5, label %11
11: ; preds = %5, %2
ret void
}