#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*/ repairheap(void *, size_t, size_t, int /*<<< orphan*/,
                              int (*)(void const *, void const *));
int /*<<< orphan*/ swap(void *, void *, size_t);
void heapsort(void *base, size_t nitems, size_t size,
              int (*compar)(const void *, const void *)) {
  size_t i;
  for (i = 1; i < nitems; i++) {
    swap((base + (size * 0)), (base + (size * (nitems - i))), size);
    repairheap(base, nitems - i, size, 0, compar);
  }
}


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)
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a6 <heapsort+0x86>
mov    -0x8(%rbp),%rdi
imul   $0x0,-0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    -0x28(%rbp),%rcx
imul   %rcx,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1040 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
call   1030 <repairheap@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1140 <heapsort+0x20>
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
cmp    $0x2,%rsi
jb     118a <heapsort+0x6a>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r12
add    $0xffffffffffffffff,%r13
mov    %rdx,%rbx
imul   %r13,%rbx
add    %rdi,%rbx
mov    %rdx,%rbp
neg    %rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <swap@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
xor    %ecx,%ecx
mov    %r14,%r8
call   1030 <repairheap@plt>
add    %rbp,%rbx
add    $0xffffffffffffffff,%r13
jne    1160 <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 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
store i64 1, i64* %9
br label %10
10: ; preds = %35, %4
%11 = load i64, i64* %9
%12 = load i64, i64* %6
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %38
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = load i64, i64* %7
%17 = mul i64 %16, 0
%18 = getelementptr i8, i8* %15, i64 %17
%19 = load i8*, i8** %5
%20 = load i64, i64* %7
%21 = load i64, i64* %6
%22 = load i64, i64* %9
%23 = sub i64 %21, %22
%24 = mul i64 %20, %23
%25 = getelementptr i8, i8* %19, i64 %24
%26 = load i64, i64* %7
%27 = call i32 @swap(i8* %18, i8* %25, i64 %26)
%28 = load i8*, i8** %5
%29 = load i64, i64* %6
%30 = load i64, i64* %9
%31 = sub i64 %29, %30
%32 = load i64, i64* %7
%33 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%34 = call i32 @repairheap(i8* %28, i64 %31, i64 %32, i32 0, i32 (i8*, i8*)* %33)
br label %35
35: ; preds = %14
%36 = load i64, i64* %9
%37 = add i64 %36, 1
store i64 %37, i64* %9
br label %10
38: ; preds = %10
ret void
}

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