#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*/ heapify(void *, size_t, int (*)(void const *, void const *),
                           size_t);
int /*<<< orphan*/ siftdown(void *, size_t, int (*)(void const *, void const *),
                            int /*<<< orphan*/, int);
int /*<<< orphan*/ swap(void *, void *, size_t);
void heapsort(void *base, size_t nmemb, size_t size,
              int (*compare)(const void *, const void *)) {
  if (base == NULL || compare == NULL)
    return;
  int end = nmemb - 1;
  heapify(base, size, compare, nmemb);
  while (end > 0) {
    swap(base + (end * size), base, size);
    siftdown(base, size, compare, 0, --end);
  }
}


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)
cmpq   $0x0,-0x8(%rbp)
je     115e <heapsort+0x2e>
cmpq   $0x0,-0x20(%rbp)
jne    1163 <heapsort+0x33>
jmp    11ce <heapsort+0x9e>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1040 <heapify@plt>
cmpl   $0x0,-0x24(%rbp)
jle    11ce <heapsort+0x9e>
mov    -0x8(%rbp),%rdi
movslq -0x24(%rbp),%rax
imul   -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%r8d
add    $0xffffffff,%r8d
mov    %r8d,-0x24(%rbp)
xor    %ecx,%ecx
call   1030 <siftdown@plt>
jmp    1183 <heapsort+0x53>
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    %rcx,(%rsp)
test   %rdi,%rdi
je     11c2 <heapsort+0x92>
cmpq   $0x0,(%rsp)
je     11c2 <heapsort+0x92>
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r12
lea    -0x1(%rsi),%ebx
mov    %rdx,%rsi
mov    (%rsp),%rdx
mov    %rbp,%rcx
call   1040 <heapify@plt>
test   %ebx,%ebx
jle    11c2 <heapsort+0x92>
mov    %ebx,%r13d
mov    %r15,%rbx
imul   %r13,%rbx
add    $0x1,%r13
add    %r12,%rbx
mov    %r15,%r14
neg    %r14
add    $0xfffffffe,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1050 <swap@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    (%rsp),%rdx
xor    %ecx,%ecx
mov    %ebp,%r8d
call   1030 <siftdown@plt>
add    $0xffffffffffffffff,%r13
add    %r14,%rbx
add    $0xffffffff,%ebp
cmp    $0x1,%r13
ja     1190 <heapsort+0x60>
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
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %4
%13 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%14 = icmp eq i32 (i8*, i8*)* %13, null
br i1 %14, label %15, label %16
15: ; preds = %12, %4
br label %44
16: ; preds = %12
%17 = load i64, i64* %6
%18 = sub i64 %17, 1
%19 = trunc i64 %18 to i32
store i32 %19, i32* %9
%20 = load i8*, i8** %5
%21 = load i64, i64* %7
%22 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%23 = load i64, i64* %6
%24 = call i32 @heapify(i8* %20, i64 %21, i32 (i8*, i8*)* %22, i64 %23)
br label %25
25: ; preds = %28, %16
%26 = load i32, i32* %9
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %25
%29 = load i8*, i8** %5
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = mul i64 %31, %32
%34 = getelementptr i8, i8* %29, i64 %33
%35 = load i8*, i8** %5
%36 = load i64, i64* %7
%37 = call i32 @swap(i8* %34, i8* %35, i64 %36)
%38 = load i8*, i8** %5
%39 = load i64, i64* %7
%40 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%41 = load i32, i32* %9
%42 = add nsw i32 %41, -1
store i32 %42, i32* %9
%43 = call i32 @siftdown(i8* %38, i64 %39, i32 (i8*, i8*)* %40, i32 0, i32 %42)
br label %25
44: ; preds = %15, %25
ret void
}

LLVM IR (-O2):
define void @heapsort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = icmp eq i8* %0, null
%6 = icmp eq i32 (i8*, i8*)* %3, null
%7 = or i1 %5, %6
br i1 %7, label %24, label %8
8: ; preds = %4
%9 = trunc i64 %1 to i32
%10 = add i32 %9, -1
%11 = tail call i32 @heapify(i8* %0, i64 %2, i32 (i8*, i8*)* %3, i64 %1)
%12 = icmp sgt i32 %10, 0
br i1 %12, label %13, label %24
13: ; preds = %8
%14 = zext i32 %10 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ %14, %13 ], [ %20, %15 ]
%17 = mul i64 %16, %2
%18 = getelementptr i8, i8* %0, i64 %17
%19 = tail call i32 @swap(i8* %18, i8* %0, i64 %2)
%20 = add nsw i64 %16, -1
%21 = trunc i64 %20 to i32
%22 = tail call i32 @siftdown(i8* %0, i64 %2, i32 (i8*, i8*)* %3, i32 0, i32 %21)
%23 = icmp ugt i64 %16, 1
br i1 %23, label %15, label %24
24: ; preds = %15, %8, %4
ret void
}