#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*/ SWAP(int *, int *);
int /*<<< orphan*/ heapify(int *, int);
int /*<<< orphan*/ siftdown(int *, int /*<<< orphan*/, int);
void heapsort(int *array, int arraylength) {
  if (array == NULL || arraylength <= 1)
    return;
  heapify(array, arraylength);
  int endind = arraylength - 1;
  while (endind > 0) {
    SWAP(array, array + endind);
    endind--;
    siftdown(array, 0, endind);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1154 <heapsort+0x24>
cmpl   $0x1,-0xc(%rbp)
jg     1159 <heapsort+0x29>
jmp    11ac <heapsort+0x7c>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <heapify@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11ac <heapsort+0x7c>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <SWAP@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1030 <siftdown@plt>
jmp    116e <heapsort+0x3e>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     118a <heapsort+0x5a>
mov    %esi,%r15d
cmp    $0x2,%esi
jl     118a <heapsort+0x5a>
mov    %rdi,%r14
mov    %r15d,%esi
call   1050 <heapify@plt>
lea    -0x1(%r15),%r12d
lea    (%r14,%r12,4),%rbx
add    $0x1,%r12
add    $0xfffffffe,%r15d
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <SWAP@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1030 <siftdown@plt>
add    $0xffffffffffffffff,%r12
add    $0xfffffffffffffffc,%rbx
add    $0xffffffff,%r15d
cmp    $0x1,%r12
ja     1160 <heapsort+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @heapsort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %8, %2
br label %33
12: ; preds = %8
%13 = load i32*, i32** %3
%14 = load i32, i32* %4
%15 = call i32 @heapify(i32* %13, i32 %14)
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %5
br label %18
18: ; preds = %21, %12
%19 = load i32, i32* %5
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i32*, i32** %3
%23 = load i32*, i32** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = call i32 @SWAP(i32* %22, i32* %26)
%28 = load i32, i32* %5
%29 = add nsw i32 %28, -1
store i32 %29, i32* %5
%30 = load i32*, i32** %3
%31 = load i32, i32* %5
%32 = call i32 @siftdown(i32* %30, i32 0, i32 %31)
br label %18
33: ; preds = %11, %18
ret void
}

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