#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);
int /*<<< orphan*/ max_heapify(int *, int, int);
void heap_sort(int *arr, int length, int heap_size) {
  int i = 0;
  build_max_heap(arr, length, heap_size);
  for (i = heap_size; i >= 2; i--) {
    int temp = arr[1];
    arr[1] = arr[i];
    arr[i] = temp;
    heap_size -= 1;
    max_heapify(arr, heap_size, 1);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <build_max_heap@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jl     11aa <heap_sort+0x8a>
mov    -0x8(%rbp),%rax
mov    0x4(%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,0x4(%rax)
mov    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    $0x1,%edx
call   1030 <max_heapify@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    114e <heap_sort+0x2e>
add    $0x20,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
define void @heap_sort(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = load i32*, i32** %4
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = call i32 @build_max_heap(i32* %9, i32 %10, i32 %11)
%13 = load i32, i32* %6
store i32 %13, i32* %7
br label %14
14: ; preds = %38, %3
%15 = load i32, i32* %7
%16 = icmp sge i32 %15, 2
br i1 %16, label %17, label %41
17: ; preds = %14
%18 = load i32*, i32** %4
%19 = getelementptr inbounds i32, i32* %18, i64 1
%20 = load i32, i32* %19
store i32 %20, i32* %8
%21 = load i32*, i32** %4
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = load i32*, i32** %4
%27 = getelementptr inbounds i32, i32* %26, i64 1
store i32 %25, i32* %27
%28 = load i32, i32* %8
%29 = load i32*, i32** %4
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
store i32 %28, i32* %32
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, 1
store i32 %34, i32* %6
%35 = load i32*, i32** %4
%36 = load i32, i32* %6
%37 = call i32 @max_heapify(i32* %35, i32 %36, i32 1)
br label %38
38: ; preds = %17
%39 = load i32, i32* %7
%40 = add nsw i32 %39, -1
store i32 %40, i32* %7
br label %14
41: ; preds = %14
ret void
}

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