#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 */
size_t build_max_heapify(void *, size_t, size_t, int (*)(void *, void *));
int /*<<< orphan*/ max_heapify(void *, size_t, size_t, int /*<<< orphan*/,
                               int (*)(void *, void *));
int /*<<< orphan*/ swap(void *, void *, size_t);
void AL_heap_sort(void *arr, size_t size, size_t count,
                  int (*comp)(void *key1, void *key2)) {
  size_t heapsize = build_max_heapify(arr, size, count, comp);
  for (size_t i = count - 1; i >= 1; i--) {
    swap(arr, arr + size * i, size);
    heapsize--;
    max_heapify(arr, size, heapsize, 0, comp);
  }
}


x86 assembly (clang, -O0):
<AL_heap_sort>:
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   1030 <build_max_heapify@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jb     11c9 <AL_heap_sort+0x99>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
imul   -0x30(%rbp),%rax
add    %rax,%rsi
mov    -0x10(%rbp),%rdx
call   1050 <swap@plt>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
call   1040 <max_heapify@plt>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    116d <AL_heap_sort+0x3d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AL_heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rcx,(%rsp)
call   1030 <build_max_heapify@plt>
add    $0xffffffffffffffff,%r13
je     119f <AL_heap_sort+0x6f>
mov    %rax,%rbx
mov    %r15,%rbp
imul   %r13,%rbp
add    %r12,%rbp
mov    %r15,%r14
neg    %r14
add    $0xffffffffffffffff,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1050 <swap@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    (%rsp),%r8
call   1040 <max_heapify@plt>
add    %r14,%rbp
add    $0xffffffffffffffff,%rbx
add    $0xffffffffffffffff,%r13
jne    1170 <AL_heap_sort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @AL_heap_sort(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 i64
%10 = alloca i64
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 i64 @build_max_heapify(i8* %11, i64 %12, i64 %13, i32 (i8*, i8*)* %14)
store i64 %15, i64* %9
%16 = load i64, i64* %7
%17 = sub i64 %16, 1
store i64 %17, i64* %10
br label %18
18: ; preds = %37, %4
%19 = load i64, i64* %10
%20 = icmp uge i64 %19, 1
br i1 %20, label %21, label %40
21: ; preds = %18
%22 = load i8*, i8** %5
%23 = load i8*, i8** %5
%24 = load i64, i64* %6
%25 = load i64, i64* %10
%26 = mul i64 %24, %25
%27 = getelementptr i8, i8* %23, i64 %26
%28 = load i64, i64* %6
%29 = call i32 @swap(i8* %22, i8* %27, i64 %28)
%30 = load i64, i64* %9
%31 = add i64 %30, -1
store i64 %31, i64* %9
%32 = load i8*, i8** %5
%33 = load i64, i64* %6
%34 = load i64, i64* %9
%35 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%36 = call i32 @max_heapify(i8* %32, i64 %33, i64 %34, i32 0, i32 (i8*, i8*)* %35)
br label %37
37: ; preds = %21
%38 = load i64, i64* %10
%39 = add i64 %38, -1
store i64 %39, i64* %10
br label %18
40: ; preds = %18
ret void
}

LLVM IR (-O2):
define void @AL_heap_sort(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = tail call i64 @build_max_heapify(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3)
%6 = add i64 %2, -1
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %9, %4
ret void
9: ; preds = %4, %9
%10 = phi i64 [ %17, %9 ], [ %6, %4 ]
%11 = phi i64 [ %15, %9 ], [ %5, %4 ]
%12 = mul i64 %10, %1
%13 = getelementptr i8, i8* %0, i64 %12
%14 = tail call i32 @swap(i8* %0, i8* %13, i64 %1)
%15 = add i64 %11, -1
%16 = tail call i32 @max_heapify(i8* %0, i64 %1, i64 %15, i32 0, i32 (i8*, i8*)* %3)
%17 = add i64 %10, -1
%18 = icmp eq i64 %17, 0
br i1 %18, label %8, label %9
}