#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);
int /*<<< orphan*/ swap(int *, int /*<<< orphan*/, int);
void heapSort(int arr[], int arrLength) {
  int i;
  build_max_heap(arr, arrLength);
  for (i = arrLength; i > 1; i--) {
    swap(arr, 0, i - 1);
    arrLength = arrLength - 1;
    max_heapify(arr, 1, arrLength);
  }
}


x86 assembly (clang, -O0):
<heapSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    1194 <heapSort+0x64>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1050 <swap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1030 <max_heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1151 <heapSort+0x21>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
call   1040 <build_max_heap@plt>
cmp    $0x2,%ebx
jl     1176 <heapSort+0x46>
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    -0x2(%rbx),%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <swap@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1030 <max_heapify@plt>
add    $0xffffffff,%ebx
cmp    $0x2,%ebx
ja     1150 <heapSort+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @heapSort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%6 = load i32*, i32** %3
%7 = load i32, i32* %4
%8 = call i32 @build_max_heap(i32* %6, i32 %7)
%9 = load i32, i32* %4
store i32 %9, i32* %5
br label %10
10: ; preds = %23, %2
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = load i32*, i32** %3
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = call i32 @swap(i32* %14, i32 0, i32 %16)
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
store i32 %19, i32* %4
%20 = load i32*, i32** %3
%21 = load i32, i32* %4
%22 = call i32 @max_heapify(i32* %20, i32 1, i32 %21)
br label %23
23: ; preds = %13
%24 = load i32, i32* %5
%25 = add nsw i32 %24, -1
store i32 %25, i32* %5
br label %10
26: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @heapSort(i32* %0, i32 %1) {
%3 = tail call i32 @build_max_heap(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %11
5: ; preds = %2, %5
%6 = phi i32 [ %7, %5 ], [ %1, %2 ]
%7 = add nsw i32 %6, -1
%8 = tail call i32 @swap(i32* %0, i32 0, i32 %7)
%9 = tail call i32 @max_heapify(i32* %0, i32 1, i32 %7)
%10 = icmp ugt i32 %6, 2
br i1 %10, label %5, label %11
11: ; preds = %5, %2
ret void
}