#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*/ MaxHeapify(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void heapsort(int *a, int n) {
  int i;
  for (i = 0; i < n; i++) {
    MaxHeapify(a, 1, n - i);
    swap(&a[1], &a[n - i]);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1184 <heapsort+0x64>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
sub    -0x10(%rbp),%edx
mov    $0x1,%esi
call   1030 <MaxHeapify@plt>
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <swap@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <heapsort+0x16>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1165 <heapsort+0x45>
mov    %rdi,%r14
lea    0x4(%rdi),%r15
mov    %esi,%r12d
lea    (%rdi,%r12,4),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
call   1030 <MaxHeapify@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <swap@plt>
add    $0xfffffffffffffffc,%rbx
add    $0xffffffffffffffff,%r12
jne    1140 <heapsort+0x20>
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
store i32 0, i32* %5
br label %6
6: ; preds = %25, %2
%7 = load i32, i32* %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %28
10: ; preds = %6
%11 = load i32*, i32** %3
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = sub nsw i32 %12, %13
%15 = call i32 @MaxHeapify(i32* %11, i32 1, i32 %14)
%16 = load i32*, i32** %3
%17 = getelementptr inbounds i32, i32* %16, i64 1
%18 = load i32*, i32** %3
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = sub nsw i32 %19, %20
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %18, i64 %22
%24 = call i32 @swap(i32* %17, i32* %23)
br label %25
25: ; preds = %10
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %6
28: ; preds = %6
ret void
}

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