#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*/ array_elem(void *, size_t, size_t);
int /*<<< orphan*/ heapify(void *, size_t, size_t,
                           int (*)(void const *, void const *));
int /*<<< orphan*/ shift_down(void *, size_t, int /*<<< orphan*/, size_t,
                              int (*)(void const *, void const *));
int /*<<< orphan*/ swap(int /*<<< orphan*/, int /*<<< orphan*/, size_t);
void heapsort(void *elems, size_t elem_count, size_t elem_size,
              int (*cmp)(const void *, const void *)) {
  heapify(elems, elem_count, elem_size, cmp);
  for (; elem_count >= 2; elem_count--) {
    swap(array_elem(elems, 0, elem_size),
         array_elem(elems, elem_count - 1, elem_size), elem_size);
    shift_down(elems, elem_count - 1, 0, elem_size, cmp);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <heapify@plt>
cmpq   $0x2,-0x10(%rbp)
jb     11db <heapsort+0x9b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <array_elem@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x18(%rbp),%rdx
call   1040 <array_elem@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
call   1060 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %edx,%edx
call   1030 <shift_down@plt>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    116d <heapsort+0x2d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
call   1050 <heapify@plt>
cmp    $0x2,%rbx
jb     11a6 <heapsort+0x66>
nop
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1040 <array_elem@plt>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <array_elem@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1060 <swap@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
mov    %r15,%rcx
mov    %r14,%r8
call   1030 <shift_down@plt>
cmp    $0x1,%rbx
ja     1160 <heapsort+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @heapsort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = tail call i32 @heapify(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3)
%6 = icmp ugt i64 %1, 1
br i1 %6, label %7, label %15
7: ; preds = %4, %7
%8 = phi i64 [ %10, %7 ], [ %1, %4 ]
%9 = tail call i32 @array_elem(i8* %0, i64 0, i64 %2)
%10 = add i64 %8, -1
%11 = tail call i32 @array_elem(i8* %0, i64 %10, i64 %2)
%12 = tail call i32 @swap(i32 %9, i32 %11, i64 %2)
%13 = tail call i32 @shift_down(i8* %0, i64 %10, i32 0, i64 %2, i32 (i8*, i8*)* %3)
%14 = icmp ugt i64 %10, 1
br i1 %14, label %7, label %15
15: ; preds = %7, %4
ret void
}