#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*/ max_heap(int *, int, int);
int heap_sort(int *array, int heapSize) {
  int i;
  int exchange;
  build_max_heap(array, heapSize);
  for (i = heapSize; i > 1; i--) {
    exchange = *(array + i);
    *(array + i) = *(array);
    *(array) = exchange;
    heapSize = heapSize - 1;
    max_heap(array, 1, heapSize);
  }
  return 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   1040 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    119b <heap_sort+0x7b>
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    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1030 <max_heap@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1141 <heap_sort+0x21>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

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

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

LLVM IR (-O2):
define i32 @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 ], [ %12, %7 ]
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = load i32, i32* %9
%11 = load i32, i32* %0
store i32 %11, i32* %9
store i32 %10, i32* %0
%12 = add nsw i64 %8, -1
%13 = trunc i64 %12 to i32
%14 = tail call i32 @max_heap(i32* %0, i32 1, i32 %13)
%15 = icmp ugt i64 %8, 2
br i1 %15, label %7, label %16
16: ; preds = %7, %2
ret i32 0
}