#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*/ heapBuild(void *, int, int, int (*)(void *, void *));
int /*<<< orphan*/ minHeapRestore(void *, int, int, int,
                                  int (*)(void *, void *));
int /*<<< orphan*/ swap(void *, void *, int);
void heap_sort(void *a, int n, int el_size,
               int (*compare)(void *one, void *two)) {
  heapBuild(a, n, el_size, compare);
  for (int i = n; i >= 2; i--) {
    swap(a + el_size * i, a, el_size);
    minHeapRestore(a, 1, i - 1, el_size, compare);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1030 <heapBuild@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jl     11ae <heap_sort+0x7e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%eax
imul   -0x1c(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    $0x1,%esi
call   1040 <minHeapRestore@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    115f <heap_sort+0x2f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r12
mov    %rcx,(%rsp)
call   1030 <heapBuild@plt>
cmp    $0x2,%ebp
jl     11a5 <heap_sort+0x75>
mov    %ebp,%ebx
movslq %r15d,%r13
mov    %r13,%rbp
imul   %rbx,%rbp
add    %r12,%rbp
neg    %r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1050 <swap@plt>
lea    -0x1(%rbx),%r14
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
mov    (%rsp),%r8
call   1040 <minHeapRestore@plt>
add    %r13,%rbp
cmp    $0x2,%rbx
mov    %r14,%rbx
ja     1170 <heap_sort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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