#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_heap_tree(int *, int);
int /*<<< orphan*/ swap(int *, int *);
void heap_sort(int *array, int n) {
  for (int i = 0; i < n; i++) {
    build_heap_tree(array, n - i);
    swap(&array[0], &array[n - 1 - i]);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    117e <heap_sort+0x5e>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    -0x10(%rbp),%esi
call   1030 <build_heap_tree@plt>
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   1040 <swap@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <heap_sort+0x16>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1152 <heap_sort+0x32>
mov    %rdi,%r14
mov    %esi,%ebx
nopl   (%rax)
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <build_heap_tree@plt>
add    $0xffffffffffffffff,%rbx
movslq %ebx,%rax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
call   1040 <swap@plt>
test   %rbx,%rbx
jne    1130 <heap_sort+0x10>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @heap_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %26, %2
%7 = load i32, i32* %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %29
10: ; preds = %6
%11 = load i32*, i32** %3
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = sub nsw i32 %12, %13
%15 = call i32 @build_heap_tree(i32* %11, i32 %14)
%16 = load i32*, i32** %3
%17 = getelementptr inbounds i32, i32* %16, i64 0
%18 = load i32*, i32** %3
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = load i32, i32* %5
%22 = sub nsw i32 %20, %21
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %18, i64 %23
%25 = call i32 @swap(i32* %17, i32* %24)
br label %26
26: ; preds = %10
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %6
29: ; preds = %6
ret void
}

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