#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_heapify(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ swap(int, int);
void heap_sort(int arr[], int len) {
  int i = 0;
  if (arr == NULL || len <= 1) {
    return;
  }
  build_max_heap(arr, len);
  for (i = len - 1; i >= 1; --i) {
    swap(arr[0], arr[i]);
    max_heapify(arr, --len, 0);
  }
}


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)
cmpq   $0x0,-0x8(%rbp)
je     115b <heap_sort+0x2b>
cmpl   $0x1,-0xc(%rbp)
jg     1160 <heap_sort+0x30>
jmp    11b7 <heap_sort+0x87>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jl     11b7 <heap_sort+0x87>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0xffffffff,%esi
mov    %esi,-0xc(%rbp)
xor    %edx,%edx
call   1030 <max_heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1175 <heap_sort+0x45>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1177 <heap_sort+0x47>
mov    %esi,%ebp
cmp    $0x2,%esi
jl     1177 <heap_sort+0x47>
mov    %rdi,%r14
mov    %ebp,%esi
call   1040 <build_max_heap@plt>
mov    %ebp,%ebp
nopl   0x0(%rax)
lea    -0x1(%rbp),%rbx
mov    (%r14),%edi
mov    %ebx,%eax
mov    (%r14,%rax,4),%esi
call   1050 <swap@plt>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1030 <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
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %8, %2
br label %38
12: ; preds = %8
%13 = load i32*, i32** %3
%14 = load i32, i32* %4
%15 = call i32 @build_max_heap(i32* %13, i32 %14)
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %5
br label %18
18: ; preds = %35, %12
%19 = load i32, i32* %5
%20 = icmp sge i32 %19, 1
br i1 %20, label %21, label %38
21: ; preds = %18
%22 = load i32*, i32** %3
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32, i32* %23
%25 = load i32*, i32** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
%30 = call i32 @swap(i32 %24, i32 %29)
%31 = load i32*, i32** %3
%32 = load i32, i32* %4
%33 = add nsw i32 %32, -1
store i32 %33, i32* %4
%34 = call i32 @max_heapify(i32* %31, i32 %33, i32 0)
br label %35
35: ; preds = %21
%36 = load i32, i32* %5
%37 = add nsw i32 %36, -1
store i32 %37, i32* %5
br label %18
38: ; preds = %11, %18
ret void
}

LLVM IR (-O2):
define void @heap_sort(i32* %0, i32 %1) {
%3 = icmp eq i32* %0, null
%4 = icmp slt i32 %1, 2
%5 = or i1 %3, %4
br i1 %5, label %20, label %6
6: ; preds = %2
%7 = tail call i32 @build_max_heap(i32* %0, i32 %1)
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %6, %9
%10 = phi i64 [ %8, %6 ], [ %11, %9 ]
%11 = add nsw i64 %10, -1
%12 = trunc i64 %11 to i32
%13 = load i32, i32* %0
%14 = and i64 %11, 4294967295
%15 = getelementptr inbounds i32, i32* %0, i64 %14
%16 = load i32, i32* %15
%17 = tail call i32 @swap(i32 %13, i32 %16)
%18 = tail call i32 @max_heapify(i32* %0, i32 %12, i32 0)
%19 = icmp ugt i64 %10, 2
br i1 %19, label %9, label %20
20: ; preds = %9, %2
ret void
}