#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(void **, int, int (*)(void *, void *));
int /*<<< orphan*/ max_heapify(void **, int /*<<< orphan*/, int,
                               int (*)(void *, void *));
int /*<<< orphan*/ swap_data(void **, void **);
void heap_sort(void **arr, int asz, int (*compare)(void *, void *)) {
  if (asz < 2)
    return;
  int i;
  build_max_heap(arr, asz, compare);
  for (i = asz - 1; i > 0; i--) {
    swap_data(&arr[0], &arr[i]);
    max_heapify(arr, 0, --asz, compare);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x2,-0xc(%rbp)
jge    1152 <heap_sort+0x22>
jmp    11b3 <heap_sort+0x83>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11b3 <heap_sort+0x83>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   1030 <swap_data@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
mov    -0x18(%rbp),%rcx
xor    %esi,%esi
call   1040 <max_heapify@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    116b <heap_sort+0x3b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%esi
jl     117a <heap_sort+0x4a>
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
call   1050 <build_max_heap@plt>
mov    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r15,%rax,8),%rsi
mov    %r15,%rdi
call   1030 <swap_data@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %r14,%rcx
call   1040 <max_heapify@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1150 <heap_sort+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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