#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*/ exchange_num(int *, int *);
int /*<<< orphan*/ max_heapify(int *, int /*<<< orphan*/, int);
void heap_sort(int *num, int num_length) {
  int leaf;
  int heap_size;
  heap_size = num_length;
  build_max_heap(num, num_length);
  for (leaf = (num_length - 1); leaf > 0; leaf--) {
    exchange_num(&num[0], &num[leaf]);
    heap_size = heap_size - 1;
    max_heapify(num, 0, heap_size);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
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   1050 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11a1 <heap_sort+0x71>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <exchange_num@plt>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1040 <max_heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    115a <heap_sort+0x2a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
call   1050 <build_max_heap@plt>
cmp    $0x2,%ebp
jl     1177 <heap_sort+0x47>
mov    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
call   1030 <exchange_num@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <max_heapify@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1150 <heap_sort+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort(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
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %5
br label %13
13: ; preds = %29, %2
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %13
%17 = load i32*, i32** %3
%18 = getelementptr inbounds i32, i32* %17, i64 0
%19 = load i32*, i32** %3
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = call i32 @exchange_num(i32* %18, i32* %22)
%24 = load i32, i32* %6
%25 = sub nsw i32 %24, 1
store i32 %25, i32* %6
%26 = load i32*, i32** %3
%27 = load i32, i32* %6
%28 = call i32 @max_heapify(i32* %26, i32 0, i32 %27)
br label %29
29: ; preds = %16
%30 = load i32, i32* %5
%31 = add nsw i32 %30, -1
store i32 %31, i32* %5
br label %13
32: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @heap_sort(i32* %0, i32 %1) {
%3 = tail call i32 @build_max_heap(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %16
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 = tail call i32 @exchange_num(i32* %0, i32* %12)
%14 = tail call i32 @max_heapify(i32* %0, i32 0, i32 %10)
%15 = icmp ugt i64 %8, 2
br i1 %15, label %7, label %16
16: ; preds = %7, %2
ret void
}