#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*/ adjust_heap_max(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ build_heap_max(int *, int);
int /*<<< orphan*/ print_heap(char *, int *, int);
int /*<<< orphan*/ swap(int *, int /*<<< orphan*/, int);
void heap_sort_max(int matrix[], int length) {
  int i;
  int ilength = length;
  print_heap("Before sort:", matrix, length);
  build_heap_max(matrix, length);
  print_heap("Built  heap:", matrix, length);
  for (i = length - 1; i >= 1; i--) {
    swap(matrix, 0, i);
    length--;
    adjust_heap_max(matrix, 0, length);
    print_heap("Sort   heap:", matrix, ilength);
  }
}


x86 assembly (clang, -O0):
<heap_sort_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe9d(%rip),%rdi
call   1050 <print_heap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <build_heap_max@plt>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe8b(%rip),%rdi
call   1050 <print_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jl     11e0 <heap_sort_max+0xa0>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1060 <swap@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1040 <adjust_heap_max@plt>
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe4d(%rip),%rdi
call   1050 <print_heap@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1190 <heap_sort_max+0x50>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort_max>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r12
lea    0xeab(%rip),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1050 <print_heap@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1030 <build_heap_max@plt>
lea    0xe9b(%rip),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1050 <print_heap@plt>
cmp    $0x2,%r14d
jl     11c1 <heap_sort_max+0x81>
lea    0x1(%r14),%ebx
lea    0xe8c(%rip),%r15
xchg   %ax,%ax
lea    -0x2(%rbx),%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <swap@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <adjust_heap_max@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1050 <print_heap@plt>
add    $0xffffffff,%ebx
cmp    $0x2,%ebx
ja     1190 <heap_sort_max+0x50>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Before sort:\00"
@.str.1 = [13 x i8] c"Built heap:\00"
@.str.2 = [13 x i8] c"Sort heap:\00"
define void @heap_sort_max(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* %4
store i32 %7, i32* %6
%8 = load i32*, i32** %3
%9 = load i32, i32* %4
%10 = call i32 @print_heap(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32* %8, i32 %9)
%11 = load i32*, i32** %3
%12 = load i32, i32* %4
%13 = call i32 @build_heap_max(i32* %11, i32 %12)
%14 = load i32*, i32** %3
%15 = load i32, i32* %4
%16 = call i32 @print_heap(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32* %14, i32 %15)
%17 = load i32, i32* %4
%18 = sub nsw i32 %17, 1
store i32 %18, i32* %5
br label %19
19: ; preds = %34, %2
%20 = load i32, i32* %5
%21 = icmp sge i32 %20, 1
br i1 %21, label %22, label %37
22: ; preds = %19
%23 = load i32*, i32** %3
%24 = load i32, i32* %5
%25 = call i32 @swap(i32* %23, i32 0, i32 %24)
%26 = load i32, i32* %4
%27 = add nsw i32 %26, -1
store i32 %27, i32* %4
%28 = load i32*, i32** %3
%29 = load i32, i32* %4
%30 = call i32 @adjust_heap_max(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %3
%32 = load i32, i32* %6
%33 = call i32 @print_heap(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %31, i32 %32)
br label %34
34: ; preds = %22
%35 = load i32, i32* %5
%36 = add nsw i32 %35, -1
store i32 %36, i32* %5
br label %19
37: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"Before sort:\00"
@.str.1 = [13 x i8] c"Built heap:\00"
@.str.2 = [13 x i8] c"Sort heap:\00"
define void @heap_sort_max(i32* %0, i32 %1) {
%3 = tail call i32 @print_heap(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32* %0, i32 %1)
%4 = tail call i32 @build_heap_max(i32* %0, i32 %1)
%5 = tail call i32 @print_heap(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32* %0, i32 %1)
%6 = icmp sgt i32 %1, 1
br i1 %6, label %7, label %14
7: ; preds = %2, %7
%8 = phi i32 [ %9, %7 ], [ %1, %2 ]
%9 = add nsw i32 %8, -1
%10 = tail call i32 @swap(i32* %0, i32 0, i32 %9)
%11 = tail call i32 @adjust_heap_max(i32* %0, i32 0, i32 %9)
%12 = tail call i32 @print_heap(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32* %0, i32 %1)
%13 = icmp ugt i32 %8, 2
br i1 %13, label %7, label %14
14: ; preds = %7, %2
ret void
}