#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 build_max_heap(int *, int);
int /*<<< orphan*/ heapify(int *, int, int /*<<< orphan*/);
void heap_sort(int array[], int array_size) {
  int heap_size = build_max_heap(array, array_size);
  int loop_v = heap_size;
  for (; loop_v >= 1; loop_v--) {
    int temp = array[0];
    array[0] = array[loop_v];
    array[loop_v] = temp;
    heap_size--;
    heapify(array, heap_size, 0);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
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 <build_max_heap@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     119b <heap_sort+0x7b>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1040 <heapify@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1144 <heap_sort+0x24>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <build_max_heap@plt>
test   %eax,%eax
jle    1168 <heap_sort+0x48>
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%eax
mov    (%r14,%rbx,4),%ecx
mov    %ecx,(%r14)
mov    %eax,(%r14,%rbx,4)
lea    -0x1(%rbx),%r15
mov    %r14,%rdi
mov    %r15d,%esi
xor    %edx,%edx
call   1040 <heapify@plt>
cmp    $0x1,%rbx
mov    %r15,%rbx
ja     1140 <heap_sort+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @heap_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32*, i32** %3
%9 = load i32, i32* %4
%10 = call i32 @build_max_heap(i32* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
store i32 %11, i32* %6
br label %12
12: ; preds = %36, %2
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 1
br i1 %14, label %15, label %39
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* %7
%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* %7
%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* %5
%32 = add nsw i32 %31, -1
store i32 %32, i32* %5
%33 = load i32*, i32** %3
%34 = load i32, i32* %5
%35 = call i32 @heapify(i32* %33, i32 %34, i32 0)
br label %36
36: ; preds = %15
%37 = load i32, i32* %6
%38 = add nsw i32 %37, -1
store i32 %38, i32* %6
br label %12
39: ; preds = %12
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 %3, 0
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = zext i32 %3 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ %6, %5 ], [ %12, %7 ]
%9 = load i32, i32* %0
%10 = getelementptr inbounds i32, i32* %0, i64 %8
%11 = load i32, i32* %10
store i32 %11, i32* %0
store i32 %9, i32* %10
%12 = add nsw i64 %8, -1
%13 = trunc i64 %12 to i32
%14 = tail call i32 @heapify(i32* %0, i32 %13, i32 0)
%15 = icmp ugt i64 %8, 1
br i1 %15, label %7, label %16
16: ; preds = %7, %2
ret void
}