#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_head(int *, int);
void heap_sort(int a[], int length) {
  for (int i = length; i > 0; i--) {
    int heap_size = i;
    build_head(a, heap_size);
    int temp = a[heap_size - 1];
    a[heap_size - 1] = a[0];
    a[0] = temp;
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1181 <heap_sort+0x71>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <build_head@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1125 <heap_sort+0x15>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1147 <heap_sort+0x37>
mov    %rdi,%r14
mov    %esi,%ebx
nopl   (%rax)
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <build_head@plt>
lea    -0x1(%rbx),%rax
mov    %eax,%ecx
mov    (%r14,%rcx,4),%edx
mov    (%r14),%esi
mov    %esi,(%r14,%rcx,4)
mov    %edx,(%r14)
cmp    $0x1,%rbx
mov    %rax,%rbx
ja     1120 <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
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
store i32 %8, i32* %5
br label %9
9: ; preds = %34, %2
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %37
12: ; preds = %9
%13 = load i32, i32* %5
store i32 %13, i32* %6
%14 = load i32*, i32** %3
%15 = load i32, i32* %6
%16 = call i32 @build_head(i32* %14, i32 %15)
%17 = load i32*, i32** %3
%18 = load i32, i32* %6
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %17, i64 %20
%22 = load i32, i32* %21
store i32 %22, i32* %7
%23 = load i32*, i32** %3
%24 = getelementptr inbounds i32, i32* %23, i64 0
%25 = load i32, i32* %24
%26 = load i32*, i32** %3
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %26, i64 %29
store i32 %25, i32* %30
%31 = load i32, i32* %7
%32 = load i32*, i32** %3
%33 = getelementptr inbounds i32, i32* %32, i64 0
store i32 %31, i32* %33
br label %34
34: ; preds = %12
%35 = load i32, i32* %5
%36 = add nsw i32 %35, -1
store i32 %36, i32* %5
br label %9
37: ; preds = %9
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 [ %5, %4 ], [ %11, %7 ]
%9 = trunc i64 %8 to i32
%10 = tail call i32 @build_head(i32* %0, i32 %9)
%11 = add nsw i64 %8, -1
%12 = and i64 %11, 4294967295
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = load i32, i32* %0
store i32 %15, i32* %13
store i32 %14, i32* %0
%16 = icmp ugt i64 %8, 1
br i1 %16, label %7, label %6
}