#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*/ buildHeap(void *, size_t, size_t,
                             int (*)(void const *, void const *));
int /*<<< orphan*/ byteSwap(char *, void *, size_t);
int /*<<< orphan*/ siftDown(void *, int /*<<< orphan*/, int, size_t,
                            int (*)(void const *, void const *));
void heapSort(void *base, size_t nitems, size_t memSize,
              int (*cmp)(const void *, const void *)) {
  buildHeap(base, nitems, memSize, cmp);
  int end = nitems - 1;
  while (end > 0) {
    char *endElementPtr = ((char *)base + (end * memSize));
    byteSwap(endElementPtr, base, memSize);
    end--;
    siftDown(base, 0, end, memSize, 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   1040 <buildHeap@plt>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11bb <heapSort+0x8b>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <byteSwap@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %esi,%esi
call   1030 <siftDown@plt>
jmp    1168 <heapSort+0x38>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r12
mov    %rcx,(%rsp)
call   1040 <buildHeap@plt>
lea    -0x1(%r13),%eax
test   %eax,%eax
jle    11a3 <heapSort+0x73>
mov    %eax,%ebp
mov    %r15,%rbx
imul   %rbp,%rbx
add    $0x1,%rbp
add    %r12,%rbx
mov    %r15,%r14
neg    %r14
add    $0xfffffffe,%r13d
nop
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1050 <byteSwap@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %r13d,%edx
mov    %r15,%rcx
mov    (%rsp),%r8
call   1030 <siftDown@plt>
add    $0xffffffffffffffff,%rbp
add    %r14,%rbx
add    $0xffffffff,%r13d
cmp    $0x1,%rbp
ja     1170 <heapSort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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*)*
%9 = alloca i32
%10 = alloca 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
%11 = load i8*, i8** %5
%12 = load i64, i64* %6
%13 = load i64, i64* %7
%14 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%15 = call i32 @buildHeap(i8* %11, i64 %12, i64 %13, i32 (i8*, i8*)* %14)
%16 = load i64, i64* %6
%17 = sub i64 %16, 1
%18 = trunc i64 %17 to i32
store i32 %18, i32* %9
br label %19
19: ; preds = %22, %4
%20 = load i32, i32* %9
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %19
%23 = load i8*, i8** %5
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = load i64, i64* %7
%27 = mul i64 %25, %26
%28 = getelementptr inbounds i8, i8* %23, i64 %27
store i8* %28, i8** %10
%29 = load i8*, i8** %10
%30 = load i8*, i8** %5
%31 = load i64, i64* %7
%32 = call i32 @byteSwap(i8* %29, i8* %30, i64 %31)
%33 = load i32, i32* %9
%34 = add nsw i32 %33, -1
store i32 %34, i32* %9
%35 = load i8*, i8** %5
%36 = load i32, i32* %9
%37 = load i64, i64* %7
%38 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%39 = call i32 @siftDown(i8* %35, i32 0, i32 %36, i64 %37, i32 (i8*, i8*)* %38)
br label %19
40: ; preds = %19
ret void
}

LLVM IR (-O2):
define void @heapSort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = tail call i32 @buildHeap(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3)
%6 = trunc i64 %1 to i32
%7 = add i32 %6, -1
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %4
%10 = zext i32 %7 to i64
br label %11
11: ; preds = %9, %11
%12 = phi i64 [ %10, %9 ], [ %16, %11 ]
%13 = mul i64 %12, %2
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = tail call i32 @byteSwap(i8* %14, i8* %0, i64 %2)
%16 = add nsw i64 %12, -1
%17 = trunc i64 %16 to i32
%18 = tail call i32 @siftDown(i8* %0, i32 0, i32 %17, i64 %2, i32 (i8*, i8*)* %3)
%19 = icmp ugt i64 %12, 1
br i1 %19, label %11, label %20
20: ; preds = %11, %4
ret void
}